github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/timer/status.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package timer
     4  
     5  import (
     6  	"sync/atomic"
     7  )
     8  
     9  // timer.start:
    10  //   status_Unset   -> status_Waiting
    11  //   anything else  -> panic: invalid value
    12  //
    13  // timer.stop:
    14  //   status_Waiting         -> status_Modifying -> status_Deleted
    15  //   status_ModifiedEarlier -> status_Modifying -> status_Deleted
    16  //   status_ModifiedLater   -> status_Modifying -> status_Deleted
    17  //   status_Unset           -> do nothing
    18  //   status_Deleted         -> do nothing
    19  //   status_Removing        -> do nothing
    20  //   status_Removed         -> do nothing
    21  //   status_Running         -> wait until status changes
    22  //   status_Moving          -> wait until status changes
    23  //   status_Modifying       -> wait until status changes
    24  //
    25  // timer.modify:
    26  //   status_Waiting    -> status_Modifying -> timerModifiedXX
    27  //   timerModifiedXX   -> status_Modifying -> timerModifiedYY
    28  //   status_Unset      -> status_Modifying -> status_Waiting
    29  //   status_Removed    -> status_Modifying -> status_Waiting
    30  //   status_Deleted    -> status_Modifying -> timerModifiedXX
    31  //   status_Running    -> wait until status changes
    32  //   status_Moving     -> wait until status changes
    33  //   status_Removing   -> wait until status changes
    34  //   status_Modifying  -> wait until status changes
    35  //
    36  // timing.cleanTimers (looks in timers heap):
    37  //   status_Deleted    -> status_Removing -> status_Removed
    38  //   timerModifiedXX   -> status_Moving -> status_Waiting
    39  //
    40  // timing.adjustTimers (looks in timers heap):
    41  //   status_Deleted    -> status_Removing -> status_Removed
    42  //   timerModifiedXX   -> status_Moving -> status_Waiting
    43  //
    44  // timing.runTimer (looks in timers heap):
    45  //   status_Unset      -> panic: uninitialized timer
    46  //   status_Waiting    -> status_Waiting or
    47  //   status_Waiting    -> status_Running -> status_Unset or
    48  //   status_Waiting    -> status_Running -> status_Waiting
    49  //   status_Modifying  -> wait until status changes
    50  //   timerModifiedXX   -> status_Moving -> status_Waiting
    51  //   status_Deleted    -> status_Removing -> status_Removed
    52  //   status_Running    -> panic: concurrent runTimer calls
    53  //   status_Removed    -> panic: inconsistent timer heap
    54  //   status_Removing   -> panic: inconsistent timer heap
    55  //   status_Moving     -> panic: inconsistent timer heap
    56  
    57  // Values for the timer status field.
    58  const (
    59  	// Timer has no status set yet.
    60  	status_Unset status = iota
    61  
    62  	// Waiting for timer to fire.
    63  	// The timer is in some P's heap.
    64  	status_Waiting
    65  
    66  	// Running the timer function.
    67  	// A timer will only have this status briefly.
    68  	status_Running
    69  
    70  	// The timer is deleted and should be removed.
    71  	// It should not be run, but it is still in some P's heap.
    72  	status_Deleted
    73  
    74  	// The timer is being removed.
    75  	// The timer will only have this status briefly.
    76  	status_Removing
    77  
    78  	// The timer has been stopped.
    79  	// It is not in any P's heap.
    80  	status_Removed
    81  
    82  	// The timer is being modified.
    83  	// The timer will only have this status briefly.
    84  	status_Modifying
    85  
    86  	// The timer has been modified to an earlier time.
    87  	// The new when value is in the timer.when and old on in the timerBucket.when field.
    88  	// The timer is in some P's heap, possibly in the wrong place.
    89  	status_ModifiedEarlier
    90  
    91  	// The timer has been modified to the same or a later time.
    92  	// The new when value is in the timer.when and old on in the timerBucket.when field.
    93  	// The timer is in some P's heap, possibly in the wrong place.
    94  	status_ModifiedLater
    95  
    96  	// The timer has been modified and is being moved.
    97  	// The timer will only have this status briefly.
    98  	status_Moving
    99  )
   100  
   101  // due to use atomic must use uint32
   102  type status uint32
   103  
   104  func (s status) Get() status       { return s }
   105  func (s *status) Set(status status) { *s = status }
   106  func (s *status) Load() status {
   107  	return status(atomic.LoadUint32((*uint32)(s)))
   108  }
   109  func (s *status) Store(status status) {
   110  	atomic.StoreUint32((*uint32)(s), uint32(status))
   111  }
   112  func (s *status) CompareAndSwap(old, new status) (swapped bool) {
   113  	return atomic.CompareAndSwapUint32((*uint32)(s), uint32(old), uint32(new))
   114  }