HazptrThrLocal.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/synchronization/Hazptr-fwd.h>
  18. #if FOLLY_HAZPTR_THR_LOCAL
  19. #include <folly/synchronization/HazptrObj.h>
  20. #include <folly/synchronization/HazptrRec.h>
  21. #include <folly/SingletonThreadLocal.h>
  22. #include <glog/logging.h>
  23. #include <atomic>
  24. /**
  25. * Thread local classes and singletons
  26. */
  27. namespace folly {
  28. /**
  29. * hazptr_tc_entry
  30. *
  31. * Thread cache entry.
  32. */
  33. template <template <typename> class Atom>
  34. class hazptr_tc_entry {
  35. hazptr_rec<Atom>* hprec_;
  36. template <uint8_t, template <typename> class>
  37. friend class hazptr_array;
  38. template <uint8_t, template <typename> class>
  39. friend class hazptr_local;
  40. friend class hazptr_tc<Atom>;
  41. FOLLY_ALWAYS_INLINE void fill(hazptr_rec<Atom>* hprec) noexcept {
  42. hprec_ = hprec;
  43. }
  44. FOLLY_ALWAYS_INLINE hazptr_rec<Atom>* get() const noexcept {
  45. return hprec_;
  46. }
  47. void evict() {
  48. hprec_->release();
  49. }
  50. }; // hazptr_tc_entry
  51. /**
  52. * hazptr_tc:
  53. *
  54. * Thread cache of hazptr_rec-s that belong to the default domain.
  55. */
  56. template <template <typename> class Atom>
  57. class hazptr_tc {
  58. static constexpr uint8_t kCapacity = 9;
  59. hazptr_tc_entry<Atom> entry_[kCapacity];
  60. uint8_t count_{0};
  61. bool local_{false}; // for debug mode only
  62. public:
  63. ~hazptr_tc() {
  64. for (uint8_t i = 0; i < count(); ++i) {
  65. entry_[i].evict();
  66. }
  67. }
  68. static constexpr uint8_t capacity() noexcept {
  69. return kCapacity;
  70. }
  71. private:
  72. template <uint8_t, template <typename> class>
  73. friend class hazptr_array;
  74. friend class hazptr_holder<Atom>;
  75. template <uint8_t, template <typename> class>
  76. friend class hazptr_local;
  77. FOLLY_ALWAYS_INLINE
  78. hazptr_tc_entry<Atom>& operator[](uint8_t i) noexcept {
  79. DCHECK(i <= capacity());
  80. return entry_[i];
  81. }
  82. FOLLY_ALWAYS_INLINE hazptr_rec<Atom>* try_get() noexcept {
  83. if (LIKELY(count_ > 0)) {
  84. auto hprec = entry_[--count_].get();
  85. return hprec;
  86. }
  87. return nullptr;
  88. }
  89. FOLLY_ALWAYS_INLINE bool try_put(hazptr_rec<Atom>* hprec) noexcept {
  90. if (LIKELY(count_ < capacity())) {
  91. entry_[count_++].fill(hprec);
  92. return true;
  93. }
  94. return false;
  95. }
  96. FOLLY_ALWAYS_INLINE uint8_t count() const noexcept {
  97. return count_;
  98. }
  99. FOLLY_ALWAYS_INLINE void set_count(uint8_t val) noexcept {
  100. count_ = val;
  101. }
  102. FOLLY_NOINLINE void fill(uint8_t num) {
  103. DCHECK_LE(count_ + num, capacity());
  104. auto& domain = default_hazptr_domain<Atom>();
  105. for (uint8_t i = 0; i < num; ++i) {
  106. auto hprec = domain.hprec_acquire();
  107. entry_[count_++].fill(hprec);
  108. }
  109. }
  110. FOLLY_NOINLINE void evict(uint8_t num) {
  111. DCHECK_GE(count_, num);
  112. for (uint8_t i = 0; i < num; ++i) {
  113. entry_[--count_].evict();
  114. }
  115. }
  116. bool local() const noexcept { // for debugging only
  117. return local_;
  118. }
  119. void set_local(bool b) noexcept { // for debugging only
  120. local_ = b;
  121. }
  122. }; // hazptr_tc
  123. /** hazptr_tc_tls */
  124. template <template <typename> class Atom>
  125. FOLLY_ALWAYS_INLINE hazptr_tc<Atom>& hazptr_tc_tls() {
  126. return folly::SingletonThreadLocal<hazptr_tc<Atom>, void>::get();
  127. }
  128. /**
  129. * hazptr_priv
  130. *
  131. * Per-thread list of retired objects to be pushed in bulk to domain.
  132. */
  133. template <template <typename> class Atom>
  134. class hazptr_priv {
  135. static constexpr int kThreshold = 20;
  136. Atom<hazptr_obj<Atom>*> head_;
  137. Atom<hazptr_obj<Atom>*> tail_;
  138. int rcount_;
  139. bool in_dtor_;
  140. public:
  141. hazptr_priv() : head_(nullptr), tail_(nullptr), rcount_(0), in_dtor_(false) {}
  142. ~hazptr_priv() {
  143. in_dtor_ = true;
  144. if (!empty()) {
  145. push_all_to_domain(false);
  146. }
  147. }
  148. private:
  149. friend class hazptr_domain<Atom>;
  150. friend class hazptr_obj<Atom>;
  151. bool empty() const noexcept {
  152. return head() == nullptr;
  153. }
  154. void push(hazptr_obj<Atom>* obj) {
  155. DCHECK(!in_dtor_);
  156. push_in_priv_list(obj);
  157. }
  158. void push_in_priv_list(hazptr_obj<Atom>* obj) {
  159. while (true) {
  160. if (tail()) {
  161. if (push_in_non_empty_list(obj)) {
  162. break;
  163. }
  164. } else {
  165. if (push_in_empty_list(obj)) {
  166. break;
  167. }
  168. }
  169. }
  170. if (++rcount_ >= kThreshold) {
  171. push_all_to_domain(true);
  172. }
  173. }
  174. void push_all_to_domain(bool check_to_reclaim) {
  175. hazptr_obj<Atom>* h = nullptr;
  176. hazptr_obj<Atom>* t = nullptr;
  177. collect(h, t);
  178. if (h) {
  179. DCHECK(t);
  180. hazptr_obj_list<Atom> l(h, t, rcount_);
  181. hazptr_domain_push_retired<Atom>(l, check_to_reclaim);
  182. rcount_ = 0;
  183. }
  184. }
  185. void collect(
  186. hazptr_obj<Atom>*& colHead,
  187. hazptr_obj<Atom>*& colTail) noexcept {
  188. // This function doesn't change rcount_.
  189. // The value rcount_ is accurate excluding the effects of calling collect().
  190. auto h = exchange_head();
  191. if (h) {
  192. auto t = exchange_tail();
  193. DCHECK(t);
  194. if (colTail) {
  195. colTail->set_next(h);
  196. } else {
  197. colHead = h;
  198. }
  199. colTail = t;
  200. }
  201. }
  202. hazptr_obj<Atom>* head() const noexcept {
  203. return head_.load(std::memory_order_acquire);
  204. }
  205. hazptr_obj<Atom>* tail() const noexcept {
  206. return tail_.load(std::memory_order_acquire);
  207. }
  208. void set_head(hazptr_obj<Atom>* obj) noexcept {
  209. head_.store(obj, std::memory_order_release);
  210. }
  211. bool cas_head(hazptr_obj<Atom>* expected, hazptr_obj<Atom>* obj) noexcept {
  212. return head_.compare_exchange_weak(
  213. expected, obj, std::memory_order_acq_rel, std::memory_order_relaxed);
  214. }
  215. bool cas_tail(hazptr_obj<Atom>* expected, hazptr_obj<Atom>* obj) noexcept {
  216. return tail_.compare_exchange_weak(
  217. expected, obj, std::memory_order_acq_rel, std::memory_order_relaxed);
  218. }
  219. hazptr_obj<Atom>* exchange_head() noexcept {
  220. return head_.exchange(nullptr, std::memory_order_acq_rel);
  221. }
  222. hazptr_obj<Atom>* exchange_tail() noexcept {
  223. return tail_.exchange(nullptr, std::memory_order_acq_rel);
  224. }
  225. bool push_in_non_empty_list(hazptr_obj<Atom>* obj) noexcept {
  226. auto h = head();
  227. if (h) {
  228. obj->set_next(h);
  229. if (cas_head(h, obj)) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. bool push_in_empty_list(hazptr_obj<Atom>* obj) noexcept {
  236. hazptr_obj<Atom>* t = nullptr;
  237. obj->set_next(nullptr);
  238. if (cas_tail(t, obj)) {
  239. set_head(obj);
  240. return true;
  241. }
  242. return false;
  243. }
  244. }; // hazptr_priv
  245. /** hazptr_priv_tls */
  246. struct HazptrTag {};
  247. template <template <typename> class Atom>
  248. using hazptr_priv_singleton =
  249. folly::SingletonThreadLocal<hazptr_priv<Atom>, HazptrTag>;
  250. template <template <typename> class Atom>
  251. FOLLY_ALWAYS_INLINE hazptr_priv<Atom>& hazptr_priv_tls() {
  252. return hazptr_priv_singleton<Atom>::get();
  253. }
  254. } // namespace folly
  255. #endif // FOLLY_HAZPTR_THR_LOCAL