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(pimpl_, -1);
57 void Actor::setAutoRestart(bool autorestart) {
58 simcall_process_auto_restart_set(pimpl_,autorestart);
61 s4u::Host *Actor::getHost() {
65 simgrid::xbt::string Actor::getName() {
73 int Actor::getPpid() {
77 void Actor::setKillTime(double time) {
78 simcall_process_set_kill_time(pimpl_,time);
81 double Actor::getKillTime() {
82 return simcall_process_get_kill_time(pimpl_);
85 void Actor::kill(int pid) {
86 msg_process_t process = SIMIX_process_from_PID(pid);
87 if(process != nullptr) {
88 simcall_process_kill(process);
90 std::ostringstream oss;
91 oss << "kill: ("<< pid <<") - No such process" << std::endl;
92 throw std::runtime_error(oss.str());
96 smx_actor_t Actor::getImpl() {
101 simcall_process_kill(pimpl_);
104 // ***** Static functions *****
106 ActorPtr Actor::forPid(int pid)
108 smx_actor_t process = SIMIX_process_from_PID(pid);
109 if (process != nullptr)
110 return ActorPtr(&process->getIface());
115 void Actor::killAll() {
116 simcall_process_killall(1);
119 // ***** this_actor *****
121 namespace this_actor {
123 void sleep_for(double duration)
126 simcall_process_sleep(duration);
129 XBT_PUBLIC(void) sleep_until(double timeout)
131 double now = SIMIX_get_clock();
133 simcall_process_sleep(timeout - now);
136 e_smx_state_t execute(double flops) {
137 smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
138 return simcall_execution_wait(s);
141 void* recv(MailboxPtr chan) {
143 Comm& c = Comm::recv_init(chan);
144 c.setDstData(&res,sizeof(res));
149 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
150 Comm& c = Comm::send_init(chan);
151 c.setRemains(simulatedSize);
152 c.setSrcData(payload);
153 // c.start() is optional.
158 return SIMIX_process_self()->pid;
162 return SIMIX_process_self()->ppid;