Logo AND Algorithmique Numérique Distribuée

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