2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
2019-07-06 13:29:15 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class T, int MaxPointersN = 1, class Deleter = std::default_delete<T>>
|
2018-12-31 20:04:05 +01:00
|
|
|
class HazardPointers {
|
|
|
|
public:
|
|
|
|
explicit HazardPointers(size_t threads_n) : threads_(threads_n) {
|
|
|
|
for (auto &data : threads_) {
|
2019-07-21 20:07:07 +02:00
|
|
|
for (auto &ptr : data.hazard_) {
|
2019-02-13 01:52:34 +01:00
|
|
|
// workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64658
|
2019-02-13 01:51:39 +01:00
|
|
|
#if TD_GCC && GCC_VERSION <= 40902
|
|
|
|
ptr = nullptr;
|
|
|
|
#else
|
2019-01-24 15:01:02 +01:00
|
|
|
std::atomic_init(&ptr, static_cast<T *>(nullptr));
|
2019-02-13 01:51:39 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HazardPointers(const HazardPointers &other) = delete;
|
|
|
|
HazardPointers &operator=(const HazardPointers &other) = delete;
|
|
|
|
HazardPointers(HazardPointers &&other) = delete;
|
|
|
|
HazardPointers &operator=(HazardPointers &&other) = delete;
|
|
|
|
|
|
|
|
class Holder {
|
|
|
|
public:
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class S>
|
|
|
|
S *protect(std::atomic<S *> &to_protect) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return do_protect(hazard_ptr_, to_protect);
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
Holder(HazardPointers &hp, size_t thread_id, size_t pos) : Holder(hp.get_hazard_ptr(thread_id, pos)) {
|
|
|
|
CHECK(hazard_ptr_.load() == 0);
|
|
|
|
hazard_ptr_.store(reinterpret_cast<T *>(1));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
Holder(const Holder &other) = delete;
|
|
|
|
Holder &operator=(const Holder &other) = delete;
|
2019-07-06 13:29:15 +02:00
|
|
|
Holder(Holder &&other) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
Holder &operator=(Holder &&other) = delete;
|
|
|
|
~Holder() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
hazard_ptr_.store(nullptr, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class HazardPointers;
|
|
|
|
explicit Holder(std::atomic<T *> &ptr) : hazard_ptr_(ptr) {
|
|
|
|
}
|
|
|
|
std::atomic<T *> &hazard_ptr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void retire(size_t thread_id, T *ptr = nullptr) {
|
|
|
|
CHECK(thread_id < threads_.size());
|
|
|
|
auto &data = threads_[thread_id];
|
|
|
|
if (ptr) {
|
2019-07-21 20:07:07 +02:00
|
|
|
data.to_delete_.push_back(std::unique_ptr<T, Deleter>(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
for (auto it = data.to_delete_.begin(); it != data.to_delete_.end();) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_protected(it->get())) {
|
|
|
|
it->reset();
|
2019-07-21 20:07:07 +02:00
|
|
|
it = data.to_delete_.erase(it);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// old inteface
|
|
|
|
T *protect(size_t thread_id, size_t pos, std::atomic<T *> &ptr) {
|
|
|
|
return do_protect(get_hazard_ptr(thread_id, pos), ptr);
|
|
|
|
}
|
|
|
|
void clear(size_t thread_id, size_t pos) {
|
|
|
|
do_clear(get_hazard_ptr(thread_id, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t to_delete_size_unsafe() const {
|
|
|
|
size_t res = 0;
|
|
|
|
for (auto &thread : threads_) {
|
2019-07-21 20:07:07 +02:00
|
|
|
res += thread.to_delete_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct ThreadData {
|
2019-07-21 20:07:07 +02:00
|
|
|
std::array<std::atomic<T *>, MaxPointersN> hazard_;
|
|
|
|
char pad[TD_CONCURRENCY_PAD - sizeof(hazard_)];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// stupid gc
|
2019-07-21 20:07:07 +02:00
|
|
|
std::vector<std::unique_ptr<T, Deleter>> to_delete_;
|
|
|
|
char pad2[TD_CONCURRENCY_PAD - sizeof(to_delete_)];
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
std::vector<ThreadData> threads_;
|
2019-07-06 13:29:15 +02:00
|
|
|
char pad2[TD_CONCURRENCY_PAD - sizeof(threads_)];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class S>
|
|
|
|
static S *do_protect(std::atomic<T *> &hazard_ptr, std::atomic<S *> &to_protect) {
|
2018-12-31 20:04:05 +01:00
|
|
|
T *saved = nullptr;
|
|
|
|
T *to_save;
|
|
|
|
while ((to_save = to_protect.load()) != saved) {
|
|
|
|
hazard_ptr.store(to_save);
|
|
|
|
saved = to_save;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
return static_cast<S *>(saved);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_clear(std::atomic<T *> &hazard_ptr) {
|
|
|
|
hazard_ptr.store(nullptr, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_protected(T *ptr) {
|
|
|
|
for (auto &thread : threads_) {
|
2019-07-21 20:07:07 +02:00
|
|
|
for (auto &hazard_ptr : thread.hazard_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (hazard_ptr.load() == ptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atomic<T *> &get_hazard_ptr(size_t thread_id, size_t pos) {
|
2018-07-17 01:46:44 +02:00
|
|
|
CHECK(thread_id < threads_.size());
|
2019-07-21 20:07:07 +02:00
|
|
|
return threads_[thread_id].hazard_[pos];
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|