|
@@ -5,8 +5,6 @@ package utils
|
5
|
5
|
|
6
|
6
|
import (
|
7
|
7
|
"context"
|
8
|
|
- "log"
|
9
|
|
- "runtime/debug"
|
10
|
8
|
"time"
|
11
|
9
|
)
|
12
|
10
|
|
|
@@ -14,21 +12,21 @@ import (
|
14
|
12
|
// A semaphore of capacity 1 can be used as a trylock.
|
15
|
13
|
type Semaphore (chan empty)
|
16
|
14
|
|
17
|
|
-// Initialize initializes a semaphore to a given capacity.
|
18
|
|
-func (semaphore *Semaphore) Initialize(capacity int) {
|
19
|
|
- *semaphore = make(chan empty, capacity)
|
|
15
|
+// NewSemaphore creates and initializes a semaphore to a given capacity.
|
|
16
|
+func NewSemaphore(capacity int) Semaphore {
|
|
17
|
+ return make(chan empty, capacity)
|
20
|
18
|
}
|
21
|
19
|
|
22
|
20
|
// Acquire acquires a semaphore, blocking if necessary.
|
23
|
|
-func (semaphore *Semaphore) Acquire() {
|
24
|
|
- (*semaphore) <- empty{}
|
|
21
|
+func (semaphore Semaphore) Acquire() {
|
|
22
|
+ semaphore <- empty{}
|
25
|
23
|
}
|
26
|
24
|
|
27
|
25
|
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
|
28
|
26
|
// successful. It never blocks.
|
29
|
|
-func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
|
27
|
+func (semaphore Semaphore) TryAcquire() (acquired bool) {
|
30
|
28
|
select {
|
31
|
|
- case (*semaphore) <- empty{}:
|
|
29
|
+ case semaphore <- empty{}:
|
32
|
30
|
return true
|
33
|
31
|
default:
|
34
|
32
|
return false
|
|
@@ -38,14 +36,14 @@ func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
38
|
36
|
// AcquireWithTimeout tries to acquire a semaphore, blocking for a maximum
|
39
|
37
|
// of approximately `d` while waiting for it. It returns whether the acquire
|
40
|
38
|
// was successful.
|
41
|
|
-func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
|
|
39
|
+func (semaphore Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
|
42
|
40
|
if timeout < 0 {
|
43
|
41
|
return semaphore.TryAcquire()
|
44
|
42
|
}
|
45
|
43
|
|
46
|
44
|
timer := time.NewTimer(timeout)
|
47
|
45
|
select {
|
48
|
|
- case (*semaphore) <- empty{}:
|
|
46
|
+ case semaphore <- empty{}:
|
49
|
47
|
acquired = true
|
50
|
48
|
case <-timer.C:
|
51
|
49
|
acquired = false
|
|
@@ -57,9 +55,9 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
57
|
55
|
// AcquireWithContext tries to acquire a semaphore, blocking at most until
|
58
|
56
|
// the context expires. It returns whether the acquire was successful.
|
59
|
57
|
// Note that if the context is already expired, the acquire may succeed anyway.
|
60
|
|
-func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
|
|
58
|
+func (semaphore Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
|
61
|
59
|
select {
|
62
|
|
- case (*semaphore) <- empty{}:
|
|
60
|
+ case semaphore <- empty{}:
|
63
|
61
|
acquired = true
|
64
|
62
|
case <-ctx.Done():
|
65
|
63
|
acquired = false
|
|
@@ -67,15 +65,7 @@ func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bo
|
67
|
65
|
return
|
68
|
66
|
}
|
69
|
67
|
|
70
|
|
-// Release releases a semaphore. It never blocks. (This is not a license
|
71
|
|
-// to program spurious releases.)
|
72
|
|
-func (semaphore *Semaphore) Release() {
|
73
|
|
- select {
|
74
|
|
- case <-(*semaphore):
|
75
|
|
- // good
|
76
|
|
- default:
|
77
|
|
- // spurious release
|
78
|
|
- log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
|
79
|
|
- debug.PrintStack()
|
80
|
|
- }
|
|
68
|
+// Release releases a semaphore.
|
|
69
|
+func (semaphore Semaphore) Release() {
|
|
70
|
+ <-semaphore
|
81
|
71
|
}
|