Draft prototype of hazard pointers C++ template library
[folly.git] / folly / experimental / hazptr / hazptr-impl.h
1 /*
2  * Copyright 2016 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
17 /* override-include-guard */
18 #ifndef HAZPTR_H
19 #error "This should only be included by hazptr.h"
20 #endif
21
22 #include <folly/experimental/hazptr/debug.h>
23
24 #include <unordered_set>
25
26 namespace folly {
27 namespace hazptr {
28
29 /** hazptr_domain */
30
31 constexpr hazptr_domain::hazptr_domain(memory_resource* mr) noexcept
32     : mr_(mr) {}
33
34 template <typename T>
35 void hazptr_domain::flush(const hazptr_obj_reclaim<T>* reclaim) {
36   DEBUG_PRINT(this << " " << reclaim);
37   flush(reinterpret_cast<const hazptr_obj_reclaim<void>*>(reclaim));
38 }
39
40 template <typename T>
41 inline void hazptr_domain::objRetire(hazptr_obj_base<T>* p) {
42   DEBUG_PRINT(this << " " << p);
43   objRetire(reinterpret_cast<hazptr_obj_base<void>*>(p));
44 }
45
46 /** hazptr_obj_base */
47
48 template <typename T>
49 inline void hazptr_obj_base<T>::retire(
50     hazptr_domain* domain,
51     const hazptr_obj_reclaim<T>* reclaim,
52     const storage_policy /* policy */) {
53   DEBUG_PRINT(this << " " << reclaim << " " << &domain);
54   reclaim_ = reclaim;
55   domain->objRetire<T>(this);
56 }
57
58 /* Definition of default_hazptr_obj_reclaim */
59
60 template <typename T>
61 inline hazptr_obj_reclaim<T>* default_hazptr_obj_reclaim() {
62   static hazptr_obj_reclaim<T> fn = [](T* p) {
63     DEBUG_PRINT("default_hazptr_obj_reclaim " << p << " " << sizeof(T));
64     delete p;
65   };
66   DEBUG_PRINT(&fn);
67   return &fn;
68 }
69
70 /** hazptr_rec */
71
72 class hazptr_rec {
73   friend class hazptr_domain;
74   template <typename> friend class hazptr_owner;
75
76   std::atomic<const void*> hazptr_ = {nullptr};
77   hazptr_rec* next_ = {nullptr};
78   std::atomic<bool> active_ = {false};
79
80   void set(const void* p) noexcept;
81   const void* get() const noexcept;
82   void clear() noexcept;
83   void release() noexcept;
84 };
85
86 /** hazptr_owner */
87
88 template <typename T>
89 inline hazptr_owner<T>::hazptr_owner(
90     hazptr_domain* domain,
91     const cache_policy /* policy */) {
92   domain_ = domain;
93   hazptr_ = domain_->hazptrAcquire();
94   DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
95   if (hazptr_ == nullptr) { std::bad_alloc e; throw e; }
96 }
97
98 template <typename T>
99 hazptr_owner<T>::~hazptr_owner() noexcept {
100   DEBUG_PRINT(this);
101   domain_->hazptrRelease(hazptr_);
102 }
103
104 template <typename T>
105 inline bool hazptr_owner<T>::protect(const T* ptr, const std::atomic<T*>& src)
106     const noexcept {
107   DEBUG_PRINT(this << " " << ptr << " " << &src);
108   hazptr_->set(ptr);
109   // ORDER: store-load
110   return (src.load() == ptr);
111 }
112
113 template <typename T>
114 inline void hazptr_owner<T>::set(const T* ptr) const noexcept {
115   DEBUG_PRINT(this << " " << ptr);
116   hazptr_->set(ptr);
117 }
118
119 template <typename T>
120 inline void hazptr_owner<T>::clear() const noexcept {
121   DEBUG_PRINT(this);
122   hazptr_->clear();
123 }
124
125 template <typename T>
126 inline void swap(hazptr_owner<T>& lhs, hazptr_owner<T>& rhs) noexcept {
127   DEBUG_PRINT(
128     &lhs << " " <<  lhs.hazptr_ << " " << lhs.domain_ << " -- "
129     << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
130   std::swap(lhs.domain_, rhs.domain_);
131   std::swap(lhs.hazptr_, rhs.hazptr_);
132 }
133
134 ////////////////////////////////////////////////////////////////////////////////
135 // Non-template part of implementation
136 ////////////////////////////////////////////////////////////////////////////////
137 // [TODO]:
138 // - Thread caching of hazptr_rec-s
139 // - Private storage of retired objects
140 // - Optimized memory order
141
142 /** Definition of default_hazptr_domain() */
143 inline hazptr_domain* default_hazptr_domain() {
144   static hazptr_domain d;
145   return &d;
146 }
147
148 /** hazptr_rec */
149
150 inline void hazptr_rec::set(const void* p) noexcept {
151   DEBUG_PRINT(this << " " << p);
152   hazptr_.store(p);
153 }
154
155 inline const void* hazptr_rec::get() const noexcept {
156   DEBUG_PRINT(this << " " << hazptr_.load());
157   return hazptr_.load();
158 }
159
160 inline void hazptr_rec::clear() noexcept {
161   DEBUG_PRINT(this);
162   // ORDER: release
163   hazptr_.store(nullptr);
164 }
165
166 inline void hazptr_rec::release() noexcept {
167   DEBUG_PRINT(this);
168   clear();
169   // ORDER: release
170   active_.store(false);
171 }
172
173 /** hazptr_domain */
174
175 inline hazptr_domain::~hazptr_domain() {
176   DEBUG_PRINT(this);
177   { /* free all hazptr_rec-s */
178     hazptr_rec* next;
179     for (auto p = hazptrs_.load(); p; p = next) {
180       next = p->next_;
181       mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
182     }
183   }
184   { /* reclaim all remaining retired objects */
185     hazptr_obj* next;
186     for (auto p = retired_.load(); p; p = next) {
187       next = p->next_;
188       (*(p->reclaim_))(p);
189     }
190   }
191 }
192
193 inline void hazptr_domain::flush() {
194   DEBUG_PRINT(this);
195   auto rcount = rcount_.exchange(0);
196   auto p = retired_.exchange(nullptr);
197   hazptr_obj* next;
198   for (; p; p = next) {
199     next = p->next_;
200     (*(p->reclaim_))(p);
201     --rcount;
202   }
203   rcount_.fetch_add(rcount);
204 }
205
206 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
207   hazptr_rec* p;
208   hazptr_rec* next;
209   for (p = hazptrs_.load(); p; p = next) {
210     next = p->next_;
211     bool active = p->active_.load();
212     if (!active) {
213       if (p->active_.compare_exchange_weak(active, true)) {
214         DEBUG_PRINT(this << " " << p);
215         return p;
216       }
217     }
218   }
219   p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
220   if (p == nullptr) {
221     return nullptr;
222   }
223   p->active_.store(true);
224   do {
225     p->next_ = hazptrs_.load();
226     if (hazptrs_.compare_exchange_weak(p->next_, p)) {
227       break;
228     }
229   } while (true);
230   auto hcount = hcount_.fetch_add(1);
231   DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
232   return p;
233 }
234
235 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) const noexcept {
236   DEBUG_PRINT(this << " " << p);
237   p->release();
238 }
239
240 inline int
241 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
242   tail->next_ = retired_.load();
243   // ORDER: store-store order
244   while (!retired_.compare_exchange_weak(tail->next_, head)) {}
245   return rcount_.fetch_add(count);
246 }
247
248 inline void hazptr_domain::objRetire(hazptr_obj* p) {
249   auto rcount = pushRetired(p, p, 1) + 1;
250   if (rcount >= kScanThreshold * hcount_.load()) {
251     bulkReclaim();
252   }
253 }
254
255 inline void hazptr_domain::bulkReclaim() {
256   DEBUG_PRINT(this);
257   auto h = hazptrs_.load();
258   auto hcount = hcount_.load();
259   auto rcount = rcount_.load();
260   do {
261     if (rcount < kScanThreshold * hcount) {
262       return;
263     }
264     if (rcount_.compare_exchange_weak(rcount, 0)) {
265       break;
266     }
267   } while (true);
268   /* ORDER: store-load order between removing each object and scanning
269    * the hazard pointers -- can be combined in one fence */
270   std::unordered_set<const void*> hs;
271   for (; h; h = h->next_) {
272     hs.insert(h->hazptr_.load());
273   }
274   rcount = 0;
275   hazptr_obj* retired = nullptr;
276   hazptr_obj* tail = nullptr;
277   auto p = retired_.exchange(nullptr);
278   hazptr_obj* next;
279   for (; p; p = next) {
280     next = p->next_;
281     if (hs.count(p) == 0) {
282       (*(p->reclaim_))(p);
283     } else {
284       p->next_ = retired;
285       retired = p;
286       if (tail == nullptr) {
287         tail = p;
288       }
289       ++rcount;
290     }
291   }
292   if (tail) {
293     pushRetired(retired, tail, rcount);
294   }
295 }
296
297 inline void hazptr_domain::flush(const hazptr_obj_reclaim<void>* reclaim) {
298   DEBUG_PRINT(this << " " << reclaim);
299   auto rcount = rcount_.exchange(0);
300   auto p = retired_.exchange(nullptr);
301   hazptr_obj* retired = nullptr;
302   hazptr_obj* tail = nullptr;
303   hazptr_obj* next;
304   for (; p; p = next) {
305     next = p->next_;
306     if (p->reclaim_ == reclaim) {
307       (*reclaim)(p);
308     } else {
309       p->next_ = retired;
310       retired = p;
311       if (tail == nullptr) {
312         tail = p;
313       }
314       ++rcount;
315     }
316   }
317   if (tail) {
318     pushRetired(retired, tail, rcount);
319   }
320 }
321
322 /** hazptr_user */
323
324 inline void hazptr_user::flush() {
325   DEBUG_PRINT("");
326 }
327
328 /** hazptr_remover */
329
330 inline void hazptr_remover::flush() {
331   DEBUG_PRINT("");
332 }
333
334 } // namespace folly
335 } // namespace hazptr