mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
25 lines
326 B
Go
25 lines
326 B
Go
package syncx
|
|
|
|
import (
|
|
"runtime"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type SpinLock struct {
|
|
lock uint32
|
|
}
|
|
|
|
func (sl *SpinLock) Lock() {
|
|
for !sl.TryLock() {
|
|
runtime.Gosched()
|
|
}
|
|
}
|
|
|
|
func (sl *SpinLock) TryLock() bool {
|
|
return atomic.CompareAndSwapUint32(&sl.lock, 0, 1)
|
|
}
|
|
|
|
func (sl *SpinLock) Unlock() {
|
|
atomic.StoreUint32(&sl.lock, 0)
|
|
}
|