Blame view

include/linux/kthread.h 3.96 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  #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>
b9075fa96   Joe Perches   treewide: use __p...
6
  __printf(4, 5)
207205a2b   Eric Dumazet   kthread: NUMA awa...
7
8
9
  struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
  					   void *data,
  					   int node,
b9075fa96   Joe Perches   treewide: use __p...
10
  					   const char namefmt[], ...);
207205a2b   Eric Dumazet   kthread: NUMA awa...
11
12
13
  
  #define kthread_create(threadfn, data, namefmt, arg...) \
  	kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14

2a1d44601   Thomas Gleixner   kthread: Implemen...
15
16
17
18
  struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
  					  void *data,
  					  unsigned int cpu,
  					  const char *namefmt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  /**
9e37bd301   Randy Dunlap   [PATCH] kthread: ...
20
   * kthread_run - create and wake a thread.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
   * @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
9e37bd301   Randy Dunlap   [PATCH] kthread: ...
26
27
   * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
  #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;								   \
  })
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  void kthread_bind(struct task_struct *k, unsigned int cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  int kthread_stop(struct task_struct *k);
2a1d44601   Thomas Gleixner   kthread: Implemen...
38
39
  bool kthread_should_stop(void);
  bool kthread_should_park(void);
8a32c441c   Tejun Heo   freezer: implemen...
40
  bool kthread_freezable_should_stop(bool *was_frozen);
82805ab77   Tejun Heo   kthread: implemen...
41
  void *kthread_data(struct task_struct *k);
cd42d559e   Tejun Heo   kthread: implemen...
42
  void *probe_kthread_data(struct task_struct *k);
2a1d44601   Thomas Gleixner   kthread: Implemen...
43
44
45
  int kthread_park(struct task_struct *k);
  void kthread_unpark(struct task_struct *k);
  void kthread_parkme(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46

73c279927   Eric W. Biederman   kthread: don't de...
47
48
  int kthreadd(void *unused);
  extern struct task_struct *kthreadd_task;
207205a2b   Eric Dumazet   kthread: NUMA awa...
49
  extern int tsk_fork_get_node(struct task_struct *tsk);
73c279927   Eric W. Biederman   kthread: don't de...
50

b56c0d893   Tejun Heo   kthread: implemen...
51
52
53
54
55
56
57
  /*
   * Simple work processor based on kthread.
   *
   * This provides easier way to make use of kthreads.  A kthread_work
   * can be queued and flushed using queue/flush_kthread_work()
   * respectively.  Queued kthread_works are processed by a kthread
   * running kthread_worker_fn().
b56c0d893   Tejun Heo   kthread: implemen...
58
59
60
61
62
63
64
65
   */
  struct kthread_work;
  typedef void (*kthread_work_func_t)(struct kthread_work *work);
  
  struct kthread_worker {
  	spinlock_t		lock;
  	struct list_head	work_list;
  	struct task_struct	*task;
46f3d9762   Tejun Heo   kthread_worker: r...
66
  	struct kthread_work	*current_work;
b56c0d893   Tejun Heo   kthread: implemen...
67
68
69
70
71
  };
  
  struct kthread_work {
  	struct list_head	node;
  	kthread_work_func_t	func;
46f3d9762   Tejun Heo   kthread_worker: r...
72
  	struct kthread_worker	*worker;
b56c0d893   Tejun Heo   kthread: implemen...
73
74
75
  };
  
  #define KTHREAD_WORKER_INIT(worker)	{				\
92578c0b8   Thomas Gleixner   kthread: Replace ...
76
  	.lock = __SPIN_LOCK_UNLOCKED((worker).lock),			\
b56c0d893   Tejun Heo   kthread: implemen...
77
78
79
80
81
82
  	.work_list = LIST_HEAD_INIT((worker).work_list),		\
  	}
  
  #define KTHREAD_WORK_INIT(work, fn)	{				\
  	.node = LIST_HEAD_INIT((work).node),				\
  	.func = (fn),							\
b56c0d893   Tejun Heo   kthread: implemen...
83
84
85
86
87
88
89
  	}
  
  #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)
4f32e9b1f   Yong Zhang   kthread_work: mak...
90
  /*
95847e1bd   Lai Jiangshan   kthread_work: rem...
91
92
   * kthread_worker.lock needs its own lockdep class key when defined on
   * stack with lockdep enabled.  Use the following macros in such cases.
4f32e9b1f   Yong Zhang   kthread_work: mak...
93
94
95
96
97
98
   */
  #ifdef CONFIG_LOCKDEP
  # define KTHREAD_WORKER_INIT_ONSTACK(worker)				\
  	({ init_kthread_worker(&worker); worker; })
  # define DEFINE_KTHREAD_WORKER_ONSTACK(worker)				\
  	struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
4f32e9b1f   Yong Zhang   kthread_work: mak...
99
100
  #else
  # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
4f32e9b1f   Yong Zhang   kthread_work: mak...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #endif
  
  extern void __init_kthread_worker(struct kthread_worker *worker,
  			const char *name, struct lock_class_key *key);
  
  #define init_kthread_worker(worker)					\
  	do {								\
  		static struct lock_class_key __key;			\
  		__init_kthread_worker((worker), "("#worker")->lock", &__key); \
  	} while (0)
  
  #define init_kthread_work(work, fn)					\
  	do {								\
  		memset((work), 0, sizeof(struct kthread_work));		\
  		INIT_LIST_HEAD(&(work)->node);				\
  		(work)->func = (fn);					\
4f32e9b1f   Yong Zhang   kthread_work: mak...
117
  	} while (0)
b56c0d893   Tejun Heo   kthread: implemen...
118
119
120
121
122
123
124
  
  int kthread_worker_fn(void *worker_ptr);
  
  bool queue_kthread_work(struct kthread_worker *worker,
  			struct kthread_work *work);
  void flush_kthread_work(struct kthread_work *work);
  void flush_kthread_worker(struct kthread_worker *worker);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  #endif /* _LINUX_KTHREAD_H */