Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 7 | __printf(4, 5) |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 8 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), |
| 9 | void *data, |
| 10 | int node, |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 11 | const char namefmt[], ...); |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 12 | |
| 13 | #define kthread_create(threadfn, data, namefmt, arg...) \ |
| 14 | kthread_create_on_node(threadfn, data, -1, namefmt, ##arg) |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 17 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), |
| 18 | void *data, |
| 19 | unsigned int cpu, |
| 20 | const char *namefmt); |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | /** |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 23 | * kthread_run - create and wake a thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 29 | * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). |
| 30 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | void kthread_bind(struct task_struct *k, unsigned int cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | int kthread_stop(struct task_struct *k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 42 | bool kthread_should_stop(void); |
| 43 | bool kthread_should_park(void); |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 44 | bool kthread_freezable_should_stop(bool *was_frozen); |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 45 | void *kthread_data(struct task_struct *k); |
Tejun Heo | cd42d55 | 2013-04-30 15:27:21 -0700 | [diff] [blame] | 46 | void *probe_kthread_data(struct task_struct *k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 47 | int kthread_park(struct task_struct *k); |
| 48 | void kthread_unpark(struct task_struct *k); |
| 49 | void kthread_parkme(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 51 | int kthreadd(void *unused); |
| 52 | extern struct task_struct *kthreadd_task; |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 53 | extern int tsk_fork_get_node(struct task_struct *tsk); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 54 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Simple work processor based on kthread. |
| 57 | * |
| 58 | * This provides easier way to make use of kthreads. A kthread_work |
| 59 | * can be queued and flushed using queue/flush_kthread_work() |
| 60 | * respectively. Queued kthread_works are processed by a kthread |
| 61 | * running kthread_worker_fn(). |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 62 | */ |
| 63 | struct kthread_work; |
| 64 | typedef void (*kthread_work_func_t)(struct kthread_work *work); |
| 65 | |
| 66 | struct kthread_worker { |
| 67 | spinlock_t lock; |
| 68 | struct list_head work_list; |
| 69 | struct task_struct *task; |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 70 | struct kthread_work *current_work; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct kthread_work { |
| 74 | struct list_head node; |
| 75 | kthread_work_func_t func; |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 76 | struct kthread_worker *worker; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #define KTHREAD_WORKER_INIT(worker) { \ |
Thomas Gleixner | 92578c0 | 2011-01-23 15:24:55 +0100 | [diff] [blame] | 80 | .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \ |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 81 | .work_list = LIST_HEAD_INIT((worker).work_list), \ |
| 82 | } |
| 83 | |
| 84 | #define KTHREAD_WORK_INIT(work, fn) { \ |
| 85 | .node = LIST_HEAD_INIT((work).node), \ |
| 86 | .func = (fn), \ |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | #define DEFINE_KTHREAD_WORKER(worker) \ |
| 90 | struct kthread_worker worker = KTHREAD_WORKER_INIT(worker) |
| 91 | |
| 92 | #define DEFINE_KTHREAD_WORK(work, fn) \ |
| 93 | struct kthread_work work = KTHREAD_WORK_INIT(work, fn) |
| 94 | |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 95 | /* |
Lai Jiangshan | 95847e1 | 2014-07-26 12:04:00 +0800 | [diff] [blame] | 96 | * kthread_worker.lock needs its own lockdep class key when defined on |
| 97 | * stack with lockdep enabled. Use the following macros in such cases. |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 98 | */ |
| 99 | #ifdef CONFIG_LOCKDEP |
| 100 | # define KTHREAD_WORKER_INIT_ONSTACK(worker) \ |
| 101 | ({ init_kthread_worker(&worker); worker; }) |
| 102 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \ |
| 103 | struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 104 | #else |
| 105 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker) |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 106 | #endif |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 107 | |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 108 | extern void __init_kthread_worker(struct kthread_worker *worker, |
| 109 | const char *name, struct lock_class_key *key); |
| 110 | |
| 111 | #define init_kthread_worker(worker) \ |
| 112 | do { \ |
| 113 | static struct lock_class_key __key; \ |
| 114 | __init_kthread_worker((worker), "("#worker")->lock", &__key); \ |
| 115 | } while (0) |
| 116 | |
| 117 | #define init_kthread_work(work, fn) \ |
| 118 | do { \ |
| 119 | memset((work), 0, sizeof(struct kthread_work)); \ |
| 120 | INIT_LIST_HEAD(&(work)->node); \ |
| 121 | (work)->func = (fn); \ |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 122 | } while (0) |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 123 | |
| 124 | int kthread_worker_fn(void *worker_ptr); |
| 125 | |
| 126 | bool queue_kthread_work(struct kthread_worker *worker, |
| 127 | struct kthread_work *work); |
| 128 | void flush_kthread_work(struct kthread_work *work); |
| 129 | void flush_kthread_worker(struct kthread_worker *worker); |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif /* _LINUX_KTHREAD_H */ |