mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-19 23:25:11 +08:00
refactor project structure
This commit is contained in:
17
lib/sync/atomic/bool.go
Normal file
17
lib/sync/atomic/bool.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package atomic
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
type AtomicBool uint32
|
||||
|
||||
func (b *AtomicBool) Get() bool {
|
||||
return atomic.LoadUint32((*uint32)(b)) != 0
|
||||
}
|
||||
|
||||
func (b *AtomicBool) Set(v bool) {
|
||||
if v {
|
||||
atomic.StoreUint32((*uint32)(b), 1)
|
||||
} else {
|
||||
atomic.StoreUint32((*uint32)(b), 0)
|
||||
}
|
||||
}
|
38
lib/sync/wait/wait.go
Normal file
38
lib/sync/wait/wait.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package wait
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Wait struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func (w *Wait) Add(delta int) {
|
||||
w.wg.Add(delta)
|
||||
}
|
||||
|
||||
func (w *Wait) Done() {
|
||||
w.wg.Done()
|
||||
}
|
||||
|
||||
func (w *Wait) Wait() {
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
// return isTimeout
|
||||
func (w *Wait) WaitWithTimeout(timeout time.Duration) bool {
|
||||
c := make(chan bool)
|
||||
go func() {
|
||||
defer close(c)
|
||||
w.wg.Wait()
|
||||
c <- true
|
||||
}()
|
||||
select {
|
||||
case <-c:
|
||||
return false // completed normally
|
||||
case <-time.After(timeout):
|
||||
return true // timed out
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user