simler lock for account guard

This commit is contained in:
Radu Ioan Fericean
2015-04-03 21:21:14 +03:00
parent 53a4895683
commit acf2b44fe3

View File

@@ -30,7 +30,7 @@ func init() {
type AccountLock struct {
queue map[string]chan bool
mu sync.RWMutex
mu sync.Mutex
}
func NewAccountLock() *AccountLock {
@@ -38,34 +38,30 @@ func NewAccountLock() *AccountLock {
}
func (cm *AccountLock) GuardCallCost(handler func() (*CallCost, error), name string) (reply *CallCost, err error) {
cm.mu.RLock()
cm.mu.Lock()
lock, exists := AccLock.queue[name]
cm.mu.RUnlock()
if !exists {
cm.mu.Lock()
lock = make(chan bool, 1)
AccLock.queue[name] = lock
cm.mu.Unlock()
}
lock <- true
cm.mu.Unlock()
reply, err = handler()
<-lock
return
}
func (cm *AccountLock) Guard(handler func() (float64, error), names ...string) (reply float64, err error) {
cm.mu.Lock()
for _, name := range names {
cm.mu.RLock()
lock, exists := AccLock.queue[name]
cm.mu.RUnlock()
if !exists {
cm.mu.Lock()
lock = make(chan bool, 1)
AccLock.queue[name] = lock
cm.mu.Unlock()
}
lock <- true
}
cm.mu.Unlock()
reply, err = handler()
for _, name := range names {
lock := AccLock.queue[name]