Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rewrite the s4u_launching example
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/log.h"
8 #include "src/msg/msg_private.h"
9
10 #include "simgrid/s4u/Actor.hpp"
11 #include "simgrid/s4u/comm.hpp"
12 #include "simgrid/s4u/host.hpp"
13 #include "simgrid/s4u/mailbox.hpp"
14
15 #include "src/kernel/context/Context.hpp"
16 #include "src/simix/smx_private.h"
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
19
20 namespace simgrid {
21 namespace s4u {
22
23 // ***** Actor creation *****
24 ActorPtr Actor::self()
25 {
26   smx_context_t self_context = SIMIX_context_self();
27   if (self_context == nullptr)
28     return simgrid::s4u::ActorPtr();
29
30   return simgrid::s4u::ActorPtr(&self_context->process()->getIface());
31 }
32
33
34 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
35 {
36   // TODO, when autorestart is used, the std::function is copied so the new
37   // instance will get a fresh (reinitialized) state. Is this what we want?
38   smx_process_t process = simcall_process_create(
39     name, std::move(code), nullptr, host->name().c_str(),
40     killTime, nullptr, 0);
41   return ActorPtr(&process->getIface());
42 }
43
44 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
45   const char* function, std::vector<std::string> args)
46 {
47   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
48   simgrid::simix::ActorCode code = factory(std::move(args));
49   smx_process_t process = simcall_process_create(
50     name, std::move(code), nullptr, host->name().c_str(),
51     killTime, nullptr, 0);
52   return ActorPtr(&process->getIface());
53 }
54
55 // ***** Actor methods *****
56
57 void Actor::join() {
58   simcall_process_join(pimpl_, -1);
59 }
60
61 void Actor::setAutoRestart(bool autorestart) {
62   simcall_process_auto_restart_set(pimpl_,autorestart);
63 }
64
65 s4u::Host *Actor::getHost() {
66   return pimpl_->host;
67 }
68
69 simgrid::xbt::string Actor::getName() {
70   return pimpl_->name;
71 }
72
73 int Actor::getPid(){
74   return pimpl_->pid;
75 }
76
77 int Actor::getPpid() {
78   return pimpl_->ppid;
79 }
80
81 void Actor::setKillTime(double time) {
82   simcall_process_set_kill_time(pimpl_,time);
83 }
84
85 double Actor::getKillTime() {
86   return simcall_process_get_kill_time(pimpl_);
87 }
88
89 void Actor::kill(int pid) {
90   msg_process_t process = SIMIX_process_from_PID(pid);
91   if(process != nullptr) {
92     simcall_process_kill(process);
93   } else {
94     std::ostringstream oss;
95     oss << "kill: ("<< pid <<") - No such process" << std::endl;
96     throw std::runtime_error(oss.str());
97   }
98 }
99
100 smx_process_t Actor::getImpl() {
101   return pimpl_;
102 }
103
104 void Actor::kill() {
105   simcall_process_kill(pimpl_);
106 }
107
108 // ***** Static functions *****
109
110 ActorPtr Actor::forPid(int pid)
111 {
112   smx_process_t process = SIMIX_process_from_PID(pid);
113   if (process != nullptr)
114     return ActorPtr(&process->getIface());
115   else
116     return nullptr;
117 }
118
119 void Actor::killAll() {
120   simcall_process_killall(1);
121 }
122
123 // ***** this_actor *****
124
125 namespace this_actor {
126
127 void sleep_for(double duration)
128 {
129   if (duration > 0)
130     simcall_process_sleep(duration);
131 }
132
133 XBT_PUBLIC(void) sleep_until(double timeout)
134 {
135   double now = SIMIX_get_clock();
136   if (timeout > now)
137     simcall_process_sleep(timeout - now);
138 }
139
140 e_smx_state_t execute(double flops) {
141   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
142   return simcall_execution_wait(s);
143 }
144
145 void* recv(MailboxPtr chan) {
146   void *res = nullptr;
147   Comm& c = Comm::recv_init(chan);
148   c.setDstData(&res,sizeof(res));
149   c.wait();
150   return res;
151 }
152
153 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
154   Comm& c = Comm::send_init(chan);
155   c.setRemains(simulatedSize);
156   c.setSrcData(payload);
157   // c.start() is optional.
158   c.wait();
159 }
160
161 int getPid() {
162   return SIMIX_process_self()->pid;
163 }
164
165 int getPpid() {
166   return SIMIX_process_self()->ppid;
167 }
168
169 }
170 }
171 }