}
ActorImplPtr ActorImpl::create(ProcessArg* args)
{
- actor::ActorImplPtr actor = actor::ActorImpl::create(args->name, args->code, nullptr, args->host, nullptr);
- auto* naked_actor = actor.get();
- naked_actor->restart_count_ = args->restart_count_;
+ ActorImplPtr actor = ActorImpl::create(args->name, args->code, nullptr, args->host, nullptr);
+ actor->restart_count_ = args->restart_count_;
actor->set_properties(args->properties);
if (args->on_exit)
*actor->on_exit = *args->on_exit;
namespace actor {
class ProcessArg;
-class XBT_PUBLIC ActorImpl : public xbt::PropertyHolder {
+class XBT_PUBLIC ActorRestartingTrait {
+ bool auto_restart_ = false;
+ int restart_count_ = 0;
+
+ friend ActorImpl;
+
+public:
+ bool has_to_auto_restart() const { return auto_restart_; }
+ void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
+ int get_restart_count() const { return restart_count_; }
+};
+
+class XBT_PUBLIC ActorImpl : public xbt::PropertyHolder, public ActorRestartingTrait {
s4u::Host* host_ = nullptr; /* the host on which the actor is running */
aid_t pid_ = 0;
aid_t ppid_ = -1;
bool daemon_ = false; /* Daemon actors are automatically killed when the last non-daemon leaves */
- bool auto_restart_ = false;
- int restart_count_ = 0;
unsigned stacksize_; // set to default value in constructor
std::vector<activity::MailboxImpl*> mailboxes;
bool is_daemon() const { return daemon_; } /** Whether this actor has been daemonized */
bool is_maestro() const; /** Whether this actor is actually maestro (cheap call but may segfault before actor creation
/ after terminaison) */
- bool has_to_auto_restart() const { return auto_restart_; }
- void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
- int get_restart_count() { return restart_count_; }
void set_stacksize(unsigned stacksize) { stacksize_ = stacksize; }
unsigned get_stacksize() const { return stacksize_; }
double kill_time = 0.0;
const std::unordered_map<std::string, std::string> properties{};
bool auto_restart = false;
- bool daemon_ = false;
+ bool daemon_;
/* list of functions executed when the actor dies */
const std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit;
int restart_count_ = 0;
explicit ProcessArg(const std::string& name, const std::function<void()>& code, void* data, s4u::Host* host,
double kill_time, const std::unordered_map<std::string, std::string>& properties,
- bool auto_restart, int restart_count)
+ bool auto_restart, bool daemon, int restart_count)
: name(name)
, code(code)
, data(data)
, kill_time(kill_time)
, properties(properties)
, auto_restart(auto_restart)
+ , daemon_(daemon)
, restart_count_(restart_count)
{
}
simgrid::kernel::actor::ActorCode code = factory(std::move(actor->args));
auto* arg = new simgrid::kernel::actor::ProcessArg(actor_name, code, nullptr, host, kill_time, actor->properties,
- auto_restart, /*restart_count=*/0);
+ auto_restart, /*daemon=*/false, /*restart_count=*/0);
host->get_impl()->add_actor_at_boot(arg);
if (start_time > simgrid::s4u::Engine::get_clock()) {
arg = new simgrid::kernel::actor::ProcessArg(actor_name, code, nullptr, host, kill_time, actor->properties,
- auto_restart, /*restart_count=*/0);
+ auto_restart, /*daemon=*/false, /*restart_count=*/0);
XBT_DEBUG("Actor %s@%s will be started at time %f", arg->name.c_str(), arg->host->get_cname(), start_time);
simgrid::kernel::timer::Timer::set(start_time, [arg, auto_restart]() {
static void dummy()
{
XBT_INFO("I start");
- simgrid::s4u::this_actor::sleep_for(200);
- XBT_INFO("I stop");
+ simgrid::s4u::this_actor::sleep_for(200);
+ XBT_INFO("I stop");
}
static void dummy_daemon()
{
- simgrid::s4u::Actor::self()->daemonize();
while (simgrid::s4u::this_actor::get_host()->is_on()) {
XBT_INFO("Hello from the infinite loop");
simgrid::s4u::this_actor::sleep_for(80.0);
XBT_INFO("starting a dummy process on %s", host->get_cname());
simgrid::s4u::ActorPtr dummy_actor = simgrid::s4u::Actor::create("Dummy", host, dummy);
- dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
+ dummy_actor->on_exit([](bool failed) { XBT_INFO("Dummy actor %s.", failed ? "failed" : "terminating"); });
dummy_actor->set_auto_restart(true);
dummy_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
XBT_INFO("starting a daemon process on %s", host->get_cname());
simgrid::s4u::ActorPtr daemon_actor = simgrid::s4u::Actor::create("Daemon", host, dummy_daemon);
- daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set before autorestart"); });
- daemon_actor->set_auto_restart(true);
+ daemon_actor->on_exit([](bool failed) { XBT_INFO("Daemon actor %s.", failed ? "failed" : "terminating"); });
+ daemon_actor->daemonize()->set_auto_restart(true);
daemon_actor->on_exit([](bool) { XBT_INFO("On_exit callback set after autorestart"); });
simgrid::s4u::this_actor::sleep_for(50);
> [Fafard:Daemon:(3) 0.000000] [s4u_test/INFO] Hello from the infinite loop
> [Tremblay:Autostart:(1) 50.000000] [s4u_test/INFO] powering off Fafard
> [Fafard:Dummy:(2) 50.000000] [s4u_test/INFO] On_exit callback set after autorestart
-> [Fafard:Dummy:(2) 50.000000] [s4u_test/INFO] On_exit callback set before autorestart
+> [Fafard:Dummy:(2) 50.000000] [s4u_test/INFO] Dummy actor failed.
> [Fafard:Daemon:(3) 50.000000] [s4u_test/INFO] On_exit callback set after autorestart
-> [Fafard:Daemon:(3) 50.000000] [s4u_test/INFO] On_exit callback set before autorestart
+> [Fafard:Daemon:(3) 50.000000] [s4u_test/INFO] Daemon actor failed.
> [Tremblay:Autostart:(1) 60.000000] [s4u_test/INFO] powering on Fafard
> [Fafard:Dummy:(4) 60.000000] [s4u_test/INFO] I start
> [Fafard:Daemon:(5) 60.000000] [s4u_test/INFO] Hello from the infinite loop
> [Fafard:Daemon:(5) 220.000000] [s4u_test/INFO] Hello from the infinite loop
> [Fafard:Dummy:(4) 260.000000] [s4u_test/INFO] I stop
> [Fafard:Dummy:(4) 260.000000] [s4u_test/INFO] On_exit callback set after autorestart
-> [Fafard:Dummy:(4) 260.000000] [s4u_test/INFO] On_exit callback set before autorestart
+> [Fafard:Dummy:(4) 260.000000] [s4u_test/INFO] Dummy actor terminating.
> [Fafard:Daemon:(5) 260.000000] [s4u_test/INFO] On_exit callback set after autorestart
-> [Fafard:Daemon:(5) 260.000000] [s4u_test/INFO] On_exit callback set before autorestart
+> [Fafard:Daemon:(5) 260.000000] [s4u_test/INFO] Daemon actor failed.
> [260.000000] [s4u_test/INFO] Simulation time 260