|
|
@@ -14,48 +14,42 @@
|
|
|
|
|
|
package wait
|
|
|
|
|
|
-import (
|
|
|
- "sync"
|
|
|
- "time"
|
|
|
-)
|
|
|
+import "sync"
|
|
|
|
|
|
type WaitTime interface {
|
|
|
- // Wait returns a chan that waits on the given deadline.
|
|
|
+ // Wait returns a chan that waits on the given logical deadline.
|
|
|
// The chan will be triggered when Trigger is called with a
|
|
|
// deadline that is later than the one it is waiting for.
|
|
|
- // The given deadline MUST be unique. The deadline should be
|
|
|
- // retrieved by calling time.Now() in most cases.
|
|
|
- Wait(deadline time.Time) <-chan struct{}
|
|
|
- // Trigger triggers all the waiting chans with an earlier deadline.
|
|
|
- Trigger(deadline time.Time)
|
|
|
+ Wait(deadline uint64) <-chan struct{}
|
|
|
+ // Trigger triggers all the waiting chans with an earlier logical deadline.
|
|
|
+ Trigger(deadline uint64)
|
|
|
}
|
|
|
|
|
|
type timeList struct {
|
|
|
l sync.Mutex
|
|
|
- m map[int64]chan struct{}
|
|
|
+ m map[uint64]chan struct{}
|
|
|
}
|
|
|
|
|
|
func NewTimeList() *timeList {
|
|
|
- return &timeList{m: make(map[int64]chan struct{})}
|
|
|
+ return &timeList{m: make(map[uint64]chan struct{})}
|
|
|
}
|
|
|
|
|
|
-func (tl *timeList) Wait(deadline time.Time) <-chan struct{} {
|
|
|
+func (tl *timeList) Wait(deadline uint64) <-chan struct{} {
|
|
|
tl.l.Lock()
|
|
|
defer tl.l.Unlock()
|
|
|
- nano := deadline.UnixNano()
|
|
|
- ch := tl.m[nano]
|
|
|
+ ch := tl.m[deadline]
|
|
|
if ch == nil {
|
|
|
ch = make(chan struct{})
|
|
|
- tl.m[nano] = ch
|
|
|
+ tl.m[deadline] = ch
|
|
|
}
|
|
|
return ch
|
|
|
}
|
|
|
|
|
|
-func (tl *timeList) Trigger(deadline time.Time) {
|
|
|
+func (tl *timeList) Trigger(deadline uint64) {
|
|
|
tl.l.Lock()
|
|
|
defer tl.l.Unlock()
|
|
|
for t, ch := range tl.m {
|
|
|
- if t < deadline.UnixNano() {
|
|
|
+ if t <= deadline {
|
|
|
delete(tl.m, t)
|
|
|
close(ch)
|
|
|
}
|