|
@@ -12,21 +12,21 @@ import (
|
12
|
12
|
// A semaphore of capacity 1 can be used as a trylock.
|
13
|
13
|
type Semaphore (chan empty)
|
14
|
14
|
|
15
|
|
-// Initialize initializes a semaphore to a given capacity.
|
16
|
|
-func (semaphore *Semaphore) Initialize(capacity int) {
|
17
|
|
- *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)
|
18
|
18
|
}
|
19
|
19
|
|
20
|
20
|
// Acquire acquires a semaphore, blocking if necessary.
|
21
|
|
-func (semaphore *Semaphore) Acquire() {
|
22
|
|
- (*semaphore) <- empty{}
|
|
21
|
+func (semaphore Semaphore) Acquire() {
|
|
22
|
+ semaphore <- empty{}
|
23
|
23
|
}
|
24
|
24
|
|
25
|
25
|
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
|
26
|
26
|
// successful. It never blocks.
|
27
|
|
-func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
|
27
|
+func (semaphore Semaphore) TryAcquire() (acquired bool) {
|
28
|
28
|
select {
|
29
|
|
- case (*semaphore) <- empty{}:
|
|
29
|
+ case semaphore <- empty{}:
|
30
|
30
|
return true
|
31
|
31
|
default:
|
32
|
32
|
return false
|
|
@@ -36,14 +36,14 @@ func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
36
|
36
|
// AcquireWithTimeout tries to acquire a semaphore, blocking for a maximum
|
37
|
37
|
// of approximately `d` while waiting for it. It returns whether the acquire
|
38
|
38
|
// was successful.
|
39
|
|
-func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
|
|
39
|
+func (semaphore Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
|
40
|
40
|
if timeout < 0 {
|
41
|
41
|
return semaphore.TryAcquire()
|
42
|
42
|
}
|
43
|
43
|
|
44
|
44
|
timer := time.NewTimer(timeout)
|
45
|
45
|
select {
|
46
|
|
- case (*semaphore) <- empty{}:
|
|
46
|
+ case semaphore <- empty{}:
|
47
|
47
|
acquired = true
|
48
|
48
|
case <-timer.C:
|
49
|
49
|
acquired = false
|
|
@@ -55,9 +55,9 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
55
|
55
|
// AcquireWithContext tries to acquire a semaphore, blocking at most until
|
56
|
56
|
// the context expires. It returns whether the acquire was successful.
|
57
|
57
|
// Note that if the context is already expired, the acquire may succeed anyway.
|
58
|
|
-func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
|
|
58
|
+func (semaphore Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
|
59
|
59
|
select {
|
60
|
|
- case (*semaphore) <- empty{}:
|
|
60
|
+ case semaphore <- empty{}:
|
61
|
61
|
acquired = true
|
62
|
62
|
case <-ctx.Done():
|
63
|
63
|
acquired = false
|
|
@@ -66,6 +66,6 @@ func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bo
|
66
|
66
|
}
|
67
|
67
|
|
68
|
68
|
// Release releases a semaphore.
|
69
|
|
-func (semaphore *Semaphore) Release() {
|
70
|
|
- <-(*semaphore)
|
|
69
|
+func (semaphore Semaphore) Release() {
|
|
70
|
+ <-semaphore
|
71
|
71
|
}
|