kthread: add kthread_create_worker*()
[deliverable/linux.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...) \
e9f06986 14 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
207205a2 15
1da177e4 16
2a1d4460
TG
17struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
18 void *data,
19 unsigned int cpu,
20 const char *namefmt);
21
1da177e4 22/**
9e37bd30 23 * kthread_run - create and wake a thread.
1da177e4
LT
24 * @threadfn: the function to run until signal_pending(current).
25 * @data: data ptr for @threadfn.
26 * @namefmt: printf-style name for the thread.
27 *
28 * Description: Convenient wrapper for kthread_create() followed by
9e37bd30
RD
29 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
30 */
1da177e4
LT
31#define kthread_run(threadfn, data, namefmt, ...) \
32({ \
33 struct task_struct *__k \
34 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
35 if (!IS_ERR(__k)) \
36 wake_up_process(__k); \
37 __k; \
38})
39
1da177e4 40void kthread_bind(struct task_struct *k, unsigned int cpu);
25834c73 41void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
1da177e4 42int kthread_stop(struct task_struct *k);
2a1d4460
TG
43bool kthread_should_stop(void);
44bool kthread_should_park(void);
8a32c441 45bool kthread_freezable_should_stop(bool *was_frozen);
82805ab7 46void *kthread_data(struct task_struct *k);
fcc12192 47void *kthread_probe_data(struct task_struct *k);
2a1d4460
TG
48int kthread_park(struct task_struct *k);
49void kthread_unpark(struct task_struct *k);
50void kthread_parkme(void);
1da177e4 51
73c27992
EB
52int kthreadd(void *unused);
53extern struct task_struct *kthreadd_task;
207205a2 54extern int tsk_fork_get_node(struct task_struct *tsk);
73c27992 55
b56c0d89
TH
56/*
57 * Simple work processor based on kthread.
58 *
59 * This provides easier way to make use of kthreads. A kthread_work
8c03cbe6 60 * can be queued and flushed using queue/kthread_flush_work()
b56c0d89
TH
61 * respectively. Queued kthread_works are processed by a kthread
62 * running kthread_worker_fn().
b56c0d89
TH
63 */
64struct kthread_work;
65typedef void (*kthread_work_func_t)(struct kthread_work *work);
66
67struct kthread_worker {
68 spinlock_t lock;
69 struct list_head work_list;
70 struct task_struct *task;
46f3d976 71 struct kthread_work *current_work;
b56c0d89
TH
72};
73
74struct kthread_work {
75 struct list_head node;
76 kthread_work_func_t func;
46f3d976 77 struct kthread_worker *worker;
b56c0d89
TH
78};
79
80#define KTHREAD_WORKER_INIT(worker) { \
92578c0b 81 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
b56c0d89
TH
82 .work_list = LIST_HEAD_INIT((worker).work_list), \
83 }
84
85#define KTHREAD_WORK_INIT(work, fn) { \
86 .node = LIST_HEAD_INIT((work).node), \
87 .func = (fn), \
b56c0d89
TH
88 }
89
90#define DEFINE_KTHREAD_WORKER(worker) \
91 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
92
93#define DEFINE_KTHREAD_WORK(work, fn) \
94 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
95
4f32e9b1 96/*
95847e1b
LJ
97 * kthread_worker.lock needs its own lockdep class key when defined on
98 * stack with lockdep enabled. Use the following macros in such cases.
4f32e9b1
YZ
99 */
100#ifdef CONFIG_LOCKDEP
101# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
8c03cbe6 102 ({ kthread_init_worker(&worker); worker; })
4f32e9b1
YZ
103# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
104 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
4f32e9b1
YZ
105#else
106# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
4f32e9b1
YZ
107#endif
108
8c03cbe6 109extern void __kthread_init_worker(struct kthread_worker *worker,
4f32e9b1
YZ
110 const char *name, struct lock_class_key *key);
111
8c03cbe6 112#define kthread_init_worker(worker) \
4f32e9b1
YZ
113 do { \
114 static struct lock_class_key __key; \
8c03cbe6 115 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
4f32e9b1
YZ
116 } while (0)
117
8c03cbe6 118#define kthread_init_work(work, fn) \
4f32e9b1
YZ
119 do { \
120 memset((work), 0, sizeof(struct kthread_work)); \
121 INIT_LIST_HEAD(&(work)->node); \
122 (work)->func = (fn); \
4f32e9b1 123 } while (0)
b56c0d89
TH
124
125int kthread_worker_fn(void *worker_ptr);
126
04555f91
PM
127__printf(1, 2)
128struct kthread_worker *
129kthread_create_worker(const char namefmt[], ...);
130
131struct kthread_worker *
132kthread_create_worker_on_cpu(int cpu, const char namefmt[], ...);
133
8c03cbe6 134bool kthread_queue_work(struct kthread_worker *worker,
b56c0d89 135 struct kthread_work *work);
8c03cbe6
PM
136void kthread_flush_work(struct kthread_work *work);
137void kthread_flush_worker(struct kthread_worker *worker);
b56c0d89 138
1da177e4 139#endif /* _LINUX_KTHREAD_H */
This page took 1.386501 seconds and 5 git commands to generate.