-
Notifications
You must be signed in to change notification settings - Fork 1
/
gate.go
93 lines (85 loc) · 2.53 KB
/
gate.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gate contains an alternative condition variable.
//
// A gate is a monitor (mutex + condition variable) with one bit of state.
//
// A gate exists in one of three states:
// - locked
// - unlocked and set
// - unlocked and unset
//
// Lock operations may be unconditional, or wait for the condition to be set.
// Unlock operations record the new state of the condition.
//
// Gates have several advantages over sync.Cond:
// - Wait operations can be easily bounded by a context.Context lifetime.
// - A Wait operation only returns successfully when the gate condition is set.
// For example, if a gate's condition is set when a queue is non-empty,
// then a successful return from Wait guarantees that an item is in the queue.
// - No need to call Signal/Broadcast to notify waiters of a change in the condition.
package gate
import "context"
// A gate is a monitor (mutex + condition variable) with one bit of state.
type Gate struct {
// When unlocked, exactly one of set or unset contains a value.
// When locked, neither chan contains a value.
set chan struct{}
unset chan struct{}
}
// New returns a new, unlocked gate with the given condition state.
func New(set bool) Gate {
g := Gate{
set: make(chan struct{}, 1),
unset: make(chan struct{}, 1),
}
g.Unlock(set)
return g
}
// Lock acquires the gate unconditionally.
// It reports whether the condition was set.
func (g *Gate) Lock() (set bool) {
// This doesn't take a Context parameter because
// we don't expect unconditional lock operations to be time-bounded.
select {
case <-g.set:
return true
case <-g.unset:
return false
}
}
// WaitAndLock waits until the condition is set before acquiring the gate.
// If the context expires, WaitAndLock returns an error and does not acquire the gate.
func (g *Gate) WaitAndLock(ctx context.Context) error {
// If the gate is available and the context is expired,
// prefer locking the gate.
select {
case <-g.set:
return nil
default:
}
select {
case <-g.set:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// LockIfSet acquires the gate if and only if the condition is set.
func (g *Gate) LockIfSet() (acquired bool) {
select {
case <-g.set:
return true
default:
return false
}
}
// Unlock sets the condition and releases the gate.
func (g *Gate) Unlock(set bool) {
if set {
g.set <- struct{}{}
} else {
g.unset <- struct{}{}
}
}