Documentation ¶
Overview ¶
Package cyclicbarrier provides an implementation of Cyclic Barrier primitive.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBrokenBarrier error used when a goroutine tries to wait upon a barrier that is in a broken state, // or which enters the broken state while the goroutine is waiting. ErrBrokenBarrier = errors.New("broken barrier") )
Functions ¶
This section is empty.
Types ¶
type CyclicBarrier ¶
type CyclicBarrier interface { // Await waits until all parties have invoked await on this barrier. // If the barrier is reset while any goroutine is waiting, or if the barrier is broken when await is invoked, // or while any goroutine is waiting, then ErrBrokenBarrier is returned. // If any goroutine is interrupted by ctx.Done() while waiting, then all other waiting goroutines // will return ErrBrokenBarrier and the barrier is placed in the broken state. // If the current goroutine is the last goroutine to arrive, and a non-nil barrier action was supplied in the constructor, // then the current goroutine runs the action before allowing the other goroutines to continue. // If an error occurs during the barrier action then that error will be returned and the barrier is placed in the broken state. Await(ctx context.Context) error // Reset resets the barrier to its initial state. // If any parties are currently waiting at the barrier, they will return with a ErrBrokenBarrier. Reset() // GetNumberWaiting returns the number of parties currently waiting at the barrier. GetNumberWaiting() int // GetParties returns the number of parties required to trip this barrier. GetParties() int // IsBroken queries if this barrier is in a broken state. // Returns true if one or more parties broke out of this barrier due to interruption by ctx.Done() or the last reset, // or a barrier action failed due to an error; false otherwise. IsBroken() bool }
CyclicBarrier is a synchronizer that allows a set of goroutines to wait for each other to reach a common execution point, also called a barrier. CyclicBarriers are useful in programs involving a fixed sized party of goroutines that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting goroutines are released. A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last goroutine in the party arrives, but before any goroutines are released. This barrier action is useful for updating shared-state before any of the parties continue.
func New ¶
func New(parties int) CyclicBarrier
New initializes a new instance of the CyclicBarrier, specifying the number of parties.
func NewWithAction ¶
func NewWithAction(parties int, barrierAction func() error) CyclicBarrier
NewWithAction initializes a new instance of the CyclicBarrier, specifying the number of parties and the barrier action.