Tasklets are the preferred way to implement deferrable functions in I/O drivers. As already explained,
tasklets are built on top of two softirqs named HI_SOFTIRQ and TASKLET_SOFTIRQ. Several tasklets
may be associated with the same softirq, each tasklet carrying its own function. There is no difference
between the two softirqs, excpet that do_softirq() executes HI_SOFTIRQ‘s tasklets before TASKLET_SO
FTIRQ‘s tasklets.
Tasklets and high-priority tasklets are stored in the tasklet_vec and tasklet_hi_vec arrays, respectively.
Both of them include NR_CPUS elements of type tasklet_head, and each element consists of a pointer to a
list of tasklet descriptors. The tasklet descriptor is a data structure of type tasklet_struct, whose fields are
shown in following table:
field name Description
next Point to next descriptor in the list
state Status of the tasklet
count Lock counter
func Pointer to the tasklet function
data An unsigned long integer that may be used by the tasklet function
The state 0f field of the tasklet descriptor includes two flags:
TASKLET_STATE_SCHED: when set, this indicates that the tasklets is pending (has been scheduled for execution);
it also means that the tasklet description is inserted in one of the lists of the tasklet_vec and tasklet_hi_vec arrays.
TASKLET_STATE_RUN: when set, this includes that the tasklet is being executed; on a uniprocessor system this flag
is not used because there is no need to check whether a specific tasklet is running.
Let‘s suppose you‘re writing a device driver and you want to use a tasklet: what has to be done? First of all, you should
allocate a new tasklet_struct data structure and initialize it by invoking tasklet_init(); this function receives as its parameters
the address of the tasklet descriptor, the address of your tasklet function, and its optional integer argument.
The tasklet may be selectively disabled by invoking either tasklet_disable_nosync() or tasklet_disable(). Both functions
increase the count field of the tasklet descriptor, but the latter function does not return until an already running instance
of the tasklet function has terminated. To enable the tasklet, use tasklet_enable().
To active the tasklet, you should invoke either the tasklet_schedule() function or tasklet_hi_schedule() function, according
to the priority that you require for the tasklet. The two functions are very similar, each of them performs the following
actions:
1. Checks the TASKLET_STATE_SCHED flag; if it is set, returns (the tasklet has already been scheduled).
2. Invokes local_irq_save to save the state of the IF flag and to disable local interrupts.
3. Adds the tasklet descriptor at the beginning of the list pointed to by the tasklet_vec[n] or tasklet_hi_vec[n], where n
denotes the logical number of the local CPU.
4. Invokes raise_softirq_irqoff() to activate either the TASKLET_SOFTIRQ or the HI_SOFTIRQ softirq (this function is similar to
raise_softirq(), except that is assumes that local interrupts are alreay disabled).
5. Invokes local_irq_restore to restore the state of the IF flag.