Commit b403ae13 authored by Dave Watson's avatar Dave Watson Committed by Facebook Github Bot

Remove hazptr_priv_list

Summary: folly::ThreadLocal already maintains this list for us.  Add an accessor in folly::SingletonThreadLocal, and use it

Reviewed By: yfeldblum

Differential Revision: D7264843

fbshipit-source-id: 45278f7a074e50b96b451437c327de5d6713363a
parent 40b40a1d
...@@ -59,7 +59,11 @@ namespace folly { ...@@ -59,7 +59,11 @@ namespace folly {
template < template <
typename T, typename T,
typename Tag = detail::DefaultTag, typename Tag = detail::DefaultTag,
typename Make = detail::DefaultMake<T>> typename Make = detail::DefaultMake<T>,
typename TLTag = _t<std::conditional<
std::is_same<Tag, detail::DefaultTag>::value,
void,
Tag>>>
class SingletonThreadLocal { class SingletonThreadLocal {
private: private:
struct Wrapper; struct Wrapper;
...@@ -123,10 +127,12 @@ class SingletonThreadLocal { ...@@ -123,10 +127,12 @@ class SingletonThreadLocal {
} }
}; };
using WrapperTL = ThreadLocal<Wrapper, TLTag>;
SingletonThreadLocal() = delete; SingletonThreadLocal() = delete;
FOLLY_EXPORT FOLLY_NOINLINE static ThreadLocal<Wrapper>& getWrapperTL() { FOLLY_EXPORT FOLLY_NOINLINE static WrapperTL& getWrapperTL() {
static auto& entry = *detail::createGlobal<ThreadLocal<Wrapper>, Tag>(); static auto& entry = *detail::createGlobal<WrapperTL, Tag>();
return entry; return entry;
} }
...@@ -153,5 +159,10 @@ class SingletonThreadLocal { ...@@ -153,5 +159,10 @@ class SingletonThreadLocal {
return getWrapper(); return getWrapper();
#endif #endif
} }
// Must use a unique Tag, takes a lock that is one per Tag
static typename WrapperTL::Accessor accessAllThreads() {
return getWrapperTL().accessAllThreads();
}
}; };
} // namespace folly } // namespace folly
...@@ -184,7 +184,6 @@ class hazptr_priv { ...@@ -184,7 +184,6 @@ class hazptr_priv {
tail_ = nullptr; tail_ = nullptr;
rcount_ = 0; rcount_ = 0;
active_ = true; active_ = true;
default_hazptr_domain().priv_add(this);
} }
bool active() { bool active() {
...@@ -322,49 +321,6 @@ void hazptr_priv_init(hazptr_priv& priv); ...@@ -322,49 +321,6 @@ void hazptr_priv_init(hazptr_priv& priv);
void hazptr_priv_shutdown(hazptr_priv& priv); void hazptr_priv_shutdown(hazptr_priv& priv);
bool hazptr_priv_try_retire(hazptr_obj* obj); bool hazptr_priv_try_retire(hazptr_obj* obj);
inline void hazptr_priv_list::insert(hazptr_priv* rec) {
std::lock_guard<std::mutex> g(m_);
auto prev = head_ == nullptr ? rec : head_->prev();
auto next = head_ == nullptr ? rec : head_;
rec->set_next(next);
rec->set_prev(prev);
if (head_) {
prev->set_next(rec);
next->set_prev(rec);
} else {
head_ = rec;
}
}
inline void hazptr_priv_list::remove(hazptr_priv* rec) {
std::lock_guard<std::mutex> g(m_);
auto prev = rec->prev();
auto next = rec->next();
if (next == rec) {
DCHECK(prev == rec);
DCHECK(head_ == rec);
head_ = nullptr;
} else {
prev->set_next(next);
next->set_prev(prev);
if (head_ == rec) {
head_ = next;
}
}
}
inline void hazptr_priv_list::collect(hazptr_obj*& head, hazptr_obj*& tail) {
std::lock_guard<std::mutex> g(m_);
auto rec = head_;
while (rec) {
rec->collect(head, tail);
rec = rec->next();
if (rec == head_) {
break;
}
}
}
/** tls globals */ /** tls globals */
struct hazptr_tls_globals_ { struct hazptr_tls_globals_ {
...@@ -386,8 +342,11 @@ struct hazptr_tls_globals_ { ...@@ -386,8 +342,11 @@ struct hazptr_tls_globals_ {
tls_state = TLS_DESTROYED; tls_state = TLS_DESTROYED;
} }
}; };
struct HazptrTag {};
typedef folly::SingletonThreadLocal<hazptr_tls_globals_, HazptrTag> PrivList;
FOLLY_ALWAYS_INLINE hazptr_tls_globals_& hazptr_tls_globals() { FOLLY_ALWAYS_INLINE hazptr_tls_globals_& hazptr_tls_globals() {
return folly::SingletonThreadLocal<hazptr_tls_globals_, void>::get(); return PrivList::get();
} }
/** /**
...@@ -935,7 +894,9 @@ inline hazptr_domain::~hazptr_domain() { ...@@ -935,7 +894,9 @@ inline hazptr_domain::~hazptr_domain() {
inline void hazptr_domain::cleanup() { inline void hazptr_domain::cleanup() {
hazptr_obj* h = nullptr; hazptr_obj* h = nullptr;
hazptr_obj* t = nullptr; hazptr_obj* t = nullptr;
priv_.collect(h, t); for (hazptr_tls_globals_& tls : PrivList::accessAllThreads()) {
tls.priv.collect(h, t);
}
if (h) { if (h) {
DCHECK(t); DCHECK(t);
pushRetired(h, t, 0); pushRetired(h, t, 0);
...@@ -943,14 +904,6 @@ inline void hazptr_domain::cleanup() { ...@@ -943,14 +904,6 @@ inline void hazptr_domain::cleanup() {
bulkReclaim(); bulkReclaim();
} }
inline void hazptr_domain::priv_add(hazptr_priv* rec) {
priv_.insert(rec);
}
inline void hazptr_domain::priv_remove(hazptr_priv* rec) {
priv_.remove(rec);
}
inline hazptr_rec* hazptr_domain::hazptrAcquire() { inline hazptr_rec* hazptr_domain::hazptrAcquire() {
hazptr_rec* p; hazptr_rec* p;
hazptr_rec* next; hazptr_rec* next;
...@@ -1243,7 +1196,6 @@ inline void hazptr_priv_shutdown(hazptr_priv& priv) { ...@@ -1243,7 +1196,6 @@ inline void hazptr_priv_shutdown(hazptr_priv& priv) {
if (!priv.empty()) { if (!priv.empty()) {
priv.push_all_to_domain(); priv.push_all_to_domain();
} }
default_hazptr_domain().priv_remove(&priv);
} }
inline bool hazptr_priv_try_retire(hazptr_obj* obj) { inline bool hazptr_priv_try_retire(hazptr_obj* obj) {
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#define HAZPTR_H #define HAZPTR_H
#include <atomic> #include <atomic>
#include <mutex>
/* Stand-in for C++17 std::pmr::memory_resource */ /* Stand-in for C++17 std::pmr::memory_resource */
#include <folly/experimental/hazptr/memory_resource.h> #include <folly/experimental/hazptr/memory_resource.h>
...@@ -53,16 +52,6 @@ class hazptr_local; ...@@ -53,16 +52,6 @@ class hazptr_local;
/** hazptr_priv: Per-thread list of retired objects pushed in bulk to domain */ /** hazptr_priv: Per-thread list of retired objects pushed in bulk to domain */
class hazptr_priv; class hazptr_priv;
class hazptr_priv_list {
std::mutex m_;
hazptr_priv* head_{nullptr};
public:
void insert(hazptr_priv* rec);
void remove(hazptr_priv* rec);
void collect(hazptr_obj*& head, hazptr_obj*& tail);
};
/** hazptr_domain: Class of hazard pointer domains. Each domain manages a set /** hazptr_domain: Class of hazard pointer domains. Each domain manages a set
* of hazard pointers and a set of retired objects. */ * of hazard pointers and a set of retired objects. */
class hazptr_domain { class hazptr_domain {
...@@ -74,7 +63,6 @@ class hazptr_domain { ...@@ -74,7 +63,6 @@ class hazptr_domain {
* involved in calculations related to the value of rcount_. */ * involved in calculations related to the value of rcount_. */
std::atomic<int> hcount_ = {0}; std::atomic<int> hcount_ = {0};
std::atomic<int> rcount_ = {0}; std::atomic<int> rcount_ = {0};
hazptr_priv_list priv_;
public: public:
constexpr explicit hazptr_domain( constexpr explicit hazptr_domain(
...@@ -90,8 +78,6 @@ class hazptr_domain { ...@@ -90,8 +78,6 @@ class hazptr_domain {
template <typename T, typename D = std::default_delete<T>> template <typename T, typename D = std::default_delete<T>>
void retire(T* obj, D reclaim = {}); void retire(T* obj, D reclaim = {});
void cleanup(); void cleanup();
void priv_add(hazptr_priv* rec);
void priv_remove(hazptr_priv* rec);
private: private:
friend class hazptr_obj_batch; friend class hazptr_obj_batch;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment