Document directory
- 1. Definition
- 2. Related functions
The kernel timer is a mechanism used by the kernel to control the scheduling of a function at a certain time point in the future, and is in the context of interruption. Therefore, the scheduling function must comply with the following rules:
1) access to user space is not allowed without the current pointer. Because there is no process context, there is no connection between the relevant code and the interrupted process.
2) The timer function must be atomic and cannot be sleep or scheduled. atomic Code cannot call schedule or some wait_event, nor call any other function that may be sleep. for example, call kmalloc (..., gfp_kernel) is against the rules. the flag must also be unavailable because they may sleep.
3) any data structure accessed by the timer function should be protected against concurrent access.
A re-register timer always runs on the same CPU. The kernel code can call the in_interrupt () function to tell whether the timer runs in the interrupt context, if the processor is currently running in the interrupt context, it returns a non-zero value, Either hardware interruption or software interruption. another function, in_atomic (), is used to determine that the return value is non-zero when scheduling is disabled;
The scheduling function of the kernel timer is automatically deregistered after it is run once, but you can run it periodically by rescheduling itself in the scheduled function.
1. Definition
During definition initialization, this macro defines a kernel timer named timer_name and initializes its function, expires, name, and base fields. Because of initialization during definition, the initialization value cannot contain variables. Define_timer (timer_name, function_name, expires_value, data); first define and initialize struct timer_list mytimer; setup_timer (& mytimer, (* function) (unsigned long), unsigned long data); mytimer. expires = jiffies + 5 * Hz; add_timer (struct timer_list * timer)
2. Related functions
Add_timer can connect the timer to the kernel scheduled linked list. If the timer is in the linked list, timer_pending will return the true static inline void add_timer (struct timer_list * timer) {bug_on (timer_pending (timer )); __mod_timer (timer, timer-> expires);} to modify the scheduling time of a timer, if the last scheduled time of the modification has not returned 1, otherwise 0 is returned. Int evaluate (struct timer_list * timer, unsigned long expires) mod_timer (timer, expires) is equivalent to: del_timer (timer); timer-> expires = expires; add_timer (timer ); it can be seen that mod_timer can replace add_timer. It is best to use mod_timer when multiple threads use the same timer. If add_timer is used, a bug may be reported. Cancel a timer, where del_timer_sync is used on the SMP system (in a non-SMP system, it is equal to del_timer). When the timer function to be deregistered is running on another CPU, del_timer_sync () wait until it is finished, so this function will sleep. In addition, it should be prevented from competing with the scheduled function to use the same lock. For a timer that has been run and has not been re-registered, there is nothing to do to deregister the function. Del_timer (struct timer_list * timer); del_timer_sync (struct timer_list * timer); this function is used to determine whether a timer is added to the kernel linked list for scheduling. Note: When a timer function is about to be run, the kernel will delete the corresponding timer from the kernel linked list (equivalent to deregistering) so that the timer function can register itself again. Int timer_pending (const struct timer_list * timer );