mutex

package
v3.0.1-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT, Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package mutex provides mutexes that wrap the thing they protect. This results in clearer code than using sync.Mutex directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func With1

func With1[T, A any](m *Mutex[T], f func(*T) A) A

With1(m, ...) is like m.With(...), except that the callback returns a value.

func With2

func With2[T, A, B any](m *Mutex[T], f func(*T) (A, B)) (A, B)

With2 is like With1, but the callback returns two values instead of one.

func With3

func With3[T, A, B, C any](m *Mutex[T], f func(*T) (A, B, C)) (A, B, C)

With3 is like With1, but the callback returns three values instead of one.

func With4

func With4[T, A, B, C, D any](m *Mutex[T], f func(*T) (A, B, C, D)) (A, B, C, D)

With4 is like With1, but the callback returns four values instead of one.

Types

type Locked

type Locked[T any] struct {
	// contains filtered or unexported fields
}

A Locked[T] is a reference to a value of type T which is guarded by a Mutex, which the caller has acquired.

func (*Locked[T]) Unlock

func (l *Locked[T]) Unlock()

Unlock releases the mutex. Any references to the value obtained via Value must not be used after this is called.

func (*Locked[T]) Value

func (l *Locked[T]) Value() *T

Value returns a reference to the protected value. It must not be used after calling Unlock.

Value will panic if called after Unlock()

We recommend against assigning the result of Value() to a variable; while repeatedly having to write l.Value() is mildly annoying, it makes it much harder to accidentally use the value after unlocking.

type Mutex

type Mutex[T any] struct {
	// contains filtered or unexported fields
}

A Mutex[T] wraps a T, protecting it with a mutex. It must not be moved after first use. The zero value is an unlocked mutex containing the zero value of T.

func New

func New[T any](val T) Mutex[T]

New returns a new mutex containing the value val.

func (*Mutex[T]) Lock

func (m *Mutex[T]) Lock() *Locked[T]

Lock acquires the mutex and returns a reference to the value. Call Unlock() on the return value to release the lock.

Where possible, you should prefer using Mutex.With and similar functions. If those are insufficient to handle your use case consider whether your locking scheme is too complicated before going ahead and using this.

func (*Mutex[T]) With

func (m *Mutex[T]) With(f func(*T))

With invokes the callback with exclusive access to the value of the mutex.

Jump to

Keyboard shortcuts

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