mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 15:39:58 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
50
h2mux/booleanfuse.go
Normal file
50
h2mux/booleanfuse.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package h2mux
|
||||
|
||||
import "sync"
|
||||
|
||||
// BooleanFuse is a data structure that can be set once to a particular value using Fuse(value).
|
||||
// Subsequent calls to Fuse() will have no effect.
|
||||
type BooleanFuse struct {
|
||||
value int32
|
||||
mu sync.Mutex
|
||||
cond *sync.Cond
|
||||
}
|
||||
|
||||
func NewBooleanFuse() *BooleanFuse {
|
||||
f := &BooleanFuse{}
|
||||
f.cond = sync.NewCond(&f.mu)
|
||||
return f
|
||||
}
|
||||
|
||||
// Value gets the value
|
||||
func (f *BooleanFuse) Value() bool {
|
||||
// 0: unset
|
||||
// 1: set true
|
||||
// 2: set false
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.value == 1
|
||||
}
|
||||
|
||||
func (f *BooleanFuse) Fuse(result bool) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
newValue := int32(2)
|
||||
if result {
|
||||
newValue = 1
|
||||
}
|
||||
if f.value == 0 {
|
||||
f.value = newValue
|
||||
f.cond.Broadcast()
|
||||
}
|
||||
}
|
||||
|
||||
// Await blocks until Fuse has been called at least once.
|
||||
func (f *BooleanFuse) Await() bool {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for f.value == 0 {
|
||||
f.cond.Wait()
|
||||
}
|
||||
return f.value == 1
|
||||
}
|
Reference in New Issue
Block a user