From: Gabriel Corona Date: Mon, 23 May 2016 08:55:08 +0000 (+0200) Subject: [s4u] Move actions by the current actor in the this_actor namespace X-Git-Tag: v3_14~1177 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/f232c72280368ced3f5706dde1014d969dac3649 [s4u] Move actions by the current actor in the this_actor namespace This mimics the design of std::thread and std::this_thread. We might want to create a convenience base class for actor implementations with those functions as instance methods. --- diff --git a/examples/s4u/basic/s4u_basic.cpp b/examples/s4u/basic/s4u_basic.cpp index 3da68af52f..11b3e11460 100644 --- a/examples/s4u/basic/s4u_basic.cpp +++ b/examples/s4u/basic/s4u_basic.cpp @@ -13,7 +13,7 @@ class Worker { public: void operator()() { XBT_INFO("Hello s4u, I'm ready to serve"); - char *msg = (char*)simgrid::s4u::Actor::recv(*simgrid::s4u::Mailbox::byName("worker")); + char *msg = (char*)simgrid::s4u::this_actor::recv(*simgrid::s4u::Mailbox::byName("worker")); XBT_INFO("I received '%s'",msg); XBT_INFO("I'm done. See you."); } @@ -24,7 +24,7 @@ public: void operator()() { const char *msg = "GaBuZoMeu"; XBT_INFO("Hello s4u, I have something to send"); - simgrid::s4u::Actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg)); + simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg)); XBT_INFO("I'm done. See you."); } }; diff --git a/include/simgrid/s4u/actor.hpp b/include/simgrid/s4u/actor.hpp index 9b461eb994..b671425915 100644 --- a/include/simgrid/s4u/actor.hpp +++ b/include/simgrid/s4u/actor.hpp @@ -79,30 +79,34 @@ public: /** Ask kindly to all actors to die. Only the issuer will survive. */ static void killAll(); +protected: + smx_process_t getInferior() {return pimpl_;} +private: + smx_process_t pimpl_ = nullptr; +}; + +namespace this_actor { + // Static methods working on the current actor: /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */ - static void sleep(double duration); + void sleep(double duration); /** Block the actor, computing the given amount of flops */ - static e_smx_state_t execute(double flop); + e_smx_state_t execute(double flop); /** Block the actor until it gets a message from the given mailbox. * * See \ref Comm for the full communication API (including non blocking communications). */ - static void *recv(Mailbox &chan); + void *recv(Mailbox &chan); /** Block the actor until it delivers a message of the given simulated size to the given mailbox * * See \ref Comm for the full communication API (including non blocking communications). */ - static void send(Mailbox &chan, void*payload, size_t simulatedSize); + void send(Mailbox &chan, void*payload, size_t simulatedSize); -protected: - smx_process_t getInferior() {return pimpl_;} -private: - smx_process_t pimpl_ = nullptr; }; }} // namespace simgrid::s4u diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 0e82ceeaf6..a220c8ce69 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -63,16 +63,21 @@ void s4u::Actor::killAll() { simcall_process_killall(1); } -void s4u::Actor::sleep(double duration) { + +namespace simgrid { +namespace s4u { +namespace this_actor { + +void sleep(double duration) { simcall_process_sleep(duration); } -e_smx_state_t s4u::Actor::execute(double flops) { +e_smx_state_t execute(double flops) { smx_synchro_t s = simcall_execution_start(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/); return simcall_execution_wait(s); } -void *s4u::Actor::recv(Mailbox &chan) { +void* recv(Mailbox &chan) { void *res = NULL; Comm c = Comm::recv_init(chan); c.setDstData(&res,sizeof(res)); @@ -80,10 +85,14 @@ void *s4u::Actor::recv(Mailbox &chan) { return res; } -void s4u::Actor::send(Mailbox &chan, void *payload, size_t simulatedSize) { +void send(Mailbox &chan, void *payload, size_t simulatedSize) { Comm c = Comm::send_init(chan); c.setRemains(simulatedSize); c.setSrcData(payload); // c.start() is optional. c.wait(); } + +} +} +}