-
-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
167 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package chansync | ||
|
||
import ( | ||
"github.com/anacrolix/sync" | ||
) | ||
|
||
// Can be used as zero-value. Due to the caller needing to bring their own synchronization, an | ||
// eqiuvalent 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. | ||
func (me *BroadcastCond) WaitChan() <-chan struct{} { | ||
me.mu.Lock() | ||
defer me.mu.Unlock() | ||
if me.ch == nil { | ||
me.ch = make(chan struct{}) | ||
} | ||
return me.ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package chansync | ||
|
||
import "sync" | ||
|
||
// SetOnce is a boolean value that can only be flipped from false to true. | ||
type SetOnce struct { | ||
ch chan struct{} | ||
initOnce sync.Once | ||
closeOnce sync.Once | ||
} | ||
|
||
func (me *SetOnce) Chan() <-chan struct{} { | ||
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 | ||
close(me.ch) | ||
}) | ||
return | ||
} | ||
|
||
func (me *SetOnce) IsSet() bool { | ||
me.init() | ||
select { | ||
case <-me.ch: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters