kthread_worker: reimplement flush_kthread_work() to allow freeing the work item being...
[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
b9075fa9 7__printf(4, 5)
207205a2
ED
8struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9 void *data,
10 int node,
b9075fa9 11 const char namefmt[], ...);
207205a2
ED
12
13#define kthread_create(threadfn, data, namefmt, arg...) \
14 kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
15
1da177e4
LT
16
17/**
9e37bd30 18 * kthread_run - create and wake a thread.
1da177e4
LT
19 * @threadfn: the function to run until signal_pending(current).
20 * @data: data ptr for @threadfn.
21 * @namefmt: printf-style name for the thread.
22 *
23 * Description: Convenient wrapper for kthread_create() followed by
9e37bd30
RD
24 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
25 */
1da177e4
LT
26#define kthread_run(threadfn, data, namefmt, ...) \
27({ \
28 struct task_struct *__k \
29 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
30 if (!IS_ERR(__k)) \
31 wake_up_process(__k); \
32 __k; \
33})
34
1da177e4 35void kthread_bind(struct task_struct *k, unsigned int cpu);
1da177e4 36int kthread_stop(struct task_struct *k);
1da177e4 37int kthread_should_stop(void);
8a32c441 38bool kthread_freezable_should_stop(bool *was_frozen);
82805ab7 39void *kthread_data(struct task_struct *k);
1da177e4 40
73c27992
EB
41int kthreadd(void *unused);
42extern struct task_struct *kthreadd_task;
207205a2 43extern int tsk_fork_get_node(struct task_struct *tsk);
73c27992 44
b56c0d89
TH
45/*
46 * Simple work processor based on kthread.
47 *
48 * This provides easier way to make use of kthreads. A kthread_work
49 * can be queued and flushed using queue/flush_kthread_work()
50 * respectively. Queued kthread_works are processed by a kthread
51 * running kthread_worker_fn().
b56c0d89
TH
52 */
53struct kthread_work;
54typedef void (*kthread_work_func_t)(struct kthread_work *work);
55
56struct kthread_worker {
57 spinlock_t lock;
58 struct list_head work_list;
59 struct task_struct *task;
46f3d976 60 struct kthread_work *current_work;
b56c0d89
TH
61};
62
63struct kthread_work {
64 struct list_head node;
65 kthread_work_func_t func;
66 wait_queue_head_t done;
46f3d976 67 struct kthread_worker *worker;
b56c0d89
TH
68};
69
70#define KTHREAD_WORKER_INIT(worker) { \
92578c0b 71 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
b56c0d89
TH
72 .work_list = LIST_HEAD_INIT((worker).work_list), \
73 }
74
75#define KTHREAD_WORK_INIT(work, fn) { \
76 .node = LIST_HEAD_INIT((work).node), \
77 .func = (fn), \
78 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
b56c0d89
TH
79 }
80
81#define DEFINE_KTHREAD_WORKER(worker) \
82 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
83
84#define DEFINE_KTHREAD_WORK(work, fn) \
85 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
86
4f32e9b1
YZ
87/*
88 * kthread_worker.lock and kthread_work.done need their own lockdep class
89 * keys if they are defined on stack with lockdep enabled. Use the
90 * following macros when defining them on stack.
91 */
92#ifdef CONFIG_LOCKDEP
93# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
94 ({ init_kthread_worker(&worker); worker; })
95# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
96 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
97# define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
98 ({ init_kthread_work((&work), fn); work; })
99# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
100 struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
101#else
102# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
103# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
104#endif
105
106extern void __init_kthread_worker(struct kthread_worker *worker,
107 const char *name, struct lock_class_key *key);
108
109#define init_kthread_worker(worker) \
110 do { \
111 static struct lock_class_key __key; \
112 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
113 } while (0)
114
115#define init_kthread_work(work, fn) \
116 do { \
117 memset((work), 0, sizeof(struct kthread_work)); \
118 INIT_LIST_HEAD(&(work)->node); \
119 (work)->func = (fn); \
120 init_waitqueue_head(&(work)->done); \
121 } while (0)
b56c0d89
TH
122
123int kthread_worker_fn(void *worker_ptr);
124
125bool queue_kthread_work(struct kthread_worker *worker,
126 struct kthread_work *work);
127void flush_kthread_work(struct kthread_work *work);
128void flush_kthread_worker(struct kthread_worker *worker);
129
1da177e4 130#endif /* _LINUX_KTHREAD_H */