Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
commit some harmless changes
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2017. 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 #include "xbt/log.h"
7
8 #include "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
12
13 #include "src/kernel/context/Context.hpp"
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
17
18 namespace simgrid {
19 namespace s4u {
20
21 // ***** Actor creation *****
22 ActorPtr Actor::self()
23 {
24   smx_context_t self_context = SIMIX_context_self();
25   if (self_context == nullptr)
26     return simgrid::s4u::ActorPtr();
27
28   return self_context->process()->iface();
29 }
30
31 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
32 {
33   simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
34   return actor->iface();
35 }
36
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
38 {
39   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
40   simgrid::simix::ActorCode code = factory(std::move(args));
41   simgrid::simix::ActorImpl* actor          = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
42   return actor->iface();
43 }
44
45 // ***** Actor methods *****
46
47 void Actor::join() {
48   simcall_process_join(this->pimpl_, -1);
49 }
50
51 void Actor::setAutoRestart(bool autorestart) {
52   simcall_process_auto_restart_set(pimpl_,autorestart);
53 }
54
55 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
56 {
57   simcall_process_on_exit(pimpl_, fun, data);
58 }
59
60 void Actor::migrate(Host* new_host)
61 {
62   simcall_process_set_host(pimpl_, new_host);
63 }
64
65 s4u::Host* Actor::host()
66 {
67   return this->pimpl_->host;
68 }
69
70 const char* Actor::cname()
71 {
72   return this->pimpl_->name.c_str();
73 }
74
75 simgrid::xbt::string Actor::name()
76 {
77   return this->pimpl_->name;
78 }
79
80 aid_t Actor::pid()
81 {
82   return this->pimpl_->pid;
83 }
84
85 aid_t Actor::ppid()
86 {
87   return this->pimpl_->ppid;
88 }
89
90 void Actor::suspend()
91 {
92   simcall_process_suspend(pimpl_);
93 }
94
95 void Actor::resume()
96 {
97   simcall_process_resume(pimpl_);
98 }
99
100 int Actor::isSuspended()
101 {
102   return simcall_process_is_suspended(pimpl_);
103 }
104
105 void Actor::setKillTime(double time) {
106   simcall_process_set_kill_time(pimpl_,time);
107 }
108
109 double Actor::killTime()
110 {
111   return simcall_process_get_kill_time(pimpl_);
112 }
113
114 void Actor::kill(aid_t pid)
115 {
116   smx_actor_t process = SIMIX_process_from_PID(pid);
117   if(process != nullptr) {
118     simcall_process_kill(process);
119   } else {
120     std::ostringstream oss;
121     oss << "kill: ("<< pid <<") - No such process" << std::endl;
122     throw std::runtime_error(oss.str());
123   }
124 }
125
126 smx_actor_t Actor::getImpl() {
127   return pimpl_;
128 }
129
130 void Actor::kill() {
131   simcall_process_kill(pimpl_);
132 }
133
134 // ***** Static functions *****
135
136 ActorPtr Actor::byPid(aid_t pid)
137 {
138   smx_actor_t process = SIMIX_process_from_PID(pid);
139   if (process != nullptr)
140     return process->iface();
141   else
142     return ActorPtr();
143 }
144
145 void Actor::killAll()
146 {
147   simcall_process_killall(1);
148 }
149
150 void Actor::killAll(int resetPid)
151 {
152   simcall_process_killall(resetPid);
153 }
154
155 /** Retrieve the property value (or nullptr if not set) */
156 const char* Actor::property(const char* key)
157 {
158   return (char*)xbt_dict_get_or_null(simcall_process_get_properties(pimpl_), key);
159 }
160 void Actor::setProperty(const char* key, const char* value)
161 {
162   simgrid::simix::kernelImmediate([this, key, value] {
163     xbt_dict_set(simcall_process_get_properties(pimpl_), key, (char*)value, (void_f_pvoid_t) nullptr);
164   });
165 }
166
167 // ***** this_actor *****
168
169 namespace this_actor {
170
171 void sleep_for(double duration)
172 {
173   if (duration > 0)
174     simcall_process_sleep(duration);
175 }
176
177 XBT_PUBLIC(void) sleep_until(double timeout)
178 {
179   double now = SIMIX_get_clock();
180   if (timeout > now)
181     simcall_process_sleep(timeout - now);
182 }
183
184 e_smx_state_t execute(double flops) {
185   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
186   return simcall_execution_wait(s);
187 }
188
189 void* recv(MailboxPtr chan) {
190   void *res = nullptr;
191   CommPtr c = Comm::recv_init(chan);
192   c->setDstData(&res, sizeof(res));
193   c->wait();
194   return res;
195 }
196
197 void send(MailboxPtr chan, void* payload, double simulatedSize)
198 {
199   CommPtr c = Comm::send_init(chan);
200   c->setRemains(simulatedSize);
201   c->setSrcData(payload);
202   // c->start() is optional.
203   c->wait();
204 }
205
206 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout)
207 {
208   CommPtr c = Comm::send_init(chan);
209   c->setRemains(simulatedSize);
210   c->setSrcData(payload);
211   // c->start() is optional.
212   c->wait(timeout);
213 }
214
215 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize)
216 {
217   return Comm::send_async(chan, payload, simulatedSize);
218 }
219
220 void dsend(MailboxPtr chan, void* payload, double simulatedSize)
221 {
222   Comm::send_detached(chan, payload, simulatedSize);
223 }
224
225 CommPtr irecv(MailboxPtr chan, void** data)
226 {
227   return Comm::recv_async(chan, data);
228 }
229
230 aid_t pid()
231 {
232   return SIMIX_process_self()->pid;
233 }
234
235 aid_t ppid()
236 {
237   return SIMIX_process_self()->ppid;
238 }
239
240 std::string name()
241 {
242   return SIMIX_process_self()->name;
243 }
244
245 Host* host()
246 {
247   return SIMIX_process_self()->host;
248 }
249
250 void suspend()
251 {
252   simcall_process_suspend(SIMIX_process_self());
253 }
254
255 void resume()
256 {
257   simcall_process_resume(SIMIX_process_self());
258 }
259
260 int isSuspended()
261 {
262   return simcall_process_is_suspended(SIMIX_process_self());
263 }
264
265 void kill()
266 {
267   simcall_process_kill(SIMIX_process_self());
268 }
269
270 void onExit(int_f_pvoid_pvoid_t fun, void* data)
271 {
272   simcall_process_on_exit(SIMIX_process_self(), fun, data);
273 }
274
275 void migrate(Host* new_host)
276 {
277   simcall_process_set_host(SIMIX_process_self(), new_host);
278 }
279 }
280 }
281 }