diff --git a/ctx.go b/ctx.go index b1a4a92..b5a5c2f 100644 --- a/ctx.go +++ b/ctx.go @@ -60,7 +60,7 @@ func (p *Ctx) MoveCaretPos(offset int) { type FilterQuery struct { query []rune - mutex *sync.Mutex + mutex sync.Locker } func (q FilterQuery) Query() []rune { @@ -113,9 +113,9 @@ type Ctx struct { maxPage int selection *Selection lines []Match - linesMutex *sync.Mutex + linesMutex sync.Locker current []Match - currentMutex *sync.Mutex + currentMutex sync.Locker bufferSize int config *Config currentMatcher int @@ -154,7 +154,7 @@ func (m *loggingMutex) Unlock() { func NewCtx(o CtxOptions) *Ctx { c := &Ctx{ Hub: NewHub(), - FilterQuery: &FilterQuery{[]rune{}, &sync.Mutex{}}, + FilterQuery: &FilterQuery{[]rune{}, newMutex()}, MatcherSet: nil, caretPosition: 0, result: []Match{}, @@ -163,9 +163,9 @@ func NewCtx(o CtxOptions) *Ctx { maxPage: 0, selection: NewSelection(), lines: []Match{}, - linesMutex: &sync.Mutex{}, + linesMutex: newMutex(), current: nil, - currentMutex: &sync.Mutex{}, + currentMutex: newMutex(), config: NewConfig(), currentMatcher: 0, exitStatus: 0, @@ -376,7 +376,7 @@ func (c *Ctx) NewView() *View { default: layout = NewDefaultLayout(c) } - return &View{c, &sync.Mutex{}, layout} + return &View{c, newMutex(), layout} } func (c *Ctx) NewFilter() *Filter { @@ -387,7 +387,7 @@ func (c *Ctx) NewInput() *Input { // Create a new keymap object k := NewKeymap(c.config.Keymap, c.config.Action) k.ApplyKeybinding() - return &Input{c, &sync.Mutex{}, nil, k, []string{}} + return &Input{c, newMutex(), nil, k, []string{}} } func (c *Ctx) SetQuery(q []rune) { diff --git a/hub.go b/hub.go index 6dbb97e..16becdc 100644 --- a/hub.go +++ b/hub.go @@ -9,13 +9,13 @@ import ( // it controls how the communication that goes through channels // are handled. type Hub struct { - isSync bool - mutex *sync.Mutex - loopCh chan struct{} - queryCh chan HubReq - drawCh chan HubReq - statusMsgCh chan HubReq - pagingCh chan HubReq + isSync bool + mutex sync.Locker + loopCh chan struct{} + queryCh chan HubReq + drawCh chan HubReq + statusMsgCh chan HubReq + pagingCh chan HubReq } // HubReq is a wrapper around the actual requst value that needs @@ -56,7 +56,7 @@ func (hr HubReq) Done() { func NewHub() *Hub { return &Hub{ false, - &sync.Mutex{}, + newMutex(), make(chan struct{}), // loopCh. You never send messages to this. no point in buffering make(chan HubReq, 5), // queryCh. make(chan HubReq, 5), // drawCh. diff --git a/input.go b/input.go index 6ed08b4..a57bd77 100644 --- a/input.go +++ b/input.go @@ -10,7 +10,7 @@ import ( // Input handles input events from termbox. type Input struct { *Ctx - mutex *sync.Mutex // Currently only used for protecting Alt/Esc workaround + mutex sync.Locker // Currently only used for protecting Alt/Esc workaround mod *time.Timer keymap Keymap currentKeySeq []string diff --git a/keymap.go b/keymap.go index 0681df0..42aa963 100644 --- a/keymap.go +++ b/keymap.go @@ -63,7 +63,7 @@ func wrapClearSequence(a Action) Action { } if len(i.currentKeySeq) > 0 { - i.SendStatusMsgAndClear(strings.Join(i.currentKeySeq, " "), 500 * time.Millisecond) + i.SendStatusMsgAndClear(strings.Join(i.currentKeySeq, " "), 500*time.Millisecond) i.currentKeySeq = []string{} } diff --git a/layout.go b/layout.go index b62b721..2685bd1 100644 --- a/layout.go +++ b/layout.go @@ -189,7 +189,7 @@ type StatusBar struct { *Ctx *AnchorSettings clearTimer *time.Timer - timerMutex *sync.Mutex + timerMutex sync.Locker } // NewStatusBar creates a new StatusBar struct @@ -198,7 +198,7 @@ func NewStatusBar(ctx *Ctx, anchor VerticalAnchor, anchorOffset int) *StatusBar ctx, NewAnchorSettings(anchor, anchorOffset), nil, - &sync.Mutex{}, + newMutex(), } } diff --git a/matchers.go b/matchers.go index 1404687..8143674 100644 --- a/matchers.go +++ b/matchers.go @@ -13,11 +13,11 @@ import ( type MatcherSet struct { current int matchers []Matcher - mutex *sync.Mutex + mutex sync.Locker } func NewMatcherSet() *MatcherSet { - return &MatcherSet{0, []Matcher{}, &sync.Mutex{}} + return &MatcherSet{0, []Matcher{}, newMutex()} } func (s *MatcherSet) GetCurrent() Matcher { diff --git a/peco_test.go b/peco_test.go index 48688d0..2ed070e 100644 --- a/peco_test.go +++ b/peco_test.go @@ -4,13 +4,13 @@ import "sync" type interceptorArgs []interface{} type interceptor struct { - m *sync.Mutex + m sync.Locker events map[string][]interceptorArgs } func newInterceptor() *interceptor { return &interceptor{ - &sync.Mutex{}, + newMutex(), make(map[string][]interceptorArgs), } } diff --git a/reader.go b/reader.go index c6e6f84..bff64d0 100644 --- a/reader.go +++ b/reader.go @@ -46,7 +46,7 @@ func (b *BufferReader) Loop() { } }() - m := &sync.Mutex{} + m := newMutex() once := &sync.Once{} var refresh *time.Timer diff --git a/screen.go b/screen.go index c29d459..e197cee 100644 --- a/screen.go +++ b/screen.go @@ -1,10 +1,6 @@ package peco -import ( - "sync" - - "github.com/nsf/termbox-go" -) +import "github.com/nsf/termbox-go" // Screen hides termbox from tne consuming code so that // it can be swapped out for testing @@ -21,7 +17,7 @@ type Termbox struct{} // termbox always gives us some sort of warning when we run // go run -race cmd/peco/peco.go -var termboxMutex = &sync.Mutex{} +var termboxMutex = newMutex() func (t Termbox) Clear(fg, bg termbox.Attribute) error { termboxMutex.Lock() diff --git a/selection.go b/selection.go index cfcb877..f91a2a4 100644 --- a/selection.go +++ b/selection.go @@ -10,11 +10,11 @@ import ( // largest line number type Selection struct { selection []int - mutex *sync.Mutex + mutex sync.Locker } func NewSelection() *Selection { - return &Selection{nil,&sync.Mutex{}} + return &Selection{nil, newMutex()} } func (s *Selection) GetSelection() []int {