mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
spin locker
This commit is contained in:
22
locker/spin_locker.go
Normal file
22
locker/spin_locker.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package locker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"sync/atomic"
|
||||||
|
)
|
||||||
|
|
||||||
|
type spinLock uint32
|
||||||
|
|
||||||
|
func NewSpinLocker() *spinLock {
|
||||||
|
return new(spinLock)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sl *spinLock) Lock() {
|
||||||
|
for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
|
||||||
|
runtime.Gosched()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sl *spinLock) Unlock() {
|
||||||
|
atomic.StoreUint32((*uint32)(sl), 0)
|
||||||
|
}
|
Reference in New Issue
Block a user