-
Notifications
You must be signed in to change notification settings - Fork 0
/
chanSemaTO.go
220 lines (195 loc) · 4.37 KB
/
chanSemaTO.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// (C)Tylor Arndt 2014, Mozilla Public License (MPL) Version 2.0
// See LICENSE file for details.
// For other licensing options please contact the author.
package sema
import (
"context"
"time"
)
//condSema is channel-based counting semaphore implementation with timeout
// support
type chanSemaTO struct {
chanSema
defTimeout time.Duration
}
//NewChanSemaTimeout constructs a new counting semaphore that will construct
// a new counting semaphore with all P/V (acquire/release) opterations timing out
// after the providing default timeout unless overridden at call time (see PTO, VTO).
//Important: If you would like P/V (acquire/release) to be non-blocking you should
// provide a defaultTimeout of zero or use NewNonBlockChanSema.
func NewChanSemaTimeout(count uint, defaultTimeout time.Duration) TimeoutCountingSema {
return &chanSemaTO{
chanSema: NewChanSemaCount(count).(chanSema),
defTimeout: defaultTimeout,
}
}
//NewNonBlockChanSema is like NewChanSemaTimeout with a defaultTimeout of zero.
func NewNonBlockChanSema(count uint) TimeoutCountingSema {
return NewChanSemaTimeout(count, 0)
}
func (s *chanSemaTO) P() bool {
return s.PTO(s.defTimeout)
}
func (s *chanSemaTO) Acquire() bool {
return s.P()
}
func (s *chanSemaTO) PTO(timeout time.Duration) bool {
select {
case <-s.chanSema:
return true
default:
if timeout < 1 {
return false
}
select {
case <-s.chanSema:
return true
case <-time.After(timeout):
return false
}
}
}
func (s *chanSemaTO) AcquireTO(timeout time.Duration) bool {
return s.PTO(timeout)
}
func (s *chanSemaTO) AcquireCtx(ctx context.Context) bool {
select {
case <-s.chanSema:
return true
default:
select {
case <-s.chanSema:
return true
case <-ctx.Done():
return false
}
}
}
func (s *chanSemaTO) Wait(units uint) (success bool) {
success, _ = s.WaitTO(units, s.defTimeout)
return
}
func (s *chanSemaTO) WaitTO(units uint, timeout time.Duration) (bool, uint) {
var opTimedOut <-chan time.Time //We avoid allocating this if possible
total := units
for ; units > 0; units-- {
select {
case <-s.chanSema:
continue
default:
if timeout < 1 {
return units == 0, total - units
}
if opTimedOut == nil {
opTimedOut = time.After(timeout)
}
select {
case <-s.chanSema:
continue
case <-opTimedOut:
return units == 0, total - units
}
}
}
return true, total
}
func (s *chanSemaTO) WaitCtx(ctx context.Context, units uint) (bool, uint) {
total, doneCh := units, ctx.Done()
for ; units > 0; units-- {
select {
case <-s.chanSema:
continue
default:
select {
case <-s.chanSema:
continue
case <-doneCh:
return units == 0, total - units
}
}
}
return true, total
}
func (s *chanSemaTO) V() bool {
return s.VTO(s.defTimeout)
}
func (s *chanSemaTO) Release() bool {
return s.V()
}
func (s *chanSemaTO) VTO(timeout time.Duration) bool {
select {
case s.chanSema <- struct{}{}:
return true
default:
if timeout < 1 {
return false
}
select {
case s.chanSema <- struct{}{}:
return true
case <-time.After(timeout):
return false
}
}
}
func (s *chanSemaTO) ReleaseTO(timeout time.Duration) bool {
return s.VTO(timeout)
}
func (s *chanSemaTO) ReleaseCtx(ctx context.Context) bool {
select {
case s.chanSema <- struct{}{}:
return true
default:
select {
case s.chanSema <- struct{}{}:
return true
case <-ctx.Done():
return false
}
}
}
func (s *chanSemaTO) Signal(units uint) (success bool) {
success, _ = s.SignalTO(units, s.defTimeout)
return
}
func (s *chanSemaTO) SignalTO(units uint, timeout time.Duration) (bool, uint) {
var opTimedOut <-chan time.Time //We avoid allocating this if possible
total := units
for ; units > 0; units-- {
select {
case s.chanSema <- struct{}{}:
continue
default:
if timeout < 1 {
return units == 0, total - units
}
if opTimedOut == nil {
opTimedOut = time.After(timeout)
}
select {
case s.chanSema <- struct{}{}:
continue
case <-opTimedOut:
return units == 0, total - units
}
}
}
return true, total
}
func (s *chanSemaTO) SignalCtx(ctx context.Context, units uint) (bool, uint) {
total, doneCh := units, ctx.Done()
for ; units > 0; units-- {
select {
case s.chanSema <- struct{}{}:
continue
default:
select {
case s.chanSema <- struct{}{}:
continue
case <-doneCh:
return units == 0, total - units
}
}
}
return true, total
}