Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no fun if you don't break evertyhing 'cos you're dumb...
[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
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/comm.hpp"
11 #include "simgrid/s4u/host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13
14 #include "src/kernel/context/Context.hpp"
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   smx_actor_t 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   smx_actor_t 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 int Actor::pid()
81 {
82   return this->pimpl_->pid;
83 }
84
85 int 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(int pid) {
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(int 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 // ***** this_actor *****
155
156 namespace this_actor {
157
158 void sleep_for(double duration)
159 {
160   if (duration > 0)
161     simcall_process_sleep(duration);
162 }
163
164 XBT_PUBLIC(void) sleep_until(double timeout)
165 {
166   double now = SIMIX_get_clock();
167   if (timeout > now)
168     simcall_process_sleep(timeout - now);
169 }
170
171 e_smx_state_t execute(double flops) {
172   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
173   return simcall_execution_wait(s);
174 }
175
176 void* recv(MailboxPtr chan) {
177   void *res = nullptr;
178   Comm& c = Comm::recv_init(chan);
179   c.setDstData(&res,sizeof(res));
180   c.wait();
181   return res;
182 }
183
184 void send(MailboxPtr chan, void* payload, double simulatedSize)
185 {
186   Comm& c = Comm::send_init(chan);
187   c.setRemains(simulatedSize);
188   c.setSrcData(payload);
189   // c.start() is optional.
190   c.wait();
191 }
192
193 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout)
194 {
195   Comm& c = Comm::send_init(chan);
196   c.setRemains(simulatedSize);
197   c.setSrcData(payload);
198   // c.start() is optional.
199   c.wait(timeout);
200 }
201
202 Comm& isend(MailboxPtr chan, void* payload, double simulatedSize)
203 {
204   return Comm::send_async(chan, payload, simulatedSize);
205 }
206
207 Comm& irecv(MailboxPtr chan, void** data)
208 {
209   return Comm::recv_async(chan, data);
210 }
211
212 int pid()
213 {
214   return SIMIX_process_self()->pid;
215 }
216
217 int ppid()
218 {
219   return SIMIX_process_self()->ppid;
220 }
221
222 std::string name()
223 {
224   return SIMIX_process_self()->name;
225 }
226
227 Host* host()
228 {
229   return SIMIX_process_self()->host;
230 }
231
232 void suspend()
233 {
234   simcall_process_suspend(SIMIX_process_self());
235 }
236
237 void resume()
238 {
239   simcall_process_resume(SIMIX_process_self());
240 }
241
242 int isSuspended()
243 {
244   return simcall_process_is_suspended(SIMIX_process_self());
245 }
246
247 void kill()
248 {
249   simcall_process_kill(SIMIX_process_self());
250 }
251
252 void onExit(int_f_pvoid_pvoid_t fun, void* data)
253 {
254   simcall_process_on_exit(SIMIX_process_self(), fun, data);
255 }
256
257 void migrate(Host* new_host)
258 {
259   simcall_process_set_host(SIMIX_process_self(), new_host);
260 }
261 }
262 }
263 }