2019-07-07 02:05:30 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-07-07 02:05:30 +02: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)
|
|
|
|
//
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/common.h"
|
2019-07-07 02:05:30 +02:00
|
|
|
#include "td/utils/EpochBasedMemoryReclamation.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
|
|
|
TEST(EpochBaseMemoryReclamation, stress) {
|
|
|
|
struct Node {
|
2019-07-22 06:01:51 +02:00
|
|
|
std::atomic<std::string *> name_{nullptr};
|
2019-07-07 02:05:30 +02:00
|
|
|
char pad[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
int threads_n = 10;
|
|
|
|
std::vector<Node> nodes(threads_n);
|
|
|
|
td::EpochBasedMemoryReclamation<std::string> ebmr(threads_n + 1);
|
|
|
|
auto locker = ebmr.get_locker(threads_n);
|
|
|
|
locker.lock();
|
|
|
|
locker.unlock();
|
|
|
|
std::vector<td::thread> threads(threads_n);
|
|
|
|
int thread_id = 0;
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread = td::thread([&, thread_id] {
|
|
|
|
auto locker = ebmr.get_locker(thread_id);
|
|
|
|
locker.lock();
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
auto &node = nodes[td::Random::fast(0, threads_n - 1)];
|
|
|
|
auto *str = node.name_.load(std::memory_order_acquire);
|
|
|
|
if (str) {
|
|
|
|
CHECK(*str == "one" || *str == "twotwo");
|
|
|
|
}
|
|
|
|
if ((i + 1) % 100 == 0) {
|
|
|
|
locker.retire();
|
|
|
|
}
|
|
|
|
if (td::Random::fast(0, 5) == 0) {
|
2021-10-21 11:51:16 +02:00
|
|
|
auto *new_str = new td::string(td::Random::fast_bool() ? "one" : "twotwo");
|
2019-07-07 02:05:30 +02:00
|
|
|
if (node.name_.compare_exchange_strong(str, new_str, std::memory_order_acq_rel)) {
|
|
|
|
locker.retire(str);
|
|
|
|
} else {
|
|
|
|
delete new_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
locker.retire_sync();
|
|
|
|
locker.unlock();
|
|
|
|
});
|
|
|
|
thread_id++;
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
2021-10-07 15:56:45 +02:00
|
|
|
LOG(INFO) << "Undeleted pointers: " << ebmr.to_delete_size_unsafe();
|
2019-07-07 02:05:30 +02:00
|
|
|
//CHECK(static_cast<int>(ebmr.to_delete_size_unsafe()) <= threads_n * threads_n);
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
ebmr.get_locker(i).retire_sync();
|
|
|
|
}
|
|
|
|
CHECK(ebmr.to_delete_size_unsafe() == 0);
|
|
|
|
}
|
2021-10-25 09:05:15 +02:00
|
|
|
#endif
|