proxygen
HazptrLockFreeLIFO.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #pragma once
17 
19 
20 namespace folly {
21 
22 template <typename T, template <typename> class Atom = std::atomic>
24  struct Node;
25 
26  Atom<Node*> head_;
27 
28  public:
30 
32  Node* next;
33  for (auto node = head(); node; node = next) {
34  next = node->next();
35  node->retire();
36  }
37  hazptr_cleanup<Atom>();
38  }
39 
40  void push(T val) {
41  auto node = new Node(val, head());
42  while (!cas_head(node->next_, node)) {
43  /* try again */;
44  }
45  }
46 
47  bool pop(T& val) {
49  hazptr_holder<Atom>& hptr = h[0];
50  Node* node;
51  while (true) {
52  node = hptr.get_protected(head_);
53  if (node == nullptr) {
54  return false;
55  }
56  auto next = node->next();
57  if (cas_head(node, next)) {
58  break;
59  }
60  }
61  hptr.reset();
62  val = node->value();
63  node->retire();
64  return true;
65  }
66 
67  private:
68  Node* head() {
69  return head_.load(std::memory_order_acquire);
70  }
71 
72  bool cas_head(Node*& expected, Node* newval) {
73  return head_.compare_exchange_weak(
74  expected, newval, std::memory_order_acq_rel, std::memory_order_acquire);
75  }
76 
77  struct Node : public hazptr_obj_base<Node, Atom> {
80 
81  Node(T v, Node* n) : value_(v), next_(n) {}
82 
83  Node* next() {
84  return next_;
85  }
86 
87  T value() {
88  return value_;
89  }
90  };
91 };
92 
93 } // namespace folly
bool cas_head(Node *&expected, Node *newval)
*than *hazptr_holder h
Definition: Hazptr.h:116
FOLLY_ALWAYS_INLINE T * get_protected(const Atom< T * > &src) noexcept
Definition: HazptrHolder.h:138
void retire(D deleter={}, hazptr_domain< Atom > &domain=default_hazptr_domain< Atom >())
Definition: HazptrObj.h:229
double val
Definition: String.cpp:273
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define nullptr
Definition: http_parser.c:41
#define Atom
Node(int v=0, Node *n=nullptr, bool=false) noexcept
Definition: HazptrTest.cpp:103
FOLLY_ALWAYS_INLINE void reset(const T *ptr) noexcept
Definition: HazptrHolder.h:153
def next(obj)
Definition: ast.py:58