Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6b6f990c468a0398f55221f8799af7b765383ef7
[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(pimpl_, -1);
55 }
56
57 void Actor::setAutoRestart(bool autorestart) {
58   simcall_process_auto_restart_set(pimpl_,autorestart);
59 }
60
61 s4u::Host *Actor::getHost() {
62   return pimpl_->host;
63 }
64
65 simgrid::xbt::string Actor::getName() {
66   return pimpl_->name;
67 }
68
69 int Actor::getPid(){
70   return pimpl_->pid;
71 }
72
73 int Actor::getPpid() {
74   return pimpl_->ppid;
75 }
76
77 void Actor::setKillTime(double time) {
78   simcall_process_set_kill_time(pimpl_,time);
79 }
80
81 double Actor::getKillTime() {
82   return simcall_process_get_kill_time(pimpl_);
83 }
84
85 void Actor::kill(int pid) {
86   msg_process_t process = SIMIX_process_from_PID(pid);
87   if(process != nullptr) {
88     simcall_process_kill(process);
89   } else {
90     std::ostringstream oss;
91     oss << "kill: ("<< pid <<") - No such process" << std::endl;
92     throw std::runtime_error(oss.str());
93   }
94 }
95
96 smx_actor_t Actor::getImpl() {
97   return pimpl_;
98 }
99
100 void Actor::kill() {
101   simcall_process_kill(pimpl_);
102 }
103
104 // ***** Static functions *****
105
106 ActorPtr Actor::forPid(int pid)
107 {
108   smx_actor_t process = SIMIX_process_from_PID(pid);
109   if (process != nullptr)
110     return ActorPtr(&process->getIface());
111   else
112     return nullptr;
113 }
114
115 void Actor::killAll() {
116   simcall_process_killall(1);
117 }
118
119 // ***** this_actor *****
120
121 namespace this_actor {
122
123 void sleep_for(double duration)
124 {
125   if (duration > 0)
126     simcall_process_sleep(duration);
127 }
128
129 XBT_PUBLIC(void) sleep_until(double timeout)
130 {
131   double now = SIMIX_get_clock();
132   if (timeout > now)
133     simcall_process_sleep(timeout - now);
134 }
135
136 e_smx_state_t execute(double flops) {
137   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
138   return simcall_execution_wait(s);
139 }
140
141 void* recv(MailboxPtr chan) {
142   void *res = nullptr;
143   Comm& c = Comm::recv_init(chan);
144   c.setDstData(&res,sizeof(res));
145   c.wait();
146   return res;
147 }
148
149 void send(MailboxPtr chan, void *payload, size_t simulatedSize) {
150   Comm& c = Comm::send_init(chan);
151   c.setRemains(simulatedSize);
152   c.setSrcData(payload);
153   // c.start() is optional.
154   c.wait();
155 }
156
157 int getPid() {
158   return SIMIX_process_self()->pid;
159 }
160
161 int getPpid() {
162   return SIMIX_process_self()->ppid;
163 }
164
165 }
166 }
167 }