sync

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2016 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

Package sync implements primitives for synchronization between processes.

Index

Constants

View Source
const (

	// FUTEX_PRIVATE_FLAG is used to optimize futex usage for process-private futexes.
	FUTEX_PRIVATE_FLAG = 128
	// FUTEX_CLOCK_REALTIME is used to tell the kernel, that is must treat timeouts for
	// FUTEX_WAIT_BITSET, FUTEX_WAIT_REQUEUE_PI, and FUTEX_WAIT as an absolute time based on CLOCK_REALTIME
	FUTEX_CLOCK_REALTIME = 256
)

Variables

View Source
var (
	// MaxCondWaiters is the maximum length of the waiting queue for this type of a cond.
	// This limit is actual for waitlist-based condvars, currently on windows and darwin.
	// If this limit is exceeded, Wait/WaitTimeout will panic with ErrTooManyWaiters.
	MaxCondWaiters = 128
	// ErrTooManyWaiters is an error, that indicates, that the waiting queue is full.
	ErrTooManyWaiters = errors.New("waiters limit has been reached")
)

Functions

func DestroyCond added in v0.3.0

func DestroyCond(name string) error

DestroyCond permanently removes condvar with the given name.

func DestroyFutexMutex

func DestroyFutexMutex(name string) error

DestroyFutexMutex permanently removes mutex with the given name.

func DestroyMutex

func DestroyMutex(name string) error

DestroyMutex permanently removes mutex with the given name.

func DestroySemaMutex

func DestroySemaMutex(name string) error

DestroySemaMutex permanently removes mutex with the given name.

func DestroySpinMutex

func DestroySpinMutex(name string) error

DestroySpinMutex removes a mutex object with the given name

func FutexWait added in v0.3.0

func FutexWait(addr unsafe.Pointer, value uint32, timeout time.Duration, flags int32) error

FutexWait checks if the the value equals futex's value. If it doesn't, Wait returns EWOULDBLOCK. Otherwise, it waits for the Wake call on the futex for not longer, than timeout.

func FutexWake added in v0.3.0

func FutexWake(addr unsafe.Pointer, count uint32, flags int32) (int, error)

FutexWake wakes count threads waiting on the futex. Returns the number of woken threads.

Types

type Cond added in v0.3.0

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

Cond is a named interprocess condition variable.

func NewCond added in v0.3.0

func NewCond(name string, flag int, perm os.FileMode, l IPCLocker) (*Cond, error)

NewCond returns new interprocess condvar.

name - unique condvar name.
flag - a combination of open flags from 'os' package.
perm - object's permission bits.
l - a locker, associated with the shared resource.

func (*Cond) Broadcast added in v0.3.0

func (c *Cond) Broadcast()

Broadcast wakes all waiters.

func (*Cond) Close added in v0.3.0

func (c *Cond) Close() error

Close releases resources of the cond's shared state.

func (*Cond) Destroy added in v0.3.0

func (c *Cond) Destroy() error

Destroy permanently removes condvar.

func (*Cond) Signal added in v0.3.0

func (c *Cond) Signal()

Signal wakes one waiter.

func (*Cond) Wait added in v0.3.0

func (c *Cond) Wait()

Wait waits for the condvar to be signaled.

func (*Cond) WaitTimeout added in v0.3.0

func (c *Cond) WaitTimeout(timeout time.Duration) bool

WaitTimeout waits for the condvar to be signaled for not longer, than timeout.

type FutexMutex

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

FutexMutex is a mutex based on linux futex object.

func NewFutexMutex

func NewFutexMutex(name string, flag int, perm os.FileMode) (*FutexMutex, error)

NewFutexMutex creates a new futex-based mutex. This implementation is based on a paper 'Futexes Are Tricky' by Ulrich Drepper, this document can be found in 'docs' folder.

name - object name.
flag - flag is a combination of open flags from 'os' package.
perm - object's permission bits.

func (*FutexMutex) Close

func (f *FutexMutex) Close() error

Close indicates, that the object is no longer in use, and that the underlying resources can be freed.

func (*FutexMutex) Destroy

func (f *FutexMutex) Destroy() error

Destroy removes the mutex object.

func (*FutexMutex) Lock

func (f *FutexMutex) Lock()

Lock locks the mutex. It panics on an error.

func (*FutexMutex) LockTimeout

func (f *FutexMutex) LockTimeout(timeout time.Duration) bool

LockTimeout tries to lock the locker, waiting for not more, than timeout.

func (*FutexMutex) Unlock

func (f *FutexMutex) Unlock()

Unlock releases the mutex. It panics on an error.

type IPCLocker

type IPCLocker interface {
	sync.Locker
	io.Closer
}

IPCLocker is a minimal interface, which must be satisfied by any synchronization primitive on any platform.

func NewMutex

func NewMutex(name string, flag int, perm os.FileMode) (IPCLocker, error)

NewMutex creates a new interprocess mutex. It uses the default implementation on the current platform.

name - object name.
flag - flag is a combination of open flags from 'os' package.
perm - object's permission bits.

type InplaceMutex added in v0.3.0

type InplaceMutex uint32

InplaceMutex is a linux futex, which can be placed into a shared memory region.

func NewInplaceMutex added in v0.3.0

func NewInplaceMutex(ptr unsafe.Pointer) *InplaceMutex

NewInplaceMutex creates a futex object on the given memory location.

ptr - memory location for the futex.

func (*InplaceMutex) Init added in v0.3.0

func (f *InplaceMutex) Init()

Init writes initial value into futex's memory location.

func (*InplaceMutex) Lock added in v0.3.0

func (f *InplaceMutex) Lock()

Lock locks the locker.

func (*InplaceMutex) LockTimeout added in v0.3.0

func (f *InplaceMutex) LockTimeout(timeout time.Duration) bool

LockTimeout tries to lock the locker, waiting for not more, than timeout.

func (*InplaceMutex) TryLock added in v0.3.0

func (f *InplaceMutex) TryLock() bool

TryLock tries to lock the locker. Return true, if it was locked.

func (*InplaceMutex) Unlock added in v0.3.0

func (f *InplaceMutex) Unlock()

Unlock releases the mutex. It panics on an error.

type SemaMutex

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

SemaMutex is a semaphore-based mutex for unix.

func NewSemaMutex

func NewSemaMutex(name string, flag int, perm os.FileMode) (*SemaMutex, error)

NewSemaMutex creates a new mutex.

func (*SemaMutex) Close

func (m *SemaMutex) Close() error

Close is a no-op for unix mutex.

func (*SemaMutex) Destroy

func (m *SemaMutex) Destroy() error

Destroy closes the mutex and removes it permanently.

func (*SemaMutex) Lock

func (m *SemaMutex) Lock()

Lock locks the mutex. It panics on an error.

func (*SemaMutex) LockTimeout

func (m *SemaMutex) LockTimeout(timeout time.Duration) bool

LockTimeout tries to lock the locker, waiting for not more, than timeout.

func (*SemaMutex) Unlock

func (m *SemaMutex) Unlock()

Unlock releases the mutex. It panics on an error.

type Semaphore

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

Semaphore is a sysV semaphore.

func NewSemaphore

func NewSemaphore(name string, mode int, perm os.FileMode, initial int) (*Semaphore, error)

NewSemaphore creates a new sysV semaphore with the given name. It generates a key from the name, and then calls NewSemaphoreKey.

func NewSemaphoreKey

func NewSemaphoreKey(key uint64, flag int, perm os.FileMode, initial int) (*Semaphore, error)

NewSemaphoreKey creates a new sysV semaphore for the given key.

key - object key. each semaphore object is identifyed by a unique key.
flag - flag is a combination of open flags from 'os' package.
perm - object's permission bits.
initial - this value will be added to the semaphore's value, if it was created.

func (*Semaphore) Add

func (s *Semaphore) Add(value int) error

Add adds the given value to the semaphore's value. It locks, if the operation cannot be done immediately.

func (*Semaphore) AddTimeout

func (s *Semaphore) AddTimeout(timeout time.Duration, value int) error

AddTimeout add the given value to the semaphore's value. If the operation locks, it waits for not more, than timeout.

func (*Semaphore) Destroy

func (s *Semaphore) Destroy() error

Destroy removes the semaphore permanently.

type SpinMutex

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

SpinMutex is a synchronization object which performs busy wait loop.

func NewSpinMutex

func NewSpinMutex(name string, flag int, perm os.FileMode) (*SpinMutex, error)

NewSpinMutex creates a new spin mutex.

name - object name.
flag - flag is a combination of open flags from 'os' package.
perm - object's permission bits.

func (*SpinMutex) Close

func (spin *SpinMutex) Close() error

Close indicates, that the object is no longer in use, and that the underlying resources can be freed.

func (*SpinMutex) Destroy

func (spin *SpinMutex) Destroy() error

Destroy removes the mutex object.

func (*SpinMutex) Lock

func (spin *SpinMutex) Lock()

Lock locks the mutex waiting in a busy loop if needed.

func (*SpinMutex) LockTimeout added in v0.3.0

func (spin *SpinMutex) LockTimeout(timeout time.Duration) bool

LockTimeout locks the mutex waiting in a busy loop for not longer, than timeout.

func (*SpinMutex) TryLock

func (spin *SpinMutex) TryLock() bool

TryLock makes one attempt to lock the mutex. It return true on succeess and false otherwise.

func (*SpinMutex) Unlock

func (spin *SpinMutex) Unlock()

Unlock releases the mutex.

type TimedIPCLocker

type TimedIPCLocker interface {
	IPCLocker
	// LockTimeout tries to lock the locker, waiting for not more, than timeout
	LockTimeout(timeout time.Duration) bool
}

TimedIPCLocker is a locker, whose lock operation can be limited with duration.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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