OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package clock | 5 package clock |
6 | 6 |
7 import ( | 7 import ( |
8 "time" | 8 "time" |
| 9 |
| 10 "golang.org/x/net/context" |
9 ) | 11 ) |
10 | 12 |
11 type systemTimer struct { | 13 type systemTimer struct { |
12 » T *time.Timer // The underlying timer. Starts as nil, is initialized on
Reset. | 14 » // base is the underlying timer. It starts as nil, and is initialized on |
| 15 » // Reset. |
| 16 » ctx context.Context |
| 17 |
| 18 » // timerC is the current timer channel. |
| 19 » timerC chan AfterResult |
| 20 » // timerDoneC is a signal channel to alert our monitor that this timer h
as |
| 21 » // been canceled. |
| 22 » timerStoppedC chan struct{} |
| 23 » // timerMonitorResultC returns true if the timer monitor was prematurely |
| 24 » // terminated, false if not. |
| 25 » timerMonitorResultC chan bool |
13 } | 26 } |
14 | 27 |
15 var _ Timer = (*systemTimer)(nil) | 28 var _ Timer = (*systemTimer)(nil) |
16 | 29 |
17 func (t *systemTimer) GetC() <-chan time.Time { | 30 func (t *systemTimer) GetC() <-chan AfterResult { |
18 » if t.T == nil { | 31 » return t.timerC |
19 » » return nil | |
20 » } | |
21 » return t.T.C | |
22 } | 32 } |
23 | 33 |
24 func (t *systemTimer) Reset(d time.Duration) bool { | 34 func (t *systemTimer) Reset(d time.Duration) (running bool) { |
25 » if t.T == nil { | 35 » running = t.Stop() |
26 » » t.T = time.NewTimer(d) | 36 » t.reset() |
27 » » return false | 37 |
| 38 » // If our Context is already done, finish immediately. |
| 39 » select { |
| 40 » case <-t.ctx.Done(): |
| 41 » » t.timerC <- AfterResult{Err: t.ctx.Err()} |
| 42 » » return |
| 43 » default: |
| 44 » » break |
28 } | 45 } |
29 » return t.T.Reset(d) | 46 |
| 47 » // Start a monitor goroutine and our actual timer. Copy our channels, si
nce |
| 48 » // future stop/reset will change the systemTimer's values and our gorout
ine |
| 49 » // should only operate on this round's values. |
| 50 » timerC := t.timerC |
| 51 » timerStoppedC := make(chan struct{}) |
| 52 » timerMonitorResultC := make(chan bool, 1) |
| 53 » go func() { |
| 54 » » interrupted := false |
| 55 » » defer func() { |
| 56 » » » timerMonitorResultC <- interrupted |
| 57 » » }() |
| 58 |
| 59 » » select { |
| 60 » » case <-timerStoppedC: |
| 61 » » » interrupted = true |
| 62 » » case <-t.ctx.Done(): |
| 63 » » » timerC <- AfterResult{Err: t.ctx.Err()} |
| 64 » » case t := <-time.After(d): |
| 65 » » » timerC <- AfterResult{Time: t} |
| 66 » » } |
| 67 » }() |
| 68 |
| 69 » t.timerStoppedC = timerStoppedC |
| 70 » t.timerMonitorResultC = timerMonitorResultC |
| 71 » return |
30 } | 72 } |
31 | 73 |
32 func (t *systemTimer) Stop() bool { | 74 func (t *systemTimer) Stop() bool { |
33 » if t.T == nil { | 75 » if t.timerStoppedC == nil { |
34 return false | 76 return false |
35 } | 77 } |
36 » return t.T.Stop() | 78 » close(t.timerStoppedC) |
| 79 » t.timerStoppedC = nil |
| 80 » return <-t.timerMonitorResultC |
37 } | 81 } |
| 82 |
| 83 func (t *systemTimer) reset() { |
| 84 t.timerC = make(chan AfterResult, 1) |
| 85 } |
OLD | NEW |