forked from mirror/ants
Improve the exponential backoff algorithm in spin-lock
This commit is contained in:
parent
1ce7c89177
commit
4b16a81116
|
@ -12,13 +12,18 @@ import (
|
|||
|
||||
type spinLock uint32
|
||||
|
||||
const maxBackoff = 64
|
||||
|
||||
func (sl *spinLock) Lock() {
|
||||
backoff := 1
|
||||
for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
|
||||
// Leverage the exponential backoff algorithm, see https://en.wikipedia.org/wiki/Exponential_backoff.
|
||||
for i := 0; i < backoff; i++ {
|
||||
runtime.Gosched()
|
||||
}
|
||||
backoff <<= 1
|
||||
if backoff < maxBackoff {
|
||||
backoff <<= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,17 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
Benchmark result for three types of locks:
|
||||
goos: darwin
|
||||
goarch: amd64
|
||||
pkg: github.com/panjf2000/ants/v2/internal
|
||||
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
|
||||
BenchmarkMutex-12 20549502 71.84 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkSpinLock-12 58629697 20.02 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBackOffSpinLock-12 72523454 15.74 ns/op 0 B/op 0 allocs/op
|
||||
*/
|
||||
|
||||
type originSpinLock uint32
|
||||
|
||||
func (sl *originSpinLock) Lock() {
|
||||
|
|
Loading…
Reference in New Issue