time_go122.mx raw

   1  //go:build !go1.23
   2  
   3  // Portions copyright 2009 The Go Authors. All rights reserved.
   4  // Use of this source code is governed by a BSD-style
   5  // license that can be found in the LICENSE file.
   6  
   7  package runtime
   8  
   9  // Time functions for Go 1.22 and below.
  10  
  11  type puintptr uintptr
  12  
  13  // Package time knows the layout of this structure.
  14  // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
  15  type timer struct {
  16  	// If this timer is on a heap, which P's heap it is on.
  17  	// puintptr rather than *p to match uintptr in the versions
  18  	// of this struct defined in other packages.
  19  	pp puintptr
  20  
  21  	// Timer wakes up at when, and then at when+period, ... (period > 0 only)
  22  	// each time calling f(arg, now) in the timer goroutine, so f must be
  23  	// a well-behaved function and not block.
  24  	//
  25  	// when must be positive on an active timer.
  26  	when   int64
  27  	period int64
  28  	f      func(any, uintptr)
  29  	arg    any
  30  	seq    uintptr
  31  
  32  	// What to set the when field to in timerModifiedXX status.
  33  	nextwhen int64
  34  
  35  	// The status field holds one of the values below.
  36  	status uint32
  37  }
  38  
  39  func (tim *timer) callCallback(delta int64) {
  40  	tim.f(tim.arg, 0)
  41  }
  42  
  43  // Defined in the time package, implemented here in the runtime.
  44  //
  45  //go:linkname startTimer time.startTimer
  46  func startTimer(tim *timer) {
  47  	addTimer(&timerNode{
  48  		timer:    tim,
  49  		callback: timerCallback,
  50  	})
  51  	scheduleLog("adding timer")
  52  }
  53  
  54  //go:linkname stopTimer time.stopTimer
  55  func stopTimer(tim *timer) bool {
  56  	return removeTimer(tim) != nil
  57  }
  58  
  59  //go:linkname resetTimer time.resetTimer
  60  func resetTimer(tim *timer, when int64) bool {
  61  	tim.when = when
  62  	n := removeTimer(tim)
  63  	startTimer(tim)
  64  	return n != nil
  65  }
  66