Documentation ¶
Overview ¶
Package syncx provides extensions to standard synchronization primitives.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter uint64
Counter is a convenience unit64 value counter with atomic operations. It is optimized for concurrent use by multiple incrementers, but is restricted to a single concurrent consumer. Protect access to the counter with a mutex if concurrent Draw attempts are anticipated.
func (*Counter) Add ¶
Add atomically adds the supplied value to the counter.
This method is safe for use in concurrent gorotines.
func (*Counter) Draw ¶
Draw atomically draws the counter counter. The counter's value it set to 0 and its previous value is returned.
This method is not safe for use in concurrent gorotines. It is however safe for use concurrently with Add method. If concurrent calls to Draw are anticipated they must be protected by a mutex.
type TickTockCounter ¶
type TickTockCounter uint64
TickTockCounter counts the number of Tick and Tock calls that it receives. Balancing ticks and tocks can then be "folded", such that both counts are reduced by the number of "tocks". I.e. 4 calls of Tick and 3 calls of Tock would result in the counts to be reduced to 1 and 0 respectively.
TickTockCounter places following constraints on its use:
1. A call to Tick must be guaranteed to have completed before calling its balancing Tock. Typically such balancing calls would be made serially from a single goroutine.
2. Concurrent calls to Fold are not allowed. Folding would typically be done in a single goroutine in a serial manner.
TickTockCounter is optimized for use with multiple concurrent "tikers" and a single folder. If concurrent calls to Fold are anticipated, they must be guarded by a mutex.
func (*TickTockCounter) Fold ¶
func (c *TickTockCounter) Fold() (ticks uint32, tocks uint32)
Fold collapses balancing "ticks" and "tocks" by reducing the counts by the number of "tocks" and returns pre-folded counts. Folding is done atomically, such that concurrent calls to Tick and Tock do not result in an imbalance as well as ensuring that no "ticks" or "tocks" are dropped or double-counted.
For performance reasons this method is not safe for use in concurrent gorotines. It is however safe for use concurrently with Tick and Tock calls. If concurrent calls to Draw are anticipated they must be protected by a mutex.
func (*TickTockCounter) Tick ¶
func (c *TickTockCounter) Tick()
Tick atomically increments "tick" counter. It is safe for use in concurrent gorotines as long as the caller ensures that corresponding Tock call is only made after Tick call.
func (*TickTockCounter) Tock ¶
func (c *TickTockCounter) Tock()
Tick atomically increments "tock" counter. It is safe for use in concurrent gorotines as long as the caller ensures that corresponding Tick call has already been made.
type TickTockFolder ¶
type TickTockFolder uint64
TickTockFolder counts the number of balanced Tick/Tock calls. Conceptually it comprises two counters, one for the number of complete pairs and one the number of pending ones, with each being a uint32.
TickTockFolder is optimized for concurrent use by multiple "tickers", but is restricted to a single concurrent consumer. Protect access to the counter with a mutex if concurrent Draw attempts are anticipated.
func (*TickTockFolder) Draw ¶
func (f *TickTockFolder) Draw() (complete uint32, pending uint32)
Draw atomically draws complete cycle counter. The counter's complete cycles count is set to 0 and the previous value is returned. Pending cycle count is also returned.
This method is not safe for use in concurrent gorotines. It is however safe for use concurrently with Tick and Tock methods. If concurrent calls to Draw are anticipated they must be protected by a mutex.
func (*TickTockFolder) Tick ¶
func (f *TickTockFolder) Tick()
Tick atomically increments pending cycle counter.
This method is safe for use in concurrent gorotines.
func (*TickTockFolder) Tock ¶
func (f *TickTockFolder) Tock()
Tock atomically decrements pending cycle counter and increments complete cycle counter.
This method is safe for use in concurrent gorotines.