LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package time | 5 package time |
6 | 6 |
7 import "errors" | 7 import "errors" |
8 | 8 |
9 // A Ticker holds a synchronous channel that delivers `ticks' of a clock | 9 // A Ticker holds a synchronous channel that delivers `ticks' of a clock |
10 // at intervals. | 10 // at intervals. |
11 type Ticker struct { | 11 type Ticker struct { |
12 » C <-chan int64 // The channel on which the ticks are delivered. | 12 » C <-chan int64 // The channel on which the ticks are delivered. |
13 r runtimeTimer | 13 r runtimeTimer |
14 } | 14 } |
15 | 15 |
16 // NewTicker returns a new Ticker containing a channel that will | 16 // NewTicker returns a new Ticker containing a channel that will |
17 // send the time, in nanoseconds, every ns nanoseconds. It adjusts the | 17 // send the time, in nanoseconds, every ns nanoseconds. It adjusts the |
18 // intervals to make up for pauses in delivery of the ticks. The value of | 18 // intervals to make up for pauses in delivery of the ticks. The value of |
19 // ns must be greater than zero; if not, NewTicker will panic. | 19 // ns must be greater than zero; if not, NewTicker will panic. |
20 func NewTicker(ns int64) *Ticker { | 20 func NewTicker(ns int64) *Ticker { |
21 if ns <= 0 { | 21 if ns <= 0 { |
22 panic(errors.New("non-positive interval for NewTicker")) | 22 panic(errors.New("non-positive interval for NewTicker")) |
23 } | 23 } |
24 // Give the channel a 1-element time buffer. | 24 // Give the channel a 1-element time buffer. |
25 // If the client falls behind while reading, we drop ticks | 25 // If the client falls behind while reading, we drop ticks |
26 // on the floor until the client catches up. | 26 // on the floor until the client catches up. |
27 c := make(chan int64, 1) | 27 c := make(chan int64, 1) |
28 t := &Ticker{ | 28 t := &Ticker{ |
29 » » C: c, | 29 » » C: c, |
30 r: runtimeTimer{ | 30 r: runtimeTimer{ |
31 » » » when: Nanoseconds() + ns, | 31 » » » when: Nanoseconds() + ns, |
32 period: ns, | 32 period: ns, |
33 » » » f: sendTime, | 33 » » » f: sendTime, |
34 » » » arg: c, | 34 » » » arg: c, |
35 }, | 35 }, |
36 } | 36 } |
37 startTimer(&t.r) | 37 startTimer(&t.r) |
38 return t | 38 return t |
39 } | 39 } |
40 | 40 |
41 // Stop turns off a ticker. After Stop, no more ticks will be sent. | 41 // Stop turns off a ticker. After Stop, no more ticks will be sent. |
42 func (t *Ticker) Stop() { | 42 func (t *Ticker) Stop() { |
43 stopTimer(&t.r) | 43 stopTimer(&t.r) |
44 } | 44 } |
45 | 45 |
46 // Tick is a convenience wrapper for NewTicker providing access to the ticking | 46 // Tick is a convenience wrapper for NewTicker providing access to the ticking |
47 // channel only. Useful for clients that have no need to shut down the ticker. | 47 // channel only. Useful for clients that have no need to shut down the ticker. |
48 func Tick(ns int64) <-chan int64 { | 48 func Tick(ns int64) <-chan int64 { |
49 if ns <= 0 { | 49 if ns <= 0 { |
50 return nil | 50 return nil |
51 } | 51 } |
52 return NewTicker(ns).C | 52 return NewTicker(ns).C |
53 } | 53 } |
LEFT | RIGHT |