spinlock

package module
v0.0.0-...-30e6d1c Latest Latest
Warning

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

Go to latest
Published: May 18, 2020 License: CC0-1.0 Imports: 4 Imported by: 7

README

GoDoc go report

CC0

spinlock

This package implements sync.Locker as a spinlock.

import (
	"github.com/xaionaro-go/atomicmap/spinlock"
)

var (
	locker = &spinlock.Locker{}
)

...

	locker.Lock()
	[doSomething]
	locker.Unlock()
BenchmarkMutexParallel-4        20000000                75.7 ns/op             0 B/op          0 allocs/op
BenchmarkSpinlockParallel-4     50000000                22.3 ns/op             0 B/op          0 allocs/op
BenchmarkMutexSingle-4          100000000               17.1 ns/op             0 B/op          0 allocs/op
BenchmarkSpinlockSingle-4       100000000               16.9 ns/op             0 B/op          0 allocs/op

go version go1.11 linux/amd64

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker is a spinlock-based implementation of sync.Locker.

func (*Locker) IsLocked

func (l *Locker) IsLocked() bool

IsLocked returns true if the locker is currently locked.

func (*Locker) Lock

func (l *Locker) Lock()

Lock waits until the locker with be unlocked (if it is not) and then locks it.

func (*Locker) LockDo

func (l *Locker) LockDo(fn func())

LockDo is just a wrapper for `Lock` + `Unlock`. It locks the locker, runs the function `fn` and then unlocks the locker.

func (*Locker) SetUnlocked

func (l *Locker) SetUnlocked()

SetUnlocked resets the state of the locker.

Use case: if you copy an object, you may want to reset this value.

func (*Locker) TryLock

func (l *Locker) TryLock() bool

TryLock performs a non-blocking attempt to lock the locker and returns true if successful.

func (*Locker) Unlock

func (l *Locker) Unlock()

Unlock unlocks the locker.

Jump to

Keyboard shortcuts

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