Skip to content

Commit

Permalink
Fix: export Spin
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Jul 18, 2023
1 parent 46e3393 commit dc2e004
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion kmutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type nMutex struct {

func DefaultKmutex() *Kmutex {
return &Kmutex{
l: new(spin),
l: new(Spin),
p: &sync.Pool{
New: func() any {
return &nMutex{
Expand Down
2 changes: 1 addition & 1 deletion krwmutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type nRWMutex struct {

func DefaultKrwmutex() *Krwmutex {
return &Krwmutex{
l: new(spin),
l: new(Spin),
p: &sync.Pool{
New: func() any {
return &nRWMutex{
Expand Down
6 changes: 3 additions & 3 deletions kspin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ type Kspin struct {
}

type nSpinLock struct {
lock *spin
lock *Spin
n uint64
}

func DefaultKspin() *Kspin {
return &Kspin{
l: new(spin),
l: new(Spin),
p: &sync.Pool{
New: func() any {
return &nSpinLock{
lock: new(spin),
lock: new(Spin),
}
},
},
Expand Down
12 changes: 6 additions & 6 deletions spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"sync/atomic"
)

type spin int32
type Spin int32

func NewSpin() *spin {
return new(spin)
func NewSpin() *Spin {
return new(Spin)
}

func (s *spin) Lock() {
func (s *Spin) Lock() {
for !atomic.CompareAndSwapInt32((*int32)(s), 0, 1) {
runtime.Gosched()
}
}

func (s *spin) Unlock() {
func (s *Spin) Unlock() {
atomic.StoreInt32((*int32)(s), 0)
}

func (s *spin) TryLock() bool {
func (s *Spin) TryLock() bool {
return atomic.CompareAndSwapInt32((*int32)(s), 0, 1)
}

0 comments on commit dc2e004

Please sign in to comment.