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