Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / actor.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_ACTOR_HPP
7 #define SIMGRID_S4U_ACTOR_HPP
8
9 #include <stdexcept>
10 #include <xbt/base.h>
11 #include <simgrid/simix.h>
12 #include <simgrid/s4u/forward.hpp>
13
14 namespace simgrid {
15 namespace s4u {
16
17 /** @brief Simulation Agent
18  *
19  * An actor may be defined as a code executing in a location (host).
20  *
21  * All actors should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}),
22  * even if you can also start new actors directly.
23  * Separating the deployment in the XML from the logic in the code is a good habit as it makes your simulation easier
24  * to adapt to new settings.
25  *
26  * The code that you define for a given actor should be placed in the main method that is virtual.
27  * For example, a Worker actor should be declared as follows:
28  *
29  * \verbatim
30  * #include "s4u/actor.hpp"
31  * 
32  * class Worker {
33  *   void operator()() {
34  *     printf("Hello s4u");
35  *     return 0;
36  *   }
37  * };
38  *
39  * new Actor("worker", host, Worker());
40  * \endverbatim
41  *
42  */
43 XBT_PUBLIC_CLASS Actor {
44   explicit Actor(smx_process_t smx_proc);
45 public:
46   Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code);
47   Actor(const char* name, s4u::Host *host, std::function<void()> code)
48     : Actor(name, host, -1, std::move(code)) {};
49   template<class C>
50   Actor(const char* name, s4u::Host *host, C code)
51     : Actor(name, host, -1, std::function<void()>(std::move(code))) {}
52   ~Actor();
53
54   /** Retrieves the actor that have the given PID (or NULL if not existing) */
55   //static Actor *byPid(int pid); not implemented
56
57   /** Retrieves the name of that actor */
58   const char* getName();
59   /** Retrieves the host on which that actor is running */
60   s4u::Host *getHost();
61   /** Retrieves the PID of that actor */
62   int getPid();
63
64   /** If set to true, the actor will automatically restart when its host reboots */
65   void setAutoRestart(bool autorestart);
66   /** Sets the time at which that actor should be killed */
67   void setKillTime(double time);
68   /** Retrieves the time at which that actor will be killed (or -1 if not set) */
69   double getKillTime();
70
71   /** Ask the actor to die.
72    *
73    * It will only notice your request when doing a simcall next time (a communication or similar).
74    * SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.
75    */
76   void kill();
77
78   static void kill(int pid);
79   
80   /**
81    * Wait for the actor to finish.
82    */ 
83   void join();
84   
85   // Static methods on all actors:
86
87   /** Ask kindly to all actors to die. Only the issuer will survive. */
88   static void killAll();
89
90 protected:
91   smx_process_t getInferior() {return pimpl_;}
92 private:
93   smx_process_t pimpl_ = nullptr;
94 };
95
96 namespace this_actor {
97
98   // Static methods working on the current actor:
99
100   /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
101   XBT_PUBLIC(void) sleep(double duration);
102
103   /** Block the actor, computing the given amount of flops */
104   XBT_PUBLIC(e_smx_state_t) execute(double flop);
105
106   /** Block the actor until it gets a message from the given mailbox.
107    *
108    * See \ref Comm for the full communication API (including non blocking communications).
109    */
110   XBT_PUBLIC(void*) recv(Mailbox &chan);
111
112   /** Block the actor until it delivers a message of the given simulated size to the given mailbox
113    *
114    * See \ref Comm for the full communication API (including non blocking communications).
115   */
116   XBT_PUBLIC(void) send(Mailbox &chan, void*payload, size_t simulatedSize);
117
118 };
119
120 }} // namespace simgrid::s4u
121
122 #endif /* SIMGRID_S4U_ACTOR_HPP */
123
124 #if 0
125
126 public final class Actor {
127   
128   public Actor(String name, Host host, double killTime, Runnable code);
129   // ....
130
131 }
132 #endif