spinlock

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: Apache-2.0 Imports: 2 Imported by: 3

README

SpinLock

Build and Test go report release


SpinLock is a spin lock implementation in Go with exponential backoff and adaptive spinning.

Installation

To install the package, run:

go get github.com/daniel-hutao/spinlock

Usage

Import the package and create a new SpinLock:

import "github.com/daniel-hutao/spinlock"

var sl spinlock.SpinLock

Then you can use the Lock and Unlock methods:

sl.Lock()
// critical section
sl.Unlock()

Performance Testing

We have conducted performance tests to compare the efficiency of our SpinLock implementation with the standard Mutex in Go. The tests were run on a MacBook Pro with an Apple M1 chip, 16GB of RAM.

SpinLock
$ go test -benchmem -run=^$ -bench ^BenchmarkSpinLock$ github.com/daniel-hutao/spinlock

goos: darwin
goarch: arm64
pkg: github.com/daniel-hutao/spinlock
=== RUN   BenchmarkSpinLock
BenchmarkSpinLock
BenchmarkSpinLock-10            111107053               10.80 ns/op            0 B/op          0 allocs/op
PASS
ok      github.com/daniel-hutao/spinlock        2.973s
Mutex
$ go test -benchmem -run=^$ -bench ^BenchmarkMutex$ github.com/daniel-hutao/spinlock

goos: darwin
goarch: arm64
pkg: github.com/daniel-hutao/spinlock
=== RUN   BenchmarkMutex
BenchmarkMutex
BenchmarkMutex-10       10366155               115.5 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/daniel-hutao/spinlock        1.793s
Conclusion

Based on our tests, the SpinLock implementation performs significantly better than the standard Mutex in Go on a MacBook Pro with an Apple M1 chip. Specifically, operations on SpinLock are approximately an order of magnitude faster than those on Mutex.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SpinLock

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

SpinLock is a simple spin lock implementation with exponential backoff and adaptive spinning.

func (*SpinLock) Lock

func (sl *SpinLock) Lock()

Lock spins until it is able to acquire the lock. It uses exponential backoff and adaptive spinning to reduce contention.

func (*SpinLock) TryLock

func (sl *SpinLock) TryLock() bool

TryLock attempts to acquire the lock without blocking. It returns true if the lock was successfully acquired, and false otherwise.

func (*SpinLock) Unlock

func (sl *SpinLock) Unlock()

Unlock releases the lock.

Jump to

Keyboard shortcuts

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