Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(152)

Delta Between Two Patch Sets: src/pkg/time/tick.go

Issue 5334051: code review 5334051: runtime: add timer support, use for package time (Closed)
Left Patch Set: diff -r c8c9ccd19020 https://go.googlecode.com/hg Created 13 years, 5 months ago
Right Patch Set: diff -r 9870fbad1533 https://go.googlecode.com/hg Created 13 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/time/sys.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b