Documentation ¶
Overview ¶
Package percpu provides best-effort CPU-local sharded values.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
A Counter is an int64 counter which may be efficiently incremented by many goroutines concurrently.
The counter is sharded into several CPU-local values which are written and read independently. Thus, the Load and Reset methods do not observe a consistent view of the total if they are called concurrently to Add.
For example, suppose goroutine G1 runs
counter.Add(1) counter.Add(2)
and, concurrently, G2 runs
t0 := counter.Reset() // wait for G1 to finish executing t1 := counter.Load()
The value of t0 may be any of 0, 1, 2, or 3. The value of t1 may be any of 0, 1, 2, or 3 as well. However, t0+t1 must equal 3.
func NewCounter ¶
func NewCounter() *Counter
NewCounter returns a fresh Counter initialized to zero.
type Values ¶
type Values struct {
// contains filtered or unexported fields
}
Values is a sharded set of values which have an affinity for a particular processor. This can be used to avoid cache contention when updating a shared value simultaneously from many goroutines.
func NewValues ¶
func NewValues(newVal func() interface{}) *Values
NewValues constructs a Values using the provided constructor function to create each shard value.
func (*Values) Do ¶
func (vs *Values) Do(fn func(interface{}))
Do runs fn on all of the values in vs.
func (*Values) Get ¶
func (vs *Values) Get() interface{}
Get returns one of the values in vs.
The value tends to be the one associated with the current processor. However, goroutines can migrate at any time, and it may be the case that a different goroutine is accessing the same value concurrently. All access of the returned value must use further synchronization mechanisms.
BUG(cespare): If GOMAXPROCS has changed since a Values was created with NewValues, Get may panic.
Notes ¶
Bugs ¶
If GOMAXPROCS has changed since a Values was created with NewValues, Get may panic.