completion.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.c for details.
  8. */
  9. #include <linux/wait.h>
  10. struct completion {
  11. unsigned int done;
  12. wait_queue_head_t wait;
  13. };
  14. #define COMPLETION_INITIALIZER(work) \
  15. { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  16. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  17. ({ init_completion(&work); work; })
  18. #define DECLARE_COMPLETION(work) \
  19. struct completion work = COMPLETION_INITIALIZER(work)
  20. /*
  21. * Lockdep needs to run a non-constant initializer for on-stack
  22. * completions - so we use the _ONSTACK() variant for those that
  23. * are on the kernel stack:
  24. */
  25. #ifdef CONFIG_LOCKDEP
  26. # define DECLARE_COMPLETION_ONSTACK(work) \
  27. struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
  28. #else
  29. # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
  30. #endif
  31. static inline void init_completion(struct completion *x)
  32. {
  33. x->done = 0;
  34. init_waitqueue_head(&x->wait);
  35. }
  36. extern void wait_for_completion(struct completion *);
  37. extern int wait_for_completion_interruptible(struct completion *x);
  38. extern int wait_for_completion_killable(struct completion *x);
  39. extern unsigned long wait_for_completion_timeout(struct completion *x,
  40. unsigned long timeout);
  41. extern unsigned long wait_for_completion_interruptible_timeout(
  42. struct completion *x, unsigned long timeout);
  43. extern void complete(struct completion *);
  44. extern void complete_all(struct completion *);
  45. #define INIT_COMPLETION(x) ((x).done = 0)
  46. /**
  47. * try_wait_for_completion - try to decrement a completion without blocking
  48. * @x: completion structure
  49. *
  50. * Returns: 0 if a decrement cannot be done without blocking
  51. * 1 if a decrement succeeded.
  52. *
  53. * If a completion is being used as a counting completion,
  54. * attempt to decrement the counter without blocking. This
  55. * enables us to avoid waiting if the resource the completion
  56. * is protecting is not available.
  57. */
  58. static inline bool try_wait_for_completion(struct completion *x)
  59. {
  60. int ret = 1;
  61. spin_lock_irq(&x->wait.lock);
  62. if (!x->done)
  63. ret = 0;
  64. else
  65. x->done--;
  66. spin_unlock_irq(&x->wait.lock);
  67. return ret;
  68. }
  69. /**
  70. * completion_done - Test to see if a completion has any waiters
  71. * @x: completion structure
  72. *
  73. * Returns: 0 if there are waiters (wait_for_completion() in progress)
  74. * 1 if there are no waiters.
  75. *
  76. */
  77. static inline bool completion_done(struct completion *x)
  78. {
  79. int ret = 1;
  80. spin_lock_irq(&x->wait.lock);
  81. if (!x->done)
  82. ret = 0;
  83. spin_unlock_irq(&x->wait.lock);
  84. return ret;
  85. }
  86. #endif