From 19c3c0380836829f6e9c64da1c52ff87da8d49ee Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 3 Jul 2018 23:22:19 +0300 Subject: [PATCH] Template ActorSendType. GitOrigin-RevId: 1dc9cc3a23935cc305e161ddf3e12a875bdf9704 --- tdactor/td/actor/PromiseFuture.h | 11 +++-- tdactor/td/actor/impl/Scheduler-decl.h | 31 ++++++------ tdactor/td/actor/impl/Scheduler.cpp | 2 +- tdactor/td/actor/impl/Scheduler.h | 66 +++++++++++++------------- tdactor/test/actors_main.cpp | 10 ++-- 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/tdactor/td/actor/PromiseFuture.h b/tdactor/td/actor/PromiseFuture.h index b19982aa3..7cd0e9dd8 100644 --- a/tdactor/td/actor/PromiseFuture.h +++ b/tdactor/td/actor/PromiseFuture.h @@ -598,12 +598,13 @@ class PromiseFuture { FutureActor future_; }; -template -FutureActor send_promise(ActorId actor_id, ActorSendType send_type, - ResultT (ActorBT::*func)(PromiseActor &&, DestArgsT...), ArgsT &&... args) { +template +FutureActor send_promise(ActorId actor_id, ResultT (ActorBT::*func)(PromiseActor &&, DestArgsT...), + ArgsT &&... args) { PromiseFuture pf; - ::td::Scheduler::instance()->send_closure( - std::move(actor_id), create_immediate_closure(func, pf.move_promise(), std::forward(args)...), send_type); + Scheduler::instance()->send_closure( + std::move(actor_id), create_immediate_closure(func, pf.move_promise(), std::forward(args)...)); return pf.move_future(); } diff --git a/tdactor/td/actor/impl/Scheduler-decl.h b/tdactor/td/actor/impl/Scheduler-decl.h index 9bde72488..09c90e438 100644 --- a/tdactor/td/actor/impl/Scheduler-decl.h +++ b/tdactor/td/actor/impl/Scheduler-decl.h @@ -93,13 +93,14 @@ class Scheduler { void send_to_scheduler(int32 sched_id, const ActorId<> &actor_id, Event &&event); void send_to_other_scheduler(int32 sched_id, const ActorId<> &actor_id, Event &&event); - template - void send_lambda(ActorRef actor_ref, EventT &&lambda, ActorSendType send_type); + template + void send_lambda(ActorRef actor_ref, EventT &&lambda); - template - void send_closure(ActorRef actor_ref, EventT &&closure, ActorSendType send_type); + template + void send_closure(ActorRef actor_ref, EventT &&closure); - void send(ActorRef actor_ref, Event &&event, ActorSendType send_type); + template + void send(ActorRef actor_ref, Event &&event); void hack(const ActorId<> &actor_id, Event &&event) { actor_id.get_actor_unsafe()->raw_event(event.data); @@ -177,9 +178,8 @@ class Scheduler { template void flush_mailbox(ActorInfo *actor_info, const RunFuncT &run_func, const EventFuncT &event_func); - template - void send_impl(const ActorId<> &actor_id, ActorSendType send_type, const RunFuncT &run_func, - const EventFuncT &event_func); + template + void send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func); void inc_wait_generation(); @@ -261,9 +261,8 @@ void send_closure(ActorIdT &&actor_id, FunctionT function, ArgsT &&... args) { using FunctionClassT = member_function_class_t; static_assert(std::is_base_of::value, "unsafe send_closure"); - Scheduler::instance()->send_closure(std::forward(actor_id), - create_immediate_closure(function, std::forward(args)...), - ActorSendType::Immediate); + Scheduler::instance()->send_closure( + std::forward(actor_id), create_immediate_closure(function, std::forward(args)...)); } template @@ -272,23 +271,23 @@ void send_closure_later(ActorIdT &&actor_id, FunctionT function, ArgsT &&... arg using FunctionClassT = member_function_class_t; static_assert(std::is_base_of::value, "unsafe send_closure"); - Scheduler::instance()->send(std::forward(actor_id), - Event::delayed_closure(function, std::forward(args)...), ActorSendType::Later); + Scheduler::instance()->send(std::forward(actor_id), + Event::delayed_closure(function, std::forward(args)...)); } template void send_lambda(ActorRef actor_ref, ArgsT &&... args) { - Scheduler::instance()->send_lambda(actor_ref, std::forward(args)..., ActorSendType::Immediate); + Scheduler::instance()->send_lambda(actor_ref, std::forward(args)...); } template void send_event(ActorRef actor_ref, ArgsT &&... args) { - Scheduler::instance()->send(actor_ref, std::forward(args)..., ActorSendType::Immediate); + Scheduler::instance()->send(actor_ref, std::forward(args)...); } template void send_event_later(ActorRef actor_ref, ArgsT &&... args) { - Scheduler::instance()->send(actor_ref, std::forward(args)..., ActorSendType::Later); + Scheduler::instance()->send(actor_ref, std::forward(args)...); } void yield_scheduler(); diff --git a/tdactor/td/actor/impl/Scheduler.cpp b/tdactor/td/actor/impl/Scheduler.cpp index 05a9aba7a..31aab67d7 100644 --- a/tdactor/td/actor/impl/Scheduler.cpp +++ b/tdactor/td/actor/impl/Scheduler.cpp @@ -469,7 +469,7 @@ double Scheduler::run_timeout() { HeapNode *node = timeout_queue_.pop(); ActorInfo *actor_info = ActorInfo::from_heap_node(node); inc_wait_generation(); - send(actor_info->actor_id(), Event::timeout(), ActorSendType::Immediate); + send(actor_info->actor_id(), Event::timeout()); } if (timeout_queue_.empty()) { return 10000; diff --git a/tdactor/td/actor/impl/Scheduler.h b/tdactor/td/actor/impl/Scheduler.h index 014805558..b30ceec40 100644 --- a/tdactor/td/actor/impl/Scheduler.h +++ b/tdactor/td/actor/impl/Scheduler.h @@ -109,12 +109,12 @@ ActorOwn Scheduler::register_actor_impl(Slice name, ActorT *actor_ptr, A ActorId actor_id = weak_info->actor_id(actor_ptr); if (sched_id != sched_id_) { - send(actor_id, Event::start(), ActorSendType::LaterWeak); + send(actor_id, Event::start()); do_migrate_actor(actor_info, sched_id); } else { pending_actors_list_.put(weak_info->get_list_node()); if (!ActorTraits::is_lite) { - send(actor_id, Event::start(), ActorSendType::LaterWeak); + send(actor_id, Event::start()); } } @@ -184,9 +184,8 @@ inline void Scheduler::inc_wait_generation() { wait_generation_++; } -template -void Scheduler::send_impl(const ActorId<> &actor_id, ActorSendType send_type, const RunFuncT &run_func, - const EventFuncT &event_func) { +template +void Scheduler::send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func) { CHECK(has_guard_); ActorInfo *actor_info = actor_id.get_actor_info(); if (unlikely(actor_info == nullptr || close_flag_)) { @@ -220,38 +219,39 @@ void Scheduler::send_impl(const ActorId<> &actor_id, ActorSendType send_type, co } } -template -void Scheduler::send_lambda(ActorRef actor_ref, EventT &&lambda, ActorSendType send_type) { - return send_impl(actor_ref.get(), send_type, - [&](ActorInfo *actor_info) { - event_context_ptr_->link_token = actor_ref.token(); - lambda(); - }, - [&]() { - auto event = Event::lambda(std::forward(lambda)); - event.set_link_token(actor_ref.token()); - return std::move(event); - }); +template +void Scheduler::send_lambda(ActorRef actor_ref, EventT &&lambda) { + return send_impl(actor_ref.get(), + [&](ActorInfo *actor_info) { + event_context_ptr_->link_token = actor_ref.token(); + lambda(); + }, + [&]() { + auto event = Event::lambda(std::forward(lambda)); + event.set_link_token(actor_ref.token()); + return std::move(event); + }); } -template -void Scheduler::send_closure(ActorRef actor_ref, EventT &&closure, ActorSendType send_type) { - return send_impl(actor_ref.get(), send_type, - [&](ActorInfo *actor_info) { - event_context_ptr_->link_token = actor_ref.token(); - closure.run(static_cast(actor_info->get_actor_unsafe())); - }, - [&]() { - auto event = Event::immediate_closure(std::forward(closure)); - event.set_link_token(actor_ref.token()); - return std::move(event); - }); +template +void Scheduler::send_closure(ActorRef actor_ref, EventT &&closure) { + return send_impl(actor_ref.get(), + [&](ActorInfo *actor_info) { + event_context_ptr_->link_token = actor_ref.token(); + closure.run(static_cast(actor_info->get_actor_unsafe())); + }, + [&]() { + auto event = Event::immediate_closure(std::forward(closure)); + event.set_link_token(actor_ref.token()); + return std::move(event); + }); } -inline void Scheduler::send(ActorRef actor_ref, Event &&event, ActorSendType send_type) { +template +void Scheduler::send(ActorRef actor_ref, Event &&event) { event.set_link_token(actor_ref.token()); - return send_impl(actor_ref.get(), send_type, [&](ActorInfo *actor_info) { do_event(actor_info, std::move(event)); }, - [&]() { return std::move(event); }); + return send_impl(actor_ref.get(), [&](ActorInfo *actor_info) { do_event(actor_info, std::move(event)); }, + [&]() { return std::move(event); }); } inline void Scheduler::subscribe(const Fd &fd, Fd::Flags flags) { @@ -270,7 +270,7 @@ inline void Scheduler::yield_actor(Actor *actor) { yield_actor(actor->get_info()); } inline void Scheduler::yield_actor(ActorInfo *actor_info) { - send(actor_info->actor_id(), Event::yield(), ActorSendType::LaterWeak); + send(actor_info->actor_id(), Event::yield()); } inline void Scheduler::stop_actor(Actor *actor) { diff --git a/tdactor/test/actors_main.cpp b/tdactor/test/actors_main.cpp index d4b44807f..ef70888d9 100644 --- a/tdactor/test/actors_main.cpp +++ b/tdactor/test/actors_main.cpp @@ -128,9 +128,9 @@ class QueryActor final : public Actor { query.result = slow_pow_mod_uint32(x, p); callback_->on_result(std::move(query)); } else { - auto future = - send_promise(rand_elem(workers_), Random::fast(0, 3) == 0 ? ActorSendType::Immediate : ActorSendType::Later, - &Worker::query, x, p); + auto future = Random::fast(0, 3) == 0 + ? send_promise(rand_elem(workers_), &Worker::query, x, p) + : send_promise(rand_elem(workers_), &Worker::query, x, p); if (future.is_ready()) { query.result = future.move_as_ok(); callback_->on_result(std::move(query)); @@ -304,8 +304,8 @@ class SimpleActor final : public Actor { } q_++; p_ = Random::fast(0, 1) ? 1 : 10000; - auto future = send_promise(worker_, Random::fast(0, 3) == 0 ? ActorSendType::Immediate : ActorSendType::Later, - &Worker::query, q_, p_); + auto future = Random::fast(0, 3) == 0 ? send_promise(worker_, &Worker::query, q_, p_) + : send_promise(worker_, &Worker::query, q_, p_); if (future.is_ready()) { auto result = future.move_as_ok(); CHECK(result == fast_pow_mod_uint32(q_, p_));