Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Move the actor logic out of the Actor class
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/log.h"
8 #include "src/msg/msg_private.h"
9
10 #include "simgrid/s4u/actor.hpp"
11 #include "simgrid/s4u/comm.hpp"
12 #include "simgrid/s4u/host.hpp"
13 #include "simgrid/s4u/mailbox.hpp"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
16
17 static int s4u_actor_runner(int argc, char **argv)
18 {
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);
25   code2 = nullptr;
26   // Call it:
27   // TODO, handle exceptions
28   return code();
29 }
30
31 using namespace simgrid;
32
33 s4u::Actor::Actor(smx_process_t smx_proc) : pimpl_(smx_proc) {}
34
35 s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<int()> code)
36 {
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);
41 }
42
43 s4u::Actor::~Actor() {}
44
45 void s4u::Actor::setAutoRestart(bool autorestart) {
46   simcall_process_auto_restart_set(pimpl_,autorestart);
47 }
48
49 s4u::Host *s4u::Actor::getHost() {
50   return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
51 }
52
53 const char* s4u::Actor::getName() {
54   return simcall_process_get_name(pimpl_);
55 }
56
57 int s4u::Actor::getPid(){
58   return simcall_process_get_PID(pimpl_);
59 }
60
61 void s4u::Actor::setKillTime(double time) {
62   simcall_process_set_kill_time(pimpl_,time);
63 }
64
65 double s4u::Actor::getKillTime() {
66   return simcall_process_get_kill_time(pimpl_);
67 }
68
69 void s4u::Actor::kill() {
70   simcall_process_kill(pimpl_);
71 }
72
73 // static stuff:
74
75 void s4u::Actor::killAll() {
76   simcall_process_killall(1);
77 }
78
79 void s4u::Actor::sleep(double duration) {
80   simcall_process_sleep(duration);
81 }
82
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);
86 }
87
88 void *s4u::Actor::recv(Mailbox &chan) {
89   void *res = NULL;
90   Comm c = Comm::recv_init(chan);
91   c.setDstData(&res,sizeof(res));
92   c.wait();
93   return res;
94 }
95
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.
101   c.wait();
102 }