21
vendor/github.com/anacrolix/chansync/LICENSE
generated
vendored
Normal file
21
vendor/github.com/anacrolix/chansync/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Matt Joiner
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
34
vendor/github.com/anacrolix/chansync/broadcast-cond.go.go
generated
vendored
Normal file
34
vendor/github.com/anacrolix/chansync/broadcast-cond.go.go
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package chansync
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/chansync/events"
|
||||
"github.com/anacrolix/sync"
|
||||
)
|
||||
|
||||
// Can be used as zero-value. Due to the caller needing to bring their own synchronization, an
|
||||
// equivalent to "sync".Cond.Signal is not provided. BroadcastCond is intended to be selected on
|
||||
// with other channels.
|
||||
type BroadcastCond struct {
|
||||
mu sync.Mutex
|
||||
ch chan struct{}
|
||||
}
|
||||
|
||||
func (me *BroadcastCond) Broadcast() {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
if me.ch != nil {
|
||||
close(me.ch)
|
||||
me.ch = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Should be called before releasing locks on resources that might trigger subsequent Broadcasts.
|
||||
// The channel is closed when the condition changes.
|
||||
func (me *BroadcastCond) Signaled() events.Signaled {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
if me.ch == nil {
|
||||
me.ch = make(chan struct{})
|
||||
}
|
||||
return me.ch
|
||||
}
|
||||
12
vendor/github.com/anacrolix/chansync/events/events.go
generated
vendored
Normal file
12
vendor/github.com/anacrolix/chansync/events/events.go
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
package events
|
||||
|
||||
// Here we'll strongly-type channels to assist correct usage, if possible.
|
||||
|
||||
type (
|
||||
Signaled <-chan struct{}
|
||||
Done <-chan struct{}
|
||||
Active <-chan struct{}
|
||||
Signal chan<- struct{}
|
||||
Acquire chan<- struct{}
|
||||
Release <-chan struct{}
|
||||
)
|
||||
79
vendor/github.com/anacrolix/chansync/flag.go
generated
vendored
Normal file
79
vendor/github.com/anacrolix/chansync/flag.go
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
package chansync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/chansync/events"
|
||||
)
|
||||
|
||||
// Flag wraps a boolean value that starts as false (off). You can wait for it to be on or off, and
|
||||
// set the value as needed.
|
||||
type Flag struct {
|
||||
mu sync.Mutex
|
||||
on chan struct{}
|
||||
off chan struct{}
|
||||
state bool
|
||||
inited bool
|
||||
}
|
||||
|
||||
func (me *Flag) Bool() bool {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
return me.state
|
||||
}
|
||||
|
||||
func (me *Flag) On() events.Active {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
me.init()
|
||||
return me.on
|
||||
}
|
||||
|
||||
func (me *Flag) Off() events.Active {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
me.init()
|
||||
return me.off
|
||||
}
|
||||
|
||||
func (me *Flag) init() {
|
||||
if me.inited {
|
||||
return
|
||||
}
|
||||
me.on = make(chan struct{})
|
||||
me.off = make(chan struct{})
|
||||
close(me.off)
|
||||
me.inited = true
|
||||
}
|
||||
|
||||
func (me *Flag) SetBool(b bool) {
|
||||
if b {
|
||||
me.Set()
|
||||
} else {
|
||||
me.Clear()
|
||||
}
|
||||
}
|
||||
|
||||
func (me *Flag) Set() {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
me.init()
|
||||
if me.state {
|
||||
return
|
||||
}
|
||||
me.state = true
|
||||
close(me.on)
|
||||
me.off = make(chan struct{})
|
||||
}
|
||||
|
||||
func (me *Flag) Clear() {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
me.init()
|
||||
if !me.state {
|
||||
return
|
||||
}
|
||||
me.state = false
|
||||
close(me.off)
|
||||
me.on = make(chan struct{})
|
||||
}
|
||||
28
vendor/github.com/anacrolix/chansync/level.go
generated
vendored
Normal file
28
vendor/github.com/anacrolix/chansync/level.go
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
package chansync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/chansync/events"
|
||||
)
|
||||
|
||||
type LevelTrigger struct {
|
||||
ch chan struct{}
|
||||
initOnce sync.Once
|
||||
}
|
||||
|
||||
func (me *LevelTrigger) Signal() events.Signal {
|
||||
me.init()
|
||||
return me.ch
|
||||
}
|
||||
|
||||
func (me *LevelTrigger) Active() events.Active {
|
||||
me.init()
|
||||
return me.ch
|
||||
}
|
||||
|
||||
func (me *LevelTrigger) init() {
|
||||
me.initOnce.Do(func() {
|
||||
me.ch = make(chan struct{})
|
||||
})
|
||||
}
|
||||
26
vendor/github.com/anacrolix/chansync/semaphore.go
generated
vendored
Normal file
26
vendor/github.com/anacrolix/chansync/semaphore.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
package chansync
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/chansync/events"
|
||||
)
|
||||
|
||||
// Channel semaphore, as is popular for controlling access to limited resources. Should not be used
|
||||
// zero-initialized.
|
||||
type Semaphore struct {
|
||||
ch chan struct{}
|
||||
}
|
||||
|
||||
// Returns an initialized semaphore with n slots.
|
||||
func NewSemaphore(n int) Semaphore {
|
||||
return Semaphore{ch: make(chan struct{}, n)}
|
||||
}
|
||||
|
||||
// Returns a channel for acquiring a slot.
|
||||
func (me Semaphore) Acquire() events.Acquire {
|
||||
return me.ch
|
||||
}
|
||||
|
||||
// Returns a channel for releasing a slot.
|
||||
func (me Semaphore) Release() events.Release {
|
||||
return me.ch
|
||||
}
|
||||
44
vendor/github.com/anacrolix/chansync/set-once.go
generated
vendored
Normal file
44
vendor/github.com/anacrolix/chansync/set-once.go
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
package chansync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/anacrolix/chansync/events"
|
||||
)
|
||||
|
||||
// SetOnce is a boolean value that can only be flipped from false to true.
|
||||
type SetOnce struct {
|
||||
ch chan struct{}
|
||||
// Could be faster than trying to receive from ch
|
||||
closed uint32
|
||||
initOnce sync.Once
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
// Returns a channel that is closed when the event is flagged.
|
||||
func (me *SetOnce) Done() events.Done {
|
||||
me.init()
|
||||
return me.ch
|
||||
}
|
||||
|
||||
func (me *SetOnce) init() {
|
||||
me.initOnce.Do(func() {
|
||||
me.ch = make(chan struct{})
|
||||
})
|
||||
}
|
||||
|
||||
// Set only returns true the first time it is called.
|
||||
func (me *SetOnce) Set() (first bool) {
|
||||
me.closeOnce.Do(func() {
|
||||
me.init()
|
||||
first = true
|
||||
atomic.StoreUint32(&me.closed, 1)
|
||||
close(me.ch)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (me *SetOnce) IsSet() bool {
|
||||
return atomic.LoadUint32(&me.closed) != 0
|
||||
}
|
||||
Reference in New Issue
Block a user