aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdactor/td/actor/impl/Actor-decl.h
blob: b0e75bd21ca493ada5bd0f4b1f0f3063a5787dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/ActorInfo-decl.h"
#include "td/actor/impl/Event.h"

#include "td/utils/ObjectPool.h"
#include "td/utils/Observer.h"
#include "td/utils/Slice.h"

#include <memory>

namespace td {

class Actor : public ObserverBase {
 public:
  using Deleter = ActorInfo::Deleter;
  Actor() = default;
  Actor(const Actor &) = delete;
  Actor &operator=(const Actor &) = delete;
  Actor(Actor &&other) noexcept;
  Actor &operator=(Actor &&other) noexcept;
  ~Actor() override {
    if (!empty()) {
      do_stop();
    }
  }

  virtual void start_up() {
    yield();
  }
  virtual void tear_down() {
  }
  virtual void wakeup() {
    loop();
  }
  virtual void hangup() {
    stop();
  }
  virtual void hangup_shared() {
    // ignore
  }
  virtual void timeout_expired() {
    loop();
  }
  virtual void raw_event(const Event::Raw &event) {
  }
  virtual void loop() {
  }

  // TODO: not called in events. Can't use stop, or migrate inside of them
  virtual void on_start_migrate(int32 sched_id) {
  }
  virtual void on_finish_migrate() {
  }

  void notify() override;

  // proxy to scheduler
  void yield();
  void stop();
  void do_stop();
  bool has_timeout() const;
  double get_timeout() const;
  void set_timeout_in(double timeout_in);
  void set_timeout_at(double timeout_at);
  void cancel_timeout();
  void migrate(int32 sched_id);
  void do_migrate(int32 sched_id);

  uint64 get_link_token();
  std::weak_ptr<ActorContext> get_context_weak_ptr() const;
  std::shared_ptr<ActorContext> set_context(std::shared_ptr<ActorContext> context);
  string set_tag(string tag);

  void always_wait_for_mailbox();

  // for ActorInfo mostly
  void init(ObjectPool<ActorInfo>::OwnerPtr &&info);
  ActorInfo *get_info();
  const ActorInfo *get_info() const;
  ObjectPool<ActorInfo>::OwnerPtr clear();

  bool empty() const;

  template <class FuncT, class... ArgsT>
  auto self_closure(FuncT &&func, ArgsT &&...args);

  template <class SelfT, class FuncT, class... ArgsT>
  auto self_closure(SelfT *self, FuncT &&func, ArgsT &&...args);

  template <class LambdaT>
  auto self_lambda(LambdaT &&lambda);

  // proxy to info_
  ActorId<> actor_id();
  template <class SelfT>
  ActorId<SelfT> actor_id(SelfT *self);

  template <class SelfT>
  ActorShared<SelfT> actor_shared(SelfT *self, uint64 id = static_cast<uint64>(-1));

  Slice get_name() const;

 private:
  ObjectPool<ActorInfo>::OwnerPtr info_;
};

template <class ActorT>
class ActorTraits {
 public:
  static constexpr bool need_context = true;
  static constexpr bool need_start_up = true;
};

}  // namespace td