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 void intrusive_ptr_add_ref(Actor* actor)
49 xbt_assert(actor != nullptr);
50 intrusive_ptr_add_ref(actor->pimpl_);
52 void intrusive_ptr_release(Actor* actor)
54 xbt_assert(actor != nullptr);
55 intrusive_ptr_release(actor->pimpl_);
58 // ***** Actor methods *****
61 simcall_process_join(this->pimpl_, -1);
64 void Actor::setAutoRestart(bool autorestart) {
65 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
68 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
70 simcall_process_on_exit(pimpl_, fun, data);
73 void Actor::migrate(Host* new_host)
75 simgrid::simix::kernelImmediate([this, new_host]() { pimpl_->new_host = new_host; });
78 s4u::Host* Actor::getHost()
80 return this->pimpl_->host;
83 void Actor::daemonize()
85 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
88 const char* Actor::getCname()
90 return this->pimpl_->name.c_str();
93 simgrid::xbt::string Actor::getName()
95 return this->pimpl_->name;
100 return this->pimpl_->pid;
103 aid_t Actor::getPpid()
105 return this->pimpl_->ppid;
108 void Actor::suspend()
110 simcall_process_suspend(pimpl_);
115 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
118 int Actor::isSuspended()
120 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
123 void Actor::setKillTime(double time) {
124 simcall_process_set_kill_time(pimpl_,time);
127 /** \brief Get the kill time of an actor(or 0 if unset). */
128 double Actor::getKillTime()
130 return SIMIX_timer_get_date(pimpl_->kill_timer);
133 void Actor::kill(aid_t pid)
135 smx_actor_t process = SIMIX_process_from_PID(pid);
136 if(process != nullptr) {
137 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
139 std::ostringstream oss;
140 oss << "kill: ("<< pid <<") - No such process" << std::endl;
141 throw std::runtime_error(oss.str());
145 smx_actor_t Actor::getImpl() {
150 smx_actor_t process = SIMIX_process_self();
151 simgrid::simix::kernelImmediate(
152 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
155 // ***** Static functions *****
157 ActorPtr Actor::byPid(aid_t pid)
159 smx_actor_t process = SIMIX_process_from_PID(pid);
160 if (process != nullptr)
161 return process->iface();
166 void Actor::killAll()
168 simcall_process_killall(1);
171 void Actor::killAll(int resetPid)
173 simcall_process_killall(resetPid);
176 /** Retrieve the property value (or nullptr if not set) */
177 const char* Actor::getProperty(const char* key)
179 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
182 void Actor::setProperty(const char* key, const char* value)
184 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
187 Actor* Actor::restart()
189 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
192 // ***** this_actor *****
194 namespace this_actor {
196 /** Returns true if run from the kernel mode, and false if run from a real actor
198 * Everything that is run out of any actor (simulation setup before the engine is run,
199 * computing the model evolutions as a result to the actors' action, etc) is run in
200 * kernel mode, just as in any operating systems.
202 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
203 * because it is the one scheduling when the others should move or wait.
207 smx_actor_t process = SIMIX_process_self();
208 return process == nullptr || process == simix_global->maestro_process;
211 void sleep_for(double duration)
214 simcall_process_sleep(duration);
217 XBT_PUBLIC(void) sleep_until(double timeout)
219 double now = SIMIX_get_clock();
221 simcall_process_sleep(timeout - now);
224 void execute(double flops)
226 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
227 simcall_execution_wait(s);
230 void* recv(MailboxPtr chan) // deprecated
235 void* recv(MailboxPtr chan, double timeout) // deprecated
237 return chan->get(timeout);
240 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
242 chan->put(payload, simulatedSize);
245 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
247 chan->put(payload, simulatedSize, timeout);
250 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
252 return chan->put_async(payload, simulatedSize);
255 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
257 return chan->get_async(data);
262 return SIMIX_process_self()->pid;
267 return SIMIX_process_self()->ppid;
270 std::string getName()
272 return SIMIX_process_self()->name;
277 return SIMIX_process_self()->host;
282 simcall_process_suspend(SIMIX_process_self());
287 smx_actor_t process = SIMIX_process_self();
288 simgrid::simix::kernelImmediate([process] { process->resume(); });
293 smx_actor_t process = SIMIX_process_self();
294 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
299 smx_actor_t process = SIMIX_process_self();
300 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
303 void onExit(int_f_pvoid_pvoid_t fun, void* data)
305 simcall_process_on_exit(SIMIX_process_self(), fun, data);
308 void migrate(Host* new_host)
310 smx_actor_t process = SIMIX_process_self();
311 simgrid::simix::kernelImmediate([process, new_host] { process->new_host = new_host; });