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. */
8 #include "src/msg/msg_private.h"
10 #include "simgrid/s4u/Actor.hpp"
11 #include "simgrid/s4u/comm.hpp"
12 #include "simgrid/s4u/host.hpp"
13 #include "simgrid/s4u/mailbox.hpp"
15 #include "src/simix/smx_private.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
22 // ***** Actor creation *****
23 ActorPtr Actor::self()
25 smx_context_t self_context = SIMIX_context_self();
26 if (self_context == nullptr)
27 return simgrid::s4u::ActorPtr();
29 return simgrid::s4u::ActorPtr(&self_context->process()->actor());
33 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
35 // TODO, when autorestart is used, the std::function is copied so the new
36 // instance will get a fresh (reinitialized) state. Is this what we want?
37 smx_process_t process = simcall_process_create(
38 name, std::move(code), nullptr, host->name().c_str(),
39 killTime, nullptr, 0);
40 return Ptr(&process->actor());
43 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
44 const char* function, std::vector<std::string> args)
46 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
47 simgrid::simix::ActorCode code = factory(std::move(args));
48 smx_process_t process = simcall_process_create(
49 name, std::move(code), nullptr, host->name().c_str(),
50 killTime, nullptr, 0);
51 return ActorPtr(&process->actor());
54 // ***** Actor methods *****
57 simcall_process_join(pimpl_, -1);
60 void Actor::setAutoRestart(bool autorestart) {
61 simcall_process_auto_restart_set(pimpl_,autorestart);
64 s4u::Host *Actor::getHost() {
65 return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
68 const char* Actor::getName() {
69 return simcall_process_get_name(pimpl_);
73 return simcall_process_get_PID(pimpl_);
76 void Actor::setKillTime(double time) {
77 simcall_process_set_kill_time(pimpl_,time);
80 double Actor::getKillTime() {
81 return simcall_process_get_kill_time(pimpl_);
84 void Actor::kill(int pid) {
85 msg_process_t process = SIMIX_process_from_PID(pid);
86 if(process != nullptr) {
87 simcall_process_kill(process);
89 std::ostringstream oss;
90 oss << "kill: ("<< pid <<") - No such process" << std::endl;
91 throw std::runtime_error(oss.str());
95 smx_process_t Actor::getImpl() {
100 simcall_process_kill(pimpl_);
103 // ***** Static functions *****
105 ActorPtr Actor::forPid(int pid)
107 smx_process_t process = SIMIX_process_from_PID(pid);
108 if (process != nullptr)
109 return ActorPtr(&process->actor());
114 void Actor::killAll() {
115 simcall_process_killall(1);
118 // ***** this_actor *****
120 namespace this_actor {
122 void sleep_for(double duration)
125 simcall_process_sleep(duration);
128 XBT_PUBLIC(void) sleep_until(double timeout)
130 double now = SIMIX_get_clock();
132 simcall_process_sleep(timeout - now);
135 e_smx_state_t execute(double flops) {
136 smx_synchro_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
137 return simcall_execution_wait(s);
140 void* recv(Mailbox &chan) {
142 Comm& c = Comm::recv_init(chan);
143 c.setDstData(&res,sizeof(res));
148 void send(Mailbox &chan, void *payload, size_t simulatedSize) {
149 Comm& c = Comm::send_init(chan);
150 c.setRemains(simulatedSize);
151 c.setSrcData(payload);
152 // c.start() is optional.
157 return simcall_process_get_PID(SIMIX_process_self());