Template ActorSendType.

GitOrigin-RevId: 1dc9cc3a23935cc305e161ddf3e12a875bdf9704
This commit is contained in:
levlam 2018-07-03 23:22:19 +03:00
parent ba027ac0f5
commit 19c3c03808
5 changed files with 60 additions and 60 deletions

View File

@ -598,12 +598,13 @@ class PromiseFuture {
FutureActor<T> future_; FutureActor<T> future_;
}; };
template <class T, class ActorAT, class ActorBT, class ResultT, class... DestArgsT, class... ArgsT> template <ActorSendType send_type, class T, class ActorAT, class ActorBT, class ResultT, class... DestArgsT,
FutureActor<T> send_promise(ActorId<ActorAT> actor_id, ActorSendType send_type, class... ArgsT>
ResultT (ActorBT::*func)(PromiseActor<T> &&, DestArgsT...), ArgsT &&... args) { FutureActor<T> send_promise(ActorId<ActorAT> actor_id, ResultT (ActorBT::*func)(PromiseActor<T> &&, DestArgsT...),
ArgsT &&... args) {
PromiseFuture<T> pf; PromiseFuture<T> pf;
::td::Scheduler::instance()->send_closure( Scheduler::instance()->send_closure<send_type>(
std::move(actor_id), create_immediate_closure(func, pf.move_promise(), std::forward<ArgsT>(args)...), send_type); std::move(actor_id), create_immediate_closure(func, pf.move_promise(), std::forward<ArgsT>(args)...));
return pf.move_future(); return pf.move_future();
} }

View File

@ -93,13 +93,14 @@ class Scheduler {
void send_to_scheduler(int32 sched_id, const ActorId<> &actor_id, Event &&event); 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); void send_to_other_scheduler(int32 sched_id, const ActorId<> &actor_id, Event &&event);
template <class EventT> template <ActorSendType send_type, class EventT>
void send_lambda(ActorRef actor_ref, EventT &&lambda, ActorSendType send_type); void send_lambda(ActorRef actor_ref, EventT &&lambda);
template <class EventT> template <ActorSendType send_type, class EventT>
void send_closure(ActorRef actor_ref, EventT &&closure, ActorSendType send_type); void send_closure(ActorRef actor_ref, EventT &&closure);
void send(ActorRef actor_ref, Event &&event, ActorSendType send_type); template <ActorSendType send_type>
void send(ActorRef actor_ref, Event &&event);
void hack(const ActorId<> &actor_id, Event &&event) { void hack(const ActorId<> &actor_id, Event &&event) {
actor_id.get_actor_unsafe()->raw_event(event.data); actor_id.get_actor_unsafe()->raw_event(event.data);
@ -177,9 +178,8 @@ class Scheduler {
template <class RunFuncT, class EventFuncT> template <class RunFuncT, class EventFuncT>
void flush_mailbox(ActorInfo *actor_info, const RunFuncT &run_func, const EventFuncT &event_func); void flush_mailbox(ActorInfo *actor_info, const RunFuncT &run_func, const EventFuncT &event_func);
template <class RunFuncT, class EventFuncT> template <ActorSendType send_type, class RunFuncT, class EventFuncT>
void send_impl(const ActorId<> &actor_id, ActorSendType send_type, const RunFuncT &run_func, void send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func);
const EventFuncT &event_func);
void inc_wait_generation(); void inc_wait_generation();
@ -261,9 +261,8 @@ void send_closure(ActorIdT &&actor_id, FunctionT function, ArgsT &&... args) {
using FunctionClassT = member_function_class_t<FunctionT>; using FunctionClassT = member_function_class_t<FunctionT>;
static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure"); static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure");
Scheduler::instance()->send_closure(std::forward<ActorIdT>(actor_id), Scheduler::instance()->send_closure<ActorSendType::Immediate>(
create_immediate_closure(function, std::forward<ArgsT>(args)...), std::forward<ActorIdT>(actor_id), create_immediate_closure(function, std::forward<ArgsT>(args)...));
ActorSendType::Immediate);
} }
template <class ActorIdT, class FunctionT, class... ArgsT> template <class ActorIdT, class FunctionT, class... ArgsT>
@ -272,23 +271,23 @@ void send_closure_later(ActorIdT &&actor_id, FunctionT function, ArgsT &&... arg
using FunctionClassT = member_function_class_t<FunctionT>; using FunctionClassT = member_function_class_t<FunctionT>;
static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure"); static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure");
Scheduler::instance()->send(std::forward<ActorIdT>(actor_id), Scheduler::instance()->send<ActorSendType::Later>(std::forward<ActorIdT>(actor_id),
Event::delayed_closure(function, std::forward<ArgsT>(args)...), ActorSendType::Later); Event::delayed_closure(function, std::forward<ArgsT>(args)...));
} }
template <class... ArgsT> template <class... ArgsT>
void send_lambda(ActorRef actor_ref, ArgsT &&... args) { void send_lambda(ActorRef actor_ref, ArgsT &&... args) {
Scheduler::instance()->send_lambda(actor_ref, std::forward<ArgsT>(args)..., ActorSendType::Immediate); Scheduler::instance()->send_lambda<ActorSendType::Immediate>(actor_ref, std::forward<ArgsT>(args)...);
} }
template <class... ArgsT> template <class... ArgsT>
void send_event(ActorRef actor_ref, ArgsT &&... args) { void send_event(ActorRef actor_ref, ArgsT &&... args) {
Scheduler::instance()->send(actor_ref, std::forward<ArgsT>(args)..., ActorSendType::Immediate); Scheduler::instance()->send<ActorSendType::Immediate>(actor_ref, std::forward<ArgsT>(args)...);
} }
template <class... ArgsT> template <class... ArgsT>
void send_event_later(ActorRef actor_ref, ArgsT &&... args) { void send_event_later(ActorRef actor_ref, ArgsT &&... args) {
Scheduler::instance()->send(actor_ref, std::forward<ArgsT>(args)..., ActorSendType::Later); Scheduler::instance()->send<ActorSendType::Later>(actor_ref, std::forward<ArgsT>(args)...);
} }
void yield_scheduler(); void yield_scheduler();

View File

@ -469,7 +469,7 @@ double Scheduler::run_timeout() {
HeapNode *node = timeout_queue_.pop(); HeapNode *node = timeout_queue_.pop();
ActorInfo *actor_info = ActorInfo::from_heap_node(node); ActorInfo *actor_info = ActorInfo::from_heap_node(node);
inc_wait_generation(); inc_wait_generation();
send(actor_info->actor_id(), Event::timeout(), ActorSendType::Immediate); send<ActorSendType::Immediate>(actor_info->actor_id(), Event::timeout());
} }
if (timeout_queue_.empty()) { if (timeout_queue_.empty()) {
return 10000; return 10000;

View File

@ -109,12 +109,12 @@ ActorOwn<ActorT> Scheduler::register_actor_impl(Slice name, ActorT *actor_ptr, A
ActorId<ActorT> actor_id = weak_info->actor_id(actor_ptr); ActorId<ActorT> actor_id = weak_info->actor_id(actor_ptr);
if (sched_id != sched_id_) { if (sched_id != sched_id_) {
send(actor_id, Event::start(), ActorSendType::LaterWeak); send<ActorSendType::LaterWeak>(actor_id, Event::start());
do_migrate_actor(actor_info, sched_id); do_migrate_actor(actor_info, sched_id);
} else { } else {
pending_actors_list_.put(weak_info->get_list_node()); pending_actors_list_.put(weak_info->get_list_node());
if (!ActorTraits<ActorT>::is_lite) { if (!ActorTraits<ActorT>::is_lite) {
send(actor_id, Event::start(), ActorSendType::LaterWeak); send<ActorSendType::LaterWeak>(actor_id, Event::start());
} }
} }
@ -184,9 +184,8 @@ inline void Scheduler::inc_wait_generation() {
wait_generation_++; wait_generation_++;
} }
template <class RunFuncT, class EventFuncT> template <ActorSendType send_type, class RunFuncT, class EventFuncT>
void Scheduler::send_impl(const ActorId<> &actor_id, ActorSendType send_type, const RunFuncT &run_func, void Scheduler::send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func) {
const EventFuncT &event_func) {
CHECK(has_guard_); CHECK(has_guard_);
ActorInfo *actor_info = actor_id.get_actor_info(); ActorInfo *actor_info = actor_id.get_actor_info();
if (unlikely(actor_info == nullptr || close_flag_)) { if (unlikely(actor_info == nullptr || close_flag_)) {
@ -220,38 +219,39 @@ void Scheduler::send_impl(const ActorId<> &actor_id, ActorSendType send_type, co
} }
} }
template <class EventT> template <ActorSendType send_type, class EventT>
void Scheduler::send_lambda(ActorRef actor_ref, EventT &&lambda, ActorSendType send_type) { void Scheduler::send_lambda(ActorRef actor_ref, EventT &&lambda) {
return send_impl(actor_ref.get(), send_type, return send_impl<send_type>(actor_ref.get(),
[&](ActorInfo *actor_info) { [&](ActorInfo *actor_info) {
event_context_ptr_->link_token = actor_ref.token(); event_context_ptr_->link_token = actor_ref.token();
lambda(); lambda();
}, },
[&]() { [&]() {
auto event = Event::lambda(std::forward<EventT>(lambda)); auto event = Event::lambda(std::forward<EventT>(lambda));
event.set_link_token(actor_ref.token()); event.set_link_token(actor_ref.token());
return std::move(event); return std::move(event);
}); });
} }
template <class EventT> template <ActorSendType send_type, class EventT>
void Scheduler::send_closure(ActorRef actor_ref, EventT &&closure, ActorSendType send_type) { void Scheduler::send_closure(ActorRef actor_ref, EventT &&closure) {
return send_impl(actor_ref.get(), send_type, return send_impl<send_type>(actor_ref.get(),
[&](ActorInfo *actor_info) { [&](ActorInfo *actor_info) {
event_context_ptr_->link_token = actor_ref.token(); event_context_ptr_->link_token = actor_ref.token();
closure.run(static_cast<typename EventT::ActorType *>(actor_info->get_actor_unsafe())); closure.run(static_cast<typename EventT::ActorType *>(actor_info->get_actor_unsafe()));
}, },
[&]() { [&]() {
auto event = Event::immediate_closure(std::forward<EventT>(closure)); auto event = Event::immediate_closure(std::forward<EventT>(closure));
event.set_link_token(actor_ref.token()); event.set_link_token(actor_ref.token());
return std::move(event); return std::move(event);
}); });
} }
inline void Scheduler::send(ActorRef actor_ref, Event &&event, ActorSendType send_type) { template <ActorSendType send_type>
void Scheduler::send(ActorRef actor_ref, Event &&event) {
event.set_link_token(actor_ref.token()); 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 send_impl<send_type>(actor_ref.get(), [&](ActorInfo *actor_info) { do_event(actor_info, std::move(event)); },
[&]() { return std::move(event); }); [&]() { return std::move(event); });
} }
inline void Scheduler::subscribe(const Fd &fd, Fd::Flags flags) { 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()); yield_actor(actor->get_info());
} }
inline void Scheduler::yield_actor(ActorInfo *actor_info) { inline void Scheduler::yield_actor(ActorInfo *actor_info) {
send(actor_info->actor_id(), Event::yield(), ActorSendType::LaterWeak); send<ActorSendType::LaterWeak>(actor_info->actor_id(), Event::yield());
} }
inline void Scheduler::stop_actor(Actor *actor) { inline void Scheduler::stop_actor(Actor *actor) {

View File

@ -128,9 +128,9 @@ class QueryActor final : public Actor {
query.result = slow_pow_mod_uint32(x, p); query.result = slow_pow_mod_uint32(x, p);
callback_->on_result(std::move(query)); callback_->on_result(std::move(query));
} else { } else {
auto future = auto future = Random::fast(0, 3) == 0
send_promise(rand_elem(workers_), Random::fast(0, 3) == 0 ? ActorSendType::Immediate : ActorSendType::Later, ? send_promise<ActorSendType::Immediate>(rand_elem(workers_), &Worker::query, x, p)
&Worker::query, x, p); : send_promise<ActorSendType::Later>(rand_elem(workers_), &Worker::query, x, p);
if (future.is_ready()) { if (future.is_ready()) {
query.result = future.move_as_ok(); query.result = future.move_as_ok();
callback_->on_result(std::move(query)); callback_->on_result(std::move(query));
@ -304,8 +304,8 @@ class SimpleActor final : public Actor {
} }
q_++; q_++;
p_ = Random::fast(0, 1) ? 1 : 10000; p_ = Random::fast(0, 1) ? 1 : 10000;
auto future = send_promise(worker_, Random::fast(0, 3) == 0 ? ActorSendType::Immediate : ActorSendType::Later, auto future = Random::fast(0, 3) == 0 ? send_promise<ActorSendType::Immediate>(worker_, &Worker::query, q_, p_)
&Worker::query, q_, p_); : send_promise<ActorSendType::Later>(worker_, &Worker::query, q_, p_);
if (future.is_ready()) { if (future.is_ready()) {
auto result = future.move_as_ok(); auto result = future.move_as_ok();
CHECK(result == fast_pow_mod_uint32(q_, p_)); CHECK(result == fast_pow_mod_uint32(q_, p_));