github.com/embeddedgo/x@v0.0.6-0.20191217015414-d79a36f562e7/time/sleep.go (about)

     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  import _ "unsafe"
     8  
     9  // Sleep pauses the current goroutine for at least the duration d.
    10  // A negative or zero duration causes Sleep to return immediately.
    11  //go:linkname Sleep time.Sleep
    12  func Sleep(d Duration)
    13  
    14  // Interface to timers implemented in package runtime.
    15  // Must be in sync with ../runtime/time.go:/^type timer
    16  type runtimeTimer struct {
    17  	tb uintptr
    18  	i  int
    19  
    20  	when   int64
    21  	period int64
    22  	f      func(interface{}, uintptr) // NOTE: must not be closure
    23  	arg    interface{}
    24  	seq    uintptr
    25  }
    26  
    27  // when is a helper function for setting the 'when' field of a runtimeTimer.
    28  // It returns what the time will be, in nanoseconds, Duration d in the future.
    29  // If d is negative, it is ignored. If the returned value would be less than
    30  // zero because of an overflow, MaxInt64 is returned.
    31  func when(d Duration) int64 {
    32  	if d <= 0 {
    33  		return runtimeNano()
    34  	}
    35  	t := runtimeNano() + int64(d)
    36  	if t < 0 {
    37  		t = 1<<63 - 1 // math.MaxInt64
    38  	}
    39  	return t
    40  }
    41  
    42  //go:linkname startTimer time.startTimer
    43  func startTimer(*runtimeTimer)
    44  
    45  //go:linkname stopTimer time.stopTimer
    46  func stopTimer(*runtimeTimer) bool
    47  
    48  // The Timer type represents a single event.
    49  // When the Timer expires, the current time will be sent on C,
    50  // unless the Timer was created by AfterFunc.
    51  // A Timer must be created with NewTimer or AfterFunc.
    52  type Timer struct {
    53  	C <-chan Time
    54  	r runtimeTimer
    55  }
    56  
    57  // Stop prevents the Timer from firing.
    58  // It returns true if the call stops the timer, false if the timer has already
    59  // expired or been stopped.
    60  // Stop does not close the channel, to prevent a read from the channel succeeding
    61  // incorrectly.
    62  //
    63  // To ensure the channel is empty after a call to Stop, check the
    64  // return value and drain the channel.
    65  // For example, assuming the program has not received from t.C already:
    66  //
    67  // 	if !t.Stop() {
    68  // 		<-t.C
    69  // 	}
    70  //
    71  // This cannot be done concurrent to other receives from the Timer's
    72  // channel.
    73  //
    74  // For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
    75  // has already expired and the function f has been started in its own goroutine;
    76  // Stop does not wait for f to complete before returning.
    77  // If the caller needs to know whether f is completed, it must coordinate
    78  // with f explicitly.
    79  func (t *Timer) Stop() bool {
    80  	if t.r.f == nil {
    81  		panic("time: Stop called on uninitialized Timer")
    82  	}
    83  	return stopTimer(&t.r)
    84  }
    85  
    86  // NewTimer creates a new Timer that will send
    87  // the current time on its channel after at least duration d.
    88  func NewTimer(d Duration) *Timer {
    89  	c := make(chan Time, 1)
    90  	t := &Timer{
    91  		C: c,
    92  		r: runtimeTimer{
    93  			when: when(d),
    94  			f:    sendTime,
    95  			arg:  c,
    96  		},
    97  	}
    98  	startTimer(&t.r)
    99  	return t
   100  }
   101  
   102  // Reset changes the timer to expire after duration d.
   103  // It returns true if the timer had been active, false if the timer had
   104  // expired or been stopped.
   105  //
   106  // Reset should be invoked only on stopped or expired timers with drained channels.
   107  // If a program has already received a value from t.C, the timer is known
   108  // to have expired and the channel drained, so t.Reset can be used directly.
   109  // If a program has not yet received a value from t.C, however,
   110  // the timer must be stopped and—if Stop reports that the timer expired
   111  // before being stopped—the channel explicitly drained:
   112  //
   113  // 	if !t.Stop() {
   114  // 		<-t.C
   115  // 	}
   116  // 	t.Reset(d)
   117  //
   118  // This should not be done concurrent to other receives from the Timer's
   119  // channel.
   120  //
   121  // Note that it is not possible to use Reset's return value correctly, as there
   122  // is a race condition between draining the channel and the new timer expiring.
   123  // Reset should always be invoked on stopped or expired channels, as described above.
   124  // The return value exists to preserve compatibility with existing programs.
   125  func (t *Timer) Reset(d Duration) bool {
   126  	if t.r.f == nil {
   127  		panic("time: Reset called on uninitialized Timer")
   128  	}
   129  	w := when(d)
   130  	active := stopTimer(&t.r)
   131  	t.r.when = w
   132  	startTimer(&t.r)
   133  	return active
   134  }
   135  
   136  func sendTime(c interface{}, seq uintptr) {
   137  	// Non-blocking send of time on c.
   138  	// Used in NewTimer, it cannot block anyway (buffer).
   139  	// Used in NewTicker, dropping sends on the floor is
   140  	// the desired behavior when the reader gets behind,
   141  	// because the sends are periodic.
   142  	select {
   143  	case c.(chan Time) <- Now():
   144  	default:
   145  	}
   146  }
   147  
   148  // After waits for the duration to elapse and then sends the current time
   149  // on the returned channel.
   150  // It is equivalent to NewTimer(d).C.
   151  // The underlying Timer is not recovered by the garbage collector
   152  // until the timer fires. If efficiency is a concern, use NewTimer
   153  // instead and call Timer.Stop if the timer is no longer needed.
   154  func After(d Duration) <-chan Time {
   155  	return NewTimer(d).C
   156  }
   157  
   158  // AfterFunc waits for the duration to elapse and then calls f
   159  // in its own goroutine. It returns a Timer that can
   160  // be used to cancel the call using its Stop method.
   161  func AfterFunc(d Duration, f func()) *Timer {
   162  	t := &Timer{
   163  		r: runtimeTimer{
   164  			when: when(d),
   165  			f:    goFunc,
   166  			arg:  f,
   167  		},
   168  	}
   169  	startTimer(&t.r)
   170  	return t
   171  }
   172  
   173  func goFunc(arg interface{}, seq uintptr) {
   174  	go arg.(func())()
   175  }