Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
TESH: msg to s4u - act 1
[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 void Actor::setKillTime(double time) {
101   simcall_process_set_kill_time(pimpl_,time);
102 }
103
104 double Actor::killTime()
105 {
106   return simcall_process_get_kill_time(pimpl_);
107 }
108
109 void Actor::kill(int pid) {
110   smx_actor_t process = SIMIX_process_from_PID(pid);
111   if(process != nullptr) {
112     simcall_process_kill(process);
113   } else {
114     std::ostringstream oss;
115     oss << "kill: ("<< pid <<") - No such process" << std::endl;
116     throw std::runtime_error(oss.str());
117   }
118 }
119
120 smx_actor_t Actor::getImpl() {
121   return pimpl_;
122 }
123
124 void Actor::kill() {
125   simcall_process_kill(pimpl_);
126 }
127
128 // ***** Static functions *****
129
130 ActorPtr Actor::byPid(int pid)
131 {
132   smx_actor_t process = SIMIX_process_from_PID(pid);
133   if (process != nullptr)
134     return process->iface();
135   else
136     return ActorPtr();
137 }
138
139 void Actor::killAll()
140 {
141   simcall_process_killall(1);
142 }
143
144 void Actor::killAll(int resetPid)
145 {
146   simcall_process_killall(resetPid);
147 }
148
149 // ***** this_actor *****
150
151 namespace this_actor {
152
153 void sleep_for(double duration)
154 {
155   if (duration > 0)
156     simcall_process_sleep(duration);
157 }
158
159 XBT_PUBLIC(void) sleep_until(double timeout)
160 {
161   double now = SIMIX_get_clock();
162   if (timeout > now)
163     simcall_process_sleep(timeout - now);
164 }
165
166 e_smx_state_t execute(double flops) {
167   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
168   return simcall_execution_wait(s);
169 }
170
171 void* recv(MailboxPtr chan) {
172   void *res = nullptr;
173   Comm& c = Comm::recv_init(chan);
174   c.setDstData(&res,sizeof(res));
175   c.wait();
176   return res;
177 }
178
179 void send(MailboxPtr chan, void* payload, double simulatedSize)
180 {
181   Comm& c = Comm::send_init(chan);
182   c.setRemains(simulatedSize);
183   c.setSrcData(payload);
184   // c.start() is optional.
185   c.wait();
186 }
187
188 int pid()
189 {
190   return SIMIX_process_self()->pid;
191 }
192
193 int ppid()
194 {
195   return SIMIX_process_self()->ppid;
196 }
197
198 std::string name()
199 {
200   return SIMIX_process_self()->name;
201 }
202
203 Host* host()
204 {
205   return SIMIX_process_self()->host;
206 }
207
208 void suspend()
209 {
210   simcall_process_suspend(SIMIX_process_self());
211 }
212
213 void resume()
214 {
215   simcall_process_resume(SIMIX_process_self());
216 }
217
218 void kill()
219 {
220   simcall_process_kill(SIMIX_process_self());
221 }
222
223 void onExit(int_f_pvoid_pvoid_t fun, void* data)
224 {
225   simcall_process_on_exit(SIMIX_process_self(), fun, data);
226 }
227
228 void migrate(Host* new_host)
229 {
230   simcall_process_set_host(SIMIX_process_self(), new_host);
231 }
232 }
233 }
234 }