mapmutex

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 2 Imported by: 8

Documentation

Overview

Package mapmutex provides an extensible, type-safe mutex implementation TODO: use generics

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IntMapMutex

type IntMapMutex interface {
	Lock(key int) Unlocker
	TryLock(key int) (Unlocker, bool)
	Keys() []int
}

IntMapMutex is a map mutex that allows locking on an int.

Example
mapMutex := mapmutex.NewIntMapMutex()
// second variable will be true
lock0, _ := mapMutex.TryLock(0)
lock1 := mapMutex.Lock(1)

lock0.Unlock()
lock1.Unlock()
Output:

func NewIntMapMutex

func NewIntMapMutex() IntMapMutex

NewIntMapMutex creates a map mutex for locking on an integer.

type StringMapMutex

type StringMapMutex interface {
	Lock(key string) Unlocker
	TryLock(key string) (Unlocker, bool)
	Keys() []string
}

StringMapMutex is an implementation of map mutex for string typed values.

Example

ExampleMapMutex provides an example implementation of a map mutex.

mapMutex := mapmutex.NewStringMapMutex()
lock1 := mapMutex.Lock("lock1")
lock2 := mapMutex.Lock("lock2")

lock1.Unlock()
lock2.Unlock()
Output:

func NewStringMapMutex

func NewStringMapMutex() StringMapMutex

NewStringMapMutex creates a map mutex for the string type.

type StringerMapMutex

type StringerMapMutex interface {
	Lock(key fmt.Stringer) Unlocker
	TryLock(key fmt.Stringer) (Unlocker, bool)
	Keys() []string
}

StringerMapMutex is an implementation of mapMutex for the fmt.Stringer conforming types.

Example
vitalik := common.HexToAddress("0xab5801a7d398351b8be11c439e05c5b3259aec9b")
tether := common.HexToAddress("0xdac17f958d2ee523a2206206994597c13d831ec7")

mapMutex := mapmutex.NewStringerMapMutex()
// second variable will be true
vitalikLock, _ := mapMutex.TryLock(vitalik)
tetherLock := mapMutex.Lock(tether)

vitalikLock.Unlock()
tetherLock.Unlock()
Output:

func NewStringerMapMutex

func NewStringerMapMutex() StringerMapMutex

NewStringerMapMutex creates an initialized locker that locks on fmt.String.

type Unlocker

type Unlocker interface {
	Unlock()
}

Unlocker provides an Unlock method to release the lock.

Jump to

Keyboard shortcuts

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