ants/internal/spinlock.go

29 lines
523 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.
2019-10-04 06:24:13 +03:00
package internal
2019-09-27 15:51:46 +03:00
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)
}
2019-10-04 06:24:13 +03:00
// NewSpinLock instantiates a spin-lock.
func NewSpinLock() sync.Locker {
2019-09-27 15:51:46 +03:00
return new(spinLock)
}