sync

package
v0.0.0-...-4bf4b70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2021 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sync provides synchronization primitives.

Index

Constants

View Source
const RaceEnabled = false

RaceEnabled is true if the Go data race detector is enabled.

View Source
const (
	TraceEvGoBlockSelect byte = 24
)

Values for the traceEv argument to gopark, from Go's src/runtime/trace.go.

View Source
const (
	WaitReasonSelect uint8 = 9
)

Values for the reason argument to gopark, from Go's src/runtime/runtime2.go.

Variables

This section is empty.

Functions

func Gopark

func Gopark(unlockf func(uintptr, unsafe.Pointer) bool, lock unsafe.Pointer, reason uint8, traceEv byte, traceskip int)

Gopark is runtime.gopark. Gopark calls unlockf(pointer to runtime.g, lock); if unlockf returns true, Gopark blocks until Goready(pointer to runtime.g) is called. unlockf and its callees must be nosplit and norace, since stack splitting and race context are not available where it is called.

func Goready

func Goready(gp uintptr, traceskip int)

Goready is runtime.goready.

func MapKeyHasher

func MapKeyHasher(m interface{}) func(unsafe.Pointer, uintptr) uintptr

MapKeyHasher returns a hash function for pointers of m's key type.

Preconditions: m must be a map.

func Memmove

func Memmove(to, from unsafe.Pointer, n uintptr)

Memmove is runtime.memmove, exported for SeqAtomicLoad/SeqAtomicTryLoad<T>.

func RaceAcquire

func RaceAcquire(addr unsafe.Pointer)

RaceAcquire has the same semantics as runtime.RaceAcquire.

func RaceDisable

func RaceDisable()

RaceDisable has the same semantics as runtime.RaceDisable.

func RaceEnable

func RaceEnable()

RaceEnable has the same semantics as runtime.RaceEnable.

func RaceRelease

func RaceRelease(addr unsafe.Pointer)

RaceRelease has the same semantics as runtime.RaceRelease.

func RaceReleaseMerge

func RaceReleaseMerge(addr unsafe.Pointer)

RaceReleaseMerge has the same semantics as runtime.RaceReleaseMerge.

func RaceUncheckedAtomicCompareAndSwapUintptr

func RaceUncheckedAtomicCompareAndSwapUintptr(ptr *uintptr, old, new uintptr) bool

RaceUncheckedAtomicCompareAndSwapUintptr is equivalent to sync/atomic.CompareAndSwapUintptr, but is not checked by the race detector. This is necessary when implementing gopark callbacks, since no race context is available during their execution.

func Rand32

func Rand32() uint32

Rand32 returns a non-cryptographically-secure random uint32.

func Rand64

func Rand64() uint64

Rand64 returns a non-cryptographically-secure random uint64.

func RandUintptr

func RandUintptr() uintptr

RandUintptr returns a non-cryptographically-secure random uintptr.

Types

type Cond

type Cond = sync.Cond

Cond is an alias of sync.Cond.

func NewCond

func NewCond(l Locker) *Cond

NewCond is a wrapper around sync.NewCond.

type CrossGoroutineMutex

type CrossGoroutineMutex struct {
	sync.Mutex
}

CrossGoroutineMutex is equivalent to Mutex, but it need not be unlocked by a the same goroutine that locked the mutex.

func (*CrossGoroutineMutex) TryLock

func (m *CrossGoroutineMutex) TryLock() bool

TryLock tries to acquire the mutex. It returns true if it succeeds and false otherwise. TryLock does not block.

type CrossGoroutineRWMutex

type CrossGoroutineRWMutex struct {
	// contains filtered or unexported fields
}

CrossGoroutineRWMutex is equivalent to RWMutex, but it need not be unlocked by a the same goroutine that locked the mutex.

func (*CrossGoroutineRWMutex) DowngradeLock

func (rw *CrossGoroutineRWMutex) DowngradeLock()

DowngradeLock atomically unlocks rw for writing and locks it for reading.

Preconditions: * rw is locked for writing.

func (*CrossGoroutineRWMutex) Lock

func (rw *CrossGoroutineRWMutex) Lock()

Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.

func (*CrossGoroutineRWMutex) RLock

func (rw *CrossGoroutineRWMutex) RLock()

RLock locks rw for reading.

It should not be used for recursive read locking; a blocked Lock call excludes new readers from acquiring the lock. See the documentation on the RWMutex type.

func (*CrossGoroutineRWMutex) RUnlock

func (rw *CrossGoroutineRWMutex) RUnlock()

RUnlock undoes a single RLock call.

Preconditions: * rw is locked for reading.

func (*CrossGoroutineRWMutex) TryLock

func (rw *CrossGoroutineRWMutex) TryLock() bool

TryLock locks rw for writing. It returns true if it succeeds and false otherwise. It does not block.

func (*CrossGoroutineRWMutex) TryRLock

func (rw *CrossGoroutineRWMutex) TryRLock() bool

TryRLock locks rw for reading. It returns true if it succeeds and false otherwise. It does not block.

func (*CrossGoroutineRWMutex) Unlock

func (rw *CrossGoroutineRWMutex) Unlock()

Unlock unlocks rw for writing.

Preconditions: * rw is locked for writing.

type Locker

type Locker = sync.Locker

Locker is an alias of sync.Locker.

type Map

type Map = sync.Map

Map is an alias of sync.Map.

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.

A Mutex must not be copied after first use.

A Mutex must be unlocked by the same goroutine that locked it. This invariant is enforced with the 'checklocks' build tag.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

TryLock tries to acquire the mutex. It returns true if it succeeds and false otherwise. TryLock does not block.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks m.

Preconditions: * m is locked. * m was locked by this goroutine.

type NoCopy

type NoCopy struct{}

NoCopy may be embedded into structs which must not be copied after the first use.

See https://golang.org/issues/8005#issuecomment-190753527 for details.

func (*NoCopy) Lock

func (*NoCopy) Lock()

Lock is a no-op used by -copylocks checker from `go vet`.

func (*NoCopy) Unlock

func (*NoCopy) Unlock()

Unlock is a no-op used by -copylocks checker from `go vet`.

type Once

type Once = sync.Once

Once is an alias of sync.Once.

type Pool

type Pool = sync.Pool

Pool is an alias of sync.Pool.

type RWMutex

type RWMutex struct {
	// contains filtered or unexported fields
}

A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.

A RWMutex must not be copied after first use.

If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.

A Mutex must be unlocked by the same goroutine that locked it. This invariant is enforced with the 'checklocks' build tag.

func (*RWMutex) DowngradeLock

func (rw *RWMutex) DowngradeLock()

DowngradeLock atomically unlocks rw for writing and locks it for reading.

Preconditions: * rw is locked for writing.

func (*RWMutex) Lock

func (rw *RWMutex) Lock()

Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.

func (*RWMutex) RLock

func (rw *RWMutex) RLock()

RLock locks rw for reading.

It should not be used for recursive read locking; a blocked Lock call excludes new readers from acquiring the lock. See the documentation on the RWMutex type.

func (*RWMutex) RUnlock

func (rw *RWMutex) RUnlock()

RUnlock undoes a single RLock call.

Preconditions: * rw is locked for reading. * rw was locked by this goroutine.

func (*RWMutex) TryLock

func (rw *RWMutex) TryLock() bool

TryLock locks rw for writing. It returns true if it succeeds and false otherwise. It does not block.

func (*RWMutex) TryRLock

func (rw *RWMutex) TryRLock() bool

TryRLock locks rw for reading. It returns true if it succeeds and false otherwise. It does not block.

func (*RWMutex) Unlock

func (rw *RWMutex) Unlock()

Unlock unlocks rw for writing.

Preconditions: * rw is locked for writing. * rw was locked by this goroutine.

type SeqCount

type SeqCount struct {
	// contains filtered or unexported fields
}

SeqCount is a synchronization primitive for optimistic reader/writer synchronization in cases where readers can work with stale data and therefore do not need to block writers.

Compared to sync/atomic.Value:

- Mutation of SeqCount-protected data does not require memory allocation, whereas atomic.Value generally does. This is a significant advantage when writes are common.

- Atomic reads of SeqCount-protected data require copying. This is a disadvantage when atomic reads are common.

- SeqCount may be more flexible: correct use of SeqCount.ReadOk allows other operations to be made atomic with reads of SeqCount-protected data.

- SeqCount is more cumbersome to use; atomic reads of SeqCount-protected data require instantiating function templates using go_generics (see seqatomic.go).

func (*SeqCount) BeginRead

func (s *SeqCount) BeginRead() SeqCountEpoch

BeginRead indicates the beginning of a reader critical section. Reader critical sections DO NOT BLOCK writer critical sections, so operations in a reader critical section MAY RACE with writer critical sections. Races are detected by ReadOk at the end of the reader critical section. Thus, the low-level structure of readers is generally:

for {
    epoch := seq.BeginRead()
    // do something idempotent with seq-protected data
    if seq.ReadOk(epoch) {
        break
    }
}

However, since reader critical sections may race with writer critical sections, the Go race detector will (accurately) flag data races in readers using this pattern. Most users of SeqCount will need to use the SeqAtomicLoad function template in seqatomic.go.

func (*SeqCount) BeginWrite

func (s *SeqCount) BeginWrite()

BeginWrite indicates the beginning of a writer critical section.

SeqCount does not support concurrent writer critical sections; clients with concurrent writers must synchronize them using e.g. sync.Mutex.

func (*SeqCount) EndWrite

func (s *SeqCount) EndWrite()

EndWrite ends the effect of a preceding BeginWrite.

func (*SeqCount) ReadOk

func (s *SeqCount) ReadOk(epoch SeqCountEpoch) bool

ReadOk returns true if the reader critical section initiated by a previous call to BeginRead() that returned epoch did not race with any writer critical sections.

ReadOk may be called any number of times during a reader critical section. Reader critical sections do not need to be explicitly terminated; the last call to ReadOk is implicitly the end of the reader critical section.

type SeqCountEpoch

type SeqCountEpoch uint32

SeqCountEpoch tracks writer critical sections in a SeqCount.

type WaitGroup

type WaitGroup = sync.WaitGroup

WaitGroup is an alias of sync.WaitGroup.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL