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");
19 using namespace simgrid;
21 s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
23 // TODO, when autorestart is used, the std::function is copied so the new
24 // instance will get a fresh (reinitialized) state. Is this what we want?
25 this->pimpl_ = SIMIX_process_ref(simcall_process_create(
26 name, std::move(code), nullptr, host->name().c_str(),
27 killTime, nullptr, 0));
30 s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime,
31 const char* function, std::vector<std::string> args)
33 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
34 simgrid::simix::ActorCode code = factory(std::move(args));
35 this->pimpl_ = SIMIX_process_ref(simcall_process_create(
36 name, std::move(code), nullptr, host->name().c_str(),
37 killTime, nullptr, 0));
40 void s4u::Actor::join() {
41 simcall_process_join(pimpl_, -1);
44 void s4u::Actor::setAutoRestart(bool autorestart) {
45 simcall_process_auto_restart_set(pimpl_,autorestart);
48 s4u::Host *s4u::Actor::getHost() {
49 return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
52 const char* s4u::Actor::getName() {
53 return simcall_process_get_name(pimpl_);
56 int s4u::Actor::getPid(){
57 return simcall_process_get_PID(pimpl_);
60 void s4u::Actor::setKillTime(double time) {
61 simcall_process_set_kill_time(pimpl_,time);
64 double s4u::Actor::getKillTime() {
65 return simcall_process_get_kill_time(pimpl_);
68 void s4u::Actor::kill(int pid) {
69 msg_process_t process = SIMIX_process_from_PID(pid);
70 if(process != nullptr) {
71 simcall_process_kill(process);
73 std::ostringstream oss;
74 oss << "kill: ("<< pid <<") - No such process" << std::endl;
75 throw std::runtime_error(oss.str());
79 void s4u::Actor::kill() {
80 simcall_process_kill(pimpl_);
83 simgrid::s4u::Actor s4u::Actor::forPid(int pid)
85 // Should we throw if we did not find it?
86 smx_process_t process = SIMIX_process_from_PID(pid);
87 return simgrid::s4u::Actor(process);
92 void s4u::Actor::killAll() {
93 simcall_process_killall(1);
99 namespace this_actor {
101 void sleep(double duration) {
102 simcall_process_sleep(duration);
105 e_smx_state_t execute(double flops) {
106 smx_synchro_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
107 return simcall_execution_wait(s);
110 void* recv(Mailbox &chan) {
112 Comm c = Comm::recv_init(chan);
113 c.setDstData(&res,sizeof(res));
118 void send(Mailbox &chan, void *payload, size_t simulatedSize) {
119 Comm c = Comm::send_init(chan);
120 c.setRemains(simulatedSize);
121 c.setSrcData(payload);
122 // c.start() is optional.
127 return simcall_process_get_PID(SIMIX_process_self());