kthread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  7. * kthread_create: create a kthread.
  8. * @threadfn: the function to run until signal_pending(current).
  9. * @data: data ptr for @threadfn.
  10. * @namefmt: printf-style name for the thread.
  11. *
  12. * Description: This helper function creates and names a kernel
  13. * thread. The thread will be stopped: use wake_up_process() to start
  14. * it. See also kthread_run(), kthread_create_on_cpu().
  15. *
  16. * When woken, the thread will run @threadfn() with @data as its
  17. * argument. @threadfn can either call do_exit() directly if it is a
  18. * standalone thread for which noone will call kthread_stop(), or
  19. * return when 'kthread_should_stop()' is true (which means
  20. * kthread_stop() has been called). The return value should be zero
  21. * or a negative error number: it will be passed to kthread_stop().
  22. *
  23. * Returns a task_struct or ERR_PTR(-ENOMEM).
  24. */
  25. struct task_struct *kthread_create(int (*threadfn)(void *data),
  26. void *data,
  27. const char namefmt[], ...);
  28. /**
  29. * kthread_run: create and wake a thread.
  30. * @threadfn: the function to run until signal_pending(current).
  31. * @data: data ptr for @threadfn.
  32. * @namefmt: printf-style name for the thread.
  33. *
  34. * Description: Convenient wrapper for kthread_create() followed by
  35. * wake_up_process(). Returns the kthread, or ERR_PTR(-ENOMEM). */
  36. #define kthread_run(threadfn, data, namefmt, ...) \
  37. ({ \
  38. struct task_struct *__k \
  39. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  40. if (!IS_ERR(__k)) \
  41. wake_up_process(__k); \
  42. __k; \
  43. })
  44. /**
  45. * kthread_bind: bind a just-created kthread to a cpu.
  46. * @k: thread created by kthread_create().
  47. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  48. *
  49. * Description: This function is equivalent to set_cpus_allowed(),
  50. * except that @cpu doesn't need to be online, and the thread must be
  51. * stopped (ie. just returned from kthread_create().
  52. */
  53. void kthread_bind(struct task_struct *k, unsigned int cpu);
  54. /**
  55. * kthread_stop: stop a thread created by kthread_create().
  56. * @k: thread created by kthread_create().
  57. *
  58. * Sets kthread_should_stop() for @k to return true, wakes it, and
  59. * waits for it to exit. Your threadfn() must not call do_exit()
  60. * itself if you use this function! This can also be called after
  61. * kthread_create() instead of calling wake_up_process(): the thread
  62. * will exit without calling threadfn().
  63. *
  64. * Returns the result of threadfn(), or -EINTR if wake_up_process()
  65. * was never called. */
  66. int kthread_stop(struct task_struct *k);
  67. /**
  68. * kthread_should_stop: should this kthread return now?
  69. *
  70. * When someone calls kthread_stop on your kthread, it will be woken
  71. * and this will return true. You should then return, and your return
  72. * value will be passed through to kthread_stop().
  73. */
  74. int kthread_should_stop(void);
  75. #endif /* _LINUX_KTHREAD_H */