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_;
605 template <typename T, typename D>
606 FOLLY_ALWAYS_INLINE void hazptr_retire(T* obj, D reclaim) {
607 default_hazptr_domain().retire(obj, std::move(reclaim));
612 FOLLY_ALWAYS_INLINE void hazptr_rec::set(const void* p) noexcept {
613 DEBUG_PRINT(this << " " << p);
614 hazptr_.store(p, std::memory_order_release);
617 inline const void* hazptr_rec::get() const noexcept {
618 auto p = hazptr_.load(std::memory_order_acquire);
619 DEBUG_PRINT(this << " " << p);
623 FOLLY_ALWAYS_INLINE void hazptr_rec::clear() noexcept {
625 hazptr_.store(nullptr, std::memory_order_release);
628 inline bool hazptr_rec::isActive() noexcept {
629 return active_.load(std::memory_order_acquire);
632 inline bool hazptr_rec::tryAcquire() noexcept {
633 bool active = isActive();
635 active_.compare_exchange_strong(
636 active, true, std::memory_order_release, std::memory_order_relaxed)) {
643 inline void hazptr_rec::release() noexcept {
645 active_.store(false, std::memory_order_release);
650 inline const void* hazptr_obj::getObjPtr() const {
657 template <typename T, typename D>
658 void hazptr_domain::retire(T* obj, D reclaim) {
659 struct hazptr_retire_node : hazptr_obj {
660 std::unique_ptr<T, D> obj_;
662 hazptr_retire_node(T* obj, D reclaim) : obj_{obj, std::move(reclaim)} {}
665 auto node = new hazptr_retire_node(obj, std::move(reclaim));
666 node->reclaim_ = [](hazptr_obj* p) {
667 delete static_cast<hazptr_retire_node*>(p);
672 inline hazptr_domain::~hazptr_domain() {
674 { /* reclaim all remaining retired objects */
676 auto retired = retired_.exchange(nullptr);
678 for (auto p = retired; p; p = next) {
680 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
683 retired = retired_.exchange(nullptr);
686 /* Leak the data for the default domain to avoid destruction order
687 * issues with thread caches.
689 if (this != &default_hazptr_domain()) {
690 /* free all hazptr_rec-s */
692 for (auto p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
694 DCHECK(!p->isActive());
695 mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
700 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
703 for (p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
705 if (p->tryAcquire()) {
709 p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
710 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec));
714 p->active_.store(true, std::memory_order_relaxed);
715 p->next_ = hazptrs_.load(std::memory_order_acquire);
716 while (!hazptrs_.compare_exchange_weak(
717 p->next_, p, std::memory_order_release, std::memory_order_acquire)) {
720 auto hcount = hcount_.fetch_add(1);
721 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
725 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
726 DEBUG_PRINT(this << " " << p);
731 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
732 /*** Full fence ***/ hazptr_mb::light();
733 tail->next_ = retired_.load(std::memory_order_acquire);
734 while (!retired_.compare_exchange_weak(
737 std::memory_order_release,
738 std::memory_order_acquire)) {
740 return rcount_.fetch_add(count) + count;
743 inline bool hazptr_domain::reachedThreshold(int rcount) {
745 rcount >= HAZPTR_SCAN_THRESHOLD &&
746 rcount >= HAZPTR_SCAN_MULT * hcount_.load(std::memory_order_acquire));
749 inline void hazptr_domain::objRetire(hazptr_obj* p) {
750 auto rcount = pushRetired(p, p, 1);
751 if (reachedThreshold(rcount)) {
756 inline void hazptr_domain::tryBulkReclaim() {
759 auto hcount = hcount_.load(std::memory_order_acquire);
760 auto rcount = rcount_.load(std::memory_order_acquire);
761 if (rcount < HAZPTR_SCAN_THRESHOLD || rcount < HAZPTR_SCAN_MULT * hcount) {
764 if (rcount_.compare_exchange_weak(
765 rcount, 0, std::memory_order_release, std::memory_order_relaxed)) {
772 inline void hazptr_domain::bulkReclaim() {
774 /*** Full fence ***/ hazptr_mb::heavy();
775 auto p = retired_.exchange(nullptr, std::memory_order_acquire);
776 auto h = hazptrs_.load(std::memory_order_acquire);
777 std::unordered_set<const void*> hs; // TODO lock-free alternative
778 for (; h; h = h->next_) {
782 hazptr_obj* retired = nullptr;
783 hazptr_obj* tail = nullptr;
785 for (; p; p = next) {
787 if (hs.count(p->getObjPtr()) == 0) {
788 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
793 if (tail == nullptr) {
800 pushRetired(retired, tail, rcount);
814 std::atomic<uint64_t> light_{0};
815 std::atomic<uint64_t> heavy_{0};
816 std::atomic<uint64_t> seq_cst_{0};
819 extern hazptr_stats hazptr_stats_;
821 inline hazptr_stats::~hazptr_stats() {
822 DEBUG_PRINT(this << " light " << light_.load());
823 DEBUG_PRINT(this << " heavy " << heavy_.load());
824 DEBUG_PRINT(this << " seq_cst " << seq_cst_.load());
827 FOLLY_ALWAYS_INLINE void hazptr_stats::light() {
829 /* atomic */ ++light_;
833 inline void hazptr_stats::heavy() {
835 /* atomic */ ++heavy_;
839 inline void hazptr_stats::seq_cst() {
841 /* atomic */ ++seq_cst_;
847 FOLLY_ALWAYS_INLINE void hazptr_mb::light() {
850 folly::asymmetricLightBarrier();
851 INC_HAZPTR_STATS(light);
853 atomic_thread_fence(std::memory_order_seq_cst);
854 INC_HAZPTR_STATS(seq_cst);
858 inline void hazptr_mb::heavy() {
861 folly::asymmetricHeavyBarrier(AMBFlags::EXPEDITED);
862 INC_HAZPTR_STATS(heavy);
864 atomic_thread_fence(std::memory_order_seq_cst);
865 INC_HAZPTR_STATS(seq_cst);
874 * hazptr_tc structures
877 /** hazptr_tc_entry */
879 FOLLY_ALWAYS_INLINE void hazptr_tc_entry::fill(hazptr_rec* hprec) {
881 DEBUG_PRINT(this << " " << hprec);
884 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_entry::get() {
886 DEBUG_PRINT(this << " " << hprec);
890 inline void hazptr_tc_entry::evict() {
893 DEBUG_PRINT(this << " " << hprec);
898 FOLLY_ALWAYS_INLINE hazptr_tc_entry& hazptr_tc::operator[](size_t i) {
899 DCHECK(i <= HAZPTR_TC_SIZE);
903 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc::get() {
904 if (LIKELY(count_ != 0)) {
905 auto hprec = entry_[--count_].get();
906 DEBUG_PRINT(this << " " << hprec);
909 DEBUG_PRINT(this << " nullptr");
913 FOLLY_ALWAYS_INLINE bool hazptr_tc::put(hazptr_rec* hprec) {
914 if (LIKELY(count_ < HAZPTR_TC_SIZE)) {
915 entry_[count_++].fill(hprec);
916 DEBUG_PRINT(this << " " << count_ - 1);
922 FOLLY_ALWAYS_INLINE size_t hazptr_tc::count() {
926 /** hazptr_tc free functions */
928 FOLLY_ALWAYS_INLINE hazptr_tc* hazptr_tc_tls() {
929 DEBUG_PRINT(tls_state_);
930 if (LIKELY(tls_state_ == TLS_ALIVE)) {
931 DEBUG_PRINT(tls_state_);
932 return &tls_tc_data_;
933 } else if (tls_state_ == TLS_UNINITIALIZED) {
935 return &tls_tc_data_;
940 inline void hazptr_tc_init() {
942 auto& tc = tls_tc_data_;
950 inline void hazptr_tc_shutdown() {
951 auto& tc = tls_tc_data_;
953 for (size_t i = 0; i < tc.count_; ++i) {
954 tc.entry_[i].evict();
958 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_try_get() {
959 DEBUG_PRINT(TLS_UNINITIALIZED << TLS_ALIVE << TLS_DESTROYED);
960 DEBUG_PRINT(tls_state_);
961 if (LIKELY(tls_state_ == TLS_ALIVE)) {
962 DEBUG_PRINT(tls_state_);
963 return tls_tc_data_.get();
964 } else if (tls_state_ == TLS_UNINITIALIZED) {
966 return tls_tc_data_.get();
971 FOLLY_ALWAYS_INLINE bool hazptr_tc_try_put(hazptr_rec* hprec) {
972 DEBUG_PRINT(tls_state_);
973 if (LIKELY(tls_state_ == TLS_ALIVE)) {
974 DEBUG_PRINT(tls_state_);
975 return tls_tc_data_.put(hprec);
984 inline void hazptr_priv::push(hazptr_obj* obj) {
985 auto& domain = default_hazptr_domain();
986 obj->next_ = nullptr;
991 domain.objRetire(obj);
998 if (domain.reachedThreshold(rcount_)) {
1003 inline void hazptr_priv::pushAllToDomain() {
1004 auto& domain = default_hazptr_domain();
1005 domain.pushRetired(head_, tail_, rcount_);
1009 domain.tryBulkReclaim();
1012 inline void hazptr_priv_init() {
1013 auto& priv = tls_priv_data_;
1015 priv.head_ = nullptr;
1016 priv.tail_ = nullptr;
1018 priv.active_ = true;
1021 inline void hazptr_priv_shutdown() {
1022 auto& priv = tls_priv_data_;
1024 DCHECK(priv.active_);
1025 priv.active_ = false;
1027 priv.pushAllToDomain();
1031 inline bool hazptr_priv_try_retire(hazptr_obj* obj) {
1032 DEBUG_PRINT(tls_state_);
1033 if (tls_state_ == TLS_ALIVE) {
1034 DEBUG_PRINT(tls_state_);
1035 tls_priv_data_.push(obj);
1037 } else if (tls_state_ == TLS_UNINITIALIZED) {
1038 DEBUG_PRINT(tls_state_);
1040 tls_priv_data_.push(obj);
1046 /** hazptr_tls_life */
1048 inline void tls_life_odr_use() {
1049 DEBUG_PRINT(tls_state_);
1050 CHECK(tls_state_ == TLS_UNINITIALIZED);
1051 auto volatile tlsOdrUse = &tls_life_;
1052 CHECK(tlsOdrUse != nullptr);
1053 DEBUG_PRINT(tlsOdrUse);
1056 inline hazptr_tls_life::hazptr_tls_life() {
1058 CHECK(tls_state_ == TLS_UNINITIALIZED);
1061 tls_state_ = TLS_ALIVE;
1064 inline hazptr_tls_life::~hazptr_tls_life() {
1066 CHECK(tls_state_ == TLS_ALIVE);
1067 hazptr_tc_shutdown();
1068 hazptr_priv_shutdown();
1069 tls_state_ = TLS_DESTROYED;
1072 } // namespace hazptr
1073 } // namespace folly