kthread.h 5.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
#ifndef _LINUX_KTHREAD_H
#define _LINUX_KTHREAD_H
/* Simple interface for creating and stopping kernel threads without mess. */
#include <linux/err.h>
#include <linux/sched.h>

7
__printf(4, 5)
8 9 10
struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
					   void *data,
					   int node,
11
					   const char namefmt[], ...);
12 13

#define kthread_create(threadfn, data, namefmt, arg...) \
14
	kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
15

L
Linus Torvalds 已提交
16

17 18 19 20 21
struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
					  void *data,
					  unsigned int cpu,
					  const char *namefmt);

L
Linus Torvalds 已提交
22
/**
23
 * kthread_run - create and wake a thread.
L
Linus Torvalds 已提交
24 25 26 27 28
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: Convenient wrapper for kthread_create() followed by
29 30
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 */
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40
#define kthread_run(threadfn, data, namefmt, ...)			   \
({									   \
	struct task_struct *__k						   \
		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
	if (!IS_ERR(__k))						   \
		wake_up_process(__k);					   \
	__k;								   \
})

void kthread_bind(struct task_struct *k, unsigned int cpu);
41
void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
L
Linus Torvalds 已提交
42
int kthread_stop(struct task_struct *k);
43 44
bool kthread_should_stop(void);
bool kthread_should_park(void);
45
bool kthread_freezable_should_stop(bool *was_frozen);
T
Tejun Heo 已提交
46
void *kthread_data(struct task_struct *k);
47
void *kthread_probe_data(struct task_struct *k);
48 49 50
int kthread_park(struct task_struct *k);
void kthread_unpark(struct task_struct *k);
void kthread_parkme(void);
L
Linus Torvalds 已提交
51

52 53
int kthreadd(void *unused);
extern struct task_struct *kthreadd_task;
54
extern int tsk_fork_get_node(struct task_struct *tsk);
55

T
Tejun Heo 已提交
56 57 58 59
/*
 * Simple work processor based on kthread.
 *
 * This provides easier way to make use of kthreads.  A kthread_work
P
Petr Mladek 已提交
60
 * can be queued and flushed using queue/kthread_flush_work()
T
Tejun Heo 已提交
61 62 63 64 65
 * respectively.  Queued kthread_works are processed by a kthread
 * running kthread_worker_fn().
 */
struct kthread_work;
typedef void (*kthread_work_func_t)(struct kthread_work *work);
66
void kthread_delayed_work_timer_fn(unsigned long __data);
T
Tejun Heo 已提交
67 68 69 70

struct kthread_worker {
	spinlock_t		lock;
	struct list_head	work_list;
71
	struct list_head	delayed_work_list;
T
Tejun Heo 已提交
72
	struct task_struct	*task;
73
	struct kthread_work	*current_work;
T
Tejun Heo 已提交
74 75 76 77 78
};

struct kthread_work {
	struct list_head	node;
	kthread_work_func_t	func;
79
	struct kthread_worker	*worker;
80 81
	/* Number of canceling calls that are running at the moment. */
	int			canceling;
T
Tejun Heo 已提交
82 83
};

84 85 86 87 88
struct kthread_delayed_work {
	struct kthread_work work;
	struct timer_list timer;
};

T
Tejun Heo 已提交
89
#define KTHREAD_WORKER_INIT(worker)	{				\
90
	.lock = __SPIN_LOCK_UNLOCKED((worker).lock),			\
T
Tejun Heo 已提交
91
	.work_list = LIST_HEAD_INIT((worker).work_list),		\
92
	.delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
T
Tejun Heo 已提交
93 94 95 96 97 98 99
	}

#define KTHREAD_WORK_INIT(work, fn)	{				\
	.node = LIST_HEAD_INIT((work).node),				\
	.func = (fn),							\
	}

100 101 102 103 104 105 106
#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {				\
	.work = KTHREAD_WORK_INIT((dwork).work, (fn)),			\
	.timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,	\
				     0, (unsigned long)&(dwork),	\
				     TIMER_IRQSAFE),			\
	}

T
Tejun Heo 已提交
107 108 109 110 111 112
#define DEFINE_KTHREAD_WORKER(worker)					\
	struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)

#define DEFINE_KTHREAD_WORK(work, fn)					\
	struct kthread_work work = KTHREAD_WORK_INIT(work, fn)

113 114 115 116
#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)				\
	struct kthread_delayed_work dwork =				\
		KTHREAD_DELAYED_WORK_INIT(dwork, fn)

Y
Yong Zhang 已提交
117
/*
118 119
 * kthread_worker.lock needs its own lockdep class key when defined on
 * stack with lockdep enabled.  Use the following macros in such cases.
Y
Yong Zhang 已提交
120 121 122
 */
#ifdef CONFIG_LOCKDEP
# define KTHREAD_WORKER_INIT_ONSTACK(worker)				\
P
Petr Mladek 已提交
123
	({ kthread_init_worker(&worker); worker; })
Y
Yong Zhang 已提交
124 125 126 127 128 129
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker)				\
	struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
#else
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
#endif

P
Petr Mladek 已提交
130
extern void __kthread_init_worker(struct kthread_worker *worker,
Y
Yong Zhang 已提交
131 132
			const char *name, struct lock_class_key *key);

P
Petr Mladek 已提交
133
#define kthread_init_worker(worker)					\
Y
Yong Zhang 已提交
134 135
	do {								\
		static struct lock_class_key __key;			\
P
Petr Mladek 已提交
136
		__kthread_init_worker((worker), "("#worker")->lock", &__key); \
Y
Yong Zhang 已提交
137 138
	} while (0)

P
Petr Mladek 已提交
139
#define kthread_init_work(work, fn)					\
Y
Yong Zhang 已提交
140 141 142 143 144
	do {								\
		memset((work), 0, sizeof(struct kthread_work));		\
		INIT_LIST_HEAD(&(work)->node);				\
		(work)->func = (fn);					\
	} while (0)
T
Tejun Heo 已提交
145

146 147 148 149 150 151 152 153 154
#define kthread_init_delayed_work(dwork, fn)				\
	do {								\
		kthread_init_work(&(dwork)->work, (fn));		\
		__setup_timer(&(dwork)->timer,				\
			      kthread_delayed_work_timer_fn,		\
			      (unsigned long)(dwork),			\
			      TIMER_IRQSAFE);				\
	} while (0)

T
Tejun Heo 已提交
155 156
int kthread_worker_fn(void *worker_ptr);

157 158 159 160 161 162 163
__printf(1, 2)
struct kthread_worker *
kthread_create_worker(const char namefmt[], ...);

struct kthread_worker *
kthread_create_worker_on_cpu(int cpu, const char namefmt[], ...);

P
Petr Mladek 已提交
164
bool kthread_queue_work(struct kthread_worker *worker,
T
Tejun Heo 已提交
165
			struct kthread_work *work);
166 167 168 169 170

bool kthread_queue_delayed_work(struct kthread_worker *worker,
				struct kthread_delayed_work *dwork,
				unsigned long delay);

171 172 173 174
bool kthread_mod_delayed_work(struct kthread_worker *worker,
			      struct kthread_delayed_work *dwork,
			      unsigned long delay);

P
Petr Mladek 已提交
175 176
void kthread_flush_work(struct kthread_work *work);
void kthread_flush_worker(struct kthread_worker *worker);
T
Tejun Heo 已提交
177

178 179 180
bool kthread_cancel_work_sync(struct kthread_work *work);
bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);

181 182
void kthread_destroy_worker(struct kthread_worker *worker);

L
Linus Torvalds 已提交
183
#endif /* _LINUX_KTHREAD_H */