completion.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef __LINUX_COMPLETION_H
  2. #define __LINUX_COMPLETION_H
  3. /*
  4. * (C) Copyright 2001 Linus Torvalds
  5. *
  6. * Atomic wait-for-completion handler data structures.
  7. * See kernel/sched/completion.c for details.
  8. */
  9. #include <linux/wait.h>
  10. /*
  11. * struct completion - structure used to maintain state for a "completion"
  12. *
  13. * This is the opaque structure used to maintain the state for a "completion".
  14. * Completions currently use a FIFO to queue threads that have to wait for
  15. * the "completion" event.
  16. *
  17. * See also: complete(), wait_for_completion() (and friends _timeout,
  18. * _interruptible, _interruptible_timeout, and _killable), init_completion(),
  19. * reinit_completion(), and macros DECLARE_COMPLETION(),
  20. * DECLARE_COMPLETION_ONSTACK().
  21. */
  22. struct completion {
  23. unsigned int done;
  24. wait_queue_head_t wait;
  25. };
  26. #define COMPLETION_INITIALIZER(work) \
  27. { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  28. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  29. ({ init_completion(&work); work; })
  30. /**
  31. * DECLARE_COMPLETION - declare and initialize a completion structure
  32. * @work: identifier for the completion structure
  33. *
  34. * This macro declares and initializes a completion structure. Generally used
  35. * for static declarations. You should use the _ONSTACK variant for automatic
  36. * variables.
  37. */
  38. #define DECLARE_COMPLETION(work) \
  39. struct completion work = COMPLETION_INITIALIZER(work)
  40. /*
  41. * Lockdep needs to run a non-constant initializer for on-stack
  42. * completions - so we use the _ONSTACK() variant for those that
  43. * are on the kernel stack:
  44. */
  45. /**
  46. * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
  47. * @work: identifier for the completion structure
  48. *
  49. * This macro declares and initializes a completion structure on the kernel
  50. * stack.
  51. */
  52. #ifdef CONFIG_LOCKDEP
  53. # define DECLARE_COMPLETION_ONSTACK(work) \
  54. struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
  55. #else
  56. # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
  57. #endif
  58. /**
  59. * init_completion - Initialize a dynamically allocated completion
  60. * @x: pointer to completion structure that is to be initialized
  61. *
  62. * This inline function will initialize a dynamically created completion
  63. * structure.
  64. */
  65. static inline void init_completion(struct completion *x)
  66. {
  67. x->done = 0;
  68. init_waitqueue_head(&x->wait);
  69. }
  70. /**
  71. * reinit_completion - reinitialize a completion structure
  72. * @x: pointer to completion structure that is to be reinitialized
  73. *
  74. * This inline function should be used to reinitialize a completion structure so it can
  75. * be reused. This is especially important after complete_all() is used.
  76. */
  77. static inline void reinit_completion(struct completion *x)
  78. {
  79. x->done = 0;
  80. }
  81. extern void wait_for_completion(struct completion *);
  82. extern void wait_for_completion_io(struct completion *);
  83. extern int wait_for_completion_interruptible(struct completion *x);
  84. extern int wait_for_completion_killable(struct completion *x);
  85. extern unsigned long wait_for_completion_timeout(struct completion *x,
  86. unsigned long timeout);
  87. extern unsigned long wait_for_completion_io_timeout(struct completion *x,
  88. unsigned long timeout);
  89. extern long wait_for_completion_interruptible_timeout(
  90. struct completion *x, unsigned long timeout);
  91. extern long wait_for_completion_killable_timeout(
  92. struct completion *x, unsigned long timeout);
  93. extern bool try_wait_for_completion(struct completion *x);
  94. extern bool completion_done(struct completion *x);
  95. extern void complete(struct completion *);
  96. extern void complete_all(struct completion *);
  97. #endif