forked from Hyperledger-TWGC/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore.go
59 lines (51 loc) · 1.26 KB
/
semaphore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// Package semaphore provides an implementation of a counting semaphore.
package semaphore
import (
"context"
)
// Semaphore is a buffered channel based implementation of a counting
// semaphore.
type Semaphore chan struct{}
// New creates a Semaphore with the specified number of permits.
func New(permits int) Semaphore {
if permits <= 0 {
panic("permits must be greater than 0")
}
return make(chan struct{}, permits)
}
// Acquire acquires a permit. This call will block until a permit is available
// or the provided context is completed.
//
// If the provided context is completed, the method will return the
// cancellation error.
func (s Semaphore) Acquire(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case s <- struct{}{}:
return nil
}
}
// TryAcquire acquires the semaphore without blocking.
// Returns true if the semaphore is acquired.
// Otherwise, returns false and leaves the semaphore unchanged.
func (s Semaphore) TryAcquire() bool {
select {
case s <- struct{}{}:
return true
default:
return false
}
}
// Release releases a permit.
func (s Semaphore) Release() {
select {
case <-s:
default:
panic("semaphore buffer is empty")
}
}