} // The mutex gets automatically freed because the only existing reference gets out of scope
-API C++ Reference
+C++ API Reference
*****************
.. _API_s4u_this_actor:
-foreach(example actor-create actor-daemon actor-join actor-kill actor-migrate actor-suspend actor-yield
+foreach(example actor-create actor-daemon actor-join actor-kill actor-migrate actor-suspend actor-yield # actor-lifetime
exec-basic)
set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh)
set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py)
endforeach()
set(tesh_files examples/python/actor-create/actor-create_d.xml
+ examples/python/actor-lifetime/actor-lifetime_d.xml
${tesh_files} PARENT_SCOPE)
set(examples_src ${examples_src} PARENT_SCOPE)
this_actor.sleep_for(2)
this_actor.info("Kill the victim A") # - and then kill it
- this_actor.kill(victimA.pid) # Kill by PID is legit
+ Actor.by_pid(victimA.pid).kill() # You can retrieve an actor from its PID (and then kill it)
this_actor.sleep_for(1)
this_actor.sleep_for(1)
this_actor.info("Killing everybody but myself")
- this_actor.kill_all()
+ Actor.kill_all()
this_actor.info("OK, goodbye now. I commit a suicide.")
this_actor.exit()
- **Creating actors:**
Most actors are started from the deployment XML file, but there is other methods.
This example show them all.
+ `examples/python/actor-create/actor-create_d.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-create/actor-create_d.xml>`_
- |cpp| `examples/s4u/actor-create/s4u-actor-create.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-create/s4u-actor-create.cpp>`_
- |py| `examples/python/actor-create/actor-create.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-create/actor-create.py>`_
- **Kill actors:**
- Actors can forcefully stop other actors with the
- :cpp:func:`void simgrid::s4u::Actor::kill(void)` or the
- :cpp:func:`void simgrid::s4u::Actor::kill(aid_t)` methods.
- |br| `examples/s4u/actor-kill/s4u-actor-kill.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-kill/s4u-actor-kill.cpp>`_
+ Actors can forcefully stop other actors.
+
+ - |cpp| `examples/s4u/actor-kill/s4u-actor-kill.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-kill/s4u-actor-kill.cpp>`_
+ :cpp:func:`void simgrid::s4u::Actor::kill(void)`,
+ :cpp:func:`void simgrid::s4u::Actor::kill_all()`,
+ :cpp:func:`simgrid::s4u::this_actor::exit`.
+ - |py| `examples/python/actor-kill/actor-kill.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-kill/actor-kill.py>`_
+ :py:func:`simgrid.Actor.kill`,
+ :py:func:`simgrid.Actor.kill_all`,
+ :py:func:`simgrid.this_actor.exit`.
- **Controling the actor life cycle from the XML:**
You can specify a start time and a kill time in the deployment
simgrid::s4u::this_actor::sleep_for(2);
XBT_INFO("Kill the victim A"); /* - and then kill it */
- simgrid::s4u::Actor::kill(victimA->get_pid()); // Kill by PID is legit
+ simgrid::s4u::Actor::by_pid(victimA->get_pid())->kill(); // You can retrieve an actor from its PID (and then kill it)
simgrid::s4u::this_actor::sleep_for(1);
XBT_INFO("Exiting now (done sleeping or got killed).");
},
nullptr);
-
+ }
+ void operator()()
+ {
XBT_INFO("Hello! I go to sleep.");
simgrid::s4u::this_actor::sleep_for(10);
+ XBT_INFO("Done sleeping.");
}
- void operator()() { XBT_INFO("Done sleeping."); }
};
int main(int argc, char* argv[])
*/
void kill();
- /** Kill an actor from its ID */
- static void kill(aid_t pid);
-
/** Retrieves the actor that have the given PID (or nullptr if not existing) */
static ActorPtr by_pid(aid_t pid);
void join(double timeout);
Actor* restart();
- /** Ask kindly to all actors to die. Only the issuer will survive. */
+ /** Kill all actors (but the issuer). Being killed is not something that actors can delay or avoid. */
static void kill_all();
/** Returns the internal implementation of this actor */
void set_property(std::string key, std::string value);
#ifndef DOXYGEN
+ XBT_ATTRIB_DEPRECATED_v325("Please use Actor::by_pid(pid).kill() instead") static void kill(aid_t pid);
+
/** @deprecated See Actor::create() */
XBT_ATTRIB_DEPRECATED_v323("Please use Actor::create()") static ActorPtr createActor(
const char* name, s4u::Host* host, std::function<void()> code)
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include <pybind11/functional.h>
#include <pybind11/pybind11.h> // Must be first
#include <pybind11/stl.h>
m2.def("yield_", &simgrid::s4u::this_actor::yield,
"Yield the actor, see :cpp:func:`void simgrid::s4u::this_actor::yield()`");
m2.def("exit", &simgrid::s4u::this_actor::exit, "kill the current actor");
- m2.def("kill", [](py::int_ pid) { Actor::kill(pid); }, "Kill an actor by pid");
- m2.def("kill_all", &Actor::kill_all, "Kill all actors but the caller.");
-
- m2.def(
- "on_exit",
- [](py::function fun) { simgrid::s4u::this_actor::on_exit([fun](int ignored, void* data) { fun(); }, nullptr); },
- "");
+ m2.def("on_exit",
+ [](py::object fun) {
+ ActorPtr act = Actor::self();
+ simgrid::s4u::this_actor::on_exit(
+ [act, fun](int ignored, void* data) {
+ try {
+ fun();
+ } catch (py::error_already_set& e) {
+ xbt_die("Error while executing the on_exit lambda: %s", e.what());
+ }
+ },
+ nullptr);
+ },
+ "");
/* Class Engine */
py::class_<Engine>(m, "Engine", "Simulation Engine, see :ref:`class s4u::Engine <API_s4u_Engine>`")
"Create an actor from a function or an object, see :cpp:func:`simgrid::s4u::Actor::create()`")
.def_property("host", &Actor::get_host, &Actor::migrate, "The host on which this actor is located")
.def_property_readonly("pid", &Actor::get_pid, "The PID (unique identifier) of this actor.")
+ .def("by_pid", &Actor::by_pid, "Retrieve an actor by its PID")
.def("daemonize", &Actor::daemonize,
"This actor will be automatically terminated when the last non-daemon actor finishes, see :cpp:func:`void "
"simgrid::s4u::Actor::daemonize()`")
.def("join", py::overload_cast<double>(&Actor::join),
"Wait for the actor to finish, see :cpp:func:`void simgrid::s4u::Actor::join(double)`", py::arg("timeout"))
- .def("kill", [](py::int_ pid) { Actor::kill(pid); }, "Kill an actor by pid")
.def("kill", [](ActorPtr act) { act->kill(); }, "Kill that actor")
.def("kill_all", &Actor::kill_all, "Kill all actors but the caller.")
.def("migrate", &Actor::migrate,
return SIMIX_timer_get_date(pimpl_->kill_timer);
}
-void Actor::kill(aid_t pid)
+void Actor::kill(aid_t pid) // deprecated
{
smx_actor_t killer = SIMIX_process_self();
smx_actor_t process = SIMIX_process_from_PID(pid);