1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
7 #define SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
10 #include "simgrid/kernel/Timer.hpp"
11 #include "simgrid/s4u/Actor.hpp"
12 #include "xbt/PropertyHolder.hpp"
13 #include <boost/intrusive/list.hpp>
24 class XBT_PUBLIC ActorImpl : public xbt::PropertyHolder {
25 s4u::Host* host_ = nullptr; /* the host on which the actor is running */
28 bool daemon_ = false; /* Daemon actors are automatically killed when the last non-daemon leaves */
29 bool auto_restart_ = false;
30 int restart_count_ = 0;
31 unsigned stacksize_; // set to default value in constructor
33 std::vector<activity::MailboxImpl*> mailboxes;
34 friend activity::MailboxImpl;
38 ActorImpl(xbt::string name, s4u::Host* host);
39 ActorImpl(const ActorImpl&) = delete;
40 ActorImpl& operator=(const ActorImpl&) = delete;
43 /** Retrieve the actor implementation from its PID (or nullptr if non-existent) */
44 static ActorImpl* by_pid(aid_t pid);
46 static ActorImpl* self();
47 double get_kill_time() const;
48 void set_kill_time(double kill_time);
49 boost::intrusive::list_member_hook<> host_actor_list_hook; /* resource::HostImpl::actor_list_ */
50 boost::intrusive::list_member_hook<> kernel_destroy_list_hook; /* EngineImpl actors_to_destroy */
51 boost::intrusive::list_member_hook<> smx_synchro_hook; /* {mutex,cond,sem}->sleeping */
53 const xbt::string& get_name() const { return name_; }
54 const char* get_cname() const { return name_.c_str(); }
56 // Accessors to private fields
57 s4u::Host* get_host() const { return host_; }
58 void set_host(s4u::Host* dest);
59 aid_t get_pid() const { return pid_; }
60 aid_t get_ppid() const { return ppid_; }
61 void set_ppid(aid_t ppid) { ppid_ = ppid; }
62 bool is_daemon() const { return daemon_; } /** Whether this actor has been daemonized */
63 bool is_maestro() const; /** Whether this actor is actually maestro (cheap call but may segfault before actor creation
64 / after terminaison) */
65 bool has_to_auto_restart() const { return auto_restart_; }
66 void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
67 int get_restart_count() { return restart_count_; }
68 void set_stacksize(unsigned stacksize) { stacksize_ = stacksize; }
69 unsigned get_stacksize() const { return stacksize_; }
71 std::unique_ptr<context::Context> context_; /* the context (uctx/raw/thread) that executes the user function */
73 std::exception_ptr exception_;
74 bool finished_ = false;
75 bool suspended_ = false;
77 activity::ActivityImplPtr waiting_synchro_ = nullptr; /* the current blocking synchro if any */
78 std::list<activity::ActivityImplPtr> activities_; /* the current non-blocking synchros */
80 /* list of functions executed when the actor dies */
81 std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit =
82 std::make_shared<std::vector<std::function<void(bool)>>>();
84 std::function<void()> code_;
85 timer::Timer* kill_timer_ = nullptr;
89 std::atomic_int_fast32_t refcount_{0};
92 int get_refcount() const { return refcount_; }
93 friend void intrusive_ptr_add_ref(ActorImpl* actor)
95 // This whole memory consistency semantic drives me nuts.
96 // std::memory_order_relaxed proves to not be enough: There is a threading issue when actors commit suicide.
97 // My guess is that the maestro context wants to propagate changes to the actor's fields after the
98 // actor context frees that memory area or something. But I'm not 100% certain of what's going on.
99 // std::memory_order_seq_cst works but that's rather demanding.
100 // AFAIK, std::memory_order_acq_rel works on all tested platforms, so let's stick to it.
101 // Reducing the requirements to _relaxed would require to fix our suicide procedure, which is a messy piece of code.
102 actor->refcount_.fetch_add(1, std::memory_order_acq_rel);
104 friend void intrusive_ptr_release(ActorImpl* actor)
106 // inspired from http://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html
107 if (actor->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
108 // Make sure that any changes done on other threads before their acquire are committed before our delete
109 // http://stackoverflow.com/questions/27751025/why-is-an-acquire-barrier-needed-before-deleting-the-data-in-an-atomically-refer
110 std::atomic_thread_fence(std::memory_order_acquire);
115 /* S4U/implem interfaces */
117 s4u::Actor piface_; // Our interface is part of ourselves
119 void cleanup_from_simix();
123 s4u::ActorPtr get_iface() { return s4u::ActorPtr(&piface_); }
124 s4u::Actor* get_ciface() { return &piface_; }
126 ActorImplPtr init(const std::string& name, s4u::Host* host) const;
127 ActorImpl* start(const ActorCode& code);
129 static ActorImplPtr create(const std::string& name, const ActorCode& code, void* data, s4u::Host* host,
130 const ActorImpl* parent_actor);
131 static ActorImplPtr create(ProcessArg* args);
132 static ActorImplPtr attach(const std::string& name, void* data, s4u::Host* host);
133 static void detach();
136 void kill(ActorImpl* actor) const;
137 void kill_all() const;
141 bool is_suspended() const { return suspended_; }
142 s4u::Actor* restart();
145 activity::ActivityImplPtr join(const ActorImpl* actor, double timeout);
146 activity::ActivityImplPtr sleep(double duration);
147 /** Ask the actor to throw an exception right away */
148 void throw_exception(std::exception_ptr e);
150 /** execute the pending simcall -- must be called from the maestro context */
151 void simcall_handle(int value);
152 /** Terminates a simcall currently executed in maestro context. The actor will be restarted in the next scheduling
154 void simcall_answer();
160 std::function<void()> code;
161 void* data = nullptr;
162 s4u::Host* host = nullptr;
163 double kill_time = 0.0;
164 const std::unordered_map<std::string, std::string> properties{};
165 bool auto_restart = false;
166 bool daemon_ = false;
167 /* list of functions executed when the actor dies */
168 const std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit;
169 int restart_count_ = 0;
171 ProcessArg() = delete;
172 ProcessArg(const ProcessArg&) = delete;
173 ProcessArg& operator=(const ProcessArg&) = delete;
175 explicit ProcessArg(const std::string& name, const std::function<void()>& code, void* data, s4u::Host* host,
176 double kill_time, const std::unordered_map<std::string, std::string>& properties,
177 bool auto_restart, int restart_count)
182 , kill_time(kill_time)
183 , properties(properties)
184 , auto_restart(auto_restart)
185 , restart_count_(restart_count)
189 explicit ProcessArg(s4u::Host* host, ActorImpl* actor)
190 : name(actor->get_name())
192 , data(actor->get_ciface()->get_data<void>())
194 , kill_time(actor->get_kill_time())
195 , auto_restart(actor->has_to_auto_restart())
196 , daemon_(actor->is_daemon())
197 , on_exit(actor->on_exit)
198 , restart_count_(actor->get_restart_count() + 1)
203 /* Used to keep the list of actors blocked on a synchro */
205 boost::intrusive::list<ActorImpl, boost::intrusive::member_hook<ActorImpl, boost::intrusive::list_member_hook<>,
206 &ActorImpl::smx_synchro_hook>>;
208 XBT_PUBLIC void create_maestro(const std::function<void()>& code);
209 XBT_PUBLIC unsigned long get_maxpid();
210 XBT_PUBLIC unsigned long* get_maxpid_addr(); // In MC mode, the application sends this pointers to the MC
213 } // namespace kernel
214 } // namespace simgrid