keymutex

package module
v0.0.0-...-f593aa8 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2017 License: BSD-3-Clause Imports: 1 Imported by: 6

README

keymutex

A golang key set mutex. Supposing you have a set of keys describing resources that each need an independent mutex keymutex allows you to provide a pseudo-independent set of mutexes in constant size. Keymutex allocates a fixed sized array of mutexes to provide locking for an entire (potentially much larger) space of keys. This works by hashing the string key to provide an index into the backing array of mutexes. If keys collide in the hash function modulo the backing array size then they will share a mutex, meaning some indepedent keys will share a mutex but some will not. This allows you to trade space (size of mutex array) against mutex contention by chaning the size of the backing array.

Example

// Create a keymutex with backing array of 47 mutexes (prime number is good for hash coverage)
// Using a larger number here increases the memory usage by decreases the mutex contention.
var kmutex = keymutex.New(47)

// ...
go func(key string) {
    kmutex.Lock(key)
    defer kmutex.Unlock(key)
    // do something with the resource decribed by the key
}(akey)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APHash

func APHash(key string) uint

APHash Function

func BKDRHash

func BKDRHash(key string) uint

BKDRHash Function

func DJBHash

func DJBHash(key string) uint

DJBHash Function

func ELFHash

func ELFHash(key string) uint

ELFHash Function

func JSHash

func JSHash(key string) uint

JSHash Function

func PJWHash

func PJWHash(key string) uint

PJWHash P. J. Weinberger Hash Function

func RSHash

func RSHash(key string) uint

RSHash Function

func SDBMHash

func SDBMHash(key string) uint

SDBMHash Function

Types

type HashHandle

type HashHandle func(key string) uint

HashHandle for keymutex

type KeyMutex

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

KeyMutex a hash key mutex

func New

func New(count uint) *KeyMutex

New return a keymutex It require the number of mutexs(prime number is better)

func NewByHash

func NewByHash(count uint, handle HashHandle) *KeyMutex

NewByHash new a keymutex with a hashhandle

func (*KeyMutex) Count

func (km *KeyMutex) Count() uint

Count the number of mutexs

func (*KeyMutex) Lock

func (km *KeyMutex) Lock(key string)

Lock the key

func (*KeyMutex) LockID

func (km *KeyMutex) LockID(idx uint)

LockID lock by idx

func (*KeyMutex) Unlock

func (km *KeyMutex) Unlock(key string)

Unlock the key

func (*KeyMutex) UnlockID

func (km *KeyMutex) UnlockID(idx uint)

UnlockID unlock by idx

Jump to

Keyboard shortcuts

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