proxygen
HazptrWideCAS.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 #include <string>
21 
22 namespace folly {
23 
26 template <typename T, template <typename> class Atom = std::atomic>
28  struct Node : public hazptr_obj_base<Node, Atom> {
30  explicit Node(T v = {}) : val_(v) {}
31  };
32 
33  Atom<Node*> node_;
34 
35  public:
36  HazptrWideCAS() : node_(new Node()) {}
37 
39  delete node_.load(std::memory_order_relaxed);
40  }
41 
42  bool cas(T& u, T& v) {
43  Node* n = new Node(v);
45  Node* p;
46  while (true) {
47  p = hptr.get_protected(node_);
48  if (p->val_ != u) {
49  delete n;
50  return false;
51  }
52  if (node_.compare_exchange_weak(
53  p, n, std::memory_order_relaxed, std::memory_order_release)) {
54  break;
55  }
56  }
57  hptr.reset();
58  p->retire();
59  return true;
60  }
61 };
62 
63 } // namespace folly
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
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
bool cas(T &u, T &v)
Definition: HazptrWideCAS.h:42
#define Atom
Atom< Node * > node_
Definition: HazptrWideCAS.h:33
FOLLY_ALWAYS_INLINE void reset(const T *ptr) noexcept
Definition: HazptrHolder.h:153