1
1
// Copyright 2024 The Go Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
- package http2
4
+
5
+ // Package gate contains an alternative condition variable.
6
+ package gate
5
7
6
8
import "context"
7
9
8
- // An gate is a monitor (mutex + condition variable) with one bit of state.
10
+ // A Gate is a monitor (mutex + condition variable) with one bit of state.
9
11
//
10
12
// The condition may be either set or unset.
11
13
// Lock operations may be unconditional, or wait for the condition to be set.
12
14
// Unlock operations record the new state of the condition.
13
- type gate struct {
15
+ type Gate struct {
14
16
// When unlocked, exactly one of set or unset contains a value.
15
17
// When locked, neither chan contains a value.
16
18
set chan struct {}
17
19
unset chan struct {}
18
20
}
19
21
20
- // newGate returns a new, unlocked gate with the condition unset.
21
- func newGate () gate {
22
- g := newLockedGate ()
23
- g .unlock (false )
24
- return g
25
- }
26
-
27
- // newLockedGate returns a new, locked gate.
28
- func newLockedGate () gate {
29
- return gate {
22
+ // New returns a new, unlocked gate.
23
+ func New (set bool ) Gate {
24
+ g := Gate {
30
25
set : make (chan struct {}, 1 ),
31
26
unset : make (chan struct {}, 1 ),
32
27
}
28
+ g .Unlock (set )
29
+ return g
33
30
}
34
31
35
- // lock acquires the gate unconditionally.
32
+ // Lock acquires the gate unconditionally.
36
33
// It reports whether the condition is set.
37
- func (g * gate ) lock () (set bool ) {
34
+ func (g * Gate ) Lock () (set bool ) {
38
35
select {
39
36
case <- g .set :
40
37
return true
@@ -43,9 +40,9 @@ func (g *gate) lock() (set bool) {
43
40
}
44
41
}
45
42
46
- // waitAndLock waits until the condition is set before acquiring the gate.
47
- // If the context expires, waitAndLock returns an error and does not acquire the gate.
48
- func (g * gate ) waitAndLock (ctx context.Context ) error {
43
+ // WaitAndLock waits until the condition is set before acquiring the gate.
44
+ // If the context expires, WaitAndLock returns an error and does not acquire the gate.
45
+ func (g * Gate ) WaitAndLock (ctx context.Context ) error {
49
46
select {
50
47
case <- g .set :
51
48
return nil
@@ -59,8 +56,8 @@ func (g *gate) waitAndLock(ctx context.Context) error {
59
56
}
60
57
}
61
58
62
- // lockIfSet acquires the gate if and only if the condition is set.
63
- func (g * gate ) lockIfSet () (acquired bool ) {
59
+ // LockIfSet acquires the gate if and only if the condition is set.
60
+ func (g * Gate ) LockIfSet () (acquired bool ) {
64
61
select {
65
62
case <- g .set :
66
63
return true
@@ -69,17 +66,11 @@ func (g *gate) lockIfSet() (acquired bool) {
69
66
}
70
67
}
71
68
72
- // unlock sets the condition and releases the gate.
73
- func (g * gate ) unlock (set bool ) {
69
+ // Unlock sets the condition and releases the gate.
70
+ func (g * Gate ) Unlock (set bool ) {
74
71
if set {
75
72
g .set <- struct {}{}
76
73
} else {
77
74
g .unset <- struct {}{}
78
75
}
79
76
}
80
-
81
- // unlockFunc sets the condition to the result of f and releases the gate.
82
- // Useful in defers.
83
- func (g * gate ) unlockFunc (f func () bool ) {
84
- g .unlock (f ())
85
- }
0 commit comments