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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2019-02-13 00:29:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/impl/Actor-decl.h"
|
|
|
|
#include "td/actor/impl/ActorId-decl.h"
|
|
|
|
#include "td/actor/impl/EventFull-decl.h"
|
|
|
|
|
|
|
|
#include "td/utils/Closure.h"
|
|
|
|
#include "td/utils/Heap.h"
|
|
|
|
#include "td/utils/List.h"
|
2020-10-08 17:30:36 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/MovableValue.h"
|
|
|
|
#include "td/utils/MpscPollableQueue.h"
|
|
|
|
#include "td/utils/ObjectPool.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/Poll.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2018-09-18 15:43:16 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/type_traits.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
extern int VERBOSITY_NAME(actor);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class ActorInfo;
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-07-03 21:50:41 +02:00
|
|
|
enum class ActorSendType { Immediate, Later, LaterWeak };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class Scheduler;
|
|
|
|
class SchedulerGuard {
|
|
|
|
public:
|
2018-08-16 15:56:16 +02:00
|
|
|
explicit SchedulerGuard(Scheduler *scheduler, bool lock = true);
|
2018-12-31 20:04:05 +01:00
|
|
|
~SchedulerGuard();
|
|
|
|
SchedulerGuard(const SchedulerGuard &other) = delete;
|
|
|
|
SchedulerGuard &operator=(const SchedulerGuard &other) = delete;
|
|
|
|
SchedulerGuard(SchedulerGuard &&other) = default;
|
|
|
|
SchedulerGuard &operator=(SchedulerGuard &&other) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MovableValue<bool> is_valid_ = true;
|
2018-08-16 15:56:16 +02:00
|
|
|
bool is_locked_;
|
2018-12-31 20:04:05 +01:00
|
|
|
Scheduler *scheduler_;
|
|
|
|
ActorContext *save_context_;
|
|
|
|
Scheduler *save_scheduler_;
|
|
|
|
const char *save_tag_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Scheduler {
|
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void on_finish() = 0;
|
|
|
|
virtual void register_at_finish(std::function<void()>) = 0;
|
|
|
|
};
|
|
|
|
Scheduler() = default;
|
|
|
|
Scheduler(const Scheduler &) = delete;
|
|
|
|
Scheduler &operator=(const Scheduler &) = delete;
|
|
|
|
Scheduler(Scheduler &&) = delete;
|
|
|
|
Scheduler &operator=(Scheduler &&) = delete;
|
|
|
|
~Scheduler();
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void init(int32 id, std::vector<std::shared_ptr<MpscPollableQueue<EventFull>>> outbound, Callback *callback);
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
int32 sched_id() const;
|
|
|
|
int32 sched_count() const;
|
|
|
|
|
|
|
|
template <class ActorT, class... Args>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> create_actor(Slice name, Args &&... args);
|
|
|
|
template <class ActorT, class... Args>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> create_actor_on_scheduler(Slice name, int32 sched_id, Args &&... args);
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_actor(Slice name, ActorT *actor_ptr, int32 sched_id = -1);
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_actor(Slice name, unique_ptr<ActorT> actor_ptr, int32 sched_id = -1);
|
|
|
|
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_existing_actor(unique_ptr<ActorT> actor_ptr);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
template <ActorSendType send_type, class EventT>
|
|
|
|
void send_lambda(ActorRef actor_ref, EventT &&lambda);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
template <ActorSendType send_type, class EventT>
|
|
|
|
void send_closure(ActorRef actor_ref, EventT &&closure);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
template <ActorSendType send_type>
|
|
|
|
void send(ActorRef actor_ref, Event &&event);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void hack(const ActorId<> &actor_id, Event &&event) {
|
|
|
|
actor_id.get_actor_unsafe()->raw_event(event.data);
|
|
|
|
}
|
|
|
|
void before_tail_send(const ActorId<> &actor_id);
|
|
|
|
|
2018-09-11 00:20:41 +02:00
|
|
|
static void subscribe(PollableFd fd, PollFlags flags = PollFlags::ReadWrite());
|
|
|
|
static void unsubscribe(PollableFdRef fd);
|
|
|
|
static void unsubscribe_before_close(PollableFdRef fd);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void yield_actor(Actor *actor);
|
|
|
|
void stop_actor(Actor *actor);
|
|
|
|
void do_stop_actor(Actor *actor);
|
|
|
|
uint64 get_link_token(Actor *actor);
|
|
|
|
void migrate_actor(Actor *actor, int32 dest_sched_id);
|
|
|
|
void do_migrate_actor(Actor *actor, int32 dest_sched_id);
|
|
|
|
void start_migrate_actor(Actor *actor, int32 dest_sched_id);
|
|
|
|
void finish_migrate_actor(Actor *actor);
|
|
|
|
|
|
|
|
bool has_actor_timeout(const Actor *actor) const;
|
|
|
|
void set_actor_timeout_in(Actor *actor, double timeout);
|
|
|
|
void set_actor_timeout_at(Actor *actor, double timeout_at);
|
|
|
|
void cancel_actor_timeout(Actor *actor);
|
|
|
|
|
|
|
|
void finish();
|
|
|
|
void yield();
|
2018-09-18 15:43:16 +02:00
|
|
|
void run(Timestamp timeout);
|
|
|
|
void run_no_guard(Timestamp timeout);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void wakeup();
|
|
|
|
|
|
|
|
static Scheduler *instance();
|
|
|
|
static ActorContext *&context();
|
|
|
|
static void on_context_updated();
|
|
|
|
|
|
|
|
SchedulerGuard get_guard();
|
2018-08-16 15:56:16 +02:00
|
|
|
SchedulerGuard get_const_guard();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-18 15:43:16 +02:00
|
|
|
Timestamp get_timeout();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
static void set_scheduler(Scheduler *scheduler);
|
2021-07-03 22:51:36 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class ServiceActor final : public Actor {
|
|
|
|
public:
|
|
|
|
void set_queue(std::shared_ptr<MpscPollableQueue<EventFull>> queues);
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<MpscPollableQueue<EventFull>> inbound_;
|
2018-08-16 15:56:16 +02:00
|
|
|
bool subscribed_{false};
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
|
|
|
void tear_down() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
friend class ServiceActor;
|
|
|
|
|
|
|
|
void do_event(ActorInfo *actor, Event &&event);
|
|
|
|
|
|
|
|
void enter_actor(ActorInfo *actor_info);
|
|
|
|
void exit_actor(ActorInfo *actor_info);
|
|
|
|
|
|
|
|
void yield_actor(ActorInfo *actor_info);
|
|
|
|
void stop_actor(ActorInfo *actor_info);
|
|
|
|
void do_stop_actor(ActorInfo *actor_info);
|
|
|
|
uint64 get_link_token(ActorInfo *actor_info);
|
|
|
|
void migrate_actor(ActorInfo *actor_info, int32 dest_sched_id);
|
|
|
|
void do_migrate_actor(ActorInfo *actor_info, int32 dest_sched_id);
|
|
|
|
void start_migrate_actor(ActorInfo *actor_info, int32 dest_sched_id);
|
|
|
|
|
|
|
|
bool has_actor_timeout(const ActorInfo *actor_info) const;
|
|
|
|
void set_actor_timeout_in(ActorInfo *actor_info, double timeout);
|
|
|
|
void set_actor_timeout_at(ActorInfo *actor_info, double timeout_at);
|
|
|
|
void cancel_actor_timeout(ActorInfo *actor_info);
|
|
|
|
|
|
|
|
void register_migrated_actor(ActorInfo *actor_info);
|
|
|
|
void add_to_mailbox(ActorInfo *actor_info, Event &&event);
|
|
|
|
void clear_mailbox(ActorInfo *actor_info);
|
|
|
|
|
|
|
|
template <class RunFuncT, class EventFuncT>
|
|
|
|
void flush_mailbox(ActorInfo *actor_info, const RunFuncT &run_func, const EventFuncT &event_func);
|
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
template <ActorSendType send_type, class RunFuncT, class EventFuncT>
|
|
|
|
void send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void inc_wait_generation();
|
|
|
|
|
2018-09-18 15:43:16 +02:00
|
|
|
Timestamp run_timeout();
|
2018-12-31 20:04:05 +01:00
|
|
|
void run_mailbox();
|
2018-09-18 15:43:16 +02:00
|
|
|
Timestamp run_events();
|
|
|
|
void run_poll(Timestamp timeout);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class ActorT>
|
|
|
|
ActorOwn<ActorT> register_actor_impl(Slice name, ActorT *actor_ptr, Actor::Deleter deleter, int32 sched_id);
|
|
|
|
void destroy_actor(ActorInfo *actor_info);
|
|
|
|
|
|
|
|
static TD_THREAD_LOCAL Scheduler *scheduler_;
|
|
|
|
static TD_THREAD_LOCAL ActorContext *context_;
|
|
|
|
|
|
|
|
Callback *callback_ = nullptr;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<ObjectPool<ActorInfo>> actor_info_pool_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 actor_count_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
ListNode pending_actors_list_;
|
|
|
|
ListNode ready_actors_list_;
|
|
|
|
KHeap<double> timeout_queue_;
|
|
|
|
|
|
|
|
std::map<ActorInfo *, std::vector<Event>> pending_events_;
|
|
|
|
|
|
|
|
ServiceActor service_actor_;
|
|
|
|
Poll poll_;
|
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
bool yield_flag_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool has_guard_ = false;
|
|
|
|
bool close_flag_ = false;
|
|
|
|
|
2020-04-11 21:30:23 +02:00
|
|
|
uint32 wait_generation_ = 1;
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 sched_id_ = 0;
|
|
|
|
int32 sched_n_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<MpscPollableQueue<EventFull>> inbound_queue_;
|
|
|
|
std::vector<std::shared_ptr<MpscPollableQueue<EventFull>>> outbound_queues_;
|
|
|
|
|
|
|
|
std::shared_ptr<ActorContext> save_context_;
|
|
|
|
|
|
|
|
struct EventContext {
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 dest_sched_id{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
enum Flags { Stop = 1, Migrate = 2 };
|
|
|
|
int32 flags{0};
|
2018-10-26 16:11:20 +02:00
|
|
|
uint64 link_token{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
ActorInfo *actor_info{nullptr};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-10-26 16:11:20 +02:00
|
|
|
EventContext *event_context_ptr_{nullptr};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
friend class GlobalScheduler;
|
|
|
|
friend class SchedulerGuard;
|
|
|
|
friend class EventGuard;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*** Interface to current scheduler ***/
|
|
|
|
template <class ActorT, class... Args>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> create_actor(Slice name, Args &&... args);
|
|
|
|
template <class ActorT, class... Args>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> create_actor_on_scheduler(Slice name, int32 sched_id, Args &&... args);
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_actor(Slice name, ActorT *actor_ptr, int32 sched_id = -1);
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_actor(Slice name, unique_ptr<ActorT> actor_ptr, int32 sched_id = -1);
|
|
|
|
|
|
|
|
template <class ActorT>
|
|
|
|
TD_WARN_UNUSED_RESULT ActorOwn<ActorT> register_existing_actor(unique_ptr<ActorT> actor_ptr);
|
|
|
|
|
|
|
|
template <class ActorIdT, class FunctionT, class... ArgsT>
|
|
|
|
void send_closure(ActorIdT &&actor_id, FunctionT function, ArgsT &&... args) {
|
|
|
|
using ActorT = typename std::decay_t<ActorIdT>::ActorT;
|
|
|
|
using FunctionClassT = member_function_class_t<FunctionT>;
|
|
|
|
static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure");
|
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send_closure<ActorSendType::Immediate>(
|
|
|
|
std::forward<ActorIdT>(actor_id), create_immediate_closure(function, std::forward<ArgsT>(args)...));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorIdT, class FunctionT, class... ArgsT>
|
|
|
|
void send_closure_later(ActorIdT &&actor_id, FunctionT function, ArgsT &&... args) {
|
|
|
|
using ActorT = typename std::decay_t<ActorIdT>::ActorT;
|
|
|
|
using FunctionClassT = member_function_class_t<FunctionT>;
|
|
|
|
static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure");
|
|
|
|
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send<ActorSendType::Later>(std::forward<ActorIdT>(actor_id),
|
|
|
|
Event::delayed_closure(function, std::forward<ArgsT>(args)...));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void send_lambda(ActorRef actor_ref, ArgsT &&... args) {
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send_lambda<ActorSendType::Immediate>(actor_ref, std::forward<ArgsT>(args)...);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void send_event(ActorRef actor_ref, ArgsT &&... args) {
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send<ActorSendType::Immediate>(actor_ref, std::forward<ArgsT>(args)...);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void send_event_later(ActorRef actor_ref, ArgsT &&... args) {
|
2018-07-03 22:22:19 +02:00
|
|
|
Scheduler::instance()->send<ActorSendType::Later>(actor_ref, std::forward<ArgsT>(args)...);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void yield_scheduler();
|
|
|
|
} // namespace td
|