rclone/vendor/storj.io/drpc/drpcstream/chmutex.go

39 lines
574 B
Go
Raw Normal View History

2020-05-11 20:57:46 +02:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package drpcstream
import "sync"
type chMutex struct {
ch chan struct{}
once sync.Once
}
func (m *chMutex) init() { m.ch = make(chan struct{}, 1) }
func (m *chMutex) Chan() chan struct{} {
m.once.Do(m.init)
return m.ch
}
func (m *chMutex) Lock() {
m.once.Do(m.init)
m.ch <- struct{}{}
}
func (m *chMutex) TryLock() bool {
m.once.Do(m.init)
select {
case m.ch <- struct{}{}:
return true
default:
return false
}
}
func (m *chMutex) Unlock() {
m.once.Do(m.init)
<-m.ch
}