2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* override-include-guard */
19 #error "This should only be included by hazptr.h"
22 #include <folly/experimental/hazptr/debug.h>
24 #include <unordered_set>
31 constexpr hazptr_domain::hazptr_domain(memory_resource* mr) noexcept
34 /** hazptr_obj_base */
36 template <typename T, typename D>
37 inline void hazptr_obj_base<T, D>::retire(
38 hazptr_domain& domain,
40 const storage_policy /* policy */) {
41 DEBUG_PRINT(this << " " << &domain);
42 deleter_ = std::move(deleter);
43 reclaim_ = [](hazptr_obj* p) {
44 auto hobp = static_cast<hazptr_obj_base*>(p);
45 auto obj = static_cast<T*>(hobp);
48 domain.objRetire(this);
54 friend class hazptr_domain;
55 template <typename> friend class hazptr_owner;
57 std::atomic<const void*> hazptr_ = {nullptr};
58 hazptr_rec* next_ = {nullptr};
59 std::atomic<bool> active_ = {false};
61 void set(const void* p) noexcept;
62 const void* get() const noexcept;
63 void clear() noexcept;
64 void release() noexcept;
70 inline hazptr_owner<T>::hazptr_owner(
71 hazptr_domain& domain,
72 const cache_policy /* policy */) {
74 hazptr_ = domain_->hazptrAcquire();
75 DEBUG_PRINT(this << " " << domain_ << " " << hazptr_);
76 if (hazptr_ == nullptr) { std::bad_alloc e; throw e; }
80 hazptr_owner<T>::~hazptr_owner() {
82 domain_->hazptrRelease(hazptr_);
86 inline bool hazptr_owner<T>::try_protect(
88 const std::atomic<T*>& src) noexcept {
89 DEBUG_PRINT(this << " " << ptr << " " << &src);
100 template <typename T>
101 inline T* hazptr_owner<T>::get_protected(const std::atomic<T*>& src) noexcept {
103 while (!try_protect(p, src)) {}
104 DEBUG_PRINT(this << " " << p << " " << &src);
108 template <typename T>
109 inline void hazptr_owner<T>::set(const T* ptr) noexcept {
110 DEBUG_PRINT(this << " " << ptr);
114 template <typename T>
115 inline void hazptr_owner<T>::clear() noexcept {
120 template <typename T>
121 inline void hazptr_owner<T>::swap(hazptr_owner<T>& rhs) noexcept {
123 this << " " << this->hazptr_ << " " << this->domain_ << " -- "
124 << &rhs << " " << rhs.hazptr_ << " " << rhs.domain_);
125 std::swap(this->domain_, rhs.domain_);
126 std::swap(this->hazptr_, rhs.hazptr_);
129 template <typename T>
130 inline void swap(hazptr_owner<T>& lhs, hazptr_owner<T>& rhs) noexcept {
134 ////////////////////////////////////////////////////////////////////////////////
135 // Non-template part of implementation
136 ////////////////////////////////////////////////////////////////////////////////
138 // - Thread caching of hazptr_rec-s
139 // - Private storage of retired objects
140 // - Optimized memory order
142 /** Definition of default_hazptr_domain() */
143 inline hazptr_domain& default_hazptr_domain() {
144 static hazptr_domain d;
150 inline void hazptr_rec::set(const void* p) noexcept {
151 DEBUG_PRINT(this << " " << p);
155 inline const void* hazptr_rec::get() const noexcept {
156 DEBUG_PRINT(this << " " << hazptr_.load());
157 return hazptr_.load();
160 inline void hazptr_rec::clear() noexcept {
162 hazptr_.store(nullptr);
165 inline void hazptr_rec::release() noexcept {
168 active_.store(false);
173 inline const void* hazptr_obj::getObjPtr() const {
179 inline hazptr_domain::~hazptr_domain() {
181 { /* free all hazptr_rec-s */
183 for (auto p = hazptrs_.load(); p; p = next) {
185 mr_->deallocate(static_cast<void*>(p), sizeof(hazptr_rec));
188 { /* reclaim all remaining retired objects */
190 for (auto p = retired_.load(); p; p = next) {
197 inline void hazptr_domain::try_reclaim() {
203 inline hazptr_rec* hazptr_domain::hazptrAcquire() {
206 for (p = hazptrs_.load(); p; p = next) {
208 bool active = p->active_.load();
210 if (p->active_.compare_exchange_weak(active, true)) {
211 DEBUG_PRINT(this << " " << p);
216 p = static_cast<hazptr_rec*>(mr_->allocate(sizeof(hazptr_rec)));
220 p->active_.store(true);
222 p->next_ = hazptrs_.load();
223 if (hazptrs_.compare_exchange_weak(p->next_, p)) {
227 auto hcount = hcount_.fetch_add(1);
228 DEBUG_PRINT(this << " " << p << " " << sizeof(hazptr_rec) << " " << hcount);
232 inline void hazptr_domain::hazptrRelease(hazptr_rec* p) noexcept {
233 DEBUG_PRINT(this << " " << p);
238 hazptr_domain::pushRetired(hazptr_obj* head, hazptr_obj* tail, int count) {
239 tail->next_ = retired_.load();
240 while (!retired_.compare_exchange_weak(tail->next_, head)) {}
241 return rcount_.fetch_add(count);
244 inline void hazptr_domain::objRetire(hazptr_obj* p) {
245 auto rcount = pushRetired(p, p, 1) + 1;
246 if (rcount >= kScanThreshold * hcount_.load()) {
251 inline void hazptr_domain::tryBulkReclaim() {
254 auto hcount = hcount_.load();
255 auto rcount = rcount_.load();
256 if (rcount < kScanThreshold * hcount) {
259 if (rcount_.compare_exchange_weak(rcount, 0)) {
266 inline void hazptr_domain::bulkReclaim() {
268 auto p = retired_.exchange(nullptr);
269 auto h = hazptrs_.load();
270 std::unordered_set<const void*> hs;
271 for (; h; h = h->next_) {
272 hs.insert(h->hazptr_.load());
275 hazptr_obj* retired = nullptr;
276 hazptr_obj* tail = nullptr;
278 for (; p; p = next) {
280 if (hs.count(p->getObjPtr()) == 0) {
281 DEBUG_PRINT(this << " " << p << " " << p->reclaim_);
286 if (tail == nullptr) {
293 pushRetired(retired, tail, rcount);
299 inline void hazptr_user::flush() {
303 /** hazptr_remover */
305 inline void hazptr_remover::flush() {
310 } // namespace hazptr