From 5ee5d42511836d581b649d57251de996d7dba17a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 9 Jun 2025 16:21:10 +0200 Subject: [PATCH 1/2] Instantiate Mutexes's fields by value Instead, pass the entire Mutexes struct by pointer to controllers. This is better because Mutexes now has a zero value and doesn't need to be initialized. --- pkg/gui/gui.go | 11 ----------- pkg/gui/gui_common.go | 4 ++-- pkg/gui/types/common.go | 22 ++++++++++------------ 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 49643791e..c38685d3a 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -681,17 +681,6 @@ func NewGui( // real value after loading the user config: ShowExtrasWindow: true, - Mutexes: types.Mutexes{ - RefreshingFilesMutex: &deadlock.Mutex{}, - RefreshingBranchesMutex: &deadlock.Mutex{}, - RefreshingStatusMutex: &deadlock.Mutex{}, - LocalCommitsMutex: &deadlock.Mutex{}, - SubCommitsMutex: &deadlock.Mutex{}, - AuthorsMutex: &deadlock.Mutex{}, - SubprocessMutex: &deadlock.Mutex{}, - PopupMutex: &deadlock.Mutex{}, - PtyMutex: &deadlock.Mutex{}, - }, InitialDir: initialDir, afterLayoutFuncs: make(chan func() error, 1000), diff --git a/pkg/gui/gui_common.go b/pkg/gui/gui_common.go index 41b409544..4a40c4f8b 100644 --- a/pkg/gui/gui_common.go +++ b/pkg/gui/gui_common.go @@ -100,8 +100,8 @@ func (self *guiCommon) Model() *types.Model { return self.gui.State.Model } -func (self *guiCommon) Mutexes() types.Mutexes { - return self.gui.Mutexes +func (self *guiCommon) Mutexes() *types.Mutexes { + return &self.gui.Mutexes } func (self *guiCommon) GocuiGui() *gocui.Gui { diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 2ca016825..47d51bb6b 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -94,7 +94,7 @@ type IGuiCommon interface { Modes() *Modes - Mutexes() Mutexes + Mutexes() *Mutexes State() IStateAccessor @@ -313,18 +313,16 @@ type Model struct { HashPool *utils.StringPool } -// if you add a new mutex here be sure to instantiate it. We're using pointers to -// mutexes so that we can pass the mutexes to controllers. type Mutexes struct { - RefreshingFilesMutex *deadlock.Mutex - RefreshingBranchesMutex *deadlock.Mutex - RefreshingStatusMutex *deadlock.Mutex - LocalCommitsMutex *deadlock.Mutex - SubCommitsMutex *deadlock.Mutex - AuthorsMutex *deadlock.Mutex - SubprocessMutex *deadlock.Mutex - PopupMutex *deadlock.Mutex - PtyMutex *deadlock.Mutex + RefreshingFilesMutex deadlock.Mutex + RefreshingBranchesMutex deadlock.Mutex + RefreshingStatusMutex deadlock.Mutex + LocalCommitsMutex deadlock.Mutex + SubCommitsMutex deadlock.Mutex + AuthorsMutex deadlock.Mutex + SubprocessMutex deadlock.Mutex + PopupMutex deadlock.Mutex + PtyMutex deadlock.Mutex } // A long-running operation associated with an item. For example, we'll show From fd270768e89b64f28923698757f629508394fac1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 9 Jun 2025 16:16:20 +0200 Subject: [PATCH 2/2] Instantiate other mutexes by value Like in the previous commit, this is preferred because the fields don't need to be initialized this way. --- pkg/commands/git_commands/main_branches.go | 3 +-- pkg/gui/context/filtered_list.go | 3 +-- pkg/gui/context/merge_conflicts_context.go | 5 ++--- pkg/gui/context/patch_explorer_context.go | 5 ++--- pkg/gui/controllers/helpers/inline_status_helper.go | 3 +-- pkg/gui/gui.go | 5 ++--- 6 files changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/commands/git_commands/main_branches.go b/pkg/commands/git_commands/main_branches.go index 8cf08840a..19f809a4f 100644 --- a/pkg/commands/git_commands/main_branches.go +++ b/pkg/commands/git_commands/main_branches.go @@ -21,7 +21,7 @@ type MainBranches struct { previousMainBranches []string cmd oscommands.ICmdObjBuilder - mutex *deadlock.Mutex + mutex deadlock.Mutex } func NewMainBranches( @@ -32,7 +32,6 @@ func NewMainBranches( c: cmn, existingMainBranches: nil, cmd: cmd, - mutex: &deadlock.Mutex{}, } } diff --git a/pkg/gui/context/filtered_list.go b/pkg/gui/context/filtered_list.go index 0724ecb3b..8164cfa16 100644 --- a/pkg/gui/context/filtered_list.go +++ b/pkg/gui/context/filtered_list.go @@ -16,14 +16,13 @@ type FilteredList[T any] struct { getFilterFields func(T) []string filter string - mutex *deadlock.Mutex + mutex deadlock.Mutex } func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string) *FilteredList[T] { return &FilteredList[T]{ getList: getList, getFilterFields: getFilterFields, - mutex: &deadlock.Mutex{}, } } diff --git a/pkg/gui/context/merge_conflicts_context.go b/pkg/gui/context/merge_conflicts_context.go index c05eeb614..2ab446c06 100644 --- a/pkg/gui/context/merge_conflicts_context.go +++ b/pkg/gui/context/merge_conflicts_context.go @@ -12,7 +12,7 @@ type MergeConflictsContext struct { types.Context viewModel *ConflictsViewModel c *ContextCommon - mutex *deadlock.Mutex + mutex deadlock.Mutex } type ConflictsViewModel struct { @@ -33,7 +33,6 @@ func NewMergeConflictsContext( return &MergeConflictsContext{ viewModel: viewModel, - mutex: &deadlock.Mutex{}, Context: NewSimpleContext( NewBaseContext(NewBaseContextOpts{ Kind: types.MAIN_CONTEXT, @@ -57,7 +56,7 @@ func (self *MergeConflictsContext) SetState(state *mergeconflicts.State) { } func (self *MergeConflictsContext) GetMutex() *deadlock.Mutex { - return self.mutex + return &self.mutex } func (self *MergeConflictsContext) SetUserScrolling(isScrolling bool) { diff --git a/pkg/gui/context/patch_explorer_context.go b/pkg/gui/context/patch_explorer_context.go index eb79dce86..c9144e1ab 100644 --- a/pkg/gui/context/patch_explorer_context.go +++ b/pkg/gui/context/patch_explorer_context.go @@ -15,7 +15,7 @@ type PatchExplorerContext struct { viewTrait *ViewTrait getIncludedLineIndices func() []int c *ContextCommon - mutex *deadlock.Mutex + mutex deadlock.Mutex } var ( @@ -36,7 +36,6 @@ func NewPatchExplorerContext( state: nil, viewTrait: NewViewTrait(view), c: c, - mutex: &deadlock.Mutex{}, getIncludedLineIndices: getIncludedLineIndices, SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ View: view, @@ -137,7 +136,7 @@ func (self *PatchExplorerContext) NavigateTo(selectedLineIdx int) { } func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex { - return self.mutex + return &self.mutex } func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition { diff --git a/pkg/gui/controllers/helpers/inline_status_helper.go b/pkg/gui/controllers/helpers/inline_status_helper.go index 2e1cf29e0..38a4e2cf7 100644 --- a/pkg/gui/controllers/helpers/inline_status_helper.go +++ b/pkg/gui/controllers/helpers/inline_status_helper.go @@ -15,7 +15,7 @@ type InlineStatusHelper struct { windowHelper *WindowHelper contextsWithInlineStatus map[types.ContextKey]*inlineStatusInfo - mutex *deadlock.Mutex + mutex deadlock.Mutex } func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineStatusHelper { @@ -23,7 +23,6 @@ func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineS c: c, windowHelper: windowHelper, contextsWithInlineStatus: make(map[types.ContextKey]*inlineStatusInfo), - mutex: &deadlock.Mutex{}, } } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index c38685d3a..ac1be6601 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -118,7 +118,7 @@ type Gui struct { // is being pushed). At the moment the rule is to use an item operation when // we need to talk to the remote. itemOperations map[string]types.ItemOperation - itemOperationsMutex *deadlock.Mutex + itemOperationsMutex deadlock.Mutex PrevLayout PrevLayout @@ -684,8 +684,7 @@ func NewGui( InitialDir: initialDir, afterLayoutFuncs: make(chan func() error, 1000), - itemOperations: make(map[string]types.ItemOperation), - itemOperationsMutex: &deadlock.Mutex{}, + itemOperations: make(map[string]types.ItemOperation), } gui.PopupHandler = popup.NewPopupHandler(