Blame view

include/linux/kthread.h 2.9 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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
  struct task_struct *kthread_create(int (*threadfn)(void *data),
  				   void *data,
ed9559d38   Rusty Russell   Label kthread_cre...
8
9
  				   const char namefmt[], ...)
  	__attribute__((format(printf, 3, 4)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
  
  /**
9e37bd301   Randy Dunlap   [PATCH] kthread: ...
12
   * kthread_run - create and wake a thread.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
   * @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: ...
18
19
   * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
27
  #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
28
  void kthread_bind(struct task_struct *k, unsigned int cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  int kthread_stop(struct task_struct *k);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  int kthread_should_stop(void);
82805ab77   Tejun Heo   kthread: implemen...
31
  void *kthread_data(struct task_struct *k);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32

73c279927   Eric W. Biederman   kthread: don't de...
33
34
  int kthreadd(void *unused);
  extern struct task_struct *kthreadd_task;
b56c0d893   Tejun Heo   kthread: implemen...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  /*
   * 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().
   *
   * A kthread_work can't be freed while it is executing.
   */
  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;
  };
  
  struct kthread_work {
  	struct list_head	node;
  	kthread_work_func_t	func;
  	wait_queue_head_t	done;
  	atomic_t		flushing;
  	int			queue_seq;
  	int			done_seq;
  };
  
  #define KTHREAD_WORKER_INIT(worker)	{				\
  	.lock = SPIN_LOCK_UNLOCKED,					\
  	.work_list = LIST_HEAD_INIT((worker).work_list),		\
  	}
  
  #define KTHREAD_WORK_INIT(work, fn)	{				\
  	.node = LIST_HEAD_INIT((work).node),				\
  	.func = (fn),							\
  	.done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done),		\
  	.flushing = ATOMIC_INIT(0),					\
  	}
  
  #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)
  
  static inline void init_kthread_worker(struct kthread_worker *worker)
  {
  	*worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker);
  }
  
  static inline void init_kthread_work(struct kthread_work *work,
  				     kthread_work_func_t fn)
  {
  	*work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn);
  }
  
  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
98
  #endif /* _LINUX_KTHREAD_H */