Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 using namespace simgrid;
18
19 s4u::Actor::Actor(smx_process_t smx_proc) : pimpl_(smx_proc) {}
20
21 s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
22 {
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_ = simcall_process_create(
26     name, std::move(code), nullptr, host->name().c_str(),
27     killTime, NULL, 0);
28 }
29
30 s4u::Actor::~Actor() {}
31
32 void s4u::Actor::setAutoRestart(bool autorestart) {
33   simcall_process_auto_restart_set(pimpl_,autorestart);
34 }
35
36 s4u::Host *s4u::Actor::getHost() {
37   return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
38 }
39
40 const char* s4u::Actor::getName() {
41   return simcall_process_get_name(pimpl_);
42 }
43
44 int s4u::Actor::getPid(){
45   return simcall_process_get_PID(pimpl_);
46 }
47
48 void s4u::Actor::setKillTime(double time) {
49   simcall_process_set_kill_time(pimpl_,time);
50 }
51
52 double s4u::Actor::getKillTime() {
53   return simcall_process_get_kill_time(pimpl_);
54 }
55
56 void s4u::Actor::kill() {
57   simcall_process_kill(pimpl_);
58 }
59
60 // static stuff:
61
62 void s4u::Actor::killAll() {
63   simcall_process_killall(1);
64 }
65
66
67 namespace simgrid {
68 namespace s4u {
69 namespace this_actor {
70
71 void sleep(double duration) {
72   simcall_process_sleep(duration);
73 }
74
75 e_smx_state_t execute(double flops) {
76   smx_synchro_t s = simcall_execution_start(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
77   return simcall_execution_wait(s);
78 }
79
80 void* recv(Mailbox &chan) {
81   void *res = NULL;
82   Comm c = Comm::recv_init(chan);
83   c.setDstData(&res,sizeof(res));
84   c.wait();
85   return res;
86 }
87
88 void send(Mailbox &chan, void *payload, size_t simulatedSize) {
89   Comm c = Comm::send_init(chan);
90   c.setRemains(simulatedSize);
91   c.setSrcData(payload);
92   // c.start() is optional.
93   c.wait();
94 }
95
96 }
97 }
98 }