github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/timer/doc.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  // Package timer provides timer functionality that supports the following
     4  // features:
     5  // 
     6  // The Timer type represents a single event.
     7  // When the Timer expires, a signal will be sent on Signal() channel,
     8  // unless the Timer was created by AfterFunc.
     9  // A Timer must be created with Init, After or AfterFunc.
    10  //
    11  // An active timer (one that has been called Start()) may be
    12  // passed to deltimer (time.stopTimer), after which it is no longer an
    13  // active timer. It is an inactive timer.
    14  // In an inactive timer the period, f, arg, and seq fields may be modified,
    15  // but not the when field.
    16  // It's OK to just drop an inactive timer and let the GC collect it.
    17  // It's not OK to pass an inactive timer to addtimer.
    18  // Only newly allocated timer values may be passed to addtimer.
    19  //
    20  // We don't permit calling addtimer/deltimer/modtimer/resettimer simultaneously,
    21  // but adjusttimers and runtimer can be called at the same time as any of those.
    22  // 
    23  // Init initialize the Timer with given callback function or make the channel and send signal on it
    24  // Be aware that given function must not be closure and must not block the caller.
    25  // 
    26  // Stop prevents the Timer from firing (no more ticks will be sent).
    27  // It returns true if the call stops the timer, false if the timer has already
    28  // expired or been stopped (It reports whether t was stopped before being run.).
    29  // Stop does not close the channel, to prevent a read from the channel succeeding
    30  // incorrectly (seeing an erroneous "tick").
    31  //
    32  // To ensure the channel is empty after a call to Stop, check the
    33  // return value and drain the channel.
    34  // For example, assuming the program has not received from t.Signal() already:
    35  //
    36  // 	if !t.Stop() {
    37  // 		<-t.Signal()
    38  // 	}
    39  //
    40  // This cannot be done concurrent to other receives from the Timer's
    41  // channel or other calls to the Timer's Stop method.
    42  //
    43  // For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
    44  // has already expired and the function f has been started in its own goroutine;
    45  // Stop does not wait for f to complete before returning.
    46  // If the caller needs to know whether f is completed, it must coordinate
    47  // with f explicitly.
    48  // 
    49  // Reset changes the timer to expire after duration d.
    50  // It returns true if the timer had been active, false if the timer had
    51  // expired or been stopped.
    52  //
    53  // For a Timer created with NewTimer, Reset should be invoked only on
    54  // stopped or expired timers with drained channels.
    55  //
    56  // If a program has already received a value from t.Signal(), the timer is known
    57  // to have expired and the channel drained, so t.Reset can be used directly.
    58  // If a program has not yet received a value from t.Signal(), however,
    59  // the timer must be stopped and—if Stop reports that the timer expired
    60  // before being stopped—the channel explicitly drained:
    61  //
    62  // 	if !t.Stop() {
    63  // 		<-t.Signal()
    64  // 	}
    65  // 	t.Reset(d)
    66  //
    67  // This should not be done concurrent to other receives from the Timer's
    68  // channel.
    69  //
    70  // Note that it is not possible to use Reset's return value correctly, as there
    71  // is a race condition between draining the channel and the new timer expiring.
    72  // Reset should always be invoked on stopped or expired channels, as described above.
    73  // The return value exists to preserve compatibility with existing programs.
    74  //
    75  // For a Timer created with AfterFunc(d, f), Reset either reschedules
    76  // when f will run, in which case Reset returns true, or schedules f
    77  // to run again, in which case it returns false.
    78  // When Reset returns false, Reset neither waits for the prior f to
    79  // complete before returning nor does it guarantee that the subsequent
    80  // goroutine running f does not run concurrently with the prior
    81  // one. If the caller needs to know whether the prior execution of
    82  // f is completed, it must coordinate with f explicitly.
    83  //
    84  // Reset stops a ticker and resets its period to the specified duration.
    85  // The next tick will arrive after the new period elapses. The duration d
    86  // must be greater than zero; if not, Reset will panic.
    87  // 
    88  // Modify modifies an existing timer.
    89  // Reports whether the timer was modified before it was run.
    90  // An inactive timer may be passed to Modify to turn into an
    91  // active timer with an updated when, period fields.
    92  // It's OK to call Modify() on a newly allocated Timer.
    93  // An active timer may call Modify(). No fields may be touched. It remains an active timer.
    94  // 
    95  // After waits for the duration to elapse and then sends signal on the returned channel.
    96  // The underlying Timer is not recovered by the garbage collector
    97  // until the timer fires. If efficiency is a concern, copy the body
    98  // instead and call timer.Stop() if the timer is no longer needed.
    99  // 
   100  // AfterFunc waits for the duration to elapse and then calls f
   101  // in its own goroutine. It returns a Timer that can
   102  // be used to cancel the call using its Stop method.
   103  package timer