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. #define init_completion(x) __init_completion(x)
  29. static inline void complete_acquire(struct completion *x) {}
  30. static inline void complete_release(struct completion *x) {}
  31. #define COMPLETION_INITIALIZER(work) \
  32. { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  33. #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
  34. (*({ init_completion_map(&(work), &(map)); &(work); }))
  35. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  36. (*({ init_completion(&work); &work; }))
  37. /**
  38. * DECLARE_COMPLETION - declare and initialize a completion structure
  39. * @work: identifier for the completion structure
  40. *
  41. * This macro declares and initializes a completion structure. Generally used
  42. * for static declarations. You should use the _ONSTACK variant for automatic
  43. * variables.
  44. */
  45. #define DECLARE_COMPLETION(work) \
  46. struct completion work = COMPLETION_INITIALIZER(work)
  47. /*
  48. * Lockdep needs to run a non-constant initializer for on-stack
  49. * completions - so we use the _ONSTACK() variant for those that
  50. * are on the kernel stack:
  51. */
  52. /**
  53. * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
  54. * @work: identifier for the completion structure
  55. *
  56. * This macro declares and initializes a completion structure on the kernel
  57. * stack.
  58. */
  59. #ifdef CONFIG_LOCKDEP
  60. # define DECLARE_COMPLETION_ONSTACK(work) \
  61. struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
  62. # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
  63. struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
  64. #else
  65. # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
  66. # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
  67. #endif
  68. /**
  69. * init_completion - Initialize a dynamically allocated completion
  70. * @x: pointer to completion structure that is to be initialized
  71. *
  72. * This inline function will initialize a dynamically created completion
  73. * structure.
  74. */
  75. static inline void __init_completion(struct completion *x)
  76. {
  77. x->done = 0;
  78. init_swait_queue_head(&x->wait);
  79. }
  80. /**
  81. * reinit_completion - reinitialize a completion structure
  82. * @x: pointer to completion structure that is to be reinitialized
  83. *
  84. * This inline function should be used to reinitialize a completion structure so it can
  85. * be reused. This is especially important after complete_all() is used.
  86. */
  87. static inline void reinit_completion(struct completion *x)
  88. {
  89. x->done = 0;
  90. }
  91. extern void wait_for_completion(struct completion *);
  92. extern void wait_for_completion_io(struct completion *);
  93. extern int wait_for_completion_interruptible(struct completion *x);
  94. extern int wait_for_completion_killable(struct completion *x);
  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