~james-page/ubuntu/utopic/gccgo-go/cgo-support

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-27 09:18:55 UTC
  • Revision ID: package-import@ubuntu.com-20140127091855-zxfshmykfsyyw4b2
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2009 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package time
 
6
 
 
7
// Sleep pauses the current goroutine for at least the duration d.
 
8
// A negative or zero duration causes Sleep to return immediately.
 
9
func Sleep(d Duration)
 
10
 
 
11
func nano() int64 {
 
12
        sec, nsec := now()
 
13
        return sec*1e9 + int64(nsec)
 
14
}
 
15
 
 
16
// Interface to timers implemented in package runtime.
 
17
// Must be in sync with ../runtime/runtime.h:/^struct.Timer$
 
18
type runtimeTimer struct {
 
19
        i      int32
 
20
        when   int64
 
21
        period int64
 
22
        f      func(int64, interface{}) // NOTE: must not be closure
 
23
        arg    interface{}
 
24
}
 
25
 
 
26
// when is a helper function for setting the 'when' field of a runtimeTimer.
 
27
// It returns what the time will be, in nanoseconds, Duration d in the future.
 
28
// If d is negative, it is ignored.  If the returned value would be less than
 
29
// zero because of an overflow, MaxInt64 is returned.
 
30
func when(d Duration) int64 {
 
31
        if d <= 0 {
 
32
                return nano()
 
33
        }
 
34
        t := nano() + int64(d)
 
35
        if t < 0 {
 
36
                t = 1<<63 - 1 // math.MaxInt64
 
37
        }
 
38
        return t
 
39
}
 
40
 
 
41
func startTimer(*runtimeTimer)
 
42
func stopTimer(*runtimeTimer) bool
 
43
 
 
44
// The Timer type represents a single event.
 
45
// When the Timer expires, the current time will be sent on C,
 
46
// unless the Timer was created by AfterFunc.
 
47
type Timer struct {
 
48
        C <-chan Time
 
49
        r runtimeTimer
 
50
}
 
51
 
 
52
// Stop prevents the Timer from firing.
 
53
// It returns true if the call stops the timer, false if the timer has already
 
54
// expired or been stopped.
 
55
// Stop does not close the channel, to prevent a read from the channel succeeding
 
56
// incorrectly.
 
57
func (t *Timer) Stop() bool {
 
58
        return stopTimer(&t.r)
 
59
}
 
60
 
 
61
// NewTimer creates a new Timer that will send
 
62
// the current time on its channel after at least duration d.
 
63
func NewTimer(d Duration) *Timer {
 
64
        c := make(chan Time, 1)
 
65
        t := &Timer{
 
66
                C: c,
 
67
                r: runtimeTimer{
 
68
                        when: when(d),
 
69
                        f:    sendTime,
 
70
                        arg:  c,
 
71
                },
 
72
        }
 
73
        startTimer(&t.r)
 
74
        return t
 
75
}
 
76
 
 
77
// Reset changes the timer to expire after duration d.
 
78
// It returns true if the timer had been active, false if the timer had
 
79
// expired or been stopped.
 
80
func (t *Timer) Reset(d Duration) bool {
 
81
        w := when(d)
 
82
        active := stopTimer(&t.r)
 
83
        t.r.when = w
 
84
        startTimer(&t.r)
 
85
        return active
 
86
}
 
87
 
 
88
func sendTime(now int64, c interface{}) {
 
89
        // Non-blocking send of time on c.
 
90
        // Used in NewTimer, it cannot block anyway (buffer).
 
91
        // Used in NewTicker, dropping sends on the floor is
 
92
        // the desired behavior when the reader gets behind,
 
93
        // because the sends are periodic.
 
94
        select {
 
95
        case c.(chan Time) <- Unix(0, now):
 
96
        default:
 
97
        }
 
98
}
 
99
 
 
100
// After waits for the duration to elapse and then sends the current time
 
101
// on the returned channel.
 
102
// It is equivalent to NewTimer(d).C.
 
103
func After(d Duration) <-chan Time {
 
104
        return NewTimer(d).C
 
105
}
 
106
 
 
107
// AfterFunc waits for the duration to elapse and then calls f
 
108
// in its own goroutine. It returns a Timer that can
 
109
// be used to cancel the call using its Stop method.
 
110
func AfterFunc(d Duration, f func()) *Timer {
 
111
        t := &Timer{
 
112
                r: runtimeTimer{
 
113
                        when: when(d),
 
114
                        f:    goFunc,
 
115
                        arg:  f,
 
116
                },
 
117
        }
 
118
        startTimer(&t.r)
 
119
        return t
 
120
}
 
121
 
 
122
func goFunc(now int64, arg interface{}) {
 
123
        go arg.(func())()
 
124
}