forked from External/ergo
move StringSet to utils package
This commit is contained in:
parent
ddac7d94a8
commit
df8be72c6f
9 changed files with 50 additions and 40 deletions
|
|
@ -10,27 +10,25 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type e struct{}
|
||||
|
||||
// Semaphore is a counting semaphore.
|
||||
// A semaphore of capacity 1 can be used as a trylock.
|
||||
type Semaphore (chan e)
|
||||
type Semaphore (chan empty)
|
||||
|
||||
// Initialize initializes a semaphore to a given capacity.
|
||||
func (semaphore *Semaphore) Initialize(capacity int) {
|
||||
*semaphore = make(chan e, capacity)
|
||||
*semaphore = make(chan empty, capacity)
|
||||
}
|
||||
|
||||
// Acquire acquires a semaphore, blocking if necessary.
|
||||
func (semaphore *Semaphore) Acquire() {
|
||||
(*semaphore) <- e{}
|
||||
(*semaphore) <- empty{}
|
||||
}
|
||||
|
||||
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
|
||||
// successful. It never blocks.
|
||||
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
||||
select {
|
||||
case (*semaphore) <- e{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
|
@ -47,7 +45,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
|||
|
||||
timer := time.NewTimer(timeout)
|
||||
select {
|
||||
case (*semaphore) <- e{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
acquired = true
|
||||
case <-timer.C:
|
||||
acquired = false
|
||||
|
|
@ -61,7 +59,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
|||
// Note that if the context is already expired, the acquire may succeed anyway.
|
||||
func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
|
||||
select {
|
||||
case (*semaphore) <- e{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
acquired = true
|
||||
case <-ctx.Done():
|
||||
acquired = false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue