src/pkg/time/sleep.go - The Go Programming Language

Golang

Source file src/pkg/time/sleep.go

     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 the duration d.
     8	func Sleep(d Duration)
     9	
    10	func nano() int64 {
    11		sec, nsec := now()
    12		return sec*1e9 + int64(nsec)
    13	}
    14	
    15	// Interface to timers implemented in package runtime.
    16	// Must be in sync with ../runtime/runtime.h:/^struct.Timer$
    17	type runtimeTimer struct {
    18		i      int32
    19		when   int64
    20		period int64
    21		f      func(int64, interface{})
    22		arg    interface{}
    23	}
    24	
    25	func startTimer(*runtimeTimer)
    26	func stopTimer(*runtimeTimer) bool
    27	
    28	// The Timer type represents a single event.
    29	// When the Timer expires, the current time will be sent on C,
    30	// unless the Timer was created by AfterFunc.
    31	type Timer struct {
    32		C <-chan Time
    33		r runtimeTimer
    34	}
    35	
    36	// Stop prevents the Timer from firing.
    37	// 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) {
    40		return stopTimer(&t.r)
    41	}
    42	
    43	// NewTimer creates a new Timer that will send
    44	// the current time on its channel after at least duration d.
    45	func NewTimer(d Duration) *Timer {
    46		c := make(chan Time, 1)
    47		t := &Timer{
    48			C: c,
    49			r: runtimeTimer{
    50				when: nano() + int64(d),
    51				f:    sendTime,
    52				arg:  c,
    53			},
    54		}
    55		startTimer(&t.r)
    56		return t
    57	}
    58	
    59	func sendTime(now int64, c interface{}) {
    60		// Non-blocking send of time on c.
    61		// Used in NewTimer, it cannot block anyway (buffer).
    62		// Used in NewTicker, dropping sends on the floor is
    63		// the desired behavior when the reader gets behind,
    64		// because the sends are periodic.
    65		select {
    66		case c.(chan Time) <- Unix(0, now):
    67		default:
    68		}
    69	}
    70	
    71	// After waits for the duration to elapse and then sends the current time
    72	// on the returned channel.
    73	// It is equivalent to NewTimer(d).C.
    74	func After(d Duration) <-chan Time {
    75		return NewTimer(d).C
    76	}
    77	
    78	// AfterFunc waits for the duration to elapse and then calls f
    79	// in its own goroutine. It returns a Timer that can
    80	// be used to cancel the call using its Stop method.
    81	func AfterFunc(d Duration, f func()) *Timer {
    82		t := &Timer{
    83			r: runtimeTimer{
    84				when: nano() + int64(d),
    85				f:    goFunc,
    86				arg:  f,
    87			},
    88		}
    89		startTimer(&t.r)
    90		return t
    91	}
    92	
    93	func goFunc(now int64, arg interface{}) {
    94		go arg.(func())()
    95	}