Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Allocate Actors on the heap and return ActorPtr
authorGabriel Corona <gabriel.corona@loria.fr>
Mon, 4 Jul 2016 08:39:24 +0000 (10:39 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Wed, 6 Jul 2016 08:29:22 +0000 (10:29 +0200)
examples/s4u/basic/s4u_basic.cpp
examples/s4u/basic/s4u_basic_function.cpp
examples/s4u/io/s4u_io.cpp
examples/s4u/mutex/s4u_mutex.cpp
include/simgrid/s4u/actor.hpp
include/simgrid/s4u/mailbox.hpp
src/s4u/s4u_actor.cpp
src/s4u/s4u_mailbox.cpp
src/simix/smx_process_private.h

index 73cf613..7ee0a43 100644 (file)
@@ -12,8 +12,8 @@
 int main(int argc, char **argv) {
   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
   e->loadPlatform("../../platforms/two_hosts.xml");
-  simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker());
-  simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), Master());
+  simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker());
+  simgrid::s4u::Actor::createActor("master", simgrid::s4u::Host::by_name("Jupiter"), Master());
   e->run();
   return 0;
 }
index 2a21d52..9759056 100644 (file)
@@ -18,8 +18,8 @@ int main(int argc, char **argv) {
   e->registerFunction<Worker>("worker");
   e->registerFunction<Master>("master");
   std::vector<std::string> args;
-  simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args);
-  simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args);
+  simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args);
+  simgrid::s4u::Actor::createActor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args);
   e->run();
   return 0;
 }
index fc78c30..9d0545e 100644 (file)
@@ -103,7 +103,7 @@ int main(int argc, char **argv)
 {
   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
   e->loadPlatform("../../platforms/storage/storage.xml");
-  simgrid::s4u::Actor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
+  simgrid::s4u::Actor::createActor("host", simgrid::s4u::Host::by_name("denise"), MyHost());
   e->run();
   return 0;
 }
index 2db0ebe..51989fa 100644 (file)
@@ -50,9 +50,9 @@ static void master()
   for (int i = 0; i < NB_ACTOR * 2 ; i++) {
     // To create a worker use the static method simgrid::s4u::Actor.
     if((i % 2) == 0 )
-      simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Jupiter"),  workerLockGuard, mutex, std::ref(result));
+      simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Jupiter"),  workerLockGuard, mutex, std::ref(result));
     else
-      simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), worker,          mutex, std::ref(result));
+      simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), worker,          mutex, std::ref(result));
   }
 
   simgrid::s4u::this_actor::sleep(10);
@@ -63,7 +63,7 @@ int main(int argc, char **argv)
 {
   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
   e->loadPlatform("../../platforms/two_hosts.xml");
-  simgrid::s4u::Actor("main", simgrid::s4u::Host::by_name("Tremblay"), master);
+  simgrid::s4u::Actor::createActor("main", simgrid::s4u::Host::by_name("Tremblay"), master);
   e->run();
   return 0;
 }
index 2a3d38b..8e1b703 100644 (file)
@@ -15,6 +15,8 @@
 #include <utility>
 #include <vector>
 
+#include <boost/intrusive_ptr.hpp>
+
 #include <xbt/base.h>
 #include <xbt/functional.hpp>
 
@@ -127,8 +129,9 @@ namespace s4u {
 /** @brief Simulation Agent (see \ref s4u_actor)*/
 XBT_PUBLIC_CLASS Actor {
   friend Mailbox;
+  friend simgrid::simix::Process;
+  smx_process_t pimpl_ = nullptr;
 
-private:
   /** Wrap a (possibly non-copyable) single-use task into a `std::function` */
   template<class F, class... Args>
   static std::function<void()> wrap_task(F f, Args... args)
@@ -140,40 +143,42 @@ private:
       (*task)();
     };
   }
+
+  Actor(smx_process_t pimpl) : pimpl_(pimpl) {}
+
 public:
-  Actor() : pimpl_(nullptr) {}
-  Actor(smx_process_t smx_proc) :
-    pimpl_(SIMIX_process_ref(smx_proc)) {}
-  ~Actor()
-  {
-    SIMIX_process_unref(pimpl_);
-  }
 
-  // Copy+move (with the copy-and-swap idiom):
-  Actor(Actor const& actor) : pimpl_(SIMIX_process_ref(actor.pimpl_)) {}
-  friend void swap(Actor& first, Actor& second)
-  {
-    using std::swap;
-    swap(first.pimpl_, second.pimpl_);
-  }
-  Actor& operator=(Actor actor)
+  // ***** No copy *****
+
+  Actor(Actor const&) = delete;
+  Actor& operator=(Actor const&) = delete;
+
+  // ***** Reference count (delegated to pimpl_) *****
+
+  friend void intrusive_ptr_add_ref(Actor* actor)
   {
-    swap(*this, actor);
-    return *this;
+    xbt_assert(actor != nullptr);
+    SIMIX_process_ref(actor->pimpl_);
   }
-  Actor(Actor&& actor) : pimpl_(nullptr)
+  friend void intrusive_ptr_release(Actor* actor)
   {
-    swap(*this, actor);
+    xbt_assert(actor != nullptr);
+    SIMIX_process_unref(actor->pimpl_);
   }
+  using Ptr = boost::intrusive_ptr<Actor>;
+
+  // ***** Actor creation *****
 
   /** Create an actor using a function
    *
    *  If the actor is restarted, the actor has a fresh copy of the function.
    */
-  Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code);
+  static Ptr createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code);
 
-  Actor(const char* name, s4u::Host *host, std::function<void()> code)
-    : Actor(name, host, -1.0, std::move(code)) {};
+  static Ptr createActor(const char* name, s4u::Host *host, std::function<void()> code)
+  {
+    return createActor(name, host, -1.0, std::move(code));
+  }
 
   /** Create an actor using code
    *
@@ -186,18 +191,23 @@ public:
     // This constructor is enabled only if the call code(args...) is valid:
     typename = typename std::result_of<F(Args...)>::type
     >
-  Actor(const char* name, s4u::Host *host, F code, Args... args) :
-    Actor(name, host, wrap_task(std::move(code), std::move(args)...))
-  {}
+  static Ptr createActor(const char* name, s4u::Host *host, F code, Args... args)
+  {
+    return createActor(name, host, wrap_task(std::move(code), std::move(args)...));
+  }
 
   // Create actor from function name:
 
-  Actor(const char* name, s4u::Host *host, double killTime,
+  static Ptr createActor(const char* name, s4u::Host *host, double killTime,
     const char* function, std::vector<std::string> args);
 
-  Actor(const char* name, s4u::Host *host, const char* function,
+  static Ptr createActor(const char* name, s4u::Host *host, const char* function,
       std::vector<std::string> args)
-    : Actor(name, host, -1.0, function, std::move(args)) {}
+  {
+    return createActor(name, host, -1.0, function, std::move(args));
+  }
+
+  // ***** Methods *****
 
   /** Retrieves the actor that have the given PID (or NULL if not existing) */
   //static Actor *byPid(int pid); not implemented
@@ -224,7 +234,7 @@ public:
   void kill();
 
   static void kill(int pid);
-  static Actor forPid(int pid);
+  static Ptr forPid(int pid);
   
   /**
    * Wait for the actor to finish.
@@ -235,14 +245,12 @@ public:
 
   /** Ask kindly to all actors to die. Only the issuer will survive. */
   static void killAll();
-
-  bool valid() const { return pimpl_ != nullptr; }
   
   smx_process_t getInferior();
-private:
-  smx_process_t pimpl_ = nullptr;
 };
 
+using ActorPtr = Actor::Ptr;
+
 /** @ingroup s4u_api
  *  @brief Static methods working on the current actor (see @ref s4u::Actor) */
 namespace this_actor {
index 3009725..1d30515 100644 (file)
@@ -49,9 +49,9 @@ public:
    * It means that the communications sent to this mailbox will start flowing to its host even before he does a recv().
    * This models the real behavior of TCP and MPI communications, amongst other.
    */
-  void setReceiver(Actor process);
+  void setReceiver(Actor* process);
   /** Return the process declared as permanent receiver, or nullptr if none **/
-  Actor receiver();
+  Actor& receiver();
 
 private:
   std::string name_;
index 35b65f6..016e38e 100644 (file)
@@ -19,25 +19,31 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
 namespace simgrid {
 namespace s4u {
 
-Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
+// ***** Actor creation *****
+
+ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
 {
   // TODO, when autorestart is used, the std::function is copied so the new
   // instance will get a fresh (reinitialized) state. Is this what we want?
-  this->pimpl_ = SIMIX_process_ref(simcall_process_create(
+  smx_process_t process = simcall_process_create(
     name, std::move(code), nullptr, host->name().c_str(),
-    killTime, nullptr, 0));
+    killTime, nullptr, 0);
+  return Ptr(&process->actor());
 }
 
-Actor::Actor(const char* name, s4u::Host *host, double killTime,
+ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
   const char* function, std::vector<std::string> args)
 {
   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
   simgrid::simix::ActorCode code = factory(std::move(args));
-  this->pimpl_ = SIMIX_process_ref(simcall_process_create(
+  smx_process_t process = simcall_process_create(
     name, std::move(code), nullptr, host->name().c_str(),
-    killTime, nullptr, 0));
+    killTime, nullptr, 0);
+  return ActorPtr(&process->actor());
 }
 
+// ***** Actor methods *****
+
 void Actor::join() {
   simcall_process_join(pimpl_, -1);
 }
@@ -81,24 +87,27 @@ smx_process_t Actor::getInferior() {
   return pimpl_;
 }
 
-
 void Actor::kill() {
   simcall_process_kill(pimpl_);
 }
 
-simgrid::s4u::Actor Actor::forPid(int pid)
+// ***** Static functions *****
+
+ActorPtr Actor::forPid(int pid)
 {
-  // Should we throw if we did not find it?
   smx_process_t process = SIMIX_process_from_PID(pid);
-  return simgrid::s4u::Actor(process);
+  if (process != nullptr)
+    return ActorPtr(&process->actor());
+  else
+    return nullptr;
 }
 
-// static stuff:
-
 void Actor::killAll() {
   simcall_process_killall(1);
 }
 
+// ***** this_actor *****
+
 namespace this_actor {
 
 void sleep(double duration) {
index 000f0de..c8c6149 100644 (file)
@@ -7,6 +7,7 @@
 #include "xbt/log.h"
 #include "src/msg/msg_private.h"
 #include "src/simix/smx_network_private.h"
+#include "src/simix/smx_process_private.h"
 
 #include "simgrid/s4u/mailbox.hpp"
 
@@ -45,12 +46,13 @@ bool Mailbox::empty() {
   return nullptr == simcall_mbox_front(pimpl_);
 }
 
-void Mailbox::setReceiver(Actor actor) {
-  simcall_mbox_set_receiver(pimpl_, actor.pimpl_);
+void Mailbox::setReceiver(Actor* actor) {
+  simcall_mbox_set_receiver(pimpl_, actor == nullptr ? nullptr : actor->pimpl_);
 }
+
 /** @brief get the receiver (process associated to the mailbox) */
-Actor Mailbox::receiver() {
-  return Actor(pimpl_->permanent_receiver.get());
+Actor& Mailbox::receiver() {
+  return pimpl_->permanent_receiver->actor();
 }
 
 }
@@ -65,7 +67,7 @@ int sg_mbox_is_empty(sg_mbox_t mbox) {
   return mbox->empty();
 }
 void sg_mbox_setReceiver(sg_mbox_t mbox, smx_process_t process) {
-  mbox->setReceiver(process);
+  mbox->setReceiver(&process->actor());
 }
 smx_process_t sg_mbox_receiver(sg_mbox_t mbox) {
   return mbox->receiver().getInferior();
index d844aba..7fd1843 100644 (file)
@@ -15,6 +15,8 @@
 #include <xbt/string.hpp>
 
 #include <simgrid/simix.hpp>
+#include <simgrid/s4u/actor.hpp>
+
 #include "simgrid/simix.h"
 #include "popping_private.h"
 
@@ -39,6 +41,7 @@ public:
 
 class Process {
 public:
+  Process() : actor_(this) {}
 
   // TODO, replace with boost intrusive container hooks
   s_xbt_swag_hookup_t process_hookup   = { nullptr, nullptr };   /* simix_global->process_list */
@@ -88,8 +91,11 @@ public:
 
   ~Process();
 
+  simgrid::s4u::Actor& actor() { return actor_; }
+
 private:
   std::atomic_int_fast32_t refcount_ { 1 };
+  simgrid::s4u::Actor actor_;
 };
 
 }