linux Tasklet 实现
记住 tasklet 是一个特殊的函数,可能被调度来运行,在软中断上下文,在一个系统决 定的安全时间中. 它们可能被调度运行多次,但是 tasklet 调度不累积; ; tasklet 只 运行一次,即便它在被投放前被重复请求. 没有 tasklet 会和它自己并行运行,因为它 只运行一次,但是 tasklet 可以与 SMP 系统上的其他 tasklet 并行运行. 因此,如果 你的驱动有多个 tasklet,它们必须采取某类加锁来避免彼此冲突. ? tasklet 也保证作为函数运行在第一个调度它们的同一个 CPU 上. 因此,一个中断处理 可以确保一个 tasklet 在处理者结束前不会开始执行. 但是,另一个中断当然可能在 tasklet 在运行时被递交,因此,tasklet 和中断处理之间加锁可能仍然需要. ? tasklet 必须使用 DECLARE_TASKLET 宏来声明: DECLARE_TASKLET(name,function,data); name 是给 tasklet 的名子,function 是调用来执行 tasklet (它带一个 unsigned long 参数并且返回 void )的函数,以及 data 是一个 unsigned long 值来传递给 tasklet 函数. ? short 驱动声明它的 tasklet 如下: ? void short_do_tasklet(unsigned long); DECLARE_TASKLET(short_tasklet,short_do_tasklet,0); ? 函数 tasklet_schedule 用来调度一个 tasklet 运行. 如果 short 使用 tasklet=1 来 加载,它安装一个不同的中断处理来保存数据并且调度 tasklet 如下: ? irqreturn_t short_tl_interrupt(int irq,void *dev_id,struct pt_regs *regs) { do_gettimeofday((struct timeval *) tv_head); /* cast to stop ‘volatile‘ warning */ short_incr_tv(&tv_head); tasklet_schedule(&short_tasklet); short_wq_count++; /* record that an interrupt arrived */ return IRQ_HANDLED; } ? 实际的 tasklet 函数,将在系统方便时很快执行. 如同前面提过,这个函数进行处理中断的大量工作; 它看来如此: ? void short_do_tasklet (unsigned long unused) { int savecount = short_wq_count,written; short_wq_count = 0; /* we have already been removed from the queue */ ? /* *? The bottom half reads the tv array,filled by the top half, *? and prints it to the circular text buffer,which is then consumed *? by reading processes */ /* First write the number of interrupts that occurred before this bh */ written = sprintf((char *)short_head,"bh after %6in",savecount); short_incr_bp(&short_head,written); /* *? Then,write the time values. Write exactly 16 bytes at a time, *? so it aligns with PAGE_SIZE */ ? ? do { ? written = sprintf((char *)short_head,"%08u.%06un", (int)(tv_tail->tv_sec % 100000000),(int)(tv_tail->tv_usec)); short_incr_bp(&short_head,written); short_incr_tv(&tv_tail); ? } while (tv_tail != tv_head); ? wake_up_interruptible(&short_queue); /* awake any reading process */ } ? 在别的东西中,这个 tasklet 记录了从它上次被调用以来有多少中断到达. 一个如 short 一样的设备能够在短时间内产生大量中断,因此在后半部执行前有几个中断到达就 不是不寻常的. 驱动必须一直准备这种可能性并且必须能够从前半部留下的信息中决定有 多少工作要做. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |