]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/kthread.h
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / include / linux / kthread.h
1 #ifndef _LINUX_KTHREAD_H
2 #define _LINUX_KTHREAD_H
3
4 /* Simple interface for creating and stopping kernel threads without mess. */
5 #include <linux/err.h>
6 #include <linux/lockdep.h>
7 #include <linux/sched.h>
8 #include <linux/spinlock.h>
9
10 __printf(3, 4)
11 struct task_struct *kthread_create(int (*threadfn)(void *data),
12                                    void *data,
13                                    const char namefmt[], ...);
14
15
16 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
17                                           void *data,
18                                           unsigned int cpu,
19                                           const char *namefmt);
20
21 /**
22  * kthread_run - create and wake a thread.
23  * @threadfn: the function to run until signal_pending(current).
24  * @data: data ptr for @threadfn.
25  * @namefmt: printf-style name for the thread.
26  *
27  * Description: Convenient wrapper for kthread_create() followed by
28  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
29  */
30 #define kthread_run(threadfn, data, namefmt, ...)                          \
31 ({                                                                         \
32         struct task_struct *__k                                            \
33                 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
34         if (!IS_ERR(__k))                                                  \
35                 wake_up_process(__k);                                      \
36         __k;                                                               \
37 })
38
39 int kthread_stop(struct task_struct *k);
40 bool kthread_should_stop(void);
41 bool kthread_should_park(void);
42 bool kthread_freezable_should_stop(bool *was_frozen);
43 void *kthread_data(struct task_struct *k);
44 void *probe_kthread_data(struct task_struct *k);
45 int kthread_park(struct task_struct *k);
46 void kthread_unpark(struct task_struct *k);
47 void kthread_parkme(void);
48
49 int kthreadd(void *unused);
50 extern struct task_struct *kthreadd_task;
51 extern int tsk_fork_get_node(struct task_struct *tsk);
52
53 /*
54  * Simple work processor based on kthread.
55  *
56  * This provides easier way to make use of kthreads.  A kthread_work
57  * can be queued and flushed using queue/flush_kthread_work()
58  * respectively.  Queued kthread_works are processed by a kthread
59  * running kthread_worker_fn().
60  */
61 struct kthread_work;
62 typedef void (*kthread_work_func_t)(struct kthread_work *work);
63
64 struct kthread_worker {
65         spinlock_t              lock;
66         struct list_head        work_list;
67         struct task_struct      *task;
68         struct kthread_work     *current_work;
69 };
70
71 struct kthread_work {
72         struct list_head        node;
73         kthread_work_func_t     func;
74         struct kthread_worker   *worker;
75 };
76
77 #define KTHREAD_WORKER_INIT(worker)     {                               \
78         .lock = __SPIN_LOCK_UNLOCKED((worker).lock),                    \
79         .work_list = LIST_HEAD_INIT((worker).work_list),                \
80         }
81
82 #define KTHREAD_WORK_INIT(work, fn)     {                               \
83         .node = LIST_HEAD_INIT((work).node),                            \
84         .func = (fn),                                                   \
85         }
86
87 #define DEFINE_KTHREAD_WORKER(worker)                                   \
88         struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
89
90 #define DEFINE_KTHREAD_WORK(work, fn)                                   \
91         struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
92
93 #define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
94
95 extern void __init_kthread_worker(struct kthread_worker *worker,
96                         const char *name, struct lock_class_key *key);
97
98 #define init_kthread_worker(worker)                                     \
99         do {                                                            \
100                 static struct lock_class_key __key;                     \
101                 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
102         } while (0)
103
104 #define init_kthread_work(work, fn)                                     \
105         do {                                                            \
106                 memset((work), 0, sizeof(struct kthread_work));         \
107                 INIT_LIST_HEAD(&(work)->node);                          \
108                 (work)->func = (fn);                                    \
109         } while (0)
110
111 int kthread_worker_fn(void *worker_ptr);
112
113 bool queue_kthread_work(struct kthread_worker *worker,
114                         struct kthread_work *work);
115 void flush_kthread_work(struct kthread_work *work);
116 void flush_kthread_worker(struct kthread_worker *worker);
117
118 #endif /* _LINUX_KTHREAD_H */