Codebase list golang-github-vbauerster-mpb / 63d2847
Utilize quit ch only, shutdown with sync once Vladimir Bauer 8 years ago
4 changed file(s) with 37 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
3030 type Bar struct {
3131 priority int
3232 index int
33 // quit channel to request b.server to quit
34 quit chan struct{}
35 // done channel is receiveable after b.server has been quit
36 done chan struct{}
33
3734 operateState chan func(*bState)
35 quit chan struct{}
36 once sync.Once
3837
3938 // cacheState is used after b.done is receiveable
4039 cacheState *bState
41
42 once sync.Once
4340 }
4441
4542 type (
9895
9996 b := &Bar{
10097 priority: id,
98 operateState: make(chan func(*bState)),
10199 quit: make(chan struct{}),
102 done: make(chan struct{}),
103 operateState: make(chan func(*bState)),
104100 }
105101
106102 go b.serve(s, wg, cancel)
193189 select {
194190 case b.operateState <- func(s *bState) { result <- len(s.appendFuncs) }:
195191 return <-result
196 case <-b.done:
192 case <-b.quit:
197193 return len(b.cacheState.appendFuncs)
198194 }
199195 }
204200 select {
205201 case b.operateState <- func(s *bState) { result <- len(s.prependFuncs) }:
206202 return <-result
207 case <-b.done:
203 case <-b.quit:
208204 return len(b.cacheState.prependFuncs)
209205 }
210206 }
215211 select {
216212 case b.operateState <- func(s *bState) { result <- s.id }:
217213 return <-result
218 case <-b.done:
214 case <-b.quit:
219215 return b.cacheState.id
220216 }
221217 }
226222 select {
227223 case b.operateState <- func(s *bState) { result <- s.current }:
228224 return <-result
229 case <-b.done:
225 case <-b.quit:
230226 return b.cacheState.current
231227 }
232228 }
237233 select {
238234 case b.operateState <- func(s *bState) { result <- s.total }:
239235 return <-result
240 case <-b.done:
236 case <-b.quit:
241237 return b.cacheState.total
242238 }
243239 }
274270 }
275271
276272 func (b *Bar) shutdown() {
277 close(b.quit)
273 b.quit <- struct{}{}
274 <-b.quit
278275 }
279276
280277 func (b *Bar) serve(s *bState, wg *sync.WaitGroup, cancel <-chan struct{}) {
281 defer func() {
282 b.cacheState = s
283 close(b.done)
284 wg.Done()
285 }()
278 defer wg.Done()
286279
287280 for {
288281 select {
291284 case <-cancel:
292285 s.aborted = true
293286 cancel = nil
294 b.Complete()
287 go b.Complete()
295288 case <-b.quit:
289 b.cacheState = s
290 close(b.quit)
296291 return
297292 }
298293 }
318313 s.draw(tw, prependWs, appendWs)
319314 ch <- &bufReader{io.MultiReader(s.bufP, s.bufB, s.bufA), s.completed}
320315 }:
321 case <-b.done:
316 case <-b.quit:
322317 s := b.cacheState
323318 var r io.Reader
324319 if s.panic != "" {
2525 // External wg
2626 ewg *sync.WaitGroup
2727
28 // quit channel to request p.server to quit
29 quit chan struct{}
30 // done channel is receiveable after p.server has been quit
31 done chan struct{}
3228 operateState chan func(*pState)
29 quit chan struct{}
30 once sync.Once
3331 }
3432
3533 type (
8179 p := &Progress{
8280 ewg: s.ewg,
8381 wg: new(sync.WaitGroup),
84 done: make(chan struct{}),
8582 operateState: make(chan func(*pState)),
8683 quit: make(chan struct{}),
8784 }
159156 if p.ewg != nil {
160157 p.ewg.Wait()
161158 }
162 select {
163 case <-p.quit:
164 return
165 default:
166 // wait for all bars to quit
167 p.wg.Wait()
168 // request p.server to quit
169 p.quitRequest()
170 // wait for p.server to quit
171 <-p.done
172 }
173 }
174
175 func (p *Progress) quitRequest() {
176 select {
177 case <-p.quit:
178 default:
179 close(p.quit)
180 }
159 // wait for all bars to quit
160 p.wg.Wait()
161 p.once.Do(p.shutdown)
162 }
163
164 func (p *Progress) shutdown() {
165 p.quit <- struct{}{}
166 <-p.quit
181167 }
182168
183169 func newWidthSync(timeout <-chan struct{}, numBars, numColumn int) *widthSync {
1515 func (p *Progress) serve(s *pState) {
1616 winch := make(chan os.Signal, 1)
1717 signal.Notify(winch, syscall.SIGWINCH)
18
19 defer func() {
20 if s.shutdownNotifier != nil {
21 close(s.shutdownNotifier)
22 }
23 signal.Stop(winch)
24 close(p.done)
25 }()
2618
2719 var numP, numA int
2820 var timer *time.Timer
6658 case <-s.cancel:
6759 s.ticker.Stop()
6860 s.cancel = nil
61 // don't return here, p.Stop() must be called eventually
6962 case <-p.quit:
7063 if s.cancel != nil {
7164 s.ticker.Stop()
7265 }
66 if s.shutdownNotifier != nil {
67 close(s.shutdownNotifier)
68 }
69 signal.Stop(winch)
70 close(p.quit)
7371 return
7472 }
7573 }
1010 )
1111
1212 func (p *Progress) serve(s *pState) {
13 defer func() {
14 if s.shutdownNotifier != nil {
15 close(s.shutdownNotifier)
16 }
17 close(p.done)
18 }()
1913
2014 var numP, numA int
2115
4135 case <-s.cancel:
4236 s.ticker.Stop()
4337 s.cancel = nil
38 // don't return here, p.Stop() must be called eventually
4439 case <-p.quit:
4540 if s.cancel != nil {
4641 s.ticker.Stop()
4742 }
43 if s.shutdownNotifier != nil {
44 close(s.shutdownNotifier)
45 }
46 close(p.quit)
4847 return
4948 }
5049 }