1ed1524448a7ec7276fb21b2dd32901f377ed5e2
[folly.git] / folly / experimental / hazptr / hazptr-impl.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
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 template <typename A>
82 inline bool hazptr_owner<T>::try_protect(T*& ptr, const A& src) noexcept {
83   static_assert(
84       std::is_same<decltype(std::declval<A>().load()), T*>::value,
85       "Return type of A::load() must be T*");
86   DEBUG_PRINT(this << " " << ptr << " " << &src);
87   set(ptr);
88   T* p = src.load();
89   if (p != ptr) {
90     ptr = p;
91     clear();
92     return false;
93   }
94   return true;
95 }
96
97 template <typename T>
98 template <typename A>
99 inline T* hazptr_owner<T>::get_protected(const A& src) noexcept {
100   static_assert(
101       std::is_same<decltype(std::declval<A>().load()), T*>::value,
102       "Return type of A::load() must be T*");
103   T* p = src.load();
104   while (!try_protect(p, src)) {}
105   DEBUG_PRINT(this << " " << p << " " << &src);
106   return p;
107 }
108
109 template <typename T>
110 inline void hazptr_owner<T>::set(const T* ptr) noexcept {
111   DEBUG_PRINT(this << " " << ptr);
112   hazptr_->set(ptr);
113 }
114
115 template <typename T>
116 inline void hazptr_owner<T>::clear() noexcept {
117   DEBUG_PRINT(this);
118   hazptr_->clear();
119 }
120
121 template <typename T>
122 inline void hazptr_owner<T>::swap(hazptr_owner<T>& rhs) noexcept {
123   DEBUG_PRINT(
124     this << " " <<  this->hazptr_ << " " << this->domain_ << " -- "
125     << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
126   std::swap(this->domain_, rhs.domain_);
127   std::swap(this->hazptr_, rhs.hazptr_);
128 }
129
130 template <typename T>
131 inline void swap(hazptr_owner<T>& lhs, hazptr_owner<T>& rhs) noexcept {
132   lhs.swap(rhs);
133 }
134
135 ////////////////////////////////////////////////////////////////////////////////
136 // Non-template part of implementation
137 ////////////////////////////////////////////////////////////////////////////////
138 // [TODO]:
139 // - Thread caching of hazptr_rec-s
140 // - Private storage of retired objects
141 // - Control of reclamation (when and by whom)
142 // - Optimized memory order
143
144 /** Definition of default_hazptr_domain() */
145 inline hazptr_domain& default_hazptr_domain() {
146   static hazptr_domain d;
147   DEBUG_PRINT(&d);
148   return d;
149 }
150
151 /** hazptr_rec */
152
153 inline void hazptr_rec::set(const void* p) noexcept {
154   DEBUG_PRINT(this << " " << p);
155   hazptr_.store(p);
156 }
157
158 inline const void* hazptr_rec::get() const noexcept {
159   DEBUG_PRINT(this << " " << hazptr_.load());
160   return hazptr_.load();
161 }
162
163 inline void hazptr_rec::clear() noexcept {
164   DEBUG_PRINT(this);
165   hazptr_.store(nullptr);
166 }
167
168 inline void hazptr_rec::release() noexcept {
169   DEBUG_PRINT(this);
170   clear();
171   active_.store(false);
172 }
173
174 /** hazptr_obj */
175
176 inline const void* hazptr_obj::getObjPtr() const {
177   DEBUG_PRINT(this);
178   return this;
179 }
180
181 /** hazptr_domain */
182
183 inline hazptr_domain::~hazptr_domain() {
184   DEBUG_PRINT(this);
185   { /* free all hazptr_rec-s */
186     hazptr_rec* next;
187     for (auto p = hazptrs_.load(); p; p = next) {
188       next = p->next_;
189       mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
190     }
191   }
192   { /* reclaim all remaining retired objects */
193     hazptr_obj* next;
194     for (auto p = retired_.load(); p; p = next) {
195       next = p->next_;
196       (*(p->reclaim_))(p);
197     }
198   }
199 }
200
201 inline void hazptr_domain::try_reclaim() {
202   DEBUG_PRINT(this);
203   rcount_.exchange(0);
204   bulkReclaim();
205 }
206
207 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
208   hazptr_rec* p;
209   hazptr_rec* next;
210   for (p = hazptrs_.load(); p; p = next) {
211     next = p->next_;
212     bool active = p->active_.load();
213     if (!active) {
214       if (p->active_.compare_exchange_weak(active, true)) {
215         DEBUG_PRINT(this << " " << p);
216         return p;
217       }
218     }
219   }
220   p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
221   if (p == nullptr) {
222     return nullptr;
223   }
224   p->active_.store(true);
225   do {
226     p->next_ = hazptrs_.load();
227     if (hazptrs_.compare_exchange_weak(p->next_, p)) {
228       break;
229     }
230   } while (true);
231   auto hcount = hcount_.fetch_add(1);
232   DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
233   return p;
234 }
235
236 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
237   DEBUG_PRINT(this << " " << p);
238   p->release();
239 }
240
241 inline int
242 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
243   tail->next_ = retired_.load();
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     tryBulkReclaim();
252   }
253 }
254
255 inline void hazptr_domain::tryBulkReclaim() {
256   DEBUG_PRINT(this);
257   do {
258     auto hcount = hcount_.load();
259     auto rcount = rcount_.load();
260     if (rcount < kScanThreshold * hcount) {
261       return;
262     }
263     if (rcount_.compare_exchange_weak(rcount, 0)) {
264       break;
265     }
266   } while (true);
267   bulkReclaim();
268 }
269
270 inline void hazptr_domain::bulkReclaim() {
271   DEBUG_PRINT(this);
272   auto p = retired_.exchange(nullptr);
273   auto h = hazptrs_.load();
274   std::unordered_set<const void*> hs;
275   for (; h; h = h->next_) {
276     hs.insert(h->hazptr_.load());
277   }
278   int rcount = 0;
279   hazptr_obj* retired = nullptr;
280   hazptr_obj* tail = nullptr;
281   hazptr_obj* next;
282   for (; p; p = next) {
283     next = p->next_;
284     if (hs.count(p->getObjPtr()) == 0) {
285       DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
286       (*(p->reclaim_))(p);
287     } else {
288       p->next_ = retired;
289       retired = p;
290       if (tail == nullptr) {
291         tail = p;
292       }
293       ++rcount;
294     }
295   }
296   if (tail) {
297     pushRetired(retired, tail, rcount);
298   }
299 }
300
301 } // namespace folly
302 } // namespace hazptr