proxygen
folly::HazptrSWMRSet< T, Atom > Class Template Reference

#include <HazptrSWMRSet.h>

Classes

struct  Node
 
struct  Reclaimer
 

Public Member Functions

 HazptrSWMRSet ()
 
 ~HazptrSWMRSet ()
 
bool add (T v)
 
bool remove (const T &v)
 
bool contains (const T &val) const
 

Private Member Functions

void locate_lower_bound (const T &v, Atom< Node * > *&prev) const
 

Private Attributes

Atom< Node * > head_ {nullptr}
 

Detailed Description

template<typename T, template< typename > class Atom = std::atomic>
class folly::HazptrSWMRSet< T, Atom >

Set implemented as an ordered singly-linked list.

A single writer thread may add or remove elements. Multiple reader threads may search the set concurrently with each other and with the writer's operations.

Definition at line 31 of file HazptrSWMRSet.h.

Constructor & Destructor Documentation

template<typename T, template< typename > class Atom = std::atomic>
folly::HazptrSWMRSet< T, Atom >::HazptrSWMRSet ( )
inline

Definition at line 49 of file HazptrSWMRSet.h.

49 : head_(nullptr) {}
Atom< Node * > head_
Definition: HazptrSWMRSet.h:46
template<typename T, template< typename > class Atom = std::atomic>
folly::HazptrSWMRSet< T, Atom >::~HazptrSWMRSet ( )
inline

Definition at line 51 of file HazptrSWMRSet.h.

References folly::HazptrSWMRSet< T, Atom >::head_, and cpp.ast::next().

51  {
52  auto p = head_.load();
53  while (p) {
54  auto next = p->next_.load();
55  delete p;
56  p = next;
57  }
58  }
Atom< Node * > head_
Definition: HazptrSWMRSet.h:46
def next(obj)
Definition: ast.py:58

Member Function Documentation

template<typename T, template< typename > class Atom = std::atomic>
bool folly::HazptrSWMRSet< T, Atom >::add ( T  v)
inline

Definition at line 60 of file HazptrSWMRSet.h.

References folly::HazptrSWMRSet< T, Atom >::head_, folly::HazptrSWMRSet< T, Atom >::locate_lower_bound(), and folly::gen::move.

Referenced by swmr_test().

60  {
61  auto prev = &head_;
62  locate_lower_bound(v, prev);
63  auto curr = prev->load(std::memory_order_relaxed);
64  if (curr && curr->elem_ == v) {
65  return false;
66  }
67  prev->store(new Node(std::move(v), curr));
68  return true;
69  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Atom< Node * > head_
Definition: HazptrSWMRSet.h:46
void locate_lower_bound(const T &v, Atom< Node * > *&prev) const
template<typename T, template< typename > class Atom = std::atomic>
bool folly::HazptrSWMRSet< T, Atom >::contains ( const T val) const
inline

Definition at line 88 of file HazptrSWMRSet.h.

References folly::HazptrSWMRSet< T, Atom >::head_, cpp.ast::next(), folly::f14::swap(), and folly::hazptr_holder< Atom >::try_protect().

Referenced by swmr_test().

88  {
89  /* Two hazard pointers for hand-over-hand traversal. */
90  hazptr_local<2, Atom> hptr;
91  hazptr_holder<Atom>* hptr_prev = &hptr[0];
92  hazptr_holder<Atom>* hptr_curr = &hptr[1];
93  while (true) {
94  auto prev = &head_;
95  auto curr = prev->load(std::memory_order_acquire);
96  while (true) {
97  if (!curr) {
98  return false;
99  }
100  if (!hptr_curr->try_protect(curr, *prev)) {
101  break;
102  }
103  auto next = curr->next_.load(std::memory_order_acquire);
104  if (prev->load(std::memory_order_acquire) != curr) {
105  break;
106  }
107  if (curr->elem_ == val) {
108  return true;
109  } else if (!(curr->elem_ < val)) {
110  return false; // because the list is sorted
111  }
112  prev = &(curr->next_);
113  curr = next;
114  std::swap(hptr_curr, hptr_prev);
115  }
116  }
117  }
double val
Definition: String.cpp:273
Atom< Node * > head_
Definition: HazptrSWMRSet.h:46
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
def next(obj)
Definition: ast.py:58
template<typename T, template< typename > class Atom = std::atomic>
void folly::HazptrSWMRSet< T, Atom >::locate_lower_bound ( const T v,
Atom< Node * > *&  prev 
) const
inlineprivate

Definition at line 121 of file HazptrSWMRSet.h.

Referenced by folly::HazptrSWMRSet< T, Atom >::add(), and folly::HazptrSWMRSet< T, Atom >::remove().

121  {
122  auto curr = prev->load(std::memory_order_relaxed);
123  while (curr) {
124  if (curr->elem_ >= v) {
125  break;
126  }
127  prev = &(curr->next_);
128  curr = curr->next_.load(std::memory_order_relaxed);
129  }
130  return;
131  }
template<typename T, template< typename > class Atom = std::atomic>
bool folly::HazptrSWMRSet< T, Atom >::remove ( const T v)
inline

Definition at line 71 of file HazptrSWMRSet.h.

References folly::HazptrSWMRSet< T, Atom >::head_, folly::HazptrSWMRSet< T, Atom >::locate_lower_bound(), and folly::HazptrSWMRSet< T, Atom >::Node::next_.

Referenced by swmr_test().

71  {
72  auto prev = &head_;
73  locate_lower_bound(v, prev);
74  auto curr = prev->load(std::memory_order_relaxed);
75  if (!curr || curr->elem_ != v) {
76  return false;
77  }
78  Node* curr_next = curr->next_.load();
79  // Patch up the actual list...
80  prev->store(curr_next, std::memory_order_release);
81  // ...and only then null out the removed node.
82  curr->next_.store(nullptr, std::memory_order_release);
83  curr->retire();
84  return true;
85  }
Atom< Node * > head_
Definition: HazptrSWMRSet.h:46
void locate_lower_bound(const T &v, Atom< Node * > *&prev) const
Atom< Node< Atom > * > next_
Definition: HazptrTest.cpp:100

Member Data Documentation

template<typename T, template< typename > class Atom = std::atomic>
Atom<Node*> folly::HazptrSWMRSet< T, Atom >::head_ {nullptr}
private

The documentation for this class was generated from the following file: