...
Run Format

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

View as plain text