Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the S4U method retrieving the internal impl to getImpl
[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 #include "src/simix/smx_private.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
18
19 namespace simgrid {
20 namespace s4u {
21
22 // ***** Actor creation *****
23
24 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
25 {
26   // TODO, when autorestart is used, the std::function is copied so the new
27   // instance will get a fresh (reinitialized) state. Is this what we want?
28   smx_process_t process = simcall_process_create(
29     name, std::move(code), nullptr, host->name().c_str(),
30     killTime, nullptr, 0);
31   return Ptr(&process->actor());
32 }
33
34 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
35   const char* function, std::vector<std::string> args)
36 {
37   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
38   simgrid::simix::ActorCode code = factory(std::move(args));
39   smx_process_t process = simcall_process_create(
40     name, std::move(code), nullptr, host->name().c_str(),
41     killTime, nullptr, 0);
42   return ActorPtr(&process->actor());
43 }
44
45 // ***** Actor methods *****
46
47 void Actor::join() {
48   simcall_process_join(pimpl_, -1);
49 }
50
51 void Actor::setAutoRestart(bool autorestart) {
52   simcall_process_auto_restart_set(pimpl_,autorestart);
53 }
54
55 s4u::Host *Actor::getHost() {
56   return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
57 }
58
59 const char* Actor::getName() {
60   return simcall_process_get_name(pimpl_);
61 }
62
63 int Actor::getPid(){
64   return simcall_process_get_PID(pimpl_);
65 }
66
67 void Actor::setKillTime(double time) {
68   simcall_process_set_kill_time(pimpl_,time);
69 }
70
71 double Actor::getKillTime() {
72   return simcall_process_get_kill_time(pimpl_);
73 }
74
75 void Actor::kill(int pid) {
76   msg_process_t process = SIMIX_process_from_PID(pid);
77   if(process != nullptr) {
78     simcall_process_kill(process);
79   } else {
80     std::ostringstream oss;
81     oss << "kill: ("<< pid <<") - No such process" << std::endl;
82     throw std::runtime_error(oss.str());
83   }
84 }
85
86 smx_process_t Actor::getImpl() {
87   return pimpl_;
88 }
89
90 void Actor::kill() {
91   simcall_process_kill(pimpl_);
92 }
93
94 // ***** Static functions *****
95
96 ActorPtr Actor::forPid(int pid)
97 {
98   smx_process_t process = SIMIX_process_from_PID(pid);
99   if (process != nullptr)
100     return ActorPtr(&process->actor());
101   else
102     return nullptr;
103 }
104
105 void Actor::killAll() {
106   simcall_process_killall(1);
107 }
108
109 // ***** this_actor *****
110
111 namespace this_actor {
112
113 void sleep_for(double duration)
114 {
115   if (duration > 0)
116     simcall_process_sleep(duration);
117 }
118
119 XBT_PUBLIC(void) sleep_until(double timeout)
120 {
121   double now = SIMIX_get_clock();
122   if (timeout > now)
123     simcall_process_sleep(timeout - now);
124 }
125
126 e_smx_state_t execute(double flops) {
127   smx_synchro_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
128   return simcall_execution_wait(s);
129 }
130
131 void* recv(Mailbox &chan) {
132   void *res = nullptr;
133   Comm& c = Comm::recv_init(chan);
134   c.setDstData(&res,sizeof(res));
135   c.wait();
136   return res;
137 }
138
139 void send(Mailbox &chan, void *payload, size_t simulatedSize) {
140   Comm& c = Comm::send_init(chan);
141   c.setRemains(simulatedSize);
142   c.setSrcData(payload);
143   // c.start() is optional.
144   c.wait();
145 }
146
147 int getPid() {
148   return simcall_process_get_PID(SIMIX_process_self());
149 }
150
151 }
152 }
153 }