Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Populate the kernel::context namespace and continue separating concerns out of simix
[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_process_t process = simcall_process_create(
39     name, std::move(code), nullptr, host->name().c_str(),
40     killTime, nullptr, 0);
41   return Ptr(&process->getIface());
42 }
43
44 ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime,
45   const char* function, std::vector<std::string> args)
46 {
47   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
48   simgrid::simix::ActorCode code = factory(std::move(args));
49   smx_process_t process = simcall_process_create(
50     name, std::move(code), nullptr, host->name().c_str(),
51     killTime, nullptr, 0);
52   return ActorPtr(&process->getIface());
53 }
54
55 // ***** Actor methods *****
56
57 void Actor::join() {
58   simcall_process_join(pimpl_, -1);
59 }
60
61 void Actor::setAutoRestart(bool autorestart) {
62   simcall_process_auto_restart_set(pimpl_,autorestart);
63 }
64
65 s4u::Host *Actor::getHost() {
66   return pimpl_->host;
67 }
68
69 simgrid::xbt::string Actor::getName() {
70   return pimpl_->name;
71 }
72
73 int Actor::getPid(){
74   return pimpl_->pid;
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_process_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_process_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_synchro_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
138   return simcall_execution_wait(s);
139 }
140
141 void* recv(Mailbox &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(Mailbox &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 }
162 }
163 }