您的位置:首页 > 运维架构 > Linux

Linux 内核工作队列

2017-03-08 20:23 323 查看
工作队列也是内核一种延时执行任务机制,显然,内核需要定义一个队列,然后队列上面存放需要执行的任务,而任务执行则有内核线程来调用任务。

工作队列定义如下:

/*
 * 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;

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: