Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Separate boolean from others, as gcc > 6 complains on some operations.
[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
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/comm.hpp"
11 #include "simgrid/s4u/host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13
14 #include "src/kernel/context/Context.hpp"
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
17
18 namespace simgrid {
19 namespace s4u {
20
21 // ***** Actor creation *****
22 ActorPtr Actor::self()
23 {
24   smx_context_t self_context = SIMIX_context_self();
25   if (self_context == nullptr)
26     return simgrid::s4u::ActorPtr();
27
28   return simgrid::s4u::ActorPtr(&self_context->process()->getIface());
29 }
30
31
32 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
33 {
34   // TODO, when autorestart is used, the std::function is copied so the new
35   // instance will get a fresh (reinitialized) state. Is this what we want?
36   smx_actor_t process = simcall_process_create(
37     name, std::move(code), nullptr, host, killTime, nullptr, 0);
38   return ActorPtr(&process->getIface());
39 }
40
41 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
42   const char* function, std::vector<std::string> args)
43 {
44   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
45   simgrid::simix::ActorCode code = factory(std::move(args));
46   smx_actor_t process = simcall_process_create(
47     name, std::move(code), nullptr, host, killTime, nullptr, 0);
48   return ActorPtr(&process->getIface());
49 }
50
51 // ***** Actor methods *****
52
53 void Actor::join() {
54   simcall_process_join(this->pimpl_, -1);
55 }
56
57 void Actor::setAutoRestart(bool autorestart) {
58   simcall_process_auto_restart_set(pimpl_,autorestart);
59 }
60
61 s4u::Host* Actor::host()
62 {
63   return this->pimpl_->host;
64 }
65
66 simgrid::xbt::string Actor::name()
67 {
68   return this->pimpl_->name;
69 }
70
71 int Actor::pid()
72 {
73   return this->pimpl_->pid;
74 }
75
76 int Actor::ppid()
77 {
78   return this->pimpl_->ppid;
79 }
80
81 void Actor::setKillTime(double time) {
82   simcall_process_set_kill_time(pimpl_,time);
83 }
84
85 double Actor::killTime()
86 {
87   return simcall_process_get_kill_time(pimpl_);
88 }
89
90 void Actor::kill(int pid) {
91   msg_process_t process = SIMIX_process_from_PID(pid);
92   if(process != nullptr) {
93     simcall_process_kill(process);
94   } else {
95     std::ostringstream oss;
96     oss << "kill: ("<< pid <<") - No such process" << std::endl;
97     throw std::runtime_error(oss.str());
98   }
99 }
100
101 smx_actor_t Actor::getImpl() {
102   return pimpl_;
103 }
104
105 void Actor::kill() {
106   simcall_process_kill(pimpl_);
107 }
108
109 // ***** Static functions *****
110
111 ActorPtr Actor::byPid(int pid)
112 {
113   smx_actor_t process = SIMIX_process_from_PID(pid);
114   if (process != nullptr)
115     return ActorPtr(&process->getIface());
116   else
117     return nullptr;
118 }
119
120 void Actor::killAll() {
121   simcall_process_killall(1);
122 }
123
124 // ***** this_actor *****
125
126 namespace this_actor {
127
128 void sleep_for(double duration)
129 {
130   if (duration > 0)
131     simcall_process_sleep(duration);
132 }
133
134 XBT_PUBLIC(void) sleep_until(double timeout)
135 {
136   double now = SIMIX_get_clock();
137   if (timeout > now)
138     simcall_process_sleep(timeout - now);
139 }
140
141 e_smx_state_t execute(double flops) {
142   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
143   return simcall_execution_wait(s);
144 }
145
146 void* recv(MailboxPtr chan) {
147   void *res = nullptr;
148   Comm& c = Comm::recv_init(chan);
149   c.setDstData(&res,sizeof(res));
150   c.wait();
151   return res;
152 }
153
154 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
155   Comm& c = Comm::send_init(chan);
156   c.setRemains(simulatedSize);
157   c.setSrcData(payload);
158   // c.start() is optional.
159   c.wait();
160 }
161
162 int pid()
163 {
164   return SIMIX_process_self()->pid;
165 }
166
167 int ppid()
168 {
169   return SIMIX_process_self()->ppid;
170 }
171
172 }
173 }
174 }