123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package time
- func Sleep(d Duration)
- // runtimeNano returns the current value of the runtime clock in nanoseconds.
- func runtimeNano() int64
- // Interface to timers implemented in package runtime.
- // Must be in sync with ../runtime/runtime.h:/^struct.Timer$
- type runtimeTimer struct {
- i int
- when int64
- period int64
- f func(interface{}, uintptr) // NOTE: must not be closure
- arg interface{}
- seq uintptr
- }
- func when(d Duration) int64 {
- if d <= 0 {
- return runtimeNano()
- }
- t := runtimeNano() + int64(d)
- if t < 0 {
- t = 1<<63 - 1
- }
- return t
- }
- func startTimer(*runtimeTimer)
- func stopTimer(*runtimeTimer) bool
- // The Timer type represents a single event.
- // When the Timer expires, the current time will be sent on C,
- // unless the Timer was created by AfterFunc.
- // A Timer must be created with NewTimer or AfterFunc.
- type Timer struct {
- C <-chan Time
- r runtimeTimer
- }
- func (t *Timer) Stop() bool {
- if t.r.f == nil {
- panic("time: Stop called on uninitialized Timer")
- }
- return stopTimer(&t.r)
- }
- func NewTimer(d Duration) *Timer {
- c := make(chan Time, 1)
- t := &Timer{
- C: c,
- r: runtimeTimer{
- when: when(d),
- f: sendTime,
- arg: c,
- },
- }
- startTimer(&t.r)
- return t
- }
- func (t *Timer) Reset(d Duration) bool {
- if t.r.f == nil {
- panic("time: Reset called on uninitialized Timer")
- }
- w := when(d)
- active := stopTimer(&t.r)
- t.r.when = w
- startTimer(&t.r)
- return active
- }
- func sendTime(c interface{}, seq uintptr) {
-
-
-
-
-
- select {
- case c.(chan Time) <- Now():
- default:
- }
- }
- func After(d Duration) <-chan Time {
- return NewTimer(d).C
- }
- func AfterFunc(d Duration, f func()) *Timer {
- t := &Timer{
- r: runtimeTimer{
- when: when(d),
- f: goFunc,
- arg: f,
- },
- }
- startTimer(&t.r)
- return t
- }
- func goFunc(arg interface{}, seq uintptr) {
- go arg.(func())()
- }
|