Update hazard pointers interface and implementation
[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 /** hazptr_obj_base */
35
36 template <typename T, typename D>
37 inline void hazptr_obj_base<T, D>::retire(hazptr_domain& domain, D deleter) {
38   DEBUG_PRINT(this << " " << &domain);
39   deleter_ = std::move(deleter);
40   reclaim_ = [](hazptr_obj* p) {
41     auto hobp = static_cast<hazptr_obj_base*>(p);
42     auto obj = static_cast<T*>(hobp);
43     hobp->deleter_(obj);
44   };
45   domain.objRetire(this);
46 }
47
48 /** hazptr_rec */
49
50 class hazptr_rec {
51   friend class hazptr_domain;
52   template <typename> friend class hazptr_owner;
53
54   std::atomic<const void*> hazptr_ = {nullptr};
55   hazptr_rec* next_ = {nullptr};
56   std::atomic<bool> active_ = {false};
57
58   void set(const void* p) noexcept;
59   const void* get() const noexcept;
60   void clear() noexcept;
61   void release() noexcept;
62 };
63
64 /** hazptr_owner */
65
66 template <typename T>
67 inline hazptr_owner<T>::hazptr_owner(hazptr_domain& domain) {
68   domain_ = &domain;
69   hazptr_ = domain_->hazptrAcquire();
70   DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
71   if (hazptr_ == nullptr) { std::bad_alloc e; throw e; }
72 }
73
74 template <typename T>
75 hazptr_owner<T>::~hazptr_owner() {
76   DEBUG_PRINT(this);
77   domain_->hazptrRelease(hazptr_);
78 }
79
80 template <typename T>
81 inline bool hazptr_owner<T>::try_protect(
82     T*& ptr,
83     const std::atomic<T*>& src) noexcept {
84   DEBUG_PRINT(this << " " << ptr << " " << &src);
85   set(ptr);
86   T* p = src.load();
87   if (p != ptr) {
88     ptr = p;
89     clear();
90     return false;
91   }
92   return true;
93 }
94
95 template <typename T>
96 inline T* hazptr_owner<T>::get_protected(const std::atomic<T*>& src) noexcept {
97   T* p = src.load();
98   while (!try_protect(p, src)) {}
99   DEBUG_PRINT(this << " " << p << " " << &src);
100   return p;
101 }
102
103 template <typename T>
104 inline void hazptr_owner<T>::set(const T* ptr) noexcept {
105   DEBUG_PRINT(this << " " << ptr);
106   hazptr_->set(ptr);
107 }
108
109 template <typename T>
110 inline void hazptr_owner<T>::clear() noexcept {
111   DEBUG_PRINT(this);
112   hazptr_->clear();
113 }
114
115 template <typename T>
116 inline void hazptr_owner<T>::swap(hazptr_owner<T>& rhs) noexcept {
117   DEBUG_PRINT(
118     this << " " <<  this->hazptr_ << " " << this->domain_ << " -- "
119     << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
120   std::swap(this->domain_, rhs.domain_);
121   std::swap(this->hazptr_, rhs.hazptr_);
122 }
123
124 template <typename T>
125 inline void swap(hazptr_owner<T>& lhs, hazptr_owner<T>& rhs) noexcept {
126   lhs.swap(rhs);
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
130 // Non-template part of implementation
131 ////////////////////////////////////////////////////////////////////////////////
132 // [TODO]:
133 // - Thread caching of hazptr_rec-s
134 // - Private storage of retired objects
135 // - Control of reclamation (when and by whom)
136 // - Optimized memory order
137
138 /** Definition of default_hazptr_domain() */
139 inline hazptr_domain& default_hazptr_domain() {
140   static hazptr_domain d;
141   DEBUG_PRINT(&d);
142   return d;
143 }
144
145 /** hazptr_rec */
146
147 inline void hazptr_rec::set(const void* p) noexcept {
148   DEBUG_PRINT(this << " " << p);
149   hazptr_.store(p);
150 }
151
152 inline const void* hazptr_rec::get() const noexcept {
153   DEBUG_PRINT(this << " " << hazptr_.load());
154   return hazptr_.load();
155 }
156
157 inline void hazptr_rec::clear() noexcept {
158   DEBUG_PRINT(this);
159   hazptr_.store(nullptr);
160 }
161
162 inline void hazptr_rec::release() noexcept {
163   DEBUG_PRINT(this);
164   clear();
165   active_.store(false);
166 }
167
168 /** hazptr_obj */
169
170 inline const void* hazptr_obj::getObjPtr() const {
171   DEBUG_PRINT(this);
172   return this;
173 }
174
175 /** hazptr_domain */
176
177 inline hazptr_domain::~hazptr_domain() {
178   DEBUG_PRINT(this);
179   { /* free all hazptr_rec-s */
180     hazptr_rec* next;
181     for (auto p = hazptrs_.load(); p; p = next) {
182       next = p->next_;
183       mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
184     }
185   }
186   { /* reclaim all remaining retired objects */
187     hazptr_obj* next;
188     for (auto p = retired_.load(); p; p = next) {
189       next = p->next_;
190       (*(p->reclaim_))(p);
191     }
192   }
193 }
194
195 inline void hazptr_domain::try_reclaim() {
196   DEBUG_PRINT(this);
197   rcount_.exchange(0);
198   bulkReclaim();
199 }
200
201 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
202   hazptr_rec* p;
203   hazptr_rec* next;
204   for (p = hazptrs_.load(); p; p = next) {
205     next = p->next_;
206     bool active = p->active_.load();
207     if (!active) {
208       if (p->active_.compare_exchange_weak(active, true)) {
209         DEBUG_PRINT(this << " " << p);
210         return p;
211       }
212     }
213   }
214   p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
215   if (p == nullptr) {
216     return nullptr;
217   }
218   p->active_.store(true);
219   do {
220     p->next_ = hazptrs_.load();
221     if (hazptrs_.compare_exchange_weak(p->next_, p)) {
222       break;
223     }
224   } while (true);
225   auto hcount = hcount_.fetch_add(1);
226   DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
227   return p;
228 }
229
230 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
231   DEBUG_PRINT(this << " " << p);
232   p->release();
233 }
234
235 inline int
236 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
237   tail->next_ = retired_.load();
238   while (!retired_.compare_exchange_weak(tail->next_, head)) {}
239   return rcount_.fetch_add(count);
240 }
241
242 inline void hazptr_domain::objRetire(hazptr_obj* p) {
243   auto rcount = pushRetired(p, p, 1) + 1;
244   if (rcount >= kScanThreshold * hcount_.load()) {
245     tryBulkReclaim();
246   }
247 }
248
249 inline void hazptr_domain::tryBulkReclaim() {
250   DEBUG_PRINT(this);
251   do {
252     auto hcount = hcount_.load();
253     auto rcount = rcount_.load();
254     if (rcount < kScanThreshold * hcount) {
255       return;
256     }
257     if (rcount_.compare_exchange_weak(rcount, 0)) {
258       break;
259     }
260   } while (true);
261   bulkReclaim();
262 }
263
264 inline void hazptr_domain::bulkReclaim() {
265   DEBUG_PRINT(this);
266   auto p = retired_.exchange(nullptr);
267   auto h = hazptrs_.load();
268   std::unordered_set<const void*> hs;
269   for (; h; h = h->next_) {
270     hs.insert(h->hazptr_.load());
271   }
272   int rcount = 0;
273   hazptr_obj* retired = nullptr;
274   hazptr_obj* tail = nullptr;
275   hazptr_obj* next;
276   for (; p; p = next) {
277     next = p->next_;
278     if (hs.count(p->getObjPtr()) == 0) {
279       DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
280       (*(p->reclaim_))(p);
281     } else {
282       p->next_ = retired;
283       retired = p;
284       if (tail == nullptr) {
285         tail = p;
286       }
287       ++rcount;
288     }
289   }
290   if (tail) {
291     pushRetired(retired, tail, rcount);
292   }
293 }
294
295 } // namespace folly
296 } // namespace hazptr