Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97d7ec1f32eb3f5a815d254708849a9bdc3db71c
[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::setKillTime(double time) {
86   simcall_process_set_kill_time(pimpl_,time);
87 }
88
89 double Actor::killTime()
90 {
91   return simcall_process_get_kill_time(pimpl_);
92 }
93
94 void Actor::kill(int pid) {
95   smx_actor_t process = SIMIX_process_from_PID(pid);
96   if(process != nullptr) {
97     simcall_process_kill(process);
98   } else {
99     std::ostringstream oss;
100     oss << "kill: ("<< pid <<") - No such process" << std::endl;
101     throw std::runtime_error(oss.str());
102   }
103 }
104
105 smx_actor_t Actor::getImpl() {
106   return pimpl_;
107 }
108
109 void Actor::kill() {
110   simcall_process_kill(pimpl_);
111 }
112
113 // ***** Static functions *****
114
115 ActorPtr Actor::byPid(int pid)
116 {
117   smx_actor_t process = SIMIX_process_from_PID(pid);
118   if (process != nullptr)
119     return process->iface();
120   else
121     return ActorPtr();
122 }
123
124 void Actor::killAll() {
125   simcall_process_killall(1);
126 }
127
128 // ***** this_actor *****
129
130 namespace this_actor {
131
132 void sleep_for(double duration)
133 {
134   if (duration > 0)
135     simcall_process_sleep(duration);
136 }
137
138 XBT_PUBLIC(void) sleep_until(double timeout)
139 {
140   double now = SIMIX_get_clock();
141   if (timeout > now)
142     simcall_process_sleep(timeout - now);
143 }
144
145 e_smx_state_t execute(double flops) {
146   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
147   return simcall_execution_wait(s);
148 }
149
150 void* recv(MailboxPtr chan) {
151   void *res = nullptr;
152   Comm& c = Comm::recv_init(chan);
153   c.setDstData(&res,sizeof(res));
154   c.wait();
155   return res;
156 }
157
158 void send(MailboxPtr chan, void* payload, double simulatedSize)
159 {
160   Comm& c = Comm::send_init(chan);
161   c.setRemains(simulatedSize);
162   c.setSrcData(payload);
163   // c.start() is optional.
164   c.wait();
165 }
166
167 int pid()
168 {
169   return SIMIX_process_self()->pid;
170 }
171
172 int ppid()
173 {
174   return SIMIX_process_self()->ppid;
175 }
176
177 std::string name()
178 {
179   return SIMIX_process_self()->name;
180 }
181
182 Host* host()
183 {
184   return SIMIX_process_self()->host;
185 }
186
187 void migrate(Host* new_host)
188 {
189   simcall_process_set_host(SIMIX_process_self(), new_host);
190 }
191 }
192 }
193 }