Logo AND Algorithmique Numérique Distribuée

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