workqueue tastlet kerneltimer BUG_ON

workqueue

#include <linux/workqueue.h>

 

 

struct work_struct {

               unsigned long pending;

                 struct list_head entry;                   /*  将任务挂载到 queue 的挂载点  */

                 void (*func)(void *);                    /*  任务方法  */

                 void *data;                                   /*  任务处理的数据 */

                 void *wq_data;                            /* work 的属主  */

                 strut timer_list timer;                    /*  任务延时处理定时器  */

};


 

简单的workqueue, 由kernel 自己内部调度

 

1、定义struct work_struct irq_queue;

2、初始化INIT_WORK(&irq_queue,do_irq_queuework);

3、调用方法:schedule_work(&rq_queue);

 

其中 work_func_t 的定义如下:
typedef void (*work_func_t)(struct work_struct *work);


work queue 主要的 API:

 

 

struct workqueue_struct *create_workqueue(const char *name);

struct workqueue_struct *create_singlethread_workqueue(const char *name);

 

 

DECLARE_WORK(name, void (*function)(void *), void *data);

 

 

set up work at run time
INIT_WORK(struct work_struct *work, work_func_t func)

PREPARE_WORK(struct work_struct *work, void (*function)(void *), void *data);

 

Schedule the work for execution

int schedule_work(struct work_struct *work)
void flush_scheduled_work(void)
int schedule_delayed_work(struct delayed_work *work, unsigned long delay)

 

add work to workqueue
int queue_work(struct workqueue_struct *wq, struct work_struct *work)
int queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay)

 

cancel a pending workqueue entry and         must follow that call with a call to flush_workqueue()

int cancel_delayed_work(struct work_struct *work);
void flush_workqueue(struct workqueue_struct *wq)

 

 

if you have done a workqueue:
void destroy_workqueue(struct workqueue_struct *wq)

 

 

 

tasklet

#include <linux/interrupt.h>

 

struct tasklet_struct
{
  struct tasklet_struct *next;
  unsigned long state;
  atomic_t count;
  void (*func)(unsigned long);
  unsigned long data;
};

各成员的含义如下:

(1)next指针:指向下一个tasklet的指针。

(2)state:定义了这个tasklet的当前状态。这一个32位的无符号长整数,当前只使用了bit[1]和bit[0]两个状态位。其中,bit[1]=1表示这个tasklet当前正在某个CPU上被执行,它仅对SMP系统才有意义,其作用就是为了防止多个CPU同时执行一个tasklet的情形出现;bit[0]=1表示这个tasklet已经被调度去等待执行了。对这两个状态位的宏定义如下所示(interrupt.h):

enum
{
  TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
};

(3)原子计数count:对这个tasklet的引用计数值。NOTE!只有当count等于0时,tasklet代码段才能执行,也即此时tasklet是被使能的;如果count非零,则这个tasklet是被禁止的。任何想要执行一个tasklet代码段的人都首先必须先检查其count成员是否为0。

(4)函数指针func:指向以函数形式表现的可执行tasklet代码段。

(5)data:函数func的参数。这是一个32位的无符号整数,其具体含义可供func函数自行解释,比如将其解释成一个指向某个用户自定义数据结构的地址值。

 

初始化:

 

void tasklet_init(struct tasklet_struct *t,

      void (*func)(unsigned long), unsigned long data);

DECLARE_TASKLET(name, func, data);

DECLARE_TASKLET_DISABLED(name, func, data);

Linux在interrupt.h头文件中又定义了两个用来定义tasklet_struct结构变量的辅助宏:

#define DECLARE_TASKLET(name, func, data) 
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }

#define DECLARE_TASKLET_DISABLED(name, func, data) 
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }

显然,从上述源代码可以看出,用DECLARE_TASKLET宏定义的tasklet在初始化时是被使能的(enabled),因为其count成员为0。而用DECLARE_TASKLET_DISABLED宏定义的tasklet在初始时是被禁止的(disabled),因为其count等于1。

 

enable and disable tasklet through atomic

void tasklet_disable(struct tasklet_struct *t);

void tasklet_disable_nosync(struct tasklet_struct *t);

void tasklet_enable(struct tasklet_struct *t);

 

Schedule the tasklet for execution

void tasklet_schedule(struct tasklet_struct *t);

void tasklet_hi_schedule(struct tasklet_struct *t);

 

void tasklet_kill(struct tasklet_struct *t);

 

 

 

BUG_ON(condition)

BUG_ON macro first checks for the condition and if condition is true then it calls BUG which do a printk of msg and call panic which halts the system.

The unlikely in BUG_ON macro is compiler directive which ask compiler to generate assembly such that the condition mentioned in unlikely isn't going to be met most-of-the time, which reduces jumps and speeds-up things, although if condition mentioned in unlikely met then there will be a long jump and speed will be effected, so must be used carefully. Same is with likely as it is the opposite of unlikely.

 

 

 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
### 回答1: create_singlethread_workqueue 函数是 Linux 内核中用来创建一个单线程工作队列的函数。 它的作用是创建一个只包含一个工作者线程的工作队列,该线程负责处理工作队列中的所有工作项。这种工作队列适合于那些只需要单个线程处理的异步任务,例如:定时器处理、任务调度等。 在调用该函数时,会返回一个指向工作队列的结构体指针,该结构体包含有关工作队列的信息。需要注意的是,在使用完工作队列后,需要调用 destroy_workqueue 函数来销毁它,以避免内存泄漏。 下面是 create_singlethread_workqueue 函数的函数原型: ``` struct workqueue_struct *create_singlethread_workqueue(const char *name); ``` 参数说明: - name:工作队列的名称,用于在系统中标识工作队列。 返回值: - 返回一个指向工作队列结构体的指针。 举例: ``` #include <linux/module.h> #include <linux/workqueue.h> static struct workqueue_struct *my_workqueue; static void my_work_handler(struct work_struct *work) { /* 处理工作项 */ } static int __init my_module_init(void) { my_workqueue = create_singlethread_workqueue("my_workqueue"); if (!my_workqueue) { printk(KERN_ERR "Failed to create workqueue\n"); return -ENOMEM; } /* 将工作项添加到工作队列 */ INIT_WORK(&my_work, my_work_handler); queue_work(my_workqueue, &my_work); return 0; } static void __exit my_module_exit(void) { /* 销毁工作队列 */ destroy_workqueue(my_workqueue); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL"); ``` 在这个例子中,我们首先调用 create_singlethread_workqueue 函数来创建一个名为 "my_workqueue" 的工作队列。然后,我们将一个工作项添加到队列中,并在工作者线程中处理它。最后,在模块退出时,我们调用 destroy_workqueue 函数来销毁工作队列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值