Logo AND Algorithmique Numérique Distribuée

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