2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Random.h"
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/utils/tests.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
2019-08-14 02:13:34 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
template <class ContainerT>
|
|
|
|
static typename ContainerT::value_type &rand_elem(ContainerT &cont) {
|
|
|
|
CHECK(0 < cont.size() && cont.size() <= static_cast<size_t>(std::numeric_limits<int>::max()));
|
2021-10-15 13:21:30 +02:00
|
|
|
return cont[td::Random::fast(0, static_cast<int>(cont.size()) - 1)];
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
static td::uint32 fast_pow_mod_uint32(td::uint32 x, td::uint32 p) {
|
|
|
|
td::uint32 res = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (p) {
|
|
|
|
if (p & 1) {
|
|
|
|
res *= x;
|
|
|
|
}
|
|
|
|
x *= x;
|
|
|
|
p >>= 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
static td::uint32 slow_pow_mod_uint32(td::uint32 x, td::uint32 p) {
|
|
|
|
td::uint32 res = 1;
|
|
|
|
for (td::uint32 i = 0; i < p; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
res *= x;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:11:02 +01:00
|
|
|
struct ActorQuery {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::uint32 query_id{};
|
|
|
|
td::uint32 result{};
|
|
|
|
td::vector<int> todo;
|
2022-02-10 22:11:02 +01:00
|
|
|
ActorQuery() = default;
|
|
|
|
ActorQuery(const ActorQuery &) = delete;
|
|
|
|
ActorQuery &operator=(const ActorQuery &) = delete;
|
|
|
|
ActorQuery(ActorQuery &&) = default;
|
|
|
|
ActorQuery &operator=(ActorQuery &&) = default;
|
|
|
|
~ActorQuery() {
|
|
|
|
LOG_CHECK(todo.empty()) << "ActorQuery lost";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
int next_pow() {
|
|
|
|
CHECK(!todo.empty());
|
|
|
|
int res = todo.back();
|
|
|
|
todo.pop_back();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
bool ready() {
|
|
|
|
return todo.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-10 22:11:02 +01:00
|
|
|
static td::uint32 fast_calc(ActorQuery &q) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::uint32 result = q.result;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto x : q.todo) {
|
|
|
|
result = fast_pow_mod_uint32(result, x);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class Worker final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Worker(int threads_n) : threads_n_(threads_n) {
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void query(td::PromiseActor<td::uint32> &&promise, td::uint32 x, td::uint32 p) {
|
|
|
|
td::uint32 result = slow_pow_mod_uint32(x, p);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(std::move(result));
|
|
|
|
|
|
|
|
(void)threads_n_;
|
2021-10-15 13:21:30 +02:00
|
|
|
// if (threads_n_ > 1 && td::Random::fast(0, 9) == 0) {
|
|
|
|
// migrate(td::Random::fast(2, threads_n));
|
2018-12-31 20:04:05 +01:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int threads_n_;
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class QueryActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
Callback(Callback &&) = delete;
|
|
|
|
Callback &operator=(Callback &&) = delete;
|
|
|
|
virtual ~Callback() = default;
|
2022-02-10 22:11:02 +01:00
|
|
|
virtual void on_result(ActorQuery &&query) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_closed() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit QueryActor(int threads_n) : threads_n_(threads_n) {
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
void set_callback(td::unique_ptr<Callback> callback) {
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void set_workers(td::vector<td::ActorId<Worker>> workers) {
|
2018-12-31 20:04:05 +01:00
|
|
|
workers_ = std::move(workers);
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:11:02 +01:00
|
|
|
void query(ActorQuery &&query) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::uint32 x = query.result;
|
|
|
|
td::uint32 p = query.next_pow();
|
|
|
|
if (td::Random::fast(0, 3) && (p <= 1000 || workers_.empty())) {
|
2018-12-31 20:04:05 +01:00
|
|
|
query.result = slow_pow_mod_uint32(x, p);
|
|
|
|
callback_->on_result(std::move(query));
|
|
|
|
} else {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto future = td::Random::fast(0, 3) == 0
|
|
|
|
? td::send_promise<td::ActorSendType::Immediate>(rand_elem(workers_), &Worker::query, x, p)
|
|
|
|
: td::send_promise<td::ActorSendType::Later>(rand_elem(workers_), &Worker::query, x, p);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (future.is_ready()) {
|
|
|
|
query.result = future.move_as_ok();
|
|
|
|
callback_->on_result(std::move(query));
|
|
|
|
} else {
|
2021-10-15 13:21:30 +02:00
|
|
|
future.set_event(td::EventCreator::raw(actor_id(), query.query_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query_id = query.query_id;
|
2020-07-13 22:20:16 +02:00
|
|
|
pending_.emplace(query_id, std::make_pair(std::move(future), std::move(query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
if (threads_n_ > 1 && td::Random::fast(0, 9) == 0) {
|
|
|
|
migrate(td::Random::fast(2, threads_n_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
void raw_event(const td::Event::Raw &event) final {
|
|
|
|
td::uint32 id = event.u32;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = pending_.find(id);
|
|
|
|
auto future = std::move(it->second.first);
|
|
|
|
auto query = std::move(it->second.second);
|
|
|
|
pending_.erase(it);
|
|
|
|
CHECK(future.is_ready());
|
|
|
|
query.result = future.move_as_ok();
|
|
|
|
callback_->on_result(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
callback_->on_closed();
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
void on_start_migrate(td::int32 sched_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : pending_) {
|
|
|
|
start_migrate(it.second.first, sched_id);
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_finish_migrate() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : pending_) {
|
|
|
|
finish_migrate(it.second.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::unique_ptr<Callback> callback_;
|
2022-02-10 22:11:02 +01:00
|
|
|
std::map<td::uint32, std::pair<td::FutureActor<td::uint32>, ActorQuery>> pending_;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::vector<td::ActorId<Worker>> workers_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int threads_n_;
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class MainQueryActor final : public td::Actor {
|
2021-07-04 04:58:54 +02:00
|
|
|
class QueryActorCallback final : public QueryActor::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-02-10 22:11:02 +01:00
|
|
|
void on_result(ActorQuery &&query) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query.ready()) {
|
|
|
|
send_closure(parent_id_, &MainQueryActor::on_result, std::move(query));
|
|
|
|
} else {
|
|
|
|
send_closure(next_solver_, &QueryActor::query, std::move(query));
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_closed() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_id_, &MainQueryActor::on_closed);
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
QueryActorCallback(td::ActorId<MainQueryActor> parent_id, td::ActorId<QueryActor> next_solver)
|
2018-12-31 20:04:05 +01:00
|
|
|
: parent_id_(parent_id), next_solver_(next_solver) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorId<MainQueryActor> parent_id_;
|
|
|
|
td::ActorId<QueryActor> next_solver_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const int ACTORS_CNT = 10;
|
|
|
|
const int WORKERS_CNT = 4;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MainQueryActor(int threads_n) : threads_n_(threads_n) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
actors_.resize(ACTORS_CNT);
|
|
|
|
for (auto &actor : actors_) {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto actor_ptr = td::make_unique<QueryActor>(threads_n_);
|
|
|
|
actor = register_actor("QueryActor", std::move(actor_ptr), threads_n_ > 1 ? td::Random::fast(2, threads_n_) : 0)
|
2018-12-31 20:04:05 +01:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
workers_.resize(WORKERS_CNT);
|
|
|
|
for (auto &worker : workers_) {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto actor_ptr = td::make_unique<Worker>(threads_n_);
|
|
|
|
worker = register_actor("Worker", std::move(actor_ptr), threads_n_ > 1 ? td::Random::fast(2, threads_n_) : 0)
|
|
|
|
.release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ACTORS_CNT; i++) {
|
|
|
|
ref_cnt_++;
|
|
|
|
send_closure(actors_[i], &QueryActor::set_callback,
|
2021-10-15 13:21:30 +02:00
|
|
|
td::make_unique<QueryActorCallback>(actor_id(this), actors_[(i + 1) % ACTORS_CNT]));
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(actors_[i], &QueryActor::set_workers, workers_);
|
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:11:02 +01:00
|
|
|
void on_result(ActorQuery &&query) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(query.ready());
|
|
|
|
CHECK(query.result == expected_[query.query_id]);
|
|
|
|
in_cnt_++;
|
|
|
|
wakeup();
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:11:02 +01:00
|
|
|
ActorQuery create_query() {
|
|
|
|
ActorQuery q;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.query_id = (query_id_ += 2);
|
|
|
|
q.result = q.query_id;
|
|
|
|
q.todo = {1, 1, 1, 1, 1, 1, 1, 1, 10000};
|
|
|
|
expected_[q.query_id] = fast_calc(q);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_closed() {
|
|
|
|
ref_cnt_--;
|
|
|
|
if (ref_cnt_ == 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void wakeup() final {
|
2022-07-27 23:15:18 +02:00
|
|
|
int cnt = 10000;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (out_cnt_ < in_cnt_ + 100 && out_cnt_ < cnt) {
|
2021-10-15 13:21:30 +02:00
|
|
|
if (td::Random::fast_bool()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(rand_elem(actors_), &QueryActor::query, create_query());
|
|
|
|
} else {
|
|
|
|
send_closure_later(rand_elem(actors_), &QueryActor::query, create_query());
|
|
|
|
}
|
|
|
|
out_cnt_++;
|
|
|
|
}
|
|
|
|
if (in_cnt_ == cnt) {
|
|
|
|
in_cnt_++;
|
|
|
|
ref_cnt_--;
|
|
|
|
for (auto &actor : actors_) {
|
|
|
|
send_closure(actor, &QueryActor::close);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
std::map<td::uint32, td::uint32> expected_;
|
|
|
|
td::vector<td::ActorId<QueryActor>> actors_;
|
|
|
|
td::vector<td::ActorId<Worker>> workers_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int out_cnt_ = 0;
|
|
|
|
int in_cnt_ = 0;
|
|
|
|
int query_id_ = 1;
|
|
|
|
int ref_cnt_ = 1;
|
|
|
|
int threads_n_;
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class SimpleActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
explicit SimpleActor(td::int32 threads_n) : threads_n_(threads_n) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto actor_ptr = td::make_unique<Worker>(threads_n_);
|
2018-12-31 20:04:05 +01:00
|
|
|
worker_ =
|
2021-10-15 13:21:30 +02:00
|
|
|
register_actor("Worker", std::move(actor_ptr), threads_n_ > 1 ? td::Random::fast(2, threads_n_) : 0).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void wakeup() final {
|
2022-07-27 23:15:18 +02:00
|
|
|
if (q_ == 10000) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
q_++;
|
2021-10-15 13:21:30 +02:00
|
|
|
p_ = td::Random::fast_bool() ? 1 : 10000;
|
|
|
|
auto future = td::Random::fast(0, 3) == 0
|
|
|
|
? td::send_promise<td::ActorSendType::Immediate>(worker_, &Worker::query, q_, p_)
|
|
|
|
: td::send_promise<td::ActorSendType::Later>(worker_, &Worker::query, q_, p_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (future.is_ready()) {
|
|
|
|
auto result = future.move_as_ok();
|
|
|
|
CHECK(result == fast_pow_mod_uint32(q_, p_));
|
|
|
|
yield();
|
|
|
|
} else {
|
2021-10-15 13:21:30 +02:00
|
|
|
future.set_event(td::EventCreator::raw(actor_id(), nullptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
future_ = std::move(future);
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
// if (threads_n_ > 1 && td::Random::fast(0, 2) == 0) {
|
|
|
|
// migrate(td::Random::fast(1, threads_n));
|
2018-12-31 20:04:05 +01:00
|
|
|
//}
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void raw_event(const td::Event::Raw &event) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result = future_.move_as_ok();
|
|
|
|
CHECK(result == fast_pow_mod_uint32(q_, p_));
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
void on_start_migrate(td::int32 sched_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
start_migrate(future_, sched_id);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_finish_migrate() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
finish_migrate(future_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::int32 threads_n_;
|
|
|
|
td::ActorId<Worker> worker_;
|
|
|
|
td::FutureActor<td::uint32> future_;
|
|
|
|
td::uint32 q_ = 1;
|
|
|
|
td::uint32 p_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class SendToDead final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
class Parent final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
explicit Parent(td::ActorShared<> parent, int ttl = 3) : parent_(std::move(parent)), ttl_(ttl) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
set_timeout_in(td::Random::fast_uint32() % 3 * 0.001);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ttl_ != 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
child_ = td::create_actor_on_scheduler<Parent>(
|
|
|
|
"Child", td::Random::fast_uint32() % td::Scheduler::instance()->sched_count(), actor_shared(this),
|
|
|
|
ttl_ - 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorOwn<Parent> child_;
|
|
|
|
td::ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int ttl_;
|
|
|
|
};
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < 2000; i++) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor_on_scheduler<Parent>(
|
|
|
|
"Parent", td::Random::fast_uint32() % td::Scheduler::instance()->sched_count(), create_reference(), 4)
|
2018-12-31 20:04:05 +01:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorShared<> create_reference() {
|
2018-12-31 20:04:05 +01:00
|
|
|
ref_cnt_++;
|
2020-08-27 00:27:34 +02:00
|
|
|
return actor_shared(this);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
ref_cnt_--;
|
|
|
|
if (ref_cnt_ == 0) {
|
|
|
|
ttl_--;
|
|
|
|
if (ttl_ <= 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
} else {
|
|
|
|
start_up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
td::uint32 ttl_{50};
|
|
|
|
td::uint32 ref_cnt_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, send_to_dead) {
|
|
|
|
//TODO: fix CHECK(storage_count_.load() == 0)
|
|
|
|
return;
|
|
|
|
int threads_n = 5;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-27 02:03:15 +02:00
|
|
|
sched.create_actor_unsafe<SendToDead>(0, "SendToDead").release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, main_simple) {
|
|
|
|
int threads_n = 3;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-27 02:03:15 +02:00
|
|
|
sched.create_actor_unsafe<SimpleActor>(threads_n > 1 ? 1 : 0, "simple", threads_n).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, main) {
|
|
|
|
int threads_n = 9;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-27 02:03:15 +02:00
|
|
|
sched.create_actor_unsafe<MainQueryActor>(threads_n > 1 ? 1 : 0, "MainQuery", threads_n).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class DoAfterStop final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
ptr = td::make_unique<int>(10);
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
CHECK(*ptr == 10);
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::unique_ptr<int> ptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, do_after_stop) {
|
|
|
|
int threads_n = 0;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-27 02:03:15 +02:00
|
|
|
sched.create_actor_unsafe<DoAfterStop>(0, "DoAfterStop").release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
2019-07-30 15:22:43 +02:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class XContext final : public td::ActorContext {
|
2019-07-30 15:22:43 +02:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::int32 get_id() const final {
|
2019-08-01 21:12:04 +02:00
|
|
|
return 123456789;
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:22:43 +02:00
|
|
|
void validate() {
|
|
|
|
CHECK(x == 1234);
|
|
|
|
}
|
2021-10-21 11:51:16 +02:00
|
|
|
~XContext() final {
|
2019-07-30 15:22:43 +02:00
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
int x = 1234;
|
|
|
|
};
|
2019-08-01 21:12:04 +02:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class WithXContext final : public td::Actor {
|
2019-07-30 15:22:43 +02:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2019-09-05 17:32:45 +02:00
|
|
|
auto old_context = set_context(std::make_shared<XContext>());
|
2019-07-30 15:22:43 +02:00
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void f(td::unique_ptr<td::Guard> guard) {
|
2019-07-30 15:22:43 +02:00
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
static void check_context() {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto ptr = static_cast<XContext *>(td::Scheduler::context());
|
|
|
|
CHECK(ptr != nullptr);
|
2019-07-30 15:22:43 +02:00
|
|
|
ptr->validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, context_during_destruction) {
|
|
|
|
int threads_n = 0;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2019-07-30 15:22:43 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto guard = sched.get_main_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
auto with_context = td::create_actor<WithXContext>("WithXContext").release();
|
|
|
|
send_closure(with_context, &WithXContext::f, td::create_lambda_guard([] { check_context(); }));
|
2019-08-13 22:52:54 +02:00
|
|
|
send_closure_later(with_context, &WithXContext::close);
|
2021-10-15 13:21:30 +02:00
|
|
|
send_closure(with_context, &WithXContext::f, td::create_lambda_guard([] { check_context(); }));
|
|
|
|
send_closure(with_context, &WithXContext::f, td::create_lambda_guard([] { td::Scheduler::instance()->finish(); }));
|
2019-07-30 15:22:43 +02:00
|
|
|
}
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|