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