48e8392ae48676fccd311e667e41e0f986ca2117
[folly.git] / folly / experimental / hazptr / hazptr.h
1 /*
2  * Copyright 2017 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 #define HAZPTR_H
18
19 #include <atomic>
20
21 /* Stand-in for C++17 std::pmr::memory_resource */
22 #include <folly/experimental/hazptr/memory_resource.h>
23
24 namespace folly {
25 namespace hazptr {
26
27 /** hazptr_rec: Private class that contains hazard pointers. */
28 class hazptr_rec;
29
30 /** hazptr_obj: Private class for objects protected by hazard pointers. */
31 class hazptr_obj;
32
33 /** hazptr_obj_base: Base template for objects protected by hazard pointers. */
34 template <typename T, typename Deleter>
35 class hazptr_obj_base;
36
37 /** hazptr_obj_base_refcounted:
38  *  Base template for reference counted objects protected by hazard pointers.
39  */
40 template <typename T, typename Deleter>
41 class hazptr_obj_base_refcounted;
42
43 /** hazptr_local: Optimized template for bulk construction and destruction of
44  *  hazard pointers */
45 template <size_t M>
46 class hazptr_array;
47
48 /** hazptr_local: Optimized template for locally-used hazard pointers */
49 template <size_t M>
50 class hazptr_local;
51
52 /** hazptr_domain: Class of hazard pointer domains. Each domain manages a set
53  *  of hazard pointers and a set of retired objects. */
54 class hazptr_domain {
55  public:
56   constexpr explicit hazptr_domain(
57       memory_resource* = get_default_resource()) noexcept;
58   ~hazptr_domain();
59
60   hazptr_domain(const hazptr_domain&) = delete;
61   hazptr_domain(hazptr_domain&&) = delete;
62   hazptr_domain& operator=(const hazptr_domain&) = delete;
63   hazptr_domain& operator=(hazptr_domain&&) = delete;
64
65   /** Free-function retire.  May allocate memory */
66   template <typename T, typename D = std::default_delete<T>>
67   void retire(T* obj, D reclaim = {});
68
69  private:
70   friend class hazptr_holder;
71   template <typename, typename>
72   friend class hazptr_obj_base;
73   template <typename, typename>
74   friend class hazptr_obj_base_refcounted;
75   friend struct hazptr_priv;
76
77   memory_resource* mr_;
78   std::atomic<hazptr_rec*> hazptrs_ = {nullptr};
79   std::atomic<hazptr_obj*> retired_ = {nullptr};
80   std::atomic<int> hcount_ = {0};
81   std::atomic<int> rcount_ = {0};
82
83   void objRetire(hazptr_obj*);
84   hazptr_rec* hazptrAcquire();
85   void hazptrRelease(hazptr_rec*) noexcept;
86   int pushRetired(hazptr_obj* head, hazptr_obj* tail, int count);
87   bool reachedThreshold(int rcount);
88   void tryBulkReclaim();
89   void bulkReclaim();
90 };
91
92 /** Get the default hazptr_domain */
93 hazptr_domain& default_hazptr_domain();
94
95 extern hazptr_domain default_domain_;
96
97 /** Free-function retire, that operates on the default domain */
98 template <typename T, typename D = std::default_delete<T>>
99 void hazptr_retire(T* obj, D reclaim = {});
100
101 /** Definition of hazptr_obj */
102 class hazptr_obj {
103   friend class hazptr_domain;
104   template <typename, typename>
105   friend class hazptr_obj_base;
106   template <typename, typename>
107   friend class hazptr_obj_base_refcounted;
108   friend struct hazptr_priv;
109
110   void (*reclaim_)(hazptr_obj*);
111   hazptr_obj* next_;
112
113   const void* getObjPtr() const;
114 };
115
116 /** Definition of hazptr_obj_base */
117 template <typename T, typename D = std::default_delete<T>>
118 class hazptr_obj_base : public hazptr_obj {
119  public:
120   /* Retire a removed object and pass the responsibility for
121    * reclaiming it to the hazptr library */
122   void retire(hazptr_domain& domain = default_hazptr_domain(), D reclaim = {});
123
124  private:
125   D deleter_;
126 };
127
128 /** Definition of hazptr_recounted_obj_base */
129 template <typename T, typename D = std::default_delete<T>>
130 class hazptr_obj_base_refcounted : public hazptr_obj {
131  public:
132   /* Retire a removed object and pass the responsibility for
133    * reclaiming it to the hazptr library */
134   void retire(hazptr_domain& domain = default_hazptr_domain(), D reclaim = {});
135
136   /* aquire_ref() increments the reference count
137    *
138    * acquire_ref_safe() is the same as acquire_ref() except that in
139    * addition the caller guarantees that the call is made in a
140    * thread-safe context, e.g., the object is not yet shared. This is
141    * just an optimization to save an atomic operation.
142    *
143    * release_ref() decrements the reference count and returns true if
144    * the object is safe to reclaim.
145    */
146   void acquire_ref();
147   void acquire_ref_safe();
148   bool release_ref();
149
150  private:
151   std::atomic<uint32_t> refcount_{0};
152   D deleter_;
153 };
154
155 /** hazptr_holder: Class for automatic acquisition and release of
156  *  hazard pointers, and interface for hazard pointer operations. */
157 class hazptr_holder {
158   template <size_t M>
159   friend class hazptr_array;
160   template <size_t M>
161   friend class hazptr_local;
162
163  public:
164   /* Constructor automatically acquires a hazard pointer. */
165   explicit hazptr_holder(hazptr_domain& domain = default_hazptr_domain());
166   /* Construct an empty hazptr_holder. */
167   // Note: This diverges from the proposal in P0233R4
168   explicit hazptr_holder(std::nullptr_t) noexcept;
169
170   /* Destructor automatically clears and releases the owned hazard pointer. */
171   ~hazptr_holder();
172
173   hazptr_holder(const hazptr_holder&) = delete;
174   hazptr_holder& operator=(const hazptr_holder&) = delete;
175   // Note: This diverges from the proposal in P0233R4 which disallows
176   // move constructor and assignment operator.
177   hazptr_holder(hazptr_holder&&) noexcept;
178   hazptr_holder& operator=(hazptr_holder&&) noexcept;
179
180   /** Hazard pointer operations */
181   /* Returns a protected pointer from the source */
182   template <typename T>
183   T* get_protected(const std::atomic<T*>& src) noexcept;
184   /* Returns a protected pointer from the source, filtering
185      the protected pointer through function Func.  Useful for
186      stealing bits of the pointer word */
187   template <typename T, typename Func>
188   T* get_protected(const std::atomic<T*>& src, Func f) noexcept;
189   /* Return true if successful in protecting ptr if src == ptr after
190    * setting the hazard pointer.  Otherwise sets ptr to src. */
191   template <typename T>
192   bool try_protect(T*& ptr, const std::atomic<T*>& src) noexcept;
193   /* Return true if successful in protecting ptr if src == ptr after
194    * setting the hazard pointer, filtering the pointer through Func.
195    * Otherwise sets ptr to src. */
196   template <typename T, typename Func>
197   bool try_protect(T*& ptr, const std::atomic<T*>& src, Func f) noexcept;
198   /* Set the hazard pointer to ptr */
199   template <typename T>
200   void reset(const T* ptr) noexcept;
201   /* Set the hazard pointer to nullptr */
202   void reset(std::nullptr_t = nullptr) noexcept;
203
204   /* Swap ownership of hazard pointers between hazptr_holder-s. */
205   /* Note: The owned hazard pointers remain unmodified during the swap
206    * and continue to protect the respective objects that they were
207    * protecting before the swap, if any. */
208   void swap(hazptr_holder&) noexcept;
209
210  private:
211   hazptr_domain* domain_;
212   hazptr_rec* hazptr_;
213 };
214
215 void swap(hazptr_holder&, hazptr_holder&) noexcept;
216
217 using aligned_hazptr_holder = typename std::
218     aligned_storage<sizeof(hazptr_holder), alignof(hazptr_holder)>::type;
219
220 /**
221  *  hazptr_array: Optimized for bulk construction and destruction of
222  *  hazptr_holder-s.
223  *
224  *  WARNING: Do not move from or to individual hazptr_holder-s.
225  *  Only move the whole hazptr_array.
226  */
227 template <size_t M = 1>
228 class hazptr_array {
229   static_assert(M > 0, "M must be a positive integer.");
230
231  public:
232   hazptr_array();
233   explicit hazptr_array(std::nullptr_t) noexcept;
234
235   hazptr_array(const hazptr_array&) = delete;
236   hazptr_array& operator=(const hazptr_array&) = delete;
237   hazptr_array(hazptr_array&& other) noexcept;
238   hazptr_array& operator=(hazptr_array&& other) noexcept;
239
240   ~hazptr_array();
241
242   hazptr_holder& operator[](size_t i) noexcept;
243
244  private:
245   aligned_hazptr_holder raw_[M];
246   bool empty_{false};
247 };
248
249 /**
250  *  hazptr_local: Optimized for construction and destruction of
251  *  one or more hazptr_holder-s with local scope.
252  *
253  *  WARNING 1: Do not move from or to individual hazptr_holder-s.
254  *
255  *  WARNING 2: There can only be one hazptr_local active for the same
256  *  thread at any time. This is not tracked and checked by the
257  *  implementation because it would negate the performance gains of
258  *  this class.
259  */
260 template <size_t M = 1>
261 class hazptr_local {
262   static_assert(M > 0, "M must be a positive integer.");
263
264  public:
265   hazptr_local();
266   hazptr_local(const hazptr_local&) = delete;
267   hazptr_local& operator=(const hazptr_local&) = delete;
268   hazptr_local(hazptr_local&&) = delete;
269   hazptr_local& operator=(hazptr_local&&) = delete;
270
271   ~hazptr_local();
272
273   hazptr_holder& operator[](size_t i) noexcept;
274
275  private:
276   aligned_hazptr_holder raw_[M];
277   bool need_destruct_{false};
278 };
279
280 } // namespace hazptr
281 } // namespace folly
282
283 #include "hazptr-impl.h"