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 self_context->process()->iface();
31 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
33 smx_actor_t actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
34 return actor->iface();
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
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();
45 // ***** Actor methods *****
48 simcall_process_join(this->pimpl_, -1);
51 void Actor::setAutoRestart(bool autorestart) {
52 simcall_process_auto_restart_set(pimpl_,autorestart);
55 s4u::Host* Actor::host()
57 return this->pimpl_->host;
60 const char* Actor::cname()
62 return this->pimpl_->name.c_str();
65 simgrid::xbt::string Actor::name()
67 return this->pimpl_->name;
72 return this->pimpl_->pid;
77 return this->pimpl_->ppid;
80 void Actor::setKillTime(double time) {
81 simcall_process_set_kill_time(pimpl_,time);
84 double Actor::killTime()
86 return simcall_process_get_kill_time(pimpl_);
89 void Actor::kill(int pid) {
90 smx_actor_t process = SIMIX_process_from_PID(pid);
91 if(process != nullptr) {
92 simcall_process_kill(process);
94 std::ostringstream oss;
95 oss << "kill: ("<< pid <<") - No such process" << std::endl;
96 throw std::runtime_error(oss.str());
100 smx_actor_t Actor::getImpl() {
105 simcall_process_kill(pimpl_);
108 // ***** Static functions *****
110 ActorPtr Actor::byPid(int pid)
112 smx_actor_t process = SIMIX_process_from_PID(pid);
113 if (process != nullptr)
114 return process->iface();
119 void Actor::killAll() {
120 simcall_process_killall(1);
123 // ***** this_actor *****
125 namespace this_actor {
127 void sleep_for(double duration)
130 simcall_process_sleep(duration);
133 XBT_PUBLIC(void) sleep_until(double timeout)
135 double now = SIMIX_get_clock();
137 simcall_process_sleep(timeout - now);
140 e_smx_state_t execute(double flops) {
141 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
142 return simcall_execution_wait(s);
145 void* recv(MailboxPtr chan) {
147 Comm& c = Comm::recv_init(chan);
148 c.setDstData(&res,sizeof(res));
153 void send(MailboxPtr chan, void* payload, double 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;
174 return SIMIX_process_self()->name;