2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* override-include-guard */
19 #error "This should only be included by hazptr.h"
22 /* quality of implementation switches */
24 // NOTE: The #ifndef pattern is prone to ODR violation. Its use for
25 // quality of implementation options is temporary. Eventually these
26 // options should be added to the API in future API extensions.
29 #define HAZPTR_AMB true
33 #define HAZPTR_TC true
36 #ifndef HAZPTR_TC_SIZE
37 #define HAZPTR_TC_SIZE 10
41 #define HAZPTR_PRIV true
44 #ifndef HAZPTR_ONE_DOMAIN
45 #define HAZPTR_ONE_DOMAIN false
48 #ifndef HAZPTR_SCAN_MULT
49 #define HAZPTR_SCAN_MULT 2
52 #ifndef HAZPTR_SCAN_THRESHOLD
53 #define HAZPTR_SCAN_THRESHOLD 1000
58 #define HAZPTR_STATS false
61 #include <folly/concurrency/CacheLocality.h>
62 #include <folly/experimental/hazptr/debug.h>
63 #include <folly/synchronization/AsymmetricMemoryBarrier.h>
65 #include <mutex> // for thread caching
66 #include <unordered_set> // for hash set in bulk reclamation
72 * Helper classes and functions
80 #define INC_HAZPTR_STATS(x) hazptr_stats_.x()
82 #define INC_HAZPTR_STATS(x)
99 enum hazptr_tls_state { TLS_ALIVE, TLS_UNINITIALIZED, TLS_DESTROYED };
101 /** hazptr_tc structures
102 * Thread caching of hazptr_rec-s that belong to the default domain.
105 struct hazptr_tc_entry {
108 void fill(hazptr_rec* hprec);
114 std::is_trivial<hazptr_tc_entry>::value,
115 "hazptr_tc_entry must be trivial"
116 " to avoid a branch to check initialization");
119 hazptr_tc_entry entry_[HAZPTR_TC_SIZE];
121 bool local_; // for debug mode only
124 hazptr_tc_entry& operator[](size_t i);
126 bool put(hazptr_rec* hprec);
131 std::is_trivial<hazptr_tc>::value,
132 "hazptr_tc must be trivial to avoid a branch to check initialization");
134 hazptr_tc* hazptr_tc_tls();
135 void hazptr_tc_init();
136 void hazptr_tc_shutdown();
137 hazptr_rec* hazptr_tc_try_get();
138 bool hazptr_tc_try_put(hazptr_rec* hprec);
140 /** hazptr_priv structures
141 * Thread private lists of retired objects that belong to the default domain.
150 void push(hazptr_obj* obj);
151 void pushAllToDomain();
155 std::is_trivial<hazptr_priv>::value,
156 "hazptr_priv must be trivial to avoid a branch to check initialization");
158 void hazptr_priv_init();
159 void hazptr_priv_shutdown();
160 bool hazptr_priv_try_retire(hazptr_obj* obj);
162 /** hazptr_tls_life */
164 struct hazptr_tls_life {
169 void tls_life_odr_use();
173 extern thread_local hazptr_tls_state tls_state_;
174 extern thread_local hazptr_tc tls_tc_data_;
175 extern thread_local hazptr_priv tls_priv_data_;
176 extern thread_local hazptr_tls_life tls_life_; // last
182 inline constexpr hazptr_domain::hazptr_domain(memory_resource* mr) noexcept
189 template <typename T, typename D>
190 inline void hazptr_obj_base<T, D>::retire(hazptr_domain& domain, D deleter) {
191 DEBUG_PRINT(this << " " << &domain);
192 deleter_ = std::move(deleter);
193 reclaim_ = [](hazptr_obj* p) {
194 auto hobp = static_cast<hazptr_obj_base*>(p);
195 auto obj = static_cast<T*>(hobp);
199 (HAZPTR_ONE_DOMAIN || (&domain == &default_hazptr_domain()))) {
200 if (hazptr_priv_try_retire(this)) {
204 domain.objRetire(this);
208 * hazptr_obj_base_refcounted
211 template <typename T, typename D>
212 inline void hazptr_obj_base_refcounted<T, D>::retire(
213 hazptr_domain& domain,
215 DEBUG_PRINT(this << " " << &domain);
216 deleter_ = std::move(deleter);
217 reclaim_ = [](hazptr_obj* p) {
218 auto hrobp = static_cast<hazptr_obj_base_refcounted*>(p);
219 if (hrobp->release_ref()) {
220 auto obj = static_cast<T*>(hrobp);
221 hrobp->deleter_(obj);
225 (HAZPTR_ONE_DOMAIN || (&domain == &default_hazptr_domain()))) {
226 if (hazptr_priv_try_retire(this)) {
230 domain.objRetire(this);
233 template <typename T, typename D>
234 inline void hazptr_obj_base_refcounted<T, D>::acquire_ref() {
236 auto oldval = refcount_.fetch_add(1);
240 template <typename T, typename D>
241 inline void hazptr_obj_base_refcounted<T, D>::acquire_ref_safe() {
243 auto oldval = refcount_.load(std::memory_order_acquire);
245 refcount_.store(oldval + 1, std::memory_order_release);
248 template <typename T, typename D>
249 inline bool hazptr_obj_base_refcounted<T, D>::release_ref() {
251 auto oldval = refcount_.load(std::memory_order_acquire);
253 oldval = refcount_.fetch_sub(1);
259 DEBUG_PRINT(this << " " << oldval);
269 friend class hazptr_domain;
270 friend class hazptr_holder;
271 friend struct hazptr_tc_entry;
273 FOLLY_ALIGN_TO_AVOID_FALSE_SHARING
274 std::atomic<const void*> hazptr_{nullptr};
275 hazptr_rec* next_{nullptr};
276 std::atomic<bool> active_{false};
278 void set(const void* p) noexcept;
279 const void* get() const noexcept;
280 void clear() noexcept;
281 bool isActive() noexcept;
282 bool tryAcquire() noexcept;
283 void release() noexcept;
290 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(hazptr_domain& domain) {
294 (HAZPTR_ONE_DOMAIN || &domain == &default_hazptr_domain()))) {
295 auto hprec = hazptr_tc_try_get();
296 if (LIKELY(hprec != nullptr)) {
298 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
302 hazptr_ = domain_->hazptrAcquire();
303 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
304 if (hazptr_ == nullptr) { std::bad_alloc e; throw e; }
307 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(std::nullptr_t) noexcept {
310 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
313 FOLLY_ALWAYS_INLINE hazptr_holder::~hazptr_holder() {
315 if (LIKELY(hazptr_ != nullptr)) {
319 (HAZPTR_ONE_DOMAIN || domain_ == &default_hazptr_domain()))) {
320 if (LIKELY(hazptr_tc_try_put(hazptr_))) {
324 domain_->hazptrRelease(hazptr_);
328 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(hazptr_holder&& rhs) noexcept {
329 domain_ = rhs.domain_;
330 hazptr_ = rhs.hazptr_;
331 rhs.domain_ = nullptr;
332 rhs.hazptr_ = nullptr;
336 hazptr_holder& hazptr_holder::operator=(hazptr_holder&& rhs) noexcept {
337 /* Self-move is a no-op. */
338 if (LIKELY(this != &rhs)) {
339 this->~hazptr_holder();
340 new (this) hazptr_holder(std::move(rhs));
345 template <typename T>
346 FOLLY_ALWAYS_INLINE bool hazptr_holder::try_protect(
348 const std::atomic<T*>& src) noexcept {
349 return try_protect(ptr, src, [](T* t) { return t; });
352 template <typename T, typename Func>
353 FOLLY_ALWAYS_INLINE bool hazptr_holder::try_protect(
355 const std::atomic<T*>& src,
357 DEBUG_PRINT(this << " " << ptr << " " << &src);
359 /*** Full fence ***/ hazptr_mb::light();
360 T* p = src.load(std::memory_order_acquire);
361 if (UNLIKELY(p != ptr)) {
369 template <typename T>
370 FOLLY_ALWAYS_INLINE T* hazptr_holder::get_protected(
371 const std::atomic<T*>& src) noexcept {
372 return get_protected(src, [](T* t) { return t; });
375 template <typename T, typename Func>
376 FOLLY_ALWAYS_INLINE T* hazptr_holder::get_protected(
377 const std::atomic<T*>& src,
379 T* p = src.load(std::memory_order_relaxed);
380 while (!try_protect(p, src, f)) {
382 DEBUG_PRINT(this << " " << p << " " << &src);
386 template <typename T>
387 FOLLY_ALWAYS_INLINE void hazptr_holder::reset(const T* ptr) noexcept {
388 auto p = static_cast<hazptr_obj*>(const_cast<T*>(ptr));
389 DEBUG_PRINT(this << " " << ptr << " p:" << p);
390 DCHECK(hazptr_); // UB if *this is empty
394 FOLLY_ALWAYS_INLINE void hazptr_holder::reset(std::nullptr_t) noexcept {
396 DCHECK(hazptr_); // UB if *this is empty
400 FOLLY_ALWAYS_INLINE void hazptr_holder::swap(hazptr_holder& rhs) noexcept {
402 this << " " << this->hazptr_ << " " << this->domain_ << " -- "
403 << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
404 if (!HAZPTR_ONE_DOMAIN) {
405 std::swap(this->domain_, rhs.domain_);
407 std::swap(this->hazptr_, rhs.hazptr_);
410 FOLLY_ALWAYS_INLINE void swap(hazptr_holder& lhs, hazptr_holder& rhs) noexcept {
419 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array() {
420 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
422 auto ptc = hazptr_tc_tls();
423 if (LIKELY(ptc != nullptr)) {
425 auto count = tc.count();
427 size_t offset = count - M;
428 for (size_t i = 0; i < M; ++i) {
429 auto hprec = tc[offset + i].hprec_;
430 DCHECK(hprec != nullptr);
431 DEBUG_PRINT(i << " " << &h[i]);
432 new (&h[i]) hazptr_holder(nullptr);
433 h[i].hazptr_ = hprec;
435 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
443 for (size_t i = 0; i < M; ++i) {
444 new (&h[i]) hazptr_holder;
446 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
451 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array(
452 hazptr_array&& other) noexcept {
453 DEBUG_PRINT(this << " " << M << " " << &other);
454 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
455 for (size_t i = 0; i < M; ++i) {
456 new (&h[i]) hazptr_holder(std::move(other.h_[i]));
457 DEBUG_PRINT(i << " " << &h[i] << " " << &other.h_[i]);
459 empty_ = other.empty_;
464 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array(std::nullptr_t) noexcept {
465 DEBUG_PRINT(this << " " << M);
466 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
467 for (size_t i = 0; i < M; ++i) {
468 new (&h[i]) hazptr_holder(nullptr);
469 DEBUG_PRINT(i << " " << &h[i]);
475 FOLLY_ALWAYS_INLINE hazptr_array<M>::~hazptr_array() {
479 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
481 auto ptc = hazptr_tc_tls();
482 if (LIKELY(ptc != nullptr)) {
484 auto count = tc.count();
485 if ((M <= HAZPTR_TC_SIZE) && (count + M <= HAZPTR_TC_SIZE)) {
486 for (size_t i = 0; i < M; ++i) {
487 tc[count + i].hprec_ = h[i].hazptr_;
488 DEBUG_PRINT(i << " " << &h[i]);
489 new (&h[i]) hazptr_holder(nullptr);
491 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
493 tc.count_ = count + M;
499 for (size_t i = 0; i < M; ++i) {
500 h[i].~hazptr_holder();
505 FOLLY_ALWAYS_INLINE hazptr_array<M>& hazptr_array<M>::operator=(
506 hazptr_array&& other) noexcept {
507 DEBUG_PRINT(this << " " << M << " " << &other);
508 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
509 for (size_t i = 0; i < M; ++i) {
510 h[i] = std::move(other[i]);
511 DEBUG_PRINT(i << " " << &h[i] << " " << &other[i]);
513 empty_ = other.empty_;
519 FOLLY_ALWAYS_INLINE hazptr_holder& hazptr_array<M>::operator[](
521 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
531 FOLLY_ALWAYS_INLINE hazptr_local<M>::hazptr_local() {
532 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
534 auto ptc = hazptr_tc_tls();
535 if (LIKELY(ptc != nullptr)) {
537 auto count = tc.count();
544 for (size_t i = 0; i < M; ++i) {
545 auto hprec = tc[i].hprec_;
546 DCHECK(hprec != nullptr);
547 DEBUG_PRINT(i << " " << &h[i]);
548 new (&h[i]) hazptr_holder(nullptr);
549 h[i].hazptr_ = hprec;
551 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
558 need_destruct_ = true;
559 for (size_t i = 0; i < M; ++i) {
560 new (&h[i]) hazptr_holder;
562 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
567 FOLLY_ALWAYS_INLINE hazptr_local<M>::~hazptr_local() {
568 if (LIKELY(!need_destruct_)) {
570 auto ptc = hazptr_tc_tls();
571 DCHECK(ptc != nullptr);
579 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
580 for (size_t i = 0; i < M; ++i) {
581 h[i].~hazptr_holder();
586 FOLLY_ALWAYS_INLINE hazptr_holder& hazptr_local<M>::operator[](
588 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
593 ////////////////////////////////////////////////////////////////////////////////
595 // - Control of reclamation (when and by whom)
596 // - End-to-end lock-free implementation
598 /** Definition of default_hazptr_domain() */
600 FOLLY_ALWAYS_INLINE hazptr_domain& default_hazptr_domain() {
601 DEBUG_PRINT(&default_domain_);
602 return default_domain_;
607 FOLLY_ALWAYS_INLINE void hazptr_rec::set(const void* p) noexcept {
608 DEBUG_PRINT(this << " " << p);
609 hazptr_.store(p, std::memory_order_release);
612 inline const void* hazptr_rec::get() const noexcept {
613 auto p = hazptr_.load(std::memory_order_acquire);
614 DEBUG_PRINT(this << " " << p);
618 FOLLY_ALWAYS_INLINE void hazptr_rec::clear() noexcept {
620 hazptr_.store(nullptr, std::memory_order_release);
623 inline bool hazptr_rec::isActive() noexcept {
624 return active_.load(std::memory_order_acquire);
627 inline bool hazptr_rec::tryAcquire() noexcept {
628 bool active = isActive();
630 active_.compare_exchange_strong(
631 active, true, std::memory_order_release, std::memory_order_relaxed)) {
638 inline void hazptr_rec::release() noexcept {
640 active_.store(false, std::memory_order_release);
645 inline const void* hazptr_obj::getObjPtr() const {
652 inline hazptr_domain::~hazptr_domain() {
654 { /* reclaim all remaining retired objects */
656 auto retired = retired_.exchange(nullptr);
658 for (auto p = retired; p; p = next) {
660 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
663 retired = retired_.exchange(nullptr);
666 /* Leak the data for the default domain to avoid destruction order
667 * issues with thread caches.
669 if (this != &default_hazptr_domain()) {
670 /* free all hazptr_rec-s */
672 for (auto p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
674 DCHECK(!p->isActive());
675 mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
680 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
683 for (p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
685 if (p->tryAcquire()) {
689 p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
690 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec));
694 p->active_.store(true, std::memory_order_relaxed);
695 p->next_ = hazptrs_.load(std::memory_order_acquire);
696 while (!hazptrs_.compare_exchange_weak(
697 p->next_, p, std::memory_order_release, std::memory_order_acquire)) {
700 auto hcount = hcount_.fetch_add(1);
701 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
705 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
706 DEBUG_PRINT(this << " " << p);
711 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
712 /*** Full fence ***/ hazptr_mb::light();
713 tail->next_ = retired_.load(std::memory_order_acquire);
714 while (!retired_.compare_exchange_weak(
717 std::memory_order_release,
718 std::memory_order_acquire)) {
720 return rcount_.fetch_add(count) + count;
723 inline bool hazptr_domain::reachedThreshold(int rcount) {
725 rcount >= HAZPTR_SCAN_THRESHOLD &&
726 rcount >= HAZPTR_SCAN_MULT * hcount_.load(std::memory_order_acquire));
729 inline void hazptr_domain::objRetire(hazptr_obj* p) {
730 auto rcount = pushRetired(p, p, 1);
731 if (reachedThreshold(rcount)) {
736 inline void hazptr_domain::tryBulkReclaim() {
739 auto hcount = hcount_.load(std::memory_order_acquire);
740 auto rcount = rcount_.load(std::memory_order_acquire);
741 if (rcount < HAZPTR_SCAN_THRESHOLD || rcount < HAZPTR_SCAN_MULT * hcount) {
744 if (rcount_.compare_exchange_weak(
745 rcount, 0, std::memory_order_release, std::memory_order_relaxed)) {
752 inline void hazptr_domain::bulkReclaim() {
754 /*** Full fence ***/ hazptr_mb::heavy();
755 auto p = retired_.exchange(nullptr, std::memory_order_acquire);
756 auto h = hazptrs_.load(std::memory_order_acquire);
757 std::unordered_set<const void*> hs; // TODO lock-free alternative
758 for (; h; h = h->next_) {
762 hazptr_obj* retired = nullptr;
763 hazptr_obj* tail = nullptr;
765 for (; p; p = next) {
767 if (hs.count(p->getObjPtr()) == 0) {
768 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
773 if (tail == nullptr) {
780 pushRetired(retired, tail, rcount);
794 std::atomic<uint64_t> light_{0};
795 std::atomic<uint64_t> heavy_{0};
796 std::atomic<uint64_t> seq_cst_{0};
799 extern hazptr_stats hazptr_stats_;
801 inline hazptr_stats::~hazptr_stats() {
802 DEBUG_PRINT(this << " light " << light_.load());
803 DEBUG_PRINT(this << " heavy " << heavy_.load());
804 DEBUG_PRINT(this << " seq_cst " << seq_cst_.load());
807 FOLLY_ALWAYS_INLINE void hazptr_stats::light() {
809 /* atomic */ ++light_;
813 inline void hazptr_stats::heavy() {
815 /* atomic */ ++heavy_;
819 inline void hazptr_stats::seq_cst() {
821 /* atomic */ ++seq_cst_;
827 FOLLY_ALWAYS_INLINE void hazptr_mb::light() {
830 folly::asymmetricLightBarrier();
831 INC_HAZPTR_STATS(light);
833 atomic_thread_fence(std::memory_order_seq_cst);
834 INC_HAZPTR_STATS(seq_cst);
838 inline void hazptr_mb::heavy() {
841 folly::asymmetricHeavyBarrier(AMBFlags::EXPEDITED);
842 INC_HAZPTR_STATS(heavy);
844 atomic_thread_fence(std::memory_order_seq_cst);
845 INC_HAZPTR_STATS(seq_cst);
854 * hazptr_tc structures
857 /** hazptr_tc_entry */
859 FOLLY_ALWAYS_INLINE void hazptr_tc_entry::fill(hazptr_rec* hprec) {
861 DEBUG_PRINT(this << " " << hprec);
864 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_entry::get() {
866 DEBUG_PRINT(this << " " << hprec);
870 inline void hazptr_tc_entry::evict() {
873 DEBUG_PRINT(this << " " << hprec);
878 FOLLY_ALWAYS_INLINE hazptr_tc_entry& hazptr_tc::operator[](size_t i) {
879 DCHECK(i <= HAZPTR_TC_SIZE);
883 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc::get() {
884 if (LIKELY(count_ != 0)) {
885 auto hprec = entry_[--count_].get();
886 DEBUG_PRINT(this << " " << hprec);
889 DEBUG_PRINT(this << " nullptr");
893 FOLLY_ALWAYS_INLINE bool hazptr_tc::put(hazptr_rec* hprec) {
894 if (LIKELY(count_ < HAZPTR_TC_SIZE)) {
895 entry_[count_++].fill(hprec);
896 DEBUG_PRINT(this << " " << count_ - 1);
902 FOLLY_ALWAYS_INLINE size_t hazptr_tc::count() {
906 /** hazptr_tc free functions */
908 FOLLY_ALWAYS_INLINE hazptr_tc* hazptr_tc_tls() {
909 DEBUG_PRINT(tls_state_);
910 if (LIKELY(tls_state_ == TLS_ALIVE)) {
911 DEBUG_PRINT(tls_state_);
912 return &tls_tc_data_;
913 } else if (tls_state_ == TLS_UNINITIALIZED) {
915 return &tls_tc_data_;
920 inline void hazptr_tc_init() {
922 auto& tc = tls_tc_data_;
930 inline void hazptr_tc_shutdown() {
931 auto& tc = tls_tc_data_;
933 for (size_t i = 0; i < tc.count_; ++i) {
934 tc.entry_[i].evict();
938 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_try_get() {
939 DEBUG_PRINT(TLS_UNINITIALIZED << TLS_ALIVE << TLS_DESTROYED);
940 DEBUG_PRINT(tls_state_);
941 if (LIKELY(tls_state_ == TLS_ALIVE)) {
942 DEBUG_PRINT(tls_state_);
943 return tls_tc_data_.get();
944 } else if (tls_state_ == TLS_UNINITIALIZED) {
946 return tls_tc_data_.get();
951 FOLLY_ALWAYS_INLINE bool hazptr_tc_try_put(hazptr_rec* hprec) {
952 DEBUG_PRINT(tls_state_);
953 if (LIKELY(tls_state_ == TLS_ALIVE)) {
954 DEBUG_PRINT(tls_state_);
955 return tls_tc_data_.put(hprec);
964 inline void hazptr_priv::push(hazptr_obj* obj) {
965 auto& domain = default_hazptr_domain();
966 obj->next_ = nullptr;
971 domain.objRetire(obj);
978 if (domain.reachedThreshold(rcount_)) {
983 inline void hazptr_priv::pushAllToDomain() {
984 auto& domain = default_hazptr_domain();
985 domain.pushRetired(head_, tail_, rcount_);
989 domain.tryBulkReclaim();
992 inline void hazptr_priv_init() {
993 auto& priv = tls_priv_data_;
995 priv.head_ = nullptr;
996 priv.tail_ = nullptr;
1001 inline void hazptr_priv_shutdown() {
1002 auto& priv = tls_priv_data_;
1004 DCHECK(priv.active_);
1005 priv.active_ = false;
1007 priv.pushAllToDomain();
1011 inline bool hazptr_priv_try_retire(hazptr_obj* obj) {
1012 DEBUG_PRINT(tls_state_);
1013 if (tls_state_ == TLS_ALIVE) {
1014 DEBUG_PRINT(tls_state_);
1015 tls_priv_data_.push(obj);
1017 } else if (tls_state_ == TLS_UNINITIALIZED) {
1018 DEBUG_PRINT(tls_state_);
1020 tls_priv_data_.push(obj);
1026 /** hazptr_tls_life */
1028 inline void tls_life_odr_use() {
1029 DEBUG_PRINT(tls_state_);
1030 CHECK(tls_state_ == TLS_UNINITIALIZED);
1031 auto volatile tlsOdrUse = &tls_life_;
1032 CHECK(tlsOdrUse != nullptr);
1033 DEBUG_PRINT(tlsOdrUse);
1036 inline hazptr_tls_life::hazptr_tls_life() {
1038 CHECK(tls_state_ == TLS_UNINITIALIZED);
1041 tls_state_ = TLS_ALIVE;
1044 inline hazptr_tls_life::~hazptr_tls_life() {
1046 CHECK(tls_state_ == TLS_ALIVE);
1047 hazptr_tc_shutdown();
1048 hazptr_priv_shutdown();
1049 tls_state_ = TLS_DESTROYED;
1052 } // namespace hazptr
1053 } // namespace folly