Linux 内核工作队列

工作队列也是内核一种延时执行任务机制,显然,内核需要定义一个队列,然后队列上面存放需要执行的任务,而任务执行则有内核线程来调用任务。
工作队列定义如下:

/*  * The externally visible workqueue.  It relays the issued work items to  * the appropriate worker_pool through its pool_workqueues.  */ struct workqueue_struct {  struct list_head pwqs;  /* WR: all pwqs of this wq */  struct list_head list;  /* PR: list of all workqueues */

 struct mutex  mutex;  /* protects this wq */  int   work_color; /* WQ: current work color */  int   flush_color; /* WQ: current flush color */  atomic_t  nr_pwqs_to_flush; /* flush in progress */  struct wq_flusher *first_flusher; /* WQ: first flusher */  struct list_head flusher_queue; /* WQ: flush waiters */  struct list_head flusher_overflow; /* WQ: flush overflow list */

 struct list_head maydays; /* MD: pwqs requesting rescue */  struct worker  *rescuer; /* I: rescue worker */

 int   nr_drainers; /* WQ: drain in progress */  int   saved_max_active; /* WQ: saved pwq max_active */

 struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */  struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */

#ifdef CONFIG_SYSFS  struct wq_device *wq_dev; /* I: for sysfs interface */ #endif #ifdef CONFIG_LOCKDEP  struct lockdep_map lockdep_map; #endif  char   name[WQ_NAME_LEN]; /* I: workqueue name */

 /*   * Destruction of workqueue_struct is sched-RCU protected to allow   * walking the workqueues list without grabbing wq_pool_mutex.   * This is used to dump all workqueues from sysrq.   */  struct rcu_head  rcu;

 /* hot fields used during command issue, aligned to cacheline */  unsigned int  flags ____cacheline_aligned; /* WQ: WQ_* flags */  struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */  struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ };

队列上任务定义如下:

struct work_struct {  atomic_long_t data;  struct list_head entry;  work_func_t func; #ifdef CONFIG_LOCKDEP  struct lockdep_map lockdep_map; #endif };

或者一种带有延时性质的任务:

struct delayed_work {  struct work_struct work;  struct timer_list timer;

 /* target workqueue and CPU ->timer uses to queue ->work */  struct workqueue_struct *wq;  int cpu; };

工作任务加入工作队列由函数__queue_work()处理,其对外提供接口有:

1. 指定CPU下队列

bool queue_work_on(int cpu, struct workqueue_struct *wq,      struct work_struct *work)

 
2. 延时加入
void delayed_work_timer_fn(unsigned long __data)
3.
void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
    struct delayed_work *dwork, unsigned long delay) 
bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
      struct delayed_work *dwork, unsigned long delay)
 
4.
bool flush_delayed_work(struct delayed_work *dwork)
 
 
内核中常用的队列:
extern struct workqueue_struct *system_wq;
extern struct workqueue_struct *system_highpri_wq;
extern struct workqueue_struct *system_long_wq;
extern struct workqueue_struct *system_unbound_wq;
extern struct workqueue_struct *system_freezable_wq;
extern struct workqueue_struct *system_power_efficient_wq;
extern struct workqueue_struct *system_freezable_power_efficient_wq;
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值