As mentioned above, high-precision timer is implemented through hrtimer. hrtimer uses a programmable timer to present it. It does not occupy CPU while waiting.
In the user State, when we call usleep, the thread will use hrtimer to wait for CPU usage during kernel state execution.
How to use it in kernel?
Let's take a look at the ep_poll function in eventpoll. C:
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, int maxevents, long timeout){int res = 0, eavail, timed_out = 0;unsigned long flags;long slack = 0;wait_queue_t wait;ktime_t expires, *to = NULL;if (timeout > 0) {struct timespec end_time = ep_set_mstimeout(timeout);slack = select_estimate_accuracy(&end_time);to = &expires;*to = timespec_to_ktime(end_time);} else if (timeout == 0) {/* * Avoid the unnecessary trip to the wait queue loop, if the * caller specified a non blocking operation. */timed_out = 1;spin_lock_irqsave(&ep->lock, flags);goto check_events;}fetch_events:spin_lock_irqsave(&ep->lock, flags);if (!ep_events_available(ep)) {/* * We don't have any available event to return to the caller. * We need to sleep here, and we will be wake up by * ep_poll_callback() when events will become available. */init_waitqueue_entry(&wait, current);__add_wait_queue_exclusive(&ep->wq, &wait);for (;;) {/* * We don't want to sleep if the ep_poll_callback() sends us * a wakeup in between. That's why we set the task state * to TASK_INTERRUPTIBLE before doing the checks. */set_current_state(TASK_INTERRUPTIBLE);if (ep_events_available(ep) || timed_out)break;if (signal_pending(current)) {res = -EINTR;break;}spin_unlock_irqrestore(&ep->lock, flags);if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))timed_out = 1;spin_lock_irqsave(&ep->lock, flags);}__remove_wait_queue(&ep->wq, &wait);set_current_state(TASK_RUNNING);}check_events:/* Is it worth to try to dig for events ? */eavail = ep_events_available(ep);spin_unlock_irqrestore(&ep->lock, flags);/* * Try to transfer events to user space. In case we get 0 events and * there's still timeout left over, we go trying again in search of * more luck. */if (!res && eavail && !(res = ep_send_events(ep, events, maxevents)) && !timed_out)goto fetch_events;return res;}
Have you seen schedule_hrtimeout_range (to, slack, hrtimer_mode_abs) above? It is called in hrtimer to execute the function waiting for timeout.
The function prototype is:
/**
* Schedule_hrtimeout_range-sleep until timeout
* @ Expires: timeout value (ktime_t)
* @ Delta: slack in expires timeout (ktime_t)
* @ Mode: Timer mode, hrtimer_mode_abs or hrtimer_mode_rel
*/
Int _ sched schedule_hrtimeout_range (ktime_t * expires, unsigned long delta,
Const Enum hrtimer_mode Mode)
{
Return schedule_hrtimeout_range_clock (expires, Delta, mode,
Clock_monotonic );
}
In ep_poll, the slack and to calculation methods are as follows:
Struct timespec end_time = ep_set_mstimeout (timeout );
Slack = select_estimate_accuracy (& end_time );
To = & expires;
* To = timespec_to_ktime (end_time );
If you need a high-precision timer in the kernel, you can refer to this method to achieve your own high-precision timer timeout.