synced

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 7 Imported by: 14 modules, 14 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Thread-safe things

  • Counter implements thread-safe integer counter.
  • Flag implements thread-safe bool flag.
  • Queue implements thread-safe queue.
  • Mutex implements a drop-in sync.Mutex replacement with callbacks.
  • RWMutex implements a drop-in sync.RWMutex replacement with callbacks.

Documentation

Index

Constants

View Source
const (
	QueueLatestElement       = -1
	QueueBeforeLatestElement = -2
)

various constants

Variables

View Source
var (
	ErrQueueIsEmpty    = errors.New("queue is empty")
	ErrQueueOverflowed = errors.New("queue overflowed")
	ErrFailedToDrop    = func(err error) error { return fmt.Errorf("failed to drop element: %v", err) }
	ErrOutOfBounds     = errors.New("index out of bounds")
)

errors

Functions

This section is empty.

Types

type Counter

type Counter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Counter that is thread-safe

func NewCounter

func NewCounter(initialValue int) Counter

NewCounter returns a new synced counter initialized by initialValue

func (*Counter) Add

func (c *Counter) Add(i int) int

Add i to counter. Returns original value

func (*Counter) Dec

func (c *Counter) Dec() int

Dec decreases counter by 1. Returns original value

func (*Counter) Get

func (c *Counter) Get() int

Get returns current counter value

func (*Counter) Inc

func (c *Counter) Inc() int

Inc increases counter by 1. Returns original value

func (*Counter) MarshalJSON added in v1.0.3

func (c *Counter) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Counter) Set

func (c *Counter) Set(i int) int

Set counter to i. Returns original value

func (*Counter) UnmarshalJSON added in v1.0.3

func (c *Counter) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

type Flag

type Flag struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Flag that is thread-safe

func NewFlag

func NewFlag(initialState bool) Flag

NewFlag returns a new synced flag initialized by initialValue

func (*Flag) Get

func (f *Flag) Get() bool

Get returns current flag state

func (*Flag) MarshalJSON added in v1.0.3

func (f *Flag) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Flag) Set

func (f *Flag) Set()

Set the flag

func (*Flag) SetState added in v1.0.4

func (f *Flag) SetState(state bool)

SetState of the flag

func (*Flag) UnmarshalJSON added in v1.0.3

func (f *Flag) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

func (*Flag) Unset

func (f *Flag) Unset()

Unset the flag

type Mutex added in v1.0.5

type Mutex struct {
	BeforeLock         func()
	AfterLock          func()
	BeforeUnlock       func()
	AfterUnlock        func()
	AfterUnlockRecover func(r interface{})
	// contains filtered or unexported fields
}

Mutex adds debugging-related functionality to sync.Mutex. It is coded on top of sync.RWMutex to minimize code duplication.

func NewMutex added in v1.0.5

func NewMutex(p MutexParams) *Mutex

NewMutex returns a pointer to a new Mutex with default callbacks assigned

func (*Mutex) Lock added in v1.0.5

func (m *Mutex) Lock()

Lock calls the underlying Mutex.Lock method. BeforeLock and AfterLock callbacks will be executed before and after such call respectively. If callback was not specified, it will be ignored.

func (*Mutex) LockWithTag added in v1.1.3

func (m *Mutex) LockWithTag(tag string)

LockWithTag works like Lock but adds a specified tag to help in debugging process

func (*Mutex) Unlock added in v1.0.5

func (m *Mutex) Unlock()

Unlock calls the underlying Mutex.Unlock method. BeforeUnlock and AfterUnlock callbacks will be executed before and after such call respectively. If a panic will occur at underlying Mutex unlocking, it will be handled by a call to recover() and BeforeUnlockRecover and AfterUnlockRecover will be called respectively. If callback was not specified, it will be ignored.

type MutexParams added in v1.1.0

type MutexParams struct {
	Name                string
	SetDefaultCallbacks bool
	AddStackTrace       bool
	Timeout             time.Duration
}

MutexParams are mutex parameters

type Queue

type Queue struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Queue that is thread-safe

func NewDroppingQueue

func NewDroppingQueue(max int) Queue

NewDroppingQueue returns a new synced dropping queue

func NewLimitedQueue

func NewLimitedQueue(max int) Queue

NewLimitedQueue returns a new synced limited queue

func NewQueue

func NewQueue() Queue

NewQueue returns a new synced queue

func (*Queue) Clear added in v1.0.1

func (q *Queue) Clear()

Clear the queue

func (*Queue) Get

func (q *Queue) Get(pos int) (interface{}, error)

Get element at position pos but don't pop it, 0 is the most early element, -1 is the latest

func (*Queue) Len

func (q *Queue) Len() int

Len returns a queue current length

func (*Queue) List

func (q *Queue) List(positions ...int) ([]interface{}, error)

List elements at positions i but don't pop them, 0 is the most early element, -1 is the latest it returns element in the same order as indexes

func (*Queue) Pop

func (q *Queue) Pop() (interface{}, error)

Pop returns an object from a queue

func (*Queue) Push

func (q *Queue) Push(object interface{}) error

Push pushed an object to a queue

type RWMutex added in v1.0.5

type RWMutex struct {
	*Mutex

	BeforeRLock         func()
	AfterRLock          func()
	BeforeRUnlock       func()
	AfterRUnlock        func()
	AfterRUnlockRecover func(r interface{})
}

RWMutex adds debugging-related functionality to sync.RWMutex

func NewRWMutex added in v1.0.5

func NewRWMutex(p MutexParams) *RWMutex

NewRWMutex returns a pointer to a new RWMutex with default callbacks assigned

func (*RWMutex) RLock added in v1.0.5

func (m *RWMutex) RLock()

RLock calls the underlying RWMutex.RLock method. BeforeRLock and AfterRLock callbacks will be executed before and after such call respectively. If callback was not specified, it will be ignored.

func (*RWMutex) RUnlock added in v1.0.5

func (m *RWMutex) RUnlock()

RUnlock calls the underlying RWMutex.RUnlock method. BeforeRUnlock and AfterRUnlock callbacks will be executed before and after such call respectively. If a panic will occur at underlying RWMutex unlocking, it will be handled by a call to recover() and BeforeRUnlockRecover and AfterRUnlockRecover will be called respectively. If callback was not specified, it will be ignored.

Jump to

Keyboard shortcuts

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