Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1c171d590d4ff775beabeacf355f210ab89e8716
[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::migrate(Host* new_host)
56 {
57   simcall_process_set_host(pimpl_, new_host);
58 }
59
60 s4u::Host* Actor::host()
61 {
62   return this->pimpl_->host;
63 }
64
65 const char* Actor::cname()
66 {
67   return this->pimpl_->name.c_str();
68 }
69
70 simgrid::xbt::string Actor::name()
71 {
72   return this->pimpl_->name;
73 }
74
75 int Actor::pid()
76 {
77   return this->pimpl_->pid;
78 }
79
80 int Actor::ppid()
81 {
82   return this->pimpl_->ppid;
83 }
84
85 void Actor::suspend()
86 {
87   simcall_process_suspend(pimpl_);
88 }
89
90 void Actor::resume()
91 {
92   simcall_process_resume(pimpl_);
93 }
94
95 void Actor::setKillTime(double time) {
96   simcall_process_set_kill_time(pimpl_,time);
97 }
98
99 double Actor::killTime()
100 {
101   return simcall_process_get_kill_time(pimpl_);
102 }
103
104 void Actor::kill(int pid) {
105   smx_actor_t process = SIMIX_process_from_PID(pid);
106   if(process != nullptr) {
107     simcall_process_kill(process);
108   } else {
109     std::ostringstream oss;
110     oss << "kill: ("<< pid <<") - No such process" << std::endl;
111     throw std::runtime_error(oss.str());
112   }
113 }
114
115 smx_actor_t Actor::getImpl() {
116   return pimpl_;
117 }
118
119 void Actor::kill() {
120   simcall_process_kill(pimpl_);
121 }
122
123 // ***** Static functions *****
124
125 ActorPtr Actor::byPid(int pid)
126 {
127   smx_actor_t process = SIMIX_process_from_PID(pid);
128   if (process != nullptr)
129     return process->iface();
130   else
131     return ActorPtr();
132 }
133
134 void Actor::killAll() {
135   simcall_process_killall(1);
136 }
137
138 // ***** this_actor *****
139
140 namespace this_actor {
141
142 void sleep_for(double duration)
143 {
144   if (duration > 0)
145     simcall_process_sleep(duration);
146 }
147
148 XBT_PUBLIC(void) sleep_until(double timeout)
149 {
150   double now = SIMIX_get_clock();
151   if (timeout > now)
152     simcall_process_sleep(timeout - now);
153 }
154
155 e_smx_state_t execute(double flops) {
156   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
157   return simcall_execution_wait(s);
158 }
159
160 void* recv(MailboxPtr chan) {
161   void *res = nullptr;
162   Comm& c = Comm::recv_init(chan);
163   c.setDstData(&res,sizeof(res));
164   c.wait();
165   return res;
166 }
167
168 void send(MailboxPtr chan, void* payload, double simulatedSize)
169 {
170   Comm& c = Comm::send_init(chan);
171   c.setRemains(simulatedSize);
172   c.setSrcData(payload);
173   // c.start() is optional.
174   c.wait();
175 }
176
177 int pid()
178 {
179   return SIMIX_process_self()->pid;
180 }
181
182 int ppid()
183 {
184   return SIMIX_process_self()->ppid;
185 }
186
187 std::string name()
188 {
189   return SIMIX_process_self()->name;
190 }
191
192 Host* host()
193 {
194   return SIMIX_process_self()->host;
195 }
196
197 void suspend()
198 {
199   simcall_process_suspend(SIMIX_process_self());
200 }
201
202 void resume()
203 {
204   simcall_process_resume(SIMIX_process_self());
205 }
206
207 void kill()
208 {
209   simcall_process_kill(SIMIX_process_self());
210 }
211
212 void migrate(Host* new_host)
213 {
214   simcall_process_set_host(SIMIX_process_self(), new_host);
215 }
216 }
217 }
218 }