2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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)
|
|
|
|
//
|
|
|
|
#include "td/utils/MpmcWaiter.h"
|
|
|
|
#include "td/utils/port/sleep.h"
|
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2020-06-24 13:47:36 +02:00
|
|
|
template <class W>
|
2020-06-26 01:24:13 +02:00
|
|
|
static void test_waiter_stress_one_one() {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::Stage run;
|
|
|
|
td::Stage check;
|
|
|
|
|
|
|
|
std::vector<td::thread> threads;
|
2019-01-24 04:18:14 +01:00
|
|
|
std::atomic<size_t> value{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t write_cnt = 10;
|
2020-06-24 13:47:36 +02:00
|
|
|
td::unique_ptr<W> waiter;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t threads_n = 2;
|
|
|
|
for (size_t i = 0; i < threads_n; i++) {
|
|
|
|
threads.push_back(td::thread([&, id = static_cast<td::uint32>(i)] {
|
|
|
|
for (td::uint64 round = 1; round < 100000; round++) {
|
|
|
|
if (id == 0) {
|
|
|
|
value = 0;
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter = td::make_unique<W>();
|
2018-12-31 20:04:05 +01:00
|
|
|
write_cnt = td::Random::fast(1, 10);
|
|
|
|
}
|
|
|
|
run.wait(round * threads_n);
|
|
|
|
if (id == 1) {
|
|
|
|
for (size_t i = 0; i < write_cnt; i++) {
|
|
|
|
value.store(i + 1, std::memory_order_relaxed);
|
|
|
|
waiter->notify();
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-24 13:47:36 +02:00
|
|
|
typename W::Slot slot;
|
2021-10-21 11:51:16 +02:00
|
|
|
W::init_slot(slot, id);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t i = 1; i <= write_cnt; i++) {
|
|
|
|
while (true) {
|
|
|
|
auto x = value.load(std::memory_order_relaxed);
|
|
|
|
if (x >= i) {
|
|
|
|
break;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->stop_wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->stop_wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
check.wait(round * threads_n);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
TEST(MpmcEagerWaiter, stress_one_one) {
|
|
|
|
test_waiter_stress_one_one<td::MpmcEagerWaiter>();
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
TEST(MpmcSleepyWaiter, stress_one_one) {
|
|
|
|
test_waiter_stress_one_one<td::MpmcSleepyWaiter>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class W>
|
2020-06-26 01:24:13 +02:00
|
|
|
static void test_waiter_stress() {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::Stage run;
|
|
|
|
td::Stage check;
|
|
|
|
|
|
|
|
std::vector<td::thread> threads;
|
|
|
|
size_t write_n;
|
|
|
|
size_t read_n;
|
2019-01-24 04:18:14 +01:00
|
|
|
std::atomic<size_t> write_pos{0};
|
|
|
|
std::atomic<size_t> read_pos{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t end_pos;
|
|
|
|
size_t write_cnt;
|
|
|
|
size_t threads_n = 20;
|
2020-06-24 13:47:36 +02:00
|
|
|
td::unique_ptr<W> waiter;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t i = 0; i < threads_n; i++) {
|
|
|
|
threads.push_back(td::thread([&, id = static_cast<td::uint32>(i)] {
|
|
|
|
for (td::uint64 round = 1; round < 1000; round++) {
|
|
|
|
if (id == 0) {
|
|
|
|
write_n = td::Random::fast(1, 10);
|
|
|
|
read_n = td::Random::fast(1, 10);
|
|
|
|
write_cnt = td::Random::fast(1, 50);
|
|
|
|
end_pos = write_n * write_cnt;
|
|
|
|
write_pos = 0;
|
|
|
|
read_pos = 0;
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter = td::make_unique<W>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
run.wait(round * threads_n);
|
|
|
|
if (id <= write_n) {
|
|
|
|
for (size_t i = 0; i < write_cnt; i++) {
|
|
|
|
if (td::Random::fast(0, 20) == 0) {
|
|
|
|
td::usleep_for(td::Random::fast(1, 300));
|
|
|
|
}
|
|
|
|
write_pos.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
waiter->notify();
|
|
|
|
}
|
|
|
|
} else if (id > 10 && id - 10 <= read_n) {
|
2020-06-24 13:47:36 +02:00
|
|
|
typename W::Slot slot;
|
2021-10-21 11:51:16 +02:00
|
|
|
W::init_slot(slot, id);
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
auto x = read_pos.load(std::memory_order_relaxed);
|
|
|
|
if (x == end_pos) {
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->stop_wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (x == write_pos.load(std::memory_order_relaxed)) {
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
waiter->stop_wait(slot);
|
2018-12-31 20:04:05 +01:00
|
|
|
read_pos.compare_exchange_strong(x, x + 1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check.wait(round * threads_n);
|
2020-06-24 13:47:36 +02:00
|
|
|
if (id == 0) {
|
|
|
|
waiter->close();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
TEST(MpmcEagerWaiter, stress_multi) {
|
|
|
|
test_waiter_stress<td::MpmcEagerWaiter>();
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
TEST(MpmcSleepyWaiter, stress_multi) {
|
|
|
|
test_waiter_stress<td::MpmcSleepyWaiter>();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif // !TD_THREAD_UNSUPPORTED
|