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