Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix dist
[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   smx_actor_t actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
34   return ActorPtr(&actor->getIface());
35 }
36
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
38 {
39   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
40   simgrid::simix::ActorCode code = factory(std::move(args));
41   smx_actor_t actor                         = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
42   return ActorPtr(&actor->getIface());
43 }
44
45 // ***** Actor methods *****
46
47 void Actor::join() {
48   simcall_process_join(this->pimpl_, -1);
49 }
50
51 void Actor::setAutoRestart(bool autorestart) {
52   simcall_process_auto_restart_set(pimpl_,autorestart);
53 }
54
55 s4u::Host* Actor::host()
56 {
57   return this->pimpl_->host;
58 }
59
60 simgrid::xbt::string Actor::name()
61 {
62   return this->pimpl_->name;
63 }
64
65 int Actor::pid()
66 {
67   return this->pimpl_->pid;
68 }
69
70 int Actor::ppid()
71 {
72   return this->pimpl_->ppid;
73 }
74
75 void Actor::setKillTime(double time) {
76   simcall_process_set_kill_time(pimpl_,time);
77 }
78
79 double Actor::killTime()
80 {
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_actor_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::byPid(int pid)
106 {
107   smx_actor_t process = SIMIX_process_from_PID(pid);
108   if (process != nullptr)
109     return ActorPtr(&process->getIface());
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_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
137   return simcall_execution_wait(s);
138 }
139
140 void* recv(MailboxPtr 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(MailboxPtr 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 pid()
157 {
158   return SIMIX_process_self()->pid;
159 }
160
161 int ppid()
162 {
163   return SIMIX_process_self()->ppid;
164 }
165
166 }
167 }
168 }