completion.h 4.1 KB

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