Logo AND Algorithmique Numérique Distribuée

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