ULK --- Chap 4: Tasklets (Note)

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.

 

转载于:https://www.cnblogs.com/miaoyong/p/5014978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值