2 * Copyright 2017-present 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.
16 /* override-include-guard */
18 #error "This should only be included by hazptr.h"
21 /* quality of implementation switches */
23 // NOTE: The #ifndef pattern is prone to ODR violation. Its use for
24 // quality of implementation options is temporary. Eventually these
25 // options should be added to the API in future API extensions.
28 #define HAZPTR_AMB true
32 #define HAZPTR_TC true
35 #ifndef HAZPTR_TC_SIZE
36 #define HAZPTR_TC_SIZE 10
40 #define HAZPTR_PRIV true
43 #ifndef HAZPTR_PRIV_THRESHOLD
44 #define HAZPTR_PRIV_THRESHOLD 20
47 #ifndef HAZPTR_ONE_DOMAIN
48 #define HAZPTR_ONE_DOMAIN false
51 #ifndef HAZPTR_SCAN_MULT
52 #define HAZPTR_SCAN_MULT 2
55 #ifndef HAZPTR_SCAN_THRESHOLD
56 #define HAZPTR_SCAN_THRESHOLD 1000
61 #define HAZPTR_STATS false
64 #include <folly/concurrency/CacheLocality.h>
65 #include <folly/experimental/hazptr/debug.h>
66 #include <folly/synchronization/AsymmetricMemoryBarrier.h>
68 #include <mutex> // for thread caching
69 #include <unordered_set> // for hash set in bulk reclamation
75 * Helper classes and functions
83 #define INC_HAZPTR_STATS(x) hazptr_stats_.x()
85 #define INC_HAZPTR_STATS(x)
100 /** TLS life state */
102 enum hazptr_tls_state { TLS_ALIVE, TLS_UNINITIALIZED, TLS_DESTROYED };
104 /** hazptr_tc structures
105 * Thread caching of hazptr_rec-s that belong to the default domain.
108 struct hazptr_tc_entry {
111 void fill(hazptr_rec* hprec);
117 std::is_trivial<hazptr_tc_entry>::value,
118 "hazptr_tc_entry must be trivial"
119 " to avoid a branch to check initialization");
122 hazptr_tc_entry entry_[HAZPTR_TC_SIZE];
124 bool local_; // for debug mode only
127 hazptr_tc_entry& operator[](size_t i);
129 bool put(hazptr_rec* hprec);
134 std::is_trivial<hazptr_tc>::value,
135 "hazptr_tc must be trivial to avoid a branch to check initialization");
137 hazptr_tc* hazptr_tc_tls();
138 void hazptr_tc_init();
139 void hazptr_tc_shutdown();
140 hazptr_rec* hazptr_tc_try_get();
141 bool hazptr_tc_try_put(hazptr_rec* hprec);
143 /** hazptr_priv structures
144 * Thread private lists of retired objects that belong to the default domain.
153 void push(hazptr_obj* obj);
154 void pushAllToDomain();
158 std::is_trivial<hazptr_priv>::value,
159 "hazptr_priv must be trivial to avoid a branch to check initialization");
161 void hazptr_priv_init();
162 void hazptr_priv_shutdown();
163 bool hazptr_priv_try_retire(hazptr_obj* obj);
165 /** hazptr_tls_life */
167 struct hazptr_tls_life {
172 void tls_life_odr_use();
176 extern thread_local hazptr_tls_state tls_state_;
177 extern thread_local hazptr_tc tls_tc_data_;
178 extern thread_local hazptr_priv tls_priv_data_;
179 extern thread_local hazptr_tls_life tls_life_; // last
185 inline constexpr hazptr_domain::hazptr_domain(memory_resource* mr) noexcept
192 template <typename T, typename D>
193 inline void hazptr_obj_base<T, D>::retire(hazptr_domain& domain, D deleter) {
194 DEBUG_PRINT(this << " " << &domain);
195 deleter_ = std::move(deleter);
196 reclaim_ = [](hazptr_obj* p) {
197 auto hobp = static_cast<hazptr_obj_base*>(p);
198 auto obj = static_cast<T*>(hobp);
202 (HAZPTR_ONE_DOMAIN || (&domain == &default_hazptr_domain()))) {
203 if (hazptr_priv_try_retire(this)) {
207 domain.objRetire(this);
211 * hazptr_obj_base_refcounted
214 template <typename T, typename D>
215 inline void hazptr_obj_base_refcounted<T, D>::retire(
216 hazptr_domain& domain,
218 DEBUG_PRINT(this << " " << &domain);
219 deleter_ = std::move(deleter);
220 reclaim_ = [](hazptr_obj* p) {
221 auto hrobp = static_cast<hazptr_obj_base_refcounted*>(p);
222 if (hrobp->release_ref()) {
223 auto obj = static_cast<T*>(hrobp);
224 hrobp->deleter_(obj);
228 (HAZPTR_ONE_DOMAIN || (&domain == &default_hazptr_domain()))) {
229 if (hazptr_priv_try_retire(this)) {
233 domain.objRetire(this);
236 template <typename T, typename D>
237 inline void hazptr_obj_base_refcounted<T, D>::acquire_ref() {
239 auto oldval = refcount_.fetch_add(1);
243 template <typename T, typename D>
244 inline void hazptr_obj_base_refcounted<T, D>::acquire_ref_safe() {
246 auto oldval = refcount_.load(std::memory_order_acquire);
248 refcount_.store(oldval + 1, std::memory_order_release);
251 template <typename T, typename D>
252 inline bool hazptr_obj_base_refcounted<T, D>::release_ref() {
254 auto oldval = refcount_.load(std::memory_order_acquire);
256 oldval = refcount_.fetch_sub(1);
262 DEBUG_PRINT(this << " " << oldval);
271 class alignas(hardware_destructive_interference_size) hazptr_rec {
272 friend class hazptr_domain;
273 friend class hazptr_holder;
274 friend struct hazptr_tc_entry;
276 std::atomic<const void*> hazptr_{nullptr};
277 hazptr_rec* next_{nullptr};
278 std::atomic<bool> active_{false};
280 void set(const void* p) noexcept;
281 const void* get() const noexcept;
282 void clear() noexcept;
283 bool isActive() noexcept;
284 bool tryAcquire() noexcept;
285 void release() noexcept;
292 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(hazptr_domain& domain) {
296 (HAZPTR_ONE_DOMAIN || &domain == &default_hazptr_domain()))) {
297 auto hprec = hazptr_tc_try_get();
298 if (LIKELY(hprec != nullptr)) {
300 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
304 hazptr_ = domain_->hazptrAcquire();
305 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
306 if (hazptr_ == nullptr) { std::bad_alloc e; throw e; }
309 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(std::nullptr_t) noexcept {
312 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
315 FOLLY_ALWAYS_INLINE hazptr_holder::~hazptr_holder() {
317 if (LIKELY(hazptr_ != nullptr)) {
321 (HAZPTR_ONE_DOMAIN || domain_ == &default_hazptr_domain()))) {
322 if (LIKELY(hazptr_tc_try_put(hazptr_))) {
326 domain_->hazptrRelease(hazptr_);
330 FOLLY_ALWAYS_INLINE hazptr_holder::hazptr_holder(hazptr_holder&& rhs) noexcept {
331 domain_ = rhs.domain_;
332 hazptr_ = rhs.hazptr_;
333 rhs.domain_ = nullptr;
334 rhs.hazptr_ = nullptr;
338 hazptr_holder& hazptr_holder::operator=(hazptr_holder&& rhs) noexcept {
339 /* Self-move is a no-op. */
340 if (LIKELY(this != &rhs)) {
341 this->~hazptr_holder();
342 new (this) hazptr_holder(std::move(rhs));
347 template <typename T>
348 FOLLY_ALWAYS_INLINE bool hazptr_holder::try_protect(
350 const std::atomic<T*>& src) noexcept {
351 return try_protect(ptr, src, [](T* t) { return t; });
354 template <typename T, typename Func>
355 FOLLY_ALWAYS_INLINE bool hazptr_holder::try_protect(
357 const std::atomic<T*>& src,
359 DEBUG_PRINT(this << " " << ptr << " " << &src);
361 /*** Full fence ***/ hazptr_mb::light();
362 T* p = src.load(std::memory_order_acquire);
363 if (UNLIKELY(p != ptr)) {
371 template <typename T>
372 FOLLY_ALWAYS_INLINE T* hazptr_holder::get_protected(
373 const std::atomic<T*>& src) noexcept {
374 return get_protected(src, [](T* t) { return t; });
377 template <typename T, typename Func>
378 FOLLY_ALWAYS_INLINE T* hazptr_holder::get_protected(
379 const std::atomic<T*>& src,
381 T* p = src.load(std::memory_order_relaxed);
382 while (!try_protect(p, src, f)) {
384 DEBUG_PRINT(this << " " << p << " " << &src);
388 template <typename T>
389 FOLLY_ALWAYS_INLINE void hazptr_holder::reset(const T* ptr) noexcept {
390 auto p = static_cast<hazptr_obj*>(const_cast<T*>(ptr));
391 DEBUG_PRINT(this << " " << ptr << " p:" << p);
392 DCHECK(hazptr_); // UB if *this is empty
396 FOLLY_ALWAYS_INLINE void hazptr_holder::reset(std::nullptr_t) noexcept {
398 DCHECK(hazptr_); // UB if *this is empty
402 FOLLY_ALWAYS_INLINE void hazptr_holder::swap(hazptr_holder& rhs) noexcept {
404 this << " " << this->hazptr_ << " " << this->domain_ << " -- "
405 << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
406 if (!HAZPTR_ONE_DOMAIN) {
407 std::swap(this->domain_, rhs.domain_);
409 std::swap(this->hazptr_, rhs.hazptr_);
412 FOLLY_ALWAYS_INLINE void swap(hazptr_holder& lhs, hazptr_holder& rhs) noexcept {
421 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array() {
422 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
424 auto ptc = hazptr_tc_tls();
425 if (LIKELY(ptc != nullptr)) {
427 auto count = tc.count();
429 size_t offset = count - M;
430 for (size_t i = 0; i < M; ++i) {
431 auto hprec = tc[offset + i].hprec_;
432 DCHECK(hprec != nullptr);
433 DEBUG_PRINT(i << " " << &h[i]);
434 new (&h[i]) hazptr_holder(nullptr);
435 h[i].hazptr_ = hprec;
437 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
445 for (size_t i = 0; i < M; ++i) {
446 new (&h[i]) hazptr_holder;
448 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
453 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array(
454 hazptr_array&& other) noexcept {
455 DEBUG_PRINT(this << " " << M << " " << &other);
456 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
457 auto hother = reinterpret_cast<hazptr_holder*>(&other.raw_);
458 for (size_t i = 0; i < M; ++i) {
459 new (&h[i]) hazptr_holder(std::move(hother[i]));
460 DEBUG_PRINT(i << " " << &h[i] << " " << &hother[i]);
462 empty_ = other.empty_;
467 FOLLY_ALWAYS_INLINE hazptr_array<M>::hazptr_array(std::nullptr_t) noexcept {
468 DEBUG_PRINT(this << " " << M);
469 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
470 for (size_t i = 0; i < M; ++i) {
471 new (&h[i]) hazptr_holder(nullptr);
472 DEBUG_PRINT(i << " " << &h[i]);
478 FOLLY_ALWAYS_INLINE hazptr_array<M>::~hazptr_array() {
482 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
484 auto ptc = hazptr_tc_tls();
485 if (LIKELY(ptc != nullptr)) {
487 auto count = tc.count();
488 if ((M <= HAZPTR_TC_SIZE) && (count + M <= HAZPTR_TC_SIZE)) {
489 for (size_t i = 0; i < M; ++i) {
490 tc[count + i].hprec_ = h[i].hazptr_;
491 DEBUG_PRINT(i << " " << &h[i]);
492 new (&h[i]) hazptr_holder(nullptr);
494 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
496 tc.count_ = count + M;
502 for (size_t i = 0; i < M; ++i) {
503 h[i].~hazptr_holder();
508 FOLLY_ALWAYS_INLINE hazptr_array<M>& hazptr_array<M>::operator=(
509 hazptr_array&& other) noexcept {
510 DEBUG_PRINT(this << " " << M << " " << &other);
511 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
512 for (size_t i = 0; i < M; ++i) {
513 h[i] = std::move(other[i]);
514 DEBUG_PRINT(i << " " << &h[i] << " " << &other[i]);
516 empty_ = other.empty_;
522 FOLLY_ALWAYS_INLINE hazptr_holder& hazptr_array<M>::operator[](
524 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
534 FOLLY_ALWAYS_INLINE hazptr_local<M>::hazptr_local() {
535 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
537 auto ptc = hazptr_tc_tls();
538 if (LIKELY(ptc != nullptr)) {
540 auto count = tc.count();
547 for (size_t i = 0; i < M; ++i) {
548 auto hprec = tc[i].hprec_;
549 DCHECK(hprec != nullptr);
550 DEBUG_PRINT(i << " " << &h[i]);
551 new (&h[i]) hazptr_holder(nullptr);
552 h[i].hazptr_ = hprec;
554 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
561 need_destruct_ = true;
562 for (size_t i = 0; i < M; ++i) {
563 new (&h[i]) hazptr_holder;
565 i << " " << &h[i] << " " << h[i].domain_ << " " << h[i].hazptr_);
570 FOLLY_ALWAYS_INLINE hazptr_local<M>::~hazptr_local() {
571 if (LIKELY(!need_destruct_)) {
573 auto ptc = hazptr_tc_tls();
574 DCHECK(ptc != nullptr);
582 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
583 for (size_t i = 0; i < M; ++i) {
584 h[i].~hazptr_holder();
589 FOLLY_ALWAYS_INLINE hazptr_holder& hazptr_local<M>::operator[](
591 auto h = reinterpret_cast<hazptr_holder*>(&raw_);
596 ////////////////////////////////////////////////////////////////////////////////
598 // - Control of reclamation (when and by whom)
599 // - End-to-end lock-free implementation
601 /** Definition of default_hazptr_domain() */
603 FOLLY_ALWAYS_INLINE hazptr_domain& default_hazptr_domain() {
604 DEBUG_PRINT(&default_domain_);
605 return default_domain_;
608 template <typename T, typename D>
609 FOLLY_ALWAYS_INLINE void hazptr_retire(T* obj, D reclaim) {
610 default_hazptr_domain().retire(obj, std::move(reclaim));
615 FOLLY_ALWAYS_INLINE void hazptr_rec::set(const void* p) noexcept {
616 DEBUG_PRINT(this << " " << p);
617 hazptr_.store(p, std::memory_order_release);
620 inline const void* hazptr_rec::get() const noexcept {
621 auto p = hazptr_.load(std::memory_order_acquire);
622 DEBUG_PRINT(this << " " << p);
626 FOLLY_ALWAYS_INLINE void hazptr_rec::clear() noexcept {
628 hazptr_.store(nullptr, std::memory_order_release);
631 inline bool hazptr_rec::isActive() noexcept {
632 return active_.load(std::memory_order_acquire);
635 inline bool hazptr_rec::tryAcquire() noexcept {
636 bool active = isActive();
638 active_.compare_exchange_strong(
639 active, true, std::memory_order_release, std::memory_order_relaxed)) {
646 inline void hazptr_rec::release() noexcept {
648 active_.store(false, std::memory_order_release);
653 inline const void* hazptr_obj::getObjPtr() const {
660 template <typename T, typename D>
661 void hazptr_domain::retire(T* obj, D reclaim) {
662 struct hazptr_retire_node : hazptr_obj {
663 std::unique_ptr<T, D> obj_;
665 hazptr_retire_node(T* obj, D reclaim) : obj_{obj, std::move(reclaim)} {}
668 auto node = new hazptr_retire_node(obj, std::move(reclaim));
669 node->reclaim_ = [](hazptr_obj* p) {
670 delete static_cast<hazptr_retire_node*>(p);
675 inline hazptr_domain::~hazptr_domain() {
677 { /* reclaim all remaining retired objects */
679 auto retired = retired_.exchange(nullptr);
681 for (auto p = retired; p; p = next) {
683 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
686 retired = retired_.exchange(nullptr);
689 /* Leak the data for the default domain to avoid destruction order
690 * issues with thread caches.
692 if (this != &default_hazptr_domain()) {
693 /* free all hazptr_rec-s */
695 for (auto p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
697 DCHECK(!p->isActive());
698 mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
703 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
706 for (p = hazptrs_.load(std::memory_order_acquire); p; p = next) {
708 if (p->tryAcquire()) {
712 p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
713 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec));
717 p->active_.store(true, std::memory_order_relaxed);
718 p->next_ = hazptrs_.load(std::memory_order_acquire);
719 while (!hazptrs_.compare_exchange_weak(
720 p->next_, p, std::memory_order_release, std::memory_order_acquire)) {
723 auto hcount = hcount_.fetch_add(1);
724 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
728 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
729 DEBUG_PRINT(this << " " << p);
734 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
735 /*** Full fence ***/ hazptr_mb::light();
736 tail->next_ = retired_.load(std::memory_order_acquire);
737 while (!retired_.compare_exchange_weak(
740 std::memory_order_release,
741 std::memory_order_acquire)) {
743 return rcount_.fetch_add(count) + count;
746 inline bool hazptr_domain::reachedThreshold(int rcount) {
748 rcount >= HAZPTR_SCAN_THRESHOLD &&
749 rcount >= HAZPTR_SCAN_MULT * hcount_.load(std::memory_order_acquire));
752 inline void hazptr_domain::objRetire(hazptr_obj* p) {
753 auto rcount = pushRetired(p, p, 1);
754 if (reachedThreshold(rcount)) {
759 inline void hazptr_domain::tryBulkReclaim() {
762 auto hcount = hcount_.load(std::memory_order_acquire);
763 auto rcount = rcount_.load(std::memory_order_acquire);
764 if (rcount < HAZPTR_SCAN_THRESHOLD || rcount < HAZPTR_SCAN_MULT * hcount) {
767 if (rcount_.compare_exchange_weak(
768 rcount, 0, std::memory_order_release, std::memory_order_relaxed)) {
775 inline void hazptr_domain::bulkReclaim() {
777 /*** Full fence ***/ hazptr_mb::heavy();
778 auto p = retired_.exchange(nullptr, std::memory_order_acquire);
779 auto h = hazptrs_.load(std::memory_order_acquire);
780 std::unordered_set<const void*> hs; // TODO lock-free alternative
781 for (; h; h = h->next_) {
785 hazptr_obj* retired = nullptr;
786 hazptr_obj* tail = nullptr;
788 for (; p; p = next) {
790 if (hs.count(p->getObjPtr()) == 0) {
791 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
796 if (tail == nullptr) {
803 pushRetired(retired, tail, rcount);
817 std::atomic<uint64_t> light_{0};
818 std::atomic<uint64_t> heavy_{0};
819 std::atomic<uint64_t> seq_cst_{0};
822 extern hazptr_stats hazptr_stats_;
824 inline hazptr_stats::~hazptr_stats() {
825 DEBUG_PRINT(this << " light " << light_.load());
826 DEBUG_PRINT(this << " heavy " << heavy_.load());
827 DEBUG_PRINT(this << " seq_cst " << seq_cst_.load());
830 FOLLY_ALWAYS_INLINE void hazptr_stats::light() {
832 /* atomic */ ++light_;
836 inline void hazptr_stats::heavy() {
838 /* atomic */ ++heavy_;
842 inline void hazptr_stats::seq_cst() {
844 /* atomic */ ++seq_cst_;
850 FOLLY_ALWAYS_INLINE void hazptr_mb::light() {
853 folly::asymmetricLightBarrier();
854 INC_HAZPTR_STATS(light);
856 atomic_thread_fence(std::memory_order_seq_cst);
857 INC_HAZPTR_STATS(seq_cst);
861 inline void hazptr_mb::heavy() {
864 folly::asymmetricHeavyBarrier(AMBFlags::EXPEDITED);
865 INC_HAZPTR_STATS(heavy);
867 atomic_thread_fence(std::memory_order_seq_cst);
868 INC_HAZPTR_STATS(seq_cst);
877 * hazptr_tc structures
880 /** hazptr_tc_entry */
882 FOLLY_ALWAYS_INLINE void hazptr_tc_entry::fill(hazptr_rec* hprec) {
884 DEBUG_PRINT(this << " " << hprec);
887 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_entry::get() {
889 DEBUG_PRINT(this << " " << hprec);
893 inline void hazptr_tc_entry::evict() {
896 DEBUG_PRINT(this << " " << hprec);
901 FOLLY_ALWAYS_INLINE hazptr_tc_entry& hazptr_tc::operator[](size_t i) {
902 DCHECK(i <= HAZPTR_TC_SIZE);
906 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc::get() {
907 if (LIKELY(count_ != 0)) {
908 auto hprec = entry_[--count_].get();
909 DEBUG_PRINT(this << " " << hprec);
912 DEBUG_PRINT(this << " nullptr");
916 FOLLY_ALWAYS_INLINE bool hazptr_tc::put(hazptr_rec* hprec) {
917 if (LIKELY(count_ < HAZPTR_TC_SIZE)) {
918 entry_[count_++].fill(hprec);
919 DEBUG_PRINT(this << " " << count_ - 1);
925 FOLLY_ALWAYS_INLINE size_t hazptr_tc::count() {
929 /** hazptr_tc free functions */
931 FOLLY_ALWAYS_INLINE hazptr_tc* hazptr_tc_tls() {
932 DEBUG_PRINT(tls_state_);
933 if (LIKELY(tls_state_ == TLS_ALIVE)) {
934 DEBUG_PRINT(tls_state_);
935 return &tls_tc_data_;
936 } else if (tls_state_ == TLS_UNINITIALIZED) {
938 return &tls_tc_data_;
943 inline void hazptr_tc_init() {
945 auto& tc = tls_tc_data_;
953 inline void hazptr_tc_shutdown() {
954 auto& tc = tls_tc_data_;
956 for (size_t i = 0; i < tc.count_; ++i) {
957 tc.entry_[i].evict();
961 FOLLY_ALWAYS_INLINE hazptr_rec* hazptr_tc_try_get() {
962 DEBUG_PRINT(TLS_UNINITIALIZED << TLS_ALIVE << TLS_DESTROYED);
963 DEBUG_PRINT(tls_state_);
964 if (LIKELY(tls_state_ == TLS_ALIVE)) {
965 DEBUG_PRINT(tls_state_);
966 return tls_tc_data_.get();
967 } else if (tls_state_ == TLS_UNINITIALIZED) {
969 return tls_tc_data_.get();
974 FOLLY_ALWAYS_INLINE bool hazptr_tc_try_put(hazptr_rec* hprec) {
975 DEBUG_PRINT(tls_state_);
976 if (LIKELY(tls_state_ == TLS_ALIVE)) {
977 DEBUG_PRINT(tls_state_);
978 return tls_tc_data_.put(hprec);
987 inline void hazptr_priv::push(hazptr_obj* obj) {
988 auto& domain = default_hazptr_domain();
989 obj->next_ = nullptr;
994 domain.objRetire(obj);
1000 if (++rcount_ >= HAZPTR_PRIV_THRESHOLD) {
1005 inline void hazptr_priv::pushAllToDomain() {
1006 auto& domain = default_hazptr_domain();
1007 domain.pushRetired(head_, tail_, rcount_);
1011 domain.tryBulkReclaim();
1014 inline void hazptr_priv_init() {
1015 auto& priv = tls_priv_data_;
1017 priv.head_ = nullptr;
1018 priv.tail_ = nullptr;
1020 priv.active_ = true;
1023 inline void hazptr_priv_shutdown() {
1024 auto& priv = tls_priv_data_;
1026 DCHECK(priv.active_);
1027 priv.active_ = false;
1029 priv.pushAllToDomain();
1033 inline bool hazptr_priv_try_retire(hazptr_obj* obj) {
1034 DEBUG_PRINT(tls_state_);
1035 if (tls_state_ == TLS_ALIVE) {
1036 DEBUG_PRINT(tls_state_);
1037 tls_priv_data_.push(obj);
1039 } else if (tls_state_ == TLS_UNINITIALIZED) {
1040 DEBUG_PRINT(tls_state_);
1042 tls_priv_data_.push(obj);
1048 /** hazptr_tls_life */
1050 inline void tls_life_odr_use() {
1051 DEBUG_PRINT(tls_state_);
1052 CHECK(tls_state_ == TLS_UNINITIALIZED);
1053 auto volatile tlsOdrUse = &tls_life_;
1054 CHECK(tlsOdrUse != nullptr);
1055 DEBUG_PRINT(tlsOdrUse);
1058 inline hazptr_tls_life::hazptr_tls_life() {
1060 CHECK(tls_state_ == TLS_UNINITIALIZED);
1063 tls_state_ = TLS_ALIVE;
1066 inline hazptr_tls_life::~hazptr_tls_life() {
1068 CHECK(tls_state_ == TLS_ALIVE);
1069 hazptr_tc_shutdown();
1070 hazptr_priv_shutdown();
1071 tls_state_ = TLS_DESTROYED;
1074 } // namespace hazptr
1075 } // namespace folly