2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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
|
|
|
|
|
|
|
|
#include "td/actor/impl/ActorId-decl.h"
|
|
|
|
#include "td/actor/impl/Event.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Heap.h"
|
|
|
|
#include "td/utils/List.h"
|
|
|
|
#include "td/utils/ObjectPool.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Actor;
|
|
|
|
|
|
|
|
class ActorContext {
|
|
|
|
public:
|
|
|
|
ActorContext() = default;
|
|
|
|
ActorContext(const ActorContext &) = delete;
|
|
|
|
ActorContext &operator=(const ActorContext &) = delete;
|
|
|
|
ActorContext(ActorContext &&) = delete;
|
|
|
|
ActorContext &operator=(ActorContext &&) = delete;
|
|
|
|
virtual ~ActorContext() = default;
|
2019-08-01 21:12:04 +02:00
|
|
|
|
|
|
|
virtual int32 get_id() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:57:05 +02:00
|
|
|
void set_tag(string tag) {
|
|
|
|
tag_storage_ = std::move(tag);
|
|
|
|
tag_ = tag_storage_.c_str();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const char *tag_ = nullptr;
|
2020-08-17 12:57:05 +02:00
|
|
|
string tag_storage_; // sometimes tag_ == tag_storage_.c_str()
|
2018-12-31 20:04:05 +01:00
|
|
|
std::weak_ptr<ActorContext> this_ptr_;
|
|
|
|
};
|
|
|
|
|
2021-07-04 17:22:20 +02:00
|
|
|
class ActorInfo final
|
2018-12-31 20:04:05 +01:00
|
|
|
: private ListNode
|
2021-07-04 17:22:20 +02:00
|
|
|
, private HeapNode {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
enum class Deleter : uint8 { Destroy, None };
|
|
|
|
|
|
|
|
ActorInfo() = default;
|
|
|
|
~ActorInfo() = default;
|
|
|
|
|
|
|
|
ActorInfo(ActorInfo &&) = delete;
|
|
|
|
ActorInfo &operator=(ActorInfo &&) = delete;
|
|
|
|
|
|
|
|
ActorInfo(const ActorInfo &) = delete;
|
|
|
|
ActorInfo &operator=(const ActorInfo &) = delete;
|
|
|
|
|
|
|
|
void init(int32 sched_id, Slice name, ObjectPool<ActorInfo>::OwnerPtr &&this_ptr, Actor *actor_ptr, Deleter deleter,
|
2021-11-03 13:10:43 +01:00
|
|
|
bool need_context, bool need_start_up);
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_actor_moved(Actor *actor_new_ptr);
|
|
|
|
|
|
|
|
template <class ActorT>
|
2018-09-27 03:19:03 +02:00
|
|
|
ActorOwn<ActorT> transfer_ownership_to_scheduler(unique_ptr<ActorT> actor);
|
2018-12-31 20:04:05 +01:00
|
|
|
void clear();
|
|
|
|
void destroy_actor();
|
|
|
|
|
|
|
|
bool empty() const;
|
|
|
|
void start_migrate(int32 to_sched_id);
|
|
|
|
bool is_migrating() const;
|
|
|
|
int32 migrate_dest() const;
|
|
|
|
std::pair<int32, bool> migrate_dest_flag_atomic() const;
|
|
|
|
|
|
|
|
void finish_migrate();
|
|
|
|
|
|
|
|
ActorId<> actor_id();
|
|
|
|
template <class SelfT>
|
|
|
|
ActorId<SelfT> actor_id(SelfT *self);
|
|
|
|
Actor *get_actor_unsafe();
|
|
|
|
const Actor *get_actor_unsafe() const;
|
|
|
|
|
2019-04-23 10:56:32 +02:00
|
|
|
std::shared_ptr<ActorContext> set_context(std::shared_ptr<ActorContext> context);
|
2021-10-07 14:28:52 +02:00
|
|
|
std::weak_ptr<ActorContext> get_context_weak_ptr() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorContext *get_context();
|
|
|
|
const ActorContext *get_context() const;
|
|
|
|
CSlice get_name() const;
|
|
|
|
|
|
|
|
HeapNode *get_heap_node();
|
|
|
|
const HeapNode *get_heap_node() const;
|
|
|
|
static ActorInfo *from_heap_node(HeapNode *node);
|
|
|
|
|
|
|
|
ListNode *get_list_node();
|
|
|
|
const ListNode *get_list_node() const;
|
|
|
|
static ActorInfo *from_list_node(ListNode *node);
|
|
|
|
|
|
|
|
void start_run();
|
|
|
|
bool is_running() const;
|
|
|
|
void finish_run();
|
|
|
|
|
|
|
|
vector<Event> mailbox_;
|
|
|
|
|
2021-11-03 13:10:43 +01:00
|
|
|
bool need_context() const;
|
|
|
|
bool need_start_up() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_wait_generation(uint32 wait_generation);
|
|
|
|
bool must_wait(uint32 wait_generation) const;
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
Deleter deleter_ = Deleter::None;
|
2021-11-03 13:10:43 +01:00
|
|
|
bool need_context_ = true;
|
|
|
|
bool need_start_up_ = true;
|
2018-10-26 16:11:20 +02:00
|
|
|
bool is_running_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
uint32 wait_generation_{0};
|
|
|
|
|
|
|
|
std::atomic<int32> sched_id_{0};
|
|
|
|
Actor *actor_ = nullptr;
|
|
|
|
|
|
|
|
#ifdef TD_DEBUG
|
|
|
|
string name_;
|
|
|
|
#endif
|
|
|
|
std::shared_ptr<ActorContext> context_;
|
|
|
|
};
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const ActorInfo &info);
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|