sync2

package
Version: v0.0.0-...-52ad444 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: BSD-3-Clause Imports: 5 Imported by: 5

Documentation

Overview

sync2 is a collection of functions meant to supplement the capabilities provided by the standard "sync" package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func With

func With(mu sync.Locker, f func())

Types

type AtomicDuration

type AtomicDuration int64

func (*AtomicDuration) Add

func (dur *AtomicDuration) Add(dururation time.Duration) time.Duration

func (*AtomicDuration) CompareAndSwap

func (dur *AtomicDuration) CompareAndSwap(oldval, newval time.Duration) (swapped bool)

func (*AtomicDuration) Get

func (dur *AtomicDuration) Get() time.Duration

func (*AtomicDuration) Set

func (dur *AtomicDuration) Set(dururation time.Duration)

type AtomicInt32

type AtomicInt32 int32

func (*AtomicInt32) Add

func (i32 *AtomicInt32) Add(n int32) int32

func (*AtomicInt32) CompareAndSwap

func (i32 *AtomicInt32) CompareAndSwap(oldval, newval int32) (swapped bool)

func (*AtomicInt32) Get

func (i32 *AtomicInt32) Get() int32

func (*AtomicInt32) Set

func (i32 *AtomicInt32) Set(n int32)

type AtomicInt64

type AtomicInt64 int64

func (*AtomicInt64) Add

func (i64 *AtomicInt64) Add(n int64) int64

func (*AtomicInt64) CompareAndSwap

func (i64 *AtomicInt64) CompareAndSwap(oldval, newval int64) (swapped bool)

func (*AtomicInt64) Get

func (i64 *AtomicInt64) Get() int64

func (*AtomicInt64) Set

func (i64 *AtomicInt64) Set(n int64)

type AtomicUint32

type AtomicUint32 uint32

func (*AtomicUint32) Add

func (u32 *AtomicUint32) Add(n uint32) uint32

func (*AtomicUint32) CompareAndSwap

func (u32 *AtomicUint32) CompareAndSwap(oldval, newval uint32) (swapped bool)

func (*AtomicUint32) Get

func (u32 *AtomicUint32) Get() uint32

func (*AtomicUint32) Set

func (u32 *AtomicUint32) Set(n uint32)

type BoundedRWLock

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

A fair RWLock with timeouts and a capacity.

Obeys the typical rules about RWLocks

1. If a writer holds the lock, only a single writer is in the lock.
2. If a writer does not hold the lock, any number of readers may hold the
lock.

The lock favors writers, but readers are not starved, and the next batch of readers will be served in before any waiting writers in FIFO order (when a writer releases the lock).

func NewBoundedRWLock

func NewBoundedRWLock(capacity int) *BoundedRWLock

Create a new BoundedRWLock with the given capacity.

RLocks or WLocks beyond this capacity will fail fast with an error.

func (*BoundedRWLock) RLock

func (rw *BoundedRWLock) RLock(timeout time.Duration) (err error)

Wait for a read lock for up to 'timeout'.

Error will be non-nil on timeout or when the wait list is at capacity.

func (*BoundedRWLock) RUnlock

func (rw *BoundedRWLock) RUnlock()

Unlock a read lock.

Should be called only on a goroutine which has gotten a non-error return value from RLock().

func (*BoundedRWLock) WLock

func (rw *BoundedRWLock) WLock(timeout time.Duration) (err error)

Lock for writing, waiting up to 'timeout' for successful exclusive acquisition of the lock.

func (*BoundedRWLock) WUnlock

func (rw *BoundedRWLock) WUnlock()

Unlock the write lock.

Should be called only on a goroutine which has gotten a non-error return value from WLock().

type Semaphore

type Semaphore interface {
	// Increment the semaphore counter by one.
	Release()

	// Decrement the semaphore counter by one, and block if counter < 0
	Acquire()

	// Decrement the semaphore counter by one, and block if counter < 0
	// Wait for up to the given duration.  Returns true if did not timeout
	TryAcquire(timeout time.Duration) bool
}

func NewBoundedSemaphore

func NewBoundedSemaphore(count uint) Semaphore

Create a bounded semaphore. The count parameter must be a positive number. NOTE: The bounded semaphore will panic if the user tries to Release beyond the specified count.

func NewUnboundedSemaphore

func NewUnboundedSemaphore(initialCount int) Semaphore

This returns an unbound counting semaphore with the specified initial count. The semaphore counter can be arbitrary large (i.e., Release can be called unlimited amount of times).

NOTE: In general, users should use bounded semaphore since it is more efficient than unbounded semaphore.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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