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 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
17 static int s4u_actor_runner(int argc, char **argv)
19 // Move the callback from the heap to the stack:
20 std::unique_ptr<std::function<int()>> code2 =
21 std::unique_ptr<std::function<int()>>(
22 static_cast<std::function<int()>*>(
23 SIMIX_process_self_get_data()));
24 std::function<int()> code = std::move(*code2);
27 // TODO, handle exceptions
31 using namespace simgrid;
33 s4u::Actor::Actor(smx_process_t smx_proc) : pimpl_(smx_proc) {}
35 s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<int()> code)
37 std::function<int()>* code2 = new std::function<int()>(std::move(code));
38 this->pimpl_ = simcall_process_create(
39 name, s4u_actor_runner, code2, host->name().c_str(),
40 killTime, 0, NULL, NULL, 0);
43 s4u::Actor::~Actor() {}
45 void s4u::Actor::setAutoRestart(bool autorestart) {
46 simcall_process_auto_restart_set(pimpl_,autorestart);
49 s4u::Host *s4u::Actor::getHost() {
50 return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
53 const char* s4u::Actor::getName() {
54 return simcall_process_get_name(pimpl_);
57 int s4u::Actor::getPid(){
58 return simcall_process_get_PID(pimpl_);
61 void s4u::Actor::setKillTime(double time) {
62 simcall_process_set_kill_time(pimpl_,time);
65 double s4u::Actor::getKillTime() {
66 return simcall_process_get_kill_time(pimpl_);
69 void s4u::Actor::kill() {
70 simcall_process_kill(pimpl_);
75 void s4u::Actor::killAll() {
76 simcall_process_killall(1);
79 void s4u::Actor::sleep(double duration) {
80 simcall_process_sleep(duration);
83 e_smx_state_t s4u::Actor::execute(double flops) {
84 smx_synchro_t s = simcall_execution_start(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
85 return simcall_execution_wait(s);
88 void *s4u::Actor::recv(Mailbox &chan) {
90 Comm c = Comm::recv_init(chan);
91 c.setDstData(&res,sizeof(res));
96 void s4u::Actor::send(Mailbox &chan, void *payload, size_t simulatedSize) {
97 Comm c = Comm::send_init(chan);
98 c.setRemains(simulatedSize);
99 c.setSrcData(payload);
100 // c.start() is optional.