Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Move the actor logic out of the Actor class
[simgrid.git] / include / simgrid / s4u / actor.hpp
index b8463e6..a58eb6c 100644 (file)
@@ -6,15 +6,13 @@
 #ifndef SIMGRID_S4U_ACTOR_HPP
 #define SIMGRID_S4U_ACTOR_HPP
 
-#include "simgrid/simix.h"
+#include <xbt/base.h>
+#include <simgrid/simix.h>
+#include <simgrid/s4u/forward.hpp>
 
 namespace simgrid {
 namespace s4u {
 
-class Comm;
-class Host;
-class Mailbox;
-
 /** @brief Simulation Agent
  *
  * An actor may be defined as a code executing in a location (host).
@@ -29,113 +27,94 @@ class Mailbox;
  *
  * \verbatim
  * #include "s4u/actor.hpp"
- *
- * class Worker : simgrid::s4u::Actor {
- *
- *       int main(int argc, char **argv) {
- *             printf("Hello s4u");
- *       }
+ * 
+ * class Worker {
+ *   int operator()() {
+ *     printf("Hello s4u");
+ *     return 0;
+ *   }
  * };
+ *
+ * new Actor("worker", host, Worker());
  * \endverbatim
  *
  */
-class Actor {
-       friend Comm;
-       Actor(smx_process_t smx_proc);
+XBT_PUBLIC_CLASS Actor {
+  Actor(smx_process_t smx_proc);
 public:
-       Actor(const char*name, s4u::Host *host, int argc, char **argv);
-       Actor(const char*name, s4u::Host *host, int argc, char **argv, double killTime);
-       virtual ~Actor() {}
-
-       /** The main method of your agent */
-       virtual int main(int argc, char **argv);
-
-       /** The Actor that is currently running */
-       static Actor *current();
-       /** Retrieves the actor that have the given PID (or NULL if not existing) */
-       static Actor *byPid(int pid);
-
-       /** Retrieves the name of that actor */
-       const char*getName();
-       /** Retrieves the host on which that actor is running */
-       s4u::Host *getHost();
-       /** Retrieves the PID of that actor */
-       int getPid();
-
-       /** If set to true, the actor will automatically restart when its host reboots */
-       void setAutoRestart(bool autorestart);
-       /** Sets the time at which that actor should be killed */
-       void setKillTime(double time);
-       /** Retrieves the time at which that actor will be killed (or -1 if not set) */
-       double getKillTime();
-
-       /** Ask kindly to all actors to die. Only the issuer will survive. */
-       static void killAll();
-       /** Ask the actor to die.
-        *
-        * It will only notice your request when doing a simcall next time (a communication or similar).
-        * SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.
-        */
-       void kill();
-
-       /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
-       void sleep(double duration);
-
-       /** Block the actor, computing the given amount of flops */
-       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).
-        */
-       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).
-       */
-       void send(Mailbox &chan, void*payload, size_t simulatedSize);
+  Actor(const char* name, s4u::Host *host, double killTime, std::function<int()> code);
+  Actor(const char* name, s4u::Host *host, std::function<int()> code)
+    : Actor(name, host, -1, std::move(code)) {};
+  template<class C>
+  Actor(const char* name, s4u::Host *host, C code)
+    : Actor(name, host, -1, std::function<int()>(std::move(code))) {}
+  ~Actor();
+
+  /** Retrieves the actor that have the given PID (or NULL if not existing) */
+  //static Actor *byPid(int pid); not implemented
+
+  /** Retrieves the name of that actor */
+  const char* getName();
+  /** Retrieves the host on which that actor is running */
+  s4u::Host *getHost();
+  /** Retrieves the PID of that actor */
+  int getPid();
+
+  /** If set to true, the actor will automatically restart when its host reboots */
+  void setAutoRestart(bool autorestart);
+  /** Sets the time at which that actor should be killed */
+  void setKillTime(double time);
+  /** Retrieves the time at which that actor will be killed (or -1 if not set) */
+  double getKillTime();
+
+  /** Ask the actor to die.
+   *
+   * It will only notice your request when doing a simcall next time (a communication or similar).
+   * SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.
+   */
+  void kill();
+
+  // Static methods on all actors:
+
+  /** Ask kindly to all actors to die. Only the issuer will survive. */
+  static void killAll();
+
+  // Static methods working on the current actor:
+
+  /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
+  static void sleep(double duration);
+
+  /** Block the actor, computing the given amount of flops */
+  static 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);
+
+  /** 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);
 
 protected:
-       smx_process_t getInferior() {return p_smx_process;}
+  smx_process_t getInferior() {return pimpl_;}
 private:
-       smx_process_t p_smx_process;
+  smx_process_t pimpl_ = nullptr;
 };
+
 }} // namespace simgrid::s4u
 
 #endif /* SIMGRID_S4U_ACTOR_HPP */
 
 #if 0
 
-public abstract class Actor implements Runnable {
-       /** Suspends the process. See {@link #resume()} to resume it afterward */
-       public native void suspend();
-       /** Resume a process that was suspended by {@link #suspend()}. */
-       public native void resume();    
-       /** Tests if a process is suspended. */
-       public native boolean isSuspended();
-       
-       /**
-        * Returns the value of a given process property. 
-        */
-       public native String getProperty(String name);
-
-
-       /**
-        * Migrates a process to another host.
-        *
-        * @param host                  The host where to migrate the process.
-        *
-        */
-       public native void migrate(Host host);  
-
-       public native void exit();    
-       /**
-        * This static method returns the current amount of processes running
-        *
-        * @return                      The count of the running processes
-        */ 
-       public native static int getCount();
+public final class Actor {
+  
+  public Actor(String name, Host host, double killTime, Runnable code);
+  // ....
 
 }
 #endif