forked from OneOfOne/go-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added atomicflag and coverted spinlock to use it.
- Loading branch information
Showing
3 changed files
with
37 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package sync | ||
|
||
import "sync/atomic" | ||
|
||
type AtomicFlag struct { | ||
v uint32 | ||
} | ||
|
||
func (a *AtomicFlag) Set() bool { | ||
return atomic.CompareAndSwapUint32(&a.v, 0, 1) | ||
} | ||
|
||
func (a *AtomicFlag) Clear() bool { | ||
return atomic.CompareAndSwapUint32(&a.v, 1, 0) | ||
} | ||
|
||
func (a *AtomicFlag) IsSet() bool { | ||
return atomic.LoadUint32(&a.v) == 1 | ||
} | ||
|
||
func (a *AtomicFlag) String() string { | ||
if a.IsSet() { | ||
return "true" | ||
} | ||
return "false" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
package spinlock | ||
package sync | ||
|
||
import ( | ||
"runtime" | ||
"sync/atomic" | ||
) | ||
|
||
var state = [2]string{"Unlocked", "Locked"} | ||
import "runtime" | ||
|
||
// SpinLock implements a simple atomic spin lock, the zero value for a SpinLock is an unlocked spinlock. | ||
type SpinLock struct { | ||
l uint32 | ||
v AtomicFlag | ||
} | ||
|
||
// Lock locks sl. If the lock is already in use, the caller blocks until Unlock is called | ||
func (sl *SpinLock) Lock() { | ||
for !atomic.CompareAndSwapUint32(&sl.l, 0, 1) { | ||
for !sl.v.Set() { | ||
runtime.Gosched() //allow other goroutines to do stuff. | ||
} | ||
} | ||
|
||
// Unlock unlocks sl, unlike [Mutex.Unlock](http://golang.org/pkg/sync/#Mutex.Unlock), | ||
// there's no harm calling it on an unlocked SpinLock | ||
func (sl *SpinLock) Unlock() { | ||
atomic.StoreUint32(&sl.l, 0) | ||
sl.v.Clear() | ||
} | ||
|
||
// TryLock will try to lock sl and return whether it succeed or not without blocking. | ||
func (sl *SpinLock) TryLock() bool { | ||
return atomic.CompareAndSwapUint32(&sl.l, 0, 1) | ||
return sl.v.Set() | ||
} | ||
|
||
func (sl *SpinLock) String() string { | ||
return state[sl.l] | ||
if sl.v.IsSet() { | ||
return "Locked" | ||
} | ||
return "Unlocked" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package spinlock | ||
package sync | ||
|
||
import ( | ||
"sync" | ||
|