kthread: implement kthread_worker
[linux-2.6-block.git] / include / linux / kthread.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_KTHREAD_H
2#define _LINUX_KTHREAD_H
3/* Simple interface for creating and stopping kernel threads without mess. */
4#include <linux/err.h>
5#include <linux/sched.h>
6
1da177e4
LT
7struct task_struct *kthread_create(int (*threadfn)(void *data),
8 void *data,
ed9559d3
RR
9 const char namefmt[], ...)
10 __attribute__((format(printf, 3, 4)));
1da177e4
LT
11
12/**
9e37bd30 13 * kthread_run - create and wake a thread.
1da177e4
LT
14 * @threadfn: the function to run until signal_pending(current).
15 * @data: data ptr for @threadfn.
16 * @namefmt: printf-style name for the thread.
17 *
18 * Description: Convenient wrapper for kthread_create() followed by
9e37bd30
RD
19 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
20 */
1da177e4
LT
21#define kthread_run(threadfn, data, namefmt, ...) \
22({ \
23 struct task_struct *__k \
24 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
25 if (!IS_ERR(__k)) \
26 wake_up_process(__k); \
27 __k; \
28})
29
1da177e4 30void kthread_bind(struct task_struct *k, unsigned int cpu);
1da177e4 31int kthread_stop(struct task_struct *k);
1da177e4
LT
32int kthread_should_stop(void);
33
73c27992
EB
34int kthreadd(void *unused);
35extern struct task_struct *kthreadd_task;
36
b56c0d89
TH
37/*
38 * Simple work processor based on kthread.
39 *
40 * This provides easier way to make use of kthreads. A kthread_work
41 * can be queued and flushed using queue/flush_kthread_work()
42 * respectively. Queued kthread_works are processed by a kthread
43 * running kthread_worker_fn().
44 *
45 * A kthread_work can't be freed while it is executing.
46 */
47struct kthread_work;
48typedef void (*kthread_work_func_t)(struct kthread_work *work);
49
50struct kthread_worker {
51 spinlock_t lock;
52 struct list_head work_list;
53 struct task_struct *task;
54};
55
56struct kthread_work {
57 struct list_head node;
58 kthread_work_func_t func;
59 wait_queue_head_t done;
60 atomic_t flushing;
61 int queue_seq;
62 int done_seq;
63};
64
65#define KTHREAD_WORKER_INIT(worker) { \
66 .lock = SPIN_LOCK_UNLOCKED, \
67 .work_list = LIST_HEAD_INIT((worker).work_list), \
68 }
69
70#define KTHREAD_WORK_INIT(work, fn) { \
71 .node = LIST_HEAD_INIT((work).node), \
72 .func = (fn), \
73 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
74 .flushing = ATOMIC_INIT(0), \
75 }
76
77#define DEFINE_KTHREAD_WORKER(worker) \
78 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
79
80#define DEFINE_KTHREAD_WORK(work, fn) \
81 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
82
83static inline void init_kthread_worker(struct kthread_worker *worker)
84{
85 *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker);
86}
87
88static inline void init_kthread_work(struct kthread_work *work,
89 kthread_work_func_t fn)
90{
91 *work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn);
92}
93
94int kthread_worker_fn(void *worker_ptr);
95
96bool queue_kthread_work(struct kthread_worker *worker,
97 struct kthread_work *work);
98void flush_kthread_work(struct kthread_work *work);
99void flush_kthread_worker(struct kthread_worker *worker);
100
1da177e4 101#endif /* _LINUX_KTHREAD_H */