time_go123.mx raw

   1  //go:build go1.23
   2  
   3  package runtime
   4  
   5  import "unsafe"
   6  
   7  // Time functions for Go 1.23 and above.
   8  
   9  // This is the timer that's used internally inside the runtime.
  10  type timer struct {
  11  	// When to call the timer, and the interval for the ticker.
  12  	when   int64
  13  	period int64
  14  
  15  	// Callback from the time package.
  16  	f   func(arg any, seq uintptr, delta int64)
  17  	arg any
  18  }
  19  
  20  func (tim *timer) callCallback(delta int64) {
  21  	tim.f(tim.arg, 0, delta)
  22  }
  23  
  24  // This is the struct used internally in the runtime. The first two fields are
  25  // the same as time.Timer and time.Ticker so it can be used as-is in the time
  26  // package.
  27  type timeTimer struct {
  28  	c    unsafe.Pointer // <-chan time.Time
  29  	init bool
  30  	timer
  31  }
  32  
  33  //go:linkname newTimer time.newTimer
  34  func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg any, c unsafe.Pointer) *timeTimer {
  35  	tim := &timeTimer{
  36  		c:    c,
  37  		init: true,
  38  		timer: timer{
  39  			when:   when,
  40  			period: period,
  41  			f:      f,
  42  			arg:    arg,
  43  		},
  44  	}
  45  	scheduleLog("new timer")
  46  	addTimer(&timerNode{
  47  		timer:    &tim.timer,
  48  		callback: timerCallback,
  49  	})
  50  	return tim
  51  }
  52  
  53  //go:linkname stopTimer time.stopTimer
  54  func stopTimer(tim *timeTimer) bool {
  55  	return removeTimer(&tim.timer) != nil
  56  }
  57  
  58  //go:linkname resetTimer time.resetTimer
  59  func resetTimer(t *timeTimer, when, period int64) bool {
  60  	t.timer.when = when
  61  	t.timer.period = period
  62  	n := removeTimer(&t.timer)
  63  	removed := n != nil
  64  	if n == nil {
  65  		n = new(timerNode)
  66  	}
  67  	n.timer = &t.timer
  68  	n.callback = timerCallback
  69  	addTimer(n)
  70  	return removed
  71  }
  72