Setting Timers

Setting a new timer is a two-step process:

1. hrtimer_init is used to initialize a hrtimer instance. <hrtimer.h>

void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, enum hrtimer_mode mode);

timer denotes the affected high-resolution timer, clock is the clock to bind the timer to, and mode specifies if absolute or relative time values (relative to the current time) are used. Two constants are available for selection: <hrtimer.h>

enum hrtimer_mode {

HRTIMER_MODE_ABS, HRTIMER_MODE_REL,

/* Time value is absolute */ /* Time value is relative to now */

2. hrtimer_start sets the expiration time of a timer and starts it.

The implementation of both functions is purely technical and not very interesting, their code need not be discussed in detail.

To cancel a scheduled timer, the kernel offers hrtimer_cancel and hrtimer_try_to_cancel. The difference between both functions is that hrtimer_try_to_cancel provides the extra return value —1 if the timer if currently executing and thus cannot be stopped anymore. hrtimer_cancel waits until the handler has executed in this case. Besides, both functions return 0 if the timer was not active, and 1 if it was active, that is, if its status is either hrtimer_state_enqueued or hrtimer_state_pending.

Restarting a canceled timer is done with hrtimer_restart:

<hrtimer.h>

int hrtimer_cancel(struct hrtimer *timer)

int hrtimer_try_to_cancel(struct hrtimer *timer)

int hrtimer_restart(struct hrtimer *timer)

Continue reading here: Implementation

Was this article helpful?

0 0