This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
waiter.go
124 lines (112 loc) · 3.03 KB
/
waiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mangos
import (
"sync"
"time"
)
// CondTimed is a condition variable (ala sync.Cond) but inclues a timeout.
type CondTimed struct {
sync.Cond
}
// WaitRelTimeout is like Wait, but it times out. The fact that
// it timed out can be determined by checking the return value. True
// indicates that it woke up without a timeout (signaled another way),
// whereas false indicates a timeout occurred.
func (cv *CondTimed) WaitRelTimeout(when time.Duration) bool {
timer := time.AfterFunc(when, func() {
cv.L.Lock()
cv.Broadcast()
cv.L.Unlock()
})
cv.Wait()
return timer.Stop()
}
// WaitAbsTimeout is like WaitRelTimeout, but expires on an absolute time
// instead of a relative one.
func (cv *CondTimed) WaitAbsTimeout(when time.Time) bool {
now := time.Now()
if when.After(now) {
return cv.WaitRelTimeout(when.Sub(now))
}
return cv.WaitRelTimeout(time.Duration(0))
}
// Waiter is a way to wait for completion, but it includes a timeout. It
// is similar in some respects to sync.WaitGroup.
type Waiter struct {
cv CondTimed
cnt int
sync.Mutex
}
// Init must be called to initialize the Waiter.
func (w *Waiter) Init() {
w.cv.L = w
w.cnt = 0
}
// Add adds a new go routine/item to wait for. This should be called before
// starting go routines you want to wait for, for example.
func (w *Waiter) Add() {
w.Lock()
w.cnt++
w.Unlock()
}
// Done is called when the item to wait for is done. There should be a one to
// one correspondance between Add and Done. When the count drops to zero,
// any callers blocked in Wait() are woken. If the count drops below zero,
// it panics.
func (w *Waiter) Done() {
w.Lock()
w.cnt--
if w.cnt < 0 {
panic("wait count dropped < 0")
}
if w.cnt == 0 {
w.cv.Broadcast()
}
w.Unlock()
}
// Wait waits without a timeout. It only completes when the count drops
// to zero.
func (w *Waiter) Wait() {
w.Lock()
for w.cnt != 0 {
w.cv.Wait()
}
w.Unlock()
}
// WaitRelTimeout waits until either the count drops to zero, or the timeout
// expires. It returns true if the count is zero, false otherwise.
func (w *Waiter) WaitRelTimeout(d time.Duration) bool {
w.Lock()
for w.cnt != 0 {
if !w.cv.WaitRelTimeout(d) {
break
}
}
done := w.cnt == 0
w.Unlock()
return done
}
// WaitAbsTimeout is like WaitRelTimeout, but waits until an absolute time.
func (w *Waiter) WaitAbsTimeout(t time.Time) bool {
w.Lock()
for w.cnt != 0 {
if !w.cv.WaitAbsTimeout(t) {
break
}
}
done := w.cnt == 0
w.Unlock()
return done
}