newMutex, sync.Locker

This commit is contained in:
Daisuke Maki 2014-10-14 16:44:33 +09:00
parent 38992a4cc0
commit 4594f2d26d
10 changed files with 29 additions and 33 deletions

16
ctx.go
View file

@ -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) {

16
hub.go
View file

@ -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.

View file

@ -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

View file

@ -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{}
}

View file

@ -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(),
}
}

View file

@ -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 {

View file

@ -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),
}
}

View file

@ -46,7 +46,7 @@ func (b *BufferReader) Loop() {
}
}()
m := &sync.Mutex{}
m := newMutex()
once := &sync.Once{}
var refresh *time.Timer

View file

@ -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()

View file

@ -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 {