Logo AND Algorithmique Numérique Distribuée

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