~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/time/sleep.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        i      int32
19
19
        when   int64
20
20
        period int64
21
 
        f      func(int64, interface{})
 
21
        f      func(int64, interface{}) // NOTE: must not be closure
22
22
        arg    interface{}
23
23
}
24
24
 
 
25
// when is a helper function for setting the 'when' field of a runtimeTimer.
 
26
// It returns what the time will be, in nanoseconds, Duration d in the future.
 
27
// If d is negative, it is ignored.  If the returned value would be less than
 
28
// zero because of an overflow, MaxInt64 is returned.
 
29
func when(d Duration) int64 {
 
30
        if d <= 0 {
 
31
                return nano()
 
32
        }
 
33
        t := nano() + int64(d)
 
34
        if t < 0 {
 
35
                t = 1<<63 - 1 // math.MaxInt64
 
36
        }
 
37
        return t
 
38
}
 
39
 
25
40
func startTimer(*runtimeTimer)
26
41
func stopTimer(*runtimeTimer) bool
27
42
 
35
50
 
36
51
// Stop prevents the Timer from firing.
37
52
// It returns true if the call stops the timer, false if the timer has already
38
 
// expired or stopped.
39
 
func (t *Timer) Stop() (ok bool) {
 
53
// expired or been stopped.
 
54
// Stop does not close the channel, to prevent a read from the channel succeeding
 
55
// incorrectly.
 
56
func (t *Timer) Stop() bool {
40
57
        return stopTimer(&t.r)
41
58
}
42
59
 
47
64
        t := &Timer{
48
65
                C: c,
49
66
                r: runtimeTimer{
50
 
                        when: nano() + int64(d),
 
67
                        when: when(d),
51
68
                        f:    sendTime,
52
69
                        arg:  c,
53
70
                },
56
73
        return t
57
74
}
58
75
 
 
76
// Reset changes the timer to expire after duration d.
 
77
// It returns true if the timer had been active, false if the timer had
 
78
// expired or been stopped.
 
79
func (t *Timer) Reset(d Duration) bool {
 
80
        w := when(d)
 
81
        active := stopTimer(&t.r)
 
82
        t.r.when = w
 
83
        startTimer(&t.r)
 
84
        return active
 
85
}
 
86
 
59
87
func sendTime(now int64, c interface{}) {
60
88
        // Non-blocking send of time on c.
61
89
        // Used in NewTimer, it cannot block anyway (buffer).
81
109
func AfterFunc(d Duration, f func()) *Timer {
82
110
        t := &Timer{
83
111
                r: runtimeTimer{
84
 
                        when: nano() + int64(d),
 
112
                        when: when(d),
85
113
                        f:    goFunc,
86
114
                        arg:  f,
87
115
                },