rcu.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Read-Copy Update.
  19. *
  20. * This module provides a synchronization framework for read-only lockless
  21. * operations, best used on read-mostly data structures.
  22. *
  23. * To learn more about RCU, see "Is Parallel Programming Hard, And, If So,
  24. * What Can You Do About It ?" by Paul E. McKenney.
  25. *
  26. * A thorough discussion of RCU's requirements can be found at [1].
  27. * However, the memory model and terminology used in that document being
  28. * different from C11, the memory barrier guarantees are hereby translated
  29. * into memory ordering guarantees :
  30. *
  31. * 1. Release-acquire ordering is enforced between the end of all
  32. * read-side critical sections starting before a grace period starts
  33. * and the start of all works deferred before the same grace period
  34. * starts.
  35. * 2. Release-acquire ordering is enforced between the start of all
  36. * read-side critical sections ending after a grace period ends
  37. * and all the work deferrals done before the same grace period
  38. * starts.
  39. * 3. Release-acquire ordering is enforced between all the work deferrals
  40. * done before a grace period starts and the start of all works deferred
  41. * before the same grace period starts.
  42. *
  43. * [1] https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html.
  44. */
  45. #ifndef KERN_RCU_H
  46. #define KERN_RCU_H
  47. #include <kern/atomic.h>
  48. #include <kern/init.h>
  49. #include <kern/rcu_i.h>
  50. #include <kern/rcu_types.h>
  51. #include <kern/thread.h>
  52. #include <kern/work.h>
  53. /*
  54. * Thread-local RCU data.
  55. */
  56. struct rcu_reader;
  57. /*
  58. * Safely store a pointer.
  59. *
  60. * This macro enforces release ordering on the given pointer.
  61. */
  62. #define rcu_store_ptr(ptr, value) atomic_store(&(ptr), value, ATOMIC_RELEASE)
  63. /*
  64. * Safely load a pointer.
  65. *
  66. * This macro enforces consume ordering on the given pointer.
  67. */
  68. #define rcu_load_ptr(ptr) atomic_load(&(ptr), ATOMIC_CONSUME)
  69. /*
  70. * Read-side critical section functions.
  71. *
  72. * Critical sections are preemptible, may safely nest, and may safely
  73. * be used in interrupt context. It is not allowed to sleep inside a
  74. * read-side critical section. However, it is allowed to acquire locks
  75. * that don't sleep, such as spin locks.
  76. *
  77. * Entering or leaving a critical section doesn't provide any memory
  78. * ordering guarantee.
  79. */
  80. static inline void
  81. rcu_read_enter(void)
  82. {
  83. rcu_reader_inc(thread_rcu_reader(thread_self()));
  84. barrier();
  85. }
  86. static inline void
  87. rcu_read_leave(void)
  88. {
  89. barrier();
  90. rcu_reader_dec(thread_rcu_reader(thread_self()));
  91. }
  92. /*
  93. * Initialize a RCU reader.
  94. */
  95. void rcu_reader_init(struct rcu_reader *reader);
  96. /*
  97. * Report a context switch on the current processor.
  98. *
  99. * The argument is the RCU reader of the preempted thread.
  100. *
  101. * Interrupts and preemption must be disabled when calling this function.
  102. */
  103. void rcu_report_context_switch(struct rcu_reader *reader);
  104. /*
  105. * Report a periodic event on the current processor.
  106. *
  107. * Interrupts and preemption must be disabled when calling this function.
  108. */
  109. void rcu_report_periodic_event(void);
  110. /*
  111. * Defer a work until all existing read-side references are dropped,
  112. * without blocking.
  113. *
  114. * The given work is scheduled when all existing read-side references
  115. * are dropped.
  116. */
  117. void rcu_defer(struct work *work);
  118. /*
  119. * Wait for all existing read-side references to be dropped.
  120. *
  121. * This function sleeps, and may do so for a moderately long duration,
  122. * at leat a few system timer ticks, sometimes a lot more.
  123. */
  124. void rcu_wait(void);
  125. /*
  126. * This init operation provides :
  127. * - read-side critical sections usable
  128. */
  129. INIT_OP_DECLARE(rcu_bootstrap);
  130. #endif /* KERN_RCU_H */