1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/comm.hpp"
11 #include "simgrid/s4u/host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
14 #include "src/kernel/context/Context.hpp"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
21 // ***** Actor creation *****
22 ActorPtr Actor::self()
24 smx_context_t self_context = SIMIX_context_self();
25 if (self_context == nullptr)
26 return simgrid::s4u::ActorPtr();
28 return simgrid::s4u::ActorPtr(&self_context->process()->getIface());
31 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
33 // TODO, when autorestart is used, the std::function is copied so the new
34 // instance will get a fresh (reinitialized) state. Is this what we want?
35 smx_actor_t process = simcall_process_create(name, std::move(code), nullptr, host, nullptr, 0);
36 return ActorPtr(&process->getIface());
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 smx_actor_t process = simcall_process_create(name, std::move(code), nullptr, host, nullptr, 0);
44 return ActorPtr(&process->getIface());
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 s4u::Host* Actor::host()
59 return this->pimpl_->host;
62 simgrid::xbt::string Actor::name()
64 return this->pimpl_->name;
69 return this->pimpl_->pid;
74 return this->pimpl_->ppid;
77 void Actor::setKillTime(double time) {
78 simcall_process_set_kill_time(pimpl_,time);
81 double Actor::killTime()
83 return simcall_process_get_kill_time(pimpl_);
86 void Actor::kill(int pid) {
87 msg_process_t process = SIMIX_process_from_PID(pid);
88 if(process != nullptr) {
89 simcall_process_kill(process);
91 std::ostringstream oss;
92 oss << "kill: ("<< pid <<") - No such process" << std::endl;
93 throw std::runtime_error(oss.str());
97 smx_actor_t Actor::getImpl() {
102 simcall_process_kill(pimpl_);
105 // ***** Static functions *****
107 ActorPtr Actor::byPid(int pid)
109 smx_actor_t process = SIMIX_process_from_PID(pid);
110 if (process != nullptr)
111 return ActorPtr(&process->getIface());
116 void Actor::killAll() {
117 simcall_process_killall(1);
120 // ***** this_actor *****
122 namespace this_actor {
124 void sleep_for(double duration)
127 simcall_process_sleep(duration);
130 XBT_PUBLIC(void) sleep_until(double timeout)
132 double now = SIMIX_get_clock();
134 simcall_process_sleep(timeout - now);
137 e_smx_state_t execute(double flops) {
138 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
139 return simcall_execution_wait(s);
142 void* recv(MailboxPtr chan) {
144 Comm& c = Comm::recv_init(chan);
145 c.setDstData(&res,sizeof(res));
150 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
151 Comm& c = Comm::send_init(chan);
152 c.setRemains(simulatedSize);
153 c.setSrcData(payload);
154 // c.start() is optional.
160 return SIMIX_process_self()->pid;
165 return SIMIX_process_self()->ppid;