Skip to content

Commit ec0f4d2

Browse files
authored
Instantiate mutexes by value (#4632)
- **PR Description** Here's a minor cleanup: instantiate mutexes by value so that they don't have to be initialized explicitly. It is always preferable for structs to have a valid zero value, and this is one small step in that direction. I read this recommendation in the [Uber style guide](https://github.com/uber-go/guide/blob/master/style.md#zero-value-mutexes-are-valid), and it makes a lot of sense to me. I find most of the rest of this style guide to be a very good read, too.
2 parents 4c92ffd + fd27076 commit ec0f4d2

File tree

8 files changed

+21
-40
lines changed

8 files changed

+21
-40
lines changed

pkg/commands/git_commands/main_branches.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type MainBranches struct {
2121
previousMainBranches []string
2222

2323
cmd oscommands.ICmdObjBuilder
24-
mutex *deadlock.Mutex
24+
mutex deadlock.Mutex
2525
}
2626

2727
func NewMainBranches(
@@ -32,7 +32,6 @@ func NewMainBranches(
3232
c: cmn,
3333
existingMainBranches: nil,
3434
cmd: cmd,
35-
mutex: &deadlock.Mutex{},
3635
}
3736
}
3837

pkg/gui/context/filtered_list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ type FilteredList[T any] struct {
1616
getFilterFields func(T) []string
1717
filter string
1818

19-
mutex *deadlock.Mutex
19+
mutex deadlock.Mutex
2020
}
2121

2222
func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string) *FilteredList[T] {
2323
return &FilteredList[T]{
2424
getList: getList,
2525
getFilterFields: getFilterFields,
26-
mutex: &deadlock.Mutex{},
2726
}
2827
}
2928

pkg/gui/context/merge_conflicts_context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type MergeConflictsContext struct {
1212
types.Context
1313
viewModel *ConflictsViewModel
1414
c *ContextCommon
15-
mutex *deadlock.Mutex
15+
mutex deadlock.Mutex
1616
}
1717

1818
type ConflictsViewModel struct {
@@ -33,7 +33,6 @@ func NewMergeConflictsContext(
3333

3434
return &MergeConflictsContext{
3535
viewModel: viewModel,
36-
mutex: &deadlock.Mutex{},
3736
Context: NewSimpleContext(
3837
NewBaseContext(NewBaseContextOpts{
3938
Kind: types.MAIN_CONTEXT,
@@ -57,7 +56,7 @@ func (self *MergeConflictsContext) SetState(state *mergeconflicts.State) {
5756
}
5857

5958
func (self *MergeConflictsContext) GetMutex() *deadlock.Mutex {
60-
return self.mutex
59+
return &self.mutex
6160
}
6261

6362
func (self *MergeConflictsContext) SetUserScrolling(isScrolling bool) {

pkg/gui/context/patch_explorer_context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type PatchExplorerContext struct {
1515
viewTrait *ViewTrait
1616
getIncludedLineIndices func() []int
1717
c *ContextCommon
18-
mutex *deadlock.Mutex
18+
mutex deadlock.Mutex
1919
}
2020

2121
var (
@@ -36,7 +36,6 @@ func NewPatchExplorerContext(
3636
state: nil,
3737
viewTrait: NewViewTrait(view),
3838
c: c,
39-
mutex: &deadlock.Mutex{},
4039
getIncludedLineIndices: getIncludedLineIndices,
4140
SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
4241
View: view,
@@ -137,7 +136,7 @@ func (self *PatchExplorerContext) NavigateTo(selectedLineIdx int) {
137136
}
138137

139138
func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {
140-
return self.mutex
139+
return &self.mutex
141140
}
142141

143142
func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {

pkg/gui/controllers/helpers/inline_status_helper.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ type InlineStatusHelper struct {
1515

1616
windowHelper *WindowHelper
1717
contextsWithInlineStatus map[types.ContextKey]*inlineStatusInfo
18-
mutex *deadlock.Mutex
18+
mutex deadlock.Mutex
1919
}
2020

2121
func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineStatusHelper {
2222
return &InlineStatusHelper{
2323
c: c,
2424
windowHelper: windowHelper,
2525
contextsWithInlineStatus: make(map[types.ContextKey]*inlineStatusInfo),
26-
mutex: &deadlock.Mutex{},
2726
}
2827
}
2928

pkg/gui/gui.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type Gui struct {
118118
// is being pushed). At the moment the rule is to use an item operation when
119119
// we need to talk to the remote.
120120
itemOperations map[string]types.ItemOperation
121-
itemOperationsMutex *deadlock.Mutex
121+
itemOperationsMutex deadlock.Mutex
122122

123123
PrevLayout PrevLayout
124124

@@ -681,22 +681,10 @@ func NewGui(
681681
// real value after loading the user config:
682682
ShowExtrasWindow: true,
683683

684-
Mutexes: types.Mutexes{
685-
RefreshingFilesMutex: &deadlock.Mutex{},
686-
RefreshingBranchesMutex: &deadlock.Mutex{},
687-
RefreshingStatusMutex: &deadlock.Mutex{},
688-
LocalCommitsMutex: &deadlock.Mutex{},
689-
SubCommitsMutex: &deadlock.Mutex{},
690-
AuthorsMutex: &deadlock.Mutex{},
691-
SubprocessMutex: &deadlock.Mutex{},
692-
PopupMutex: &deadlock.Mutex{},
693-
PtyMutex: &deadlock.Mutex{},
694-
},
695684
InitialDir: initialDir,
696685
afterLayoutFuncs: make(chan func() error, 1000),
697686

698-
itemOperations: make(map[string]types.ItemOperation),
699-
itemOperationsMutex: &deadlock.Mutex{},
687+
itemOperations: make(map[string]types.ItemOperation),
700688
}
701689

702690
gui.PopupHandler = popup.NewPopupHandler(

pkg/gui/gui_common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func (self *guiCommon) Model() *types.Model {
100100
return self.gui.State.Model
101101
}
102102

103-
func (self *guiCommon) Mutexes() types.Mutexes {
104-
return self.gui.Mutexes
103+
func (self *guiCommon) Mutexes() *types.Mutexes {
104+
return &self.gui.Mutexes
105105
}
106106

107107
func (self *guiCommon) GocuiGui() *gocui.Gui {

pkg/gui/types/common.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type IGuiCommon interface {
9494

9595
Modes() *Modes
9696

97-
Mutexes() Mutexes
97+
Mutexes() *Mutexes
9898

9999
State() IStateAccessor
100100

@@ -313,18 +313,16 @@ type Model struct {
313313
HashPool *utils.StringPool
314314
}
315315

316-
// if you add a new mutex here be sure to instantiate it. We're using pointers to
317-
// mutexes so that we can pass the mutexes to controllers.
318316
type Mutexes struct {
319-
RefreshingFilesMutex *deadlock.Mutex
320-
RefreshingBranchesMutex *deadlock.Mutex
321-
RefreshingStatusMutex *deadlock.Mutex
322-
LocalCommitsMutex *deadlock.Mutex
323-
SubCommitsMutex *deadlock.Mutex
324-
AuthorsMutex *deadlock.Mutex
325-
SubprocessMutex *deadlock.Mutex
326-
PopupMutex *deadlock.Mutex
327-
PtyMutex *deadlock.Mutex
317+
RefreshingFilesMutex deadlock.Mutex
318+
RefreshingBranchesMutex deadlock.Mutex
319+
RefreshingStatusMutex deadlock.Mutex
320+
LocalCommitsMutex deadlock.Mutex
321+
SubCommitsMutex deadlock.Mutex
322+
AuthorsMutex deadlock.Mutex
323+
SubprocessMutex deadlock.Mutex
324+
PopupMutex deadlock.Mutex
325+
PtyMutex deadlock.Mutex
328326
}
329327

330328
// A long-running operation associated with an item. For example, we'll show

0 commit comments

Comments
 (0)