1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved. */
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. */
8 #include "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
13 #include "src/kernel/context/Context.hpp"
14 #include "src/simix/smx_private.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
23 // ***** Actor creation *****
24 ActorPtr Actor::self()
26 smx_context_t self_context = SIMIX_context_self();
27 if (self_context == nullptr)
28 return simgrid::s4u::ActorPtr();
30 return self_context->process()->iface();
33 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
35 simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
36 return actor->iface();
39 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
41 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
42 simgrid::simix::ActorCode code = factory(std::move(args));
43 simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
44 return actor->iface();
47 // ***** Actor methods *****
50 simcall_process_join(this->pimpl_, -1);
53 void Actor::setAutoRestart(bool autorestart) {
54 simcall_process_auto_restart_set(pimpl_,autorestart);
57 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
59 simcall_process_on_exit(pimpl_, fun, data);
62 void Actor::migrate(Host* new_host)
64 simcall_process_set_host(pimpl_, new_host);
67 s4u::Host* Actor::host()
69 return this->pimpl_->host;
72 const char* Actor::cname()
74 return this->pimpl_->name.c_str();
77 simgrid::xbt::string Actor::name()
79 return this->pimpl_->name;
84 return this->pimpl_->pid;
89 return this->pimpl_->ppid;
94 simcall_process_suspend(pimpl_);
99 simcall_process_resume(pimpl_);
102 int Actor::isSuspended()
104 return simcall_process_is_suspended(pimpl_);
107 void Actor::setKillTime(double time) {
108 simcall_process_set_kill_time(pimpl_,time);
111 double Actor::killTime()
113 return simcall_process_get_kill_time(pimpl_);
116 void Actor::kill(aid_t pid)
118 smx_actor_t process = SIMIX_process_from_PID(pid);
119 if(process != nullptr) {
120 simcall_process_kill(process);
122 std::ostringstream oss;
123 oss << "kill: ("<< pid <<") - No such process" << std::endl;
124 throw std::runtime_error(oss.str());
128 smx_actor_t Actor::getImpl() {
133 simcall_process_kill(pimpl_);
136 // ***** Static functions *****
138 ActorPtr Actor::byPid(aid_t pid)
140 smx_actor_t process = SIMIX_process_from_PID(pid);
141 if (process != nullptr)
142 return process->iface();
147 void Actor::killAll()
149 simcall_process_killall(1);
152 void Actor::killAll(int resetPid)
154 simcall_process_killall(resetPid);
157 /** Retrieve the property value (or nullptr if not set) */
158 const char* Actor::property(const char* key)
160 return (char*)xbt_dict_get_or_null(simcall_process_get_properties(pimpl_), key);
162 void Actor::setProperty(const char* key, const char* value)
164 simgrid::simix::kernelImmediate([this, key, value] {
165 xbt_dict_set(simcall_process_get_properties(pimpl_), key, (char*)value, (void_f_pvoid_t) nullptr);
169 // ***** this_actor *****
171 namespace this_actor {
173 /** Returns true if run from the kernel mode, and false if run from a real actor
175 * Everything that is run out of any actor (simulation setup before the engine is run,
176 * computing the model evolutions as a result to the actors' action, etc) is run in
177 * kernel mode, just as in any operating systems.
179 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
180 * because it is the one scheduling when the others should move or wait.
184 smx_actor_t process = SIMIX_process_self();
185 return process == nullptr || process == simix_global->maestro_process;
188 void sleep_for(double duration)
191 simcall_process_sleep(duration);
194 XBT_PUBLIC(void) sleep_until(double timeout)
196 double now = SIMIX_get_clock();
198 simcall_process_sleep(timeout - now);
201 e_smx_state_t execute(double flops) {
202 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
203 return simcall_execution_wait(s);
206 void* recv(MailboxPtr chan) {
208 CommPtr c = Comm::recv_init(chan);
209 c->setDstData(&res, sizeof(res));
214 void send(MailboxPtr chan, void* payload, double simulatedSize)
216 CommPtr c = Comm::send_init(chan);
217 c->setRemains(simulatedSize);
218 c->setSrcData(payload);
219 // c->start() is optional.
223 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout)
225 CommPtr c = Comm::send_init(chan);
226 c->setRemains(simulatedSize);
227 c->setSrcData(payload);
228 // c->start() is optional.
232 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize)
234 return Comm::send_async(chan, payload, simulatedSize);
237 void dsend(MailboxPtr chan, void* payload, double simulatedSize)
239 Comm::send_detached(chan, payload, simulatedSize);
242 CommPtr irecv(MailboxPtr chan, void** data)
244 return Comm::recv_async(chan, data);
249 return SIMIX_process_self()->pid;
254 return SIMIX_process_self()->ppid;
259 return SIMIX_process_self()->name;
264 return SIMIX_process_self()->host;
269 simcall_process_suspend(SIMIX_process_self());
274 simcall_process_resume(SIMIX_process_self());
279 return simcall_process_is_suspended(SIMIX_process_self());
284 simcall_process_kill(SIMIX_process_self());
287 void onExit(int_f_pvoid_pvoid_t fun, void* data)
289 simcall_process_on_exit(SIMIX_process_self(), fun, data);
292 void migrate(Host* new_host)
294 simcall_process_set_host(SIMIX_process_self(), new_host);