mirror of
https://github.com/rclone/rclone.git
synced 2024-12-01 21:04:56 +01:00
39 lines
574 B
Go
39 lines
574 B
Go
// 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
|
|
}
|