Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 15: Time Management


nodeis used to keep the timer on the red-black tree as mentioned above, andbasepoints to the timer base.
The fields that are interesting for the timer’s user arefunctionandexpires. While the latter denotes the
expiration time,functionis the callback employed when the timer expires.cb_entryis the list element
that allows for keeping the timer on the callback list headed byhrtimer_cpu_base->cb_pending.Each
timer may specify conditions under which it may or must be run. The following choices are possible:

<hrtimer.h>
/*
* hrtimer callback modes:
*
* HRTIMER_CB_SOFTIRQ: Callback must run in softirq context
* HRTIMER_CB_IRQSAFE: Callback may run in hardirq context
* HRTIMER_CB_IRQSAFE_NO_RESTART: Callback may run in hardirq context and
* does not restart the timer
* HRTIMER_CB_IRQSAFE_NO_SOFTIRQ: Callback must run in hardirq context
* Special mode for tick emultation
*/
enum hrtimer_cb_mode {
HRTIMER_CB_SOFTIRQ,
HRTIMER_CB_IRQSAFE,
HRTIMER_CB_IRQSAFE_NO_RESTART,
HRTIMER_CB_IRQSAFE_NO_SOFTIRQ,
};

The comment explains the meaning of the individual constants well, and nothing need be added. The
current state of a timer is kept instate. The following values are possible^16 :

❑ HRTIMER_STATE_INACTIVEdenotes an inactive timer.
❑ A timer that is enqueued on a clock base and waiting for expiration is in state
HRTIMER_STATE_ENQUEUED.
❑ HRTIMER_STATE_CALLBACKstates that the callback is currently executing.
❑ When the timer has expired and is waiting on the callback list to be executed, the state is
HRTIMER_STATE_PENDING.

The callback function deserves some special consideration. Two return values are possible:

<hrtimer.h>
enum hrtimer_restart {
HRTIMER_NORESTART, /* Timer is not restarted */
HRTIMER_RESTART, /* Timer must be restarted */
};

Usually, the callback will returnHRTIMER_NORESTARTwhen it has finished executing. In this case, the
timer will simply disappear from the system. However, the timer can also choose to be restarted. This
requires two steps from the callback:


  1. The result of the callback must beHRTIMER_RESTART.


(^16) In a rare corner case, it is also possible that a timer is both in the statesHRTIMER_STATE_ENQUEUEDand
HRTIMER_STATE_CALLBACK. See the commentary in<hrtimer.h>for more information.

Free download pdf