Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics in ActorImpl
[simgrid.git] / src / kernel / actor / ActorImpl.hpp
1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
7 #define SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
8
9 #include "Simcall.hpp"
10 #include "simgrid/kernel/Timer.hpp"
11 #include "simgrid/s4u/Actor.hpp"
12 #include "xbt/PropertyHolder.hpp"
13 #include <boost/intrusive/list.hpp>
14 #include <functional>
15 #include <list>
16 #include <map>
17 #include <memory>
18
19 namespace simgrid {
20 namespace kernel {
21 namespace actor {
22 class ProcessArg;
23
24 class XBT_PUBLIC ActorRestartingTrait {
25   bool auto_restart_ = false;
26   int restart_count_ = 0;
27
28   friend ActorImpl;
29
30 public:
31   bool has_to_auto_restart() const { return auto_restart_; }
32   void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
33   int get_restart_count() const { return restart_count_; }
34 };
35
36 class XBT_PUBLIC ActorImpl : public xbt::PropertyHolder, public ActorRestartingTrait {
37   s4u::Host* host_   = nullptr; /* the host on which the actor is running */
38   xbt::string name_;
39   aid_t pid_         = 0;
40   aid_t ppid_        = -1;
41   bool daemon_       = false; /* Daemon actors are automatically killed when the last non-daemon leaves */
42   unsigned stacksize_; // set to default value in constructor
43
44   std::vector<activity::MailboxImpl*> mailboxes;
45   friend activity::MailboxImpl;
46
47 public:
48   ActorImpl(xbt::string name, s4u::Host* host);
49   ActorImpl(const ActorImpl&) = delete;
50   ActorImpl& operator=(const ActorImpl&) = delete;
51   ~ActorImpl();
52
53   /** Retrieve the actor implementation from its PID (or nullptr if non-existent) */
54   static ActorImpl* by_pid(aid_t pid);
55
56   static ActorImpl* self();
57   double get_kill_time() const;
58   void set_kill_time(double kill_time);
59   boost::intrusive::list_member_hook<> host_actor_list_hook;     /* resource::HostImpl::actor_list_ */
60   boost::intrusive::list_member_hook<> kernel_destroy_list_hook; /* EngineImpl actors_to_destroy */
61   boost::intrusive::list_member_hook<> smx_synchro_hook;       /* {mutex,cond,sem}->sleeping */
62
63   const xbt::string& get_name() const { return name_; }
64   const char* get_cname() const { return name_.c_str(); }
65
66   // Accessors to private fields
67   s4u::Host* get_host() const { return host_; }
68   void set_host(s4u::Host* dest);
69   aid_t get_pid() const { return pid_; }
70   aid_t get_ppid() const { return ppid_; }
71   void set_ppid(aid_t ppid) { ppid_ = ppid; }
72   bool is_daemon() const { return daemon_; } /** Whether this actor has been daemonized */
73   bool is_maestro() const; /** Whether this actor is actually maestro (cheap call but may segfault before actor creation
74                               / after terminaison) */
75   void set_stacksize(unsigned stacksize) { stacksize_ = stacksize; }
76   unsigned get_stacksize() const { return stacksize_; }
77
78   std::unique_ptr<context::Context> context_; /* the context (uctx/raw/thread) that executes the user function */
79
80   std::exception_ptr exception_;
81   bool suspended_ = false;
82
83   activity::ActivityImplPtr waiting_synchro_ = nullptr; /* the current blocking synchro if any */
84   std::list<activity::ActivityImplPtr> activities_;     /* the current non-blocking synchros */
85   Simcall simcall_;
86   /* list of functions executed when the actor dies */
87   std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit =
88       std::make_shared<std::vector<std::function<void(bool)>>>();
89
90   std::function<void()> code_; // to restart the actor on host reboot
91   timer::Timer* kill_timer_ = nullptr;
92
93 private:
94   /* Refcounting */
95   std::atomic_int_fast32_t refcount_{0};
96
97 public:
98   int get_refcount() const { return refcount_; }
99   friend void intrusive_ptr_add_ref(ActorImpl* actor)
100   {
101     // This whole memory consistency semantic drives me nuts.
102     // std::memory_order_relaxed proves to not be enough: There is a threading issue when actors commit suicide.
103     //   My guess is that the maestro context wants to propagate changes to the actor's fields after the
104     //   actor context frees that memory area or something. But I'm not 100% certain of what's going on.
105     // std::memory_order_seq_cst works but that's rather demanding.
106     // AFAIK, std::memory_order_acq_rel works on all tested platforms, so let's stick to it.
107     // Reducing the requirements to _relaxed would require to fix our suicide procedure, which is a messy piece of code.
108     actor->refcount_.fetch_add(1, std::memory_order_acq_rel);
109   }
110   friend void intrusive_ptr_release(ActorImpl* actor)
111   {
112     // inspired from http://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html
113     if (actor->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
114       // Make sure that any changes done on other threads before their acquire are committed before our delete
115       // http://stackoverflow.com/questions/27751025/why-is-an-acquire-barrier-needed-before-deleting-the-data-in-an-atomically-refer
116       std::atomic_thread_fence(std::memory_order_acquire);
117       delete actor;
118     }
119   }
120
121   /* S4U/implem interfaces */
122 private:
123   s4u::Actor piface_; // Our interface is part of ourselves
124
125   void undaemonize();
126
127 public:
128   s4u::ActorPtr get_iface() { return s4u::ActorPtr(&piface_); }
129   s4u::Actor* get_ciface() { return &piface_; }
130
131   ActorImplPtr init(const std::string& name, s4u::Host* host) const;
132   ActorImpl* start(const ActorCode& code);
133
134   static ActorImplPtr create(const std::string& name, const ActorCode& code, void* data, s4u::Host* host,
135                              const ActorImpl* parent_actor);
136   static ActorImplPtr create(ProcessArg* args);
137   static ActorImplPtr attach(const std::string& name, void* data, s4u::Host* host);
138   static void detach();
139   void cleanup_from_self();
140   void cleanup_from_kernel();
141   void exit();
142   void kill(ActorImpl* actor) const;
143   void kill_all() const;
144
145   void yield();
146   void daemonize();
147   bool is_suspended() const { return suspended_; }
148   s4u::Actor* restart();
149   void suspend();
150   void resume();
151   activity::ActivityImplPtr join(const ActorImpl* actor, double timeout);
152   activity::ActivityImplPtr sleep(double duration);
153   /** Ask the actor to throw an exception right away */
154   void throw_exception(std::exception_ptr e);
155
156   /** execute the pending simcall -- must be called from the maestro context */
157   void simcall_handle(int value);
158   /** Terminates a simcall currently executed in maestro context. The actor will be restarted in the next scheduling
159    * round */
160   void simcall_answer();
161 };
162
163 class ProcessArg {
164 public:
165   std::string name;
166   std::function<void()> code;
167   void* data                                                               = nullptr;
168   s4u::Host* host                                                          = nullptr;
169   double kill_time                                                         = 0.0;
170   const std::unordered_map<std::string, std::string> properties{};
171   bool auto_restart                                                        = false;
172   bool daemon_;
173   /* list of functions executed when the actor dies */
174   const std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit;
175   int restart_count_ = 0;
176
177   ProcessArg()                  = delete;
178   ProcessArg(const ProcessArg&) = delete;
179   ProcessArg& operator=(const ProcessArg&) = delete;
180
181   explicit ProcessArg(const std::string& name, const std::function<void()>& code, void* data, s4u::Host* host,
182                       double kill_time, const std::unordered_map<std::string, std::string>& properties,
183                       bool auto_restart, bool daemon, int restart_count)
184       : name(name)
185       , code(code)
186       , data(data)
187       , host(host)
188       , kill_time(kill_time)
189       , properties(properties)
190       , auto_restart(auto_restart)
191       , daemon_(daemon)
192       , restart_count_(restart_count)
193   {
194   }
195
196   explicit ProcessArg(s4u::Host* host, ActorImpl* actor)
197       : name(actor->get_name())
198       , code(actor->code_)
199       , data(actor->get_ciface()->get_data<void>())
200       , host(host)
201       , kill_time(actor->get_kill_time())
202       , auto_restart(actor->has_to_auto_restart())
203       , daemon_(actor->is_daemon())
204       , on_exit(actor->on_exit)
205       , restart_count_(actor->get_restart_count() + 1)
206   {
207   }
208 };
209
210 /* Used to keep the list of actors blocked on a synchro  */
211 using SynchroList =
212     boost::intrusive::list<ActorImpl, boost::intrusive::member_hook<ActorImpl, boost::intrusive::list_member_hook<>,
213                                                                     &ActorImpl::smx_synchro_hook>>;
214
215 XBT_PUBLIC void create_maestro(const std::function<void()>& code);
216 XBT_PUBLIC unsigned long get_maxpid();
217 XBT_PUBLIC unsigned long* get_maxpid_addr(); // In MC mode, the application sends this pointers to the MC
218
219 } // namespace actor
220 } // namespace kernel
221 } // namespace simgrid
222
223 #endif