annotate libgo/go/time/tick.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package time
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 import "errors"
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 // A Ticker holds a channel that delivers `ticks' of a clock
kono
parents:
diff changeset
10 // at intervals.
kono
parents:
diff changeset
11 type Ticker struct {
kono
parents:
diff changeset
12 C <-chan Time // The channel on which the ticks are delivered.
kono
parents:
diff changeset
13 r runtimeTimer
kono
parents:
diff changeset
14 }
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 // NewTicker returns a new Ticker containing a channel that will send the
kono
parents:
diff changeset
17 // time with a period specified by the duration argument.
kono
parents:
diff changeset
18 // It adjusts the intervals or drops ticks to make up for slow receivers.
kono
parents:
diff changeset
19 // The duration d must be greater than zero; if not, NewTicker will panic.
kono
parents:
diff changeset
20 // Stop the ticker to release associated resources.
kono
parents:
diff changeset
21 func NewTicker(d Duration) *Ticker {
kono
parents:
diff changeset
22 if d <= 0 {
kono
parents:
diff changeset
23 panic(errors.New("non-positive interval for NewTicker"))
kono
parents:
diff changeset
24 }
kono
parents:
diff changeset
25 // Give the channel a 1-element time buffer.
kono
parents:
diff changeset
26 // If the client falls behind while reading, we drop ticks
kono
parents:
diff changeset
27 // on the floor until the client catches up.
kono
parents:
diff changeset
28 c := make(chan Time, 1)
kono
parents:
diff changeset
29 t := &Ticker{
kono
parents:
diff changeset
30 C: c,
kono
parents:
diff changeset
31 r: runtimeTimer{
kono
parents:
diff changeset
32 when: when(d),
kono
parents:
diff changeset
33 period: int64(d),
kono
parents:
diff changeset
34 f: sendTime,
kono
parents:
diff changeset
35 arg: c,
kono
parents:
diff changeset
36 },
kono
parents:
diff changeset
37 }
kono
parents:
diff changeset
38 startTimer(&t.r)
kono
parents:
diff changeset
39 return t
kono
parents:
diff changeset
40 }
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 // Stop turns off a ticker. After Stop, no more ticks will be sent.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
43 // Stop does not close the channel, to prevent a concurrent goroutine
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
44 // reading from the channel from seeing an erroneous "tick".
111
kono
parents:
diff changeset
45 func (t *Ticker) Stop() {
kono
parents:
diff changeset
46 stopTimer(&t.r)
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 // Tick is a convenience wrapper for NewTicker providing access to the ticking
kono
parents:
diff changeset
50 // channel only. While Tick is useful for clients that have no need to shut down
kono
parents:
diff changeset
51 // the Ticker, be aware that without a way to shut it down the underlying
kono
parents:
diff changeset
52 // Ticker cannot be recovered by the garbage collector; it "leaks".
kono
parents:
diff changeset
53 // Unlike NewTicker, Tick will return nil if d <= 0.
kono
parents:
diff changeset
54 func Tick(d Duration) <-chan Time {
kono
parents:
diff changeset
55 if d <= 0 {
kono
parents:
diff changeset
56 return nil
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58 return NewTicker(d).C
kono
parents:
diff changeset
59 }