25 lines
356 B
Go
25 lines
356 B
Go
package internal
|
|
|
|
import (
|
|
"runtime"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type SpinLock struct {
|
|
v int32
|
|
}
|
|
|
|
func (l *SpinLock) Unlock() {
|
|
l.v = 0
|
|
}
|
|
|
|
func (l *SpinLock) Lock() {
|
|
for {
|
|
if l.v == 0 && atomic.CompareAndSwapInt32(&l.v, 0, 1) {
|
|
return
|
|
}
|
|
// Nginx中考虑了多核的情况,Go中不考虑,因为可能是协程的切换。
|
|
runtime.Gosched()
|
|
}
|
|
}
|