ants/spinlock.go

28 lines
475 B
Go
Raw Normal View History

2019-09-27 15:51:46 +03:00
// Copyright 2019 Andy Pan. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package ants
import (
"runtime"
"sync"
"sync/atomic"
)
type spinLock uint32
func (sl *spinLock) Lock() {
for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
runtime.Gosched()
}
}
func (sl *spinLock) Unlock() {
atomic.StoreUint32((*uint32)(sl), 0)
}
func SpinLock() sync.Locker {
return new(spinLock)
}