rcuwait.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef _LINUX_RCUWAIT_H_
  2. #define _LINUX_RCUWAIT_H_
  3. #include <linux/rcupdate.h>
  4. /*
  5. * rcuwait provides a way of blocking and waking up a single
  6. * task in an rcu-safe manner; where it is forbidden to use
  7. * after exit_notify(). task_struct is not properly rcu protected,
  8. * unless dealing with rcu-aware lists, ie: find_task_by_*().
  9. *
  10. * Alternatively we have task_rcu_dereference(), but the return
  11. * semantics have different implications which would break the
  12. * wakeup side. The only time @task is non-nil is when a user is
  13. * blocked (or checking if it needs to) on a condition, and reset
  14. * as soon as we know that the condition has succeeded and are
  15. * awoken.
  16. */
  17. struct rcuwait {
  18. struct task_struct *task;
  19. };
  20. #define __RCUWAIT_INITIALIZER(name) \
  21. { .task = NULL, }
  22. static inline void rcuwait_init(struct rcuwait *w)
  23. {
  24. w->task = NULL;
  25. }
  26. extern void rcuwait_wake_up(struct rcuwait *w);
  27. /*
  28. * The caller is responsible for locking around rcuwait_wait_event(),
  29. * such that writes to @task are properly serialized.
  30. */
  31. #define rcuwait_wait_event(w, condition) \
  32. ({ \
  33. /* \
  34. * Complain if we are called after do_exit()/exit_notify(), \
  35. * as we cannot rely on the rcu critical region for the \
  36. * wakeup side. \
  37. */ \
  38. WARN_ON(current->exit_state); \
  39. \
  40. rcu_assign_pointer((w)->task, current); \
  41. for (;;) { \
  42. /* \
  43. * Implicit barrier (A) pairs with (B) in \
  44. * rcuwait_wake_up(). \
  45. */ \
  46. set_current_state(TASK_UNINTERRUPTIBLE); \
  47. if (condition) \
  48. break; \
  49. \
  50. schedule(); \
  51. } \
  52. \
  53. WRITE_ONCE((w)->task, NULL); \
  54. __set_current_state(TASK_RUNNING); \
  55. })
  56. #endif /* _LINUX_RCUWAIT_H_ */