tasklet使用

                                                                 tasklet

Tasklet的使用比较简单,只需要定义tasklet及其处理函数并将两者关联

例子:

Void my_tasklet_func(unsigned long)

DECLARE_TASKLET(my_tasklet.my_tasklet_func,data)

代码DECLARE_TASKLET实现了定义名称为my_tasklet的tasklet并将其与my_tasklet_func这个函数绑定,而传入这个函数的参数为data。

需要调度tasklet的时候引用一个tasklet_schedule()函数就能使系统在适当的时候进行调度,如下所示

Tasklet_schedule(&my_tasklet)

下面给出驱动模板

 

  1. void xxx_do_tasklet(unsigned long);  
  2.   
  3. DECLARE_TASKLET(xxx_tasklet,xxx_do_tasklet,0);  
  4.   
  5. void xxx_do_tasklet(unsigned long)  
  6.   
  7. {  
  8.   
  9. ……  
  10.   
  11. }  
  12.   
  13. irqreturn_t xxx_interrupt(int irq,void *dev_id,struct pt_regs *regs)  
  14.   
  15. {  
  16.   
  17.       ……  
  18.   
  19.       tasklet_schedule(&xxx_tasklet);  
  20.   
  21.       ……  
  22.   
  23. }  
  24.   
  25. int _init xxx_init(void)  
  26.   
  27. {  
  28.   
  29.       ……  
  30.   
  31.       result=request_irq(xxx_irq,xxx_interrupt,SA_INTERRUPT,”xxx”,NULL)  
  32.   
  33.       ……  
  34.   
  35. }  
  36.   
  37. void _exit xxx_exit(void)  
  38.   
  39. {  
  40.   
  41.       ……  
  42.   
  43.       free_irq(xxx_irq,xxx_irq_interrupt);  
  44.   
  45.       ……  
  46.   
  47. }  


二、tasklet函数详解

它对于中断处理特别有用:硬件中断必须尽快处理, 但大部分的数据管理可以延后到以后安全的时间执行。

tasklet 以一个数据结构形式存在,使用前必须被初始化。初始化能够通过调用一个特定函数或者通过使用某些宏定义声明结构:



 

  1. #include <linux/interrupt.h>   
  2. struct tasklet_struct  
  3. {  
  4.     struct tasklet_struct *next;  
  5.     unsigned long state;  
  6.     atomic_t count;  
  7.     void (*func)(unsigned long);  
  8.     unsigned long data;  
  9. };  
  10. void tasklet_init(struct tasklet_struct *t,  
  11.  void (*func)(unsigned long), unsigned long data);  
  12.   
  13. #define DECLARE_TASKLET(name, func, data) \  
  14. struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }  
  15. #define DECLARE_TASKLET_DISABLED(name, func, data) \  
  16. struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }  
  17.   
  18. void tasklet_disable(struct tasklet_struct *t);   
  19. /*函数暂时禁止给定的 tasklet被 tasklet_schedule 调度,直到这个 tasklet 被再次被enable;若这个 tasklet 当前在运行, 这个函数忙等待直到这个tasklet退出*/  
  20. void tasklet_disable_nosync(struct tasklet_struct *t);   
  21. /*和tasklet_disable类似,但是tasklet可能仍然运行在另一个 CPU */  
  22. void tasklet_enable(struct tasklet_struct *t);   
  23. /*使能一个之前被disable的 tasklet;若这个 tasklet 已经被调度, 它会很快运行。 tasklet_enable 和tasklet_disable必须匹配调用, 因为内核跟踪每个 tasklet 的"禁止次数"*/   
  24. void tasklet_schedule(struct tasklet_struct *t);   
  25. /*调度 tasklet 执行,如果tasklet在运行中被调度, 它在完成后会再次运行; 这保证了在其他事件被处理当中发生的事件受到应有的注意. 这个做法也允许一个 tasklet 重新调度它自己*/  
  26. void tasklet_hi_schedule(struct tasklet_struct *t);   
  27. /*和tasklet_schedule类似,只是在更高优先级执行。当软中断处理运行时, 它处理高优先级 tasklet 在其他软中断之前,只有具有低响应周期要求的驱动才应使用这个函数, 可避免其他软件中断处理引入的附加周期*/  
  28. void tasklet_kill(struct tasklet_struct *t);   
  29. /* 确保了 tasklet 不会被再次调度来运行,通常当一个设备正被关闭或者模块卸载时被调用。如果 tasklet 正在运行, 这个函数等待直到它执行完毕。若 tasklet 重新调度它自己,则必须阻止在调用 tasklet_kill 前它重新调度它自己,如同使用 del_timer_sync*/  

工作队列

一、       工作队列的使用

定义一个工作队列

Struct work_struct my_wq;

Void my_wq_func(unsigned long);

通过INIT_WORK可以初始化这个工作队列并将工作队列与处理函数绑定

INIT_WORK(&my_wq,(void(*)(void*))my_wq_func,NULL)

调度工作队列

Schedule_work(&my_wq)

使用模板

  1. struct work_struct xxx_wq;  
  2.   
  3. void xxx_do_work(unsigned long);  
  4.   
  5. void xxx_do_work(unsigned long)  
  6.   
  7. {  
  8.   
  9.       ……  
  10.   
  11. }  
  12.   
  13. irqreturn_t xxx_interrupt(int irq,void *dev_id,struct pt_regs *regs)  
  14.   
  15. {  
  16.   
  17.       ……  
  18.   
  19.       schedule_work(&xxx_wq);  
  20.   
  21.       ……  
  22.   
  23. }  
  24.   
  25. int init(void)  
  26.   
  27. {  
  28.   
  29.       ……  
  30.   
  31.       result=request_irq(xxx_irq,xxx_interrupt,SA_INTERRUPT,“xxx”,NULL)  
  32.   
  33.       ……  
  34.   
  35.       INIT_WORK(&xxx_wq,(void(*)(void*))xxx_do_work,NULL);  
  36.   
  37.       …….  
  38.   
  39. }  
  40.   
  41. void xxx_exit(void)  
  42.   
  43. {  
  44.   
  45.       ……  
  46.   
  47.       free_irq(xxx_irq,xxx_interrupt);  
  48.   
  49.       ……  
  50.   
  51. }  


 

二、       工作队列函数详解

工作队列有 struct workqueue_struct 类型,在 <linux/workqueue.h> 中定义。一个工作队列必须明确的在使用前创建,宏为:

struct workqueue_struct *create_workqueue(const char *name);
struct workqueue_struct *create_singlethread_workqueue(const char *name);

每个工作队列有一个或多个专用的进程("内核线程"), 这些进程运行提交给这个队列的函数。 若使用 create_workqueue, 就得到一个工作队列它在系统的每个处理器上有一个专用的线程。在很多情况下,过多线程对系统性能有影响,如果单个线程就足够则使用 create_singlethread_workqueue 来创建工作队列。

提交一个任务给一个工作队列,在这里《LDD3》介绍的内核2.6.10和我用的新内核2.6.22.2已经有不同了,老接口已经不能用了,编译会出错。这里我只讲2.6.22.2的新接口,至于老的接口我想今后内核不会再有了。从这一点我们可以看出内核发展。

  1. /*需要填充work_struct或delayed_work结构,可以在编译时完成, 宏如下: */  
  2.   
  3. struct work_struct {  
  4.     atomic_long_t data;  
  5. #define WORK_STRUCT_PENDING 0        /* T if work item pending execution */  
  6. #define WORK_STRUCT_FLAG_MASK (3UL)  
  7. #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)  
  8.     struct list_head entry;  
  9.     work_func_t func;  
  10. };  
  11.   
  12. struct delayed_work {  
  13.     struct work_struct work;  
  14.     struct timer_list timer;  
  15. };  
  16.   
  17. DECLARE_WORK(n, f)      
  18. /*n 是声明的work_struct结构名称, f是要从工作队列被调用的函数*/  
  19. DECLARE_DELAYED_WORK(n, f)  
  20. /*n是声明的delayed_work结构名称, f是要从工作队列被调用的函数*/  
  21.   
  22. /*若在运行时需要建立 work_struct 或 delayed_work结构, 使用下面 2 个宏定义:*/  
  23. INIT_WORK(struct work_struct *work, void (*function)(void *));   
  24. PREPARE_WORK(struct work_struct *work, void (*function)(void *));   
  25. INIT_DELAYED_WORK(struct delayed_work *work, void (*function)(void *));   
  26. PREPARE_DELAYED_WORK(struct delayed_work *work, void (*function)(void *));   
  27. /* INIT_* 做更加全面的初始化结构的工作,在第一次建立结构时使用. PREPARE_* 做几乎同样的工作, 但是它不初始化用来连接 work_struct或 delayed_work 结构到工作队列的指针。如果这个结构已经被提交给一个工作队列, 且只需要修改该结构,则使用 PREPARE_* 而不是 INIT_* */  
  28.   
  29. /*有 2 个函数来提交工作给一个工作队列:*/  
  30. int queue_work(struct workqueue_struct *queue, struct work_struct *work);  
  31. int queue_delayed_work(struct workqueue_struct *queue, struct delayed_work *work, unsigned long delay);  
  32. /* 每个都添加work到给定的workqueue。如果使用 queue_delay_work, 则实际的工作至少要经过指定的 jiffies 才会被执行。 这些函数若返回 1 则工作被成功加入到队列; 若为0,则意味着这个 work 已经在队列中等待,不能再次加入*/  


 

在将来的某个时间, 这个工作函数将被传入给定的 data 值来调用。这个函数将在工作线程的上下文运行, 因此它可以睡眠 (你应当知道这个睡眠可能影响提交给同一个工作队列的其他任务) 工作函数不能访问用户空间,因为它在一个内核线程中运行, 完全没有对应的用户空间来访问。
取消一个挂起的工作队列入口项可以调用:

int cancel_delayed_work(struct delayed_work *work);
void cancel_work_sync(struct work_struct *work)

如果这个入口在它开始执行前被取消,则返回非零。内核保证给定入口的执行不会在调用 cancel_delay_work 后被初始化. 如果 cancel_delay_work 返回 0, 但是, 这个入口可能已经运行在一个不同的处理器, 并且可能仍然在调用 cancel_delayed_work 后在运行. 要绝对确保工作函数没有在 cancel_delayed_work 返回 0 后在任何地方运行, 你必须跟随这个调用来调用:

void flush_workqueue(struct workqueue_struct *queue);

在 flush_workqueue 返回后, 没有在这个调用前提交的函数在系统中任何地方运行。
而cancel_work_sync会取消相应的work,但是如果这个work已经在运行那么cancel_work_sync会阻塞,直到work完成并取消相应的work。

当用完一个工作队列,可以去掉它,使用:

void destroy_workqueue(struct workqueue_struct *queue);

三、与tasklet的联系

工作队列类似 tasklets,允许内核代码请求在将来某个时间调用一个函数,不同在于:
(1)tasklet 在软件中断上下文中运行,所以 tasklet 代码必须是原子的。而工作队列函数在一个特殊内核进程上下文运行,有更多的灵活性,且能够休眠。
(2)tasklet 只能在最初被提交的处理器上运行,这只是工作队列默认工作方式。
(3)内核代码可以请求工作队列函数被延后一个给定的时间间隔。
(4)tasklet 执行的很快, 短时期, 并且在原子态, 而工作队列函数可能是长周期且不需要是原子的,两个机制有它适合的情形。







代码列子:

  1 #include<linux/module.h>
  2 #include<linux/init.h>
  3 #include<linux/interrupt.h>
  4
  5 MODULE_LICENSE("GPL");
  6
  7 struct tasklet_struct tasklet_test;
  8
  9 void tasklet_hander_function(unsigned long data)
 10 {
 11     printk("Hello tasklet\n");
 12     printk("tasklet data is %ld \n",data);
 13 }
 14
 15 static int __init tasklet_test_init(void)
 16 {
 17     printk("tasklet init\n");
 18     tasklet_init(&tasklet_test,tasklet_hander_function,tasklet_test.data);
 19     tasklet_test.data = 10;
 20     tasklet_schedule(&tasklet_test);
 21
 22     return 0;
 23 }
 24
 25 static void __exit tasklet_test_exit(void)
 26 {
 27     printk("tasklet exit\n");
 28     tasklet_kill(&tasklet_test);
 29 }
 30
 31 module_init(tasklet_test_init);
 32 module_exit(tasklet_test_exit);



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Linux tasklet 示例代码: ``` #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/interrupt.h> #define SHARED_IRQ 19 static int irq = SHARED_IRQ, my_dev_id, irq_counter = 0; module_param (irq,int,0664); void tasklet_handler(unsigned long data) { printk(KERN_INFO "In the tasklet handler function\n"); } DECLARE_TASKLET(my_tasklet, tasklet_handler, 0); static irqreturn_t my_interrupt(int irq, void *dev_id) { irq_counter++; printk(KERN_INFO "In the ISR: counter = %d\n", irq_counter); tasklet_schedule(&my_tasklet); return IRQ_HANDLED; } static int __init my_tasklet_init(void) { if (request_irq(irq, my_interrupt, IRQF_SHARED, "my_interrupt", &my_dev_id)) return -1; printk(KERN_INFO "Successfully loaded the tasklet module\n"); return 0; } static void __exit my_tasklet_exit(void) { tasklet_kill(&my_tasklet); free_irq(irq, &my_dev_id); printk(KERN_INFO "Successfully removed the tasklet module\n"); } module_init(my_tasklet_init); module_exit(my_tasklet_exit); MODULE_AUTHOR("TechBeamers"); MODULE_DESCRIPTION("Tasklet Example Module"); MODULE_LICENSE("GPL"); ``` 在此示例中,我们首先声明了一个名为“my_tasklet”的任务,在其中定义了一个称为“tasklet_handler”的函数,当任务激活时将调用此函数。然后我们使用“DECLARE_TASKLET”宏将任务声明为全局。 我们还定义了一个中断处理程序(“my_interrupt”),它会增加一个计数器并调度任务。最后,我们还为模块提供了一个加载和卸载函数,实现请求和释放共享中断,并在系统日志中显示状态消息。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值