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());
32 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
34 // TODO, when autorestart is used, the std::function is copied so the new
35 // instance will get a fresh (reinitialized) state. Is this what we want?
36 smx_actor_t process = simcall_process_create(
37 name, std::move(code), nullptr, host, killTime, nullptr, 0);
38 return ActorPtr(&process->getIface());
41 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
42 const char* function, std::vector<std::string> args)
44 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
45 simgrid::simix::ActorCode code = factory(std::move(args));
46 smx_actor_t process = simcall_process_create(
47 name, std::move(code), nullptr, host, killTime, nullptr, 0);
48 return ActorPtr(&process->getIface());
51 // ***** Actor methods *****
54 simcall_process_join(this->pimpl_, -1);
57 void Actor::setAutoRestart(bool autorestart) {
58 simcall_process_auto_restart_set(pimpl_,autorestart);
61 s4u::Host* Actor::host()
63 return this->pimpl_->host;
66 simgrid::xbt::string Actor::name()
68 return this->pimpl_->name;
73 return this->pimpl_->pid;
78 return this->pimpl_->ppid;
81 void Actor::setKillTime(double time) {
82 simcall_process_set_kill_time(pimpl_,time);
85 double Actor::killTime()
87 return simcall_process_get_kill_time(pimpl_);
90 void Actor::kill(int pid) {
91 msg_process_t process = SIMIX_process_from_PID(pid);
92 if(process != nullptr) {
93 simcall_process_kill(process);
95 std::ostringstream oss;
96 oss << "kill: ("<< pid <<") - No such process" << std::endl;
97 throw std::runtime_error(oss.str());
101 smx_actor_t Actor::getImpl() {
106 simcall_process_kill(pimpl_);
109 // ***** Static functions *****
111 ActorPtr Actor::byPid(int pid)
113 smx_actor_t process = SIMIX_process_from_PID(pid);
114 if (process != nullptr)
115 return ActorPtr(&process->getIface());
120 void Actor::killAll() {
121 simcall_process_killall(1);
124 // ***** this_actor *****
126 namespace this_actor {
128 void sleep_for(double duration)
131 simcall_process_sleep(duration);
134 XBT_PUBLIC(void) sleep_until(double timeout)
136 double now = SIMIX_get_clock();
138 simcall_process_sleep(timeout - now);
141 e_smx_state_t execute(double flops) {
142 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
143 return simcall_execution_wait(s);
146 void* recv(MailboxPtr chan) {
148 Comm& c = Comm::recv_init(chan);
149 c.setDstData(&res,sizeof(res));
154 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
155 Comm& c = Comm::send_init(chan);
156 c.setRemains(simulatedSize);
157 c.setSrcData(payload);
158 // c.start() is optional.
164 return SIMIX_process_self()->pid;
169 return SIMIX_process_self()->ppid;