Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
35b65f6a0a91f44a4f11fef27c68dce17d5edbde
[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/simix/smx_private.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
18
19 namespace simgrid {
20 namespace s4u {
21
22 Actor::Actor(const char* name, s4u::Host *host, double killTime, std::function<void()> code)
23 {
24   // TODO, when autorestart is used, the std::function is copied so the new
25   // instance will get a fresh (reinitialized) state. Is this what we want?
26   this->pimpl_ = SIMIX_process_ref(simcall_process_create(
27     name, std::move(code), nullptr, host->name().c_str(),
28     killTime, nullptr, 0));
29 }
30
31 Actor::Actor(const char* name, s4u::Host *host, double killTime,
32   const char* function, std::vector<std::string> args)
33 {
34   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
35   simgrid::simix::ActorCode code = factory(std::move(args));
36   this->pimpl_ = SIMIX_process_ref(simcall_process_create(
37     name, std::move(code), nullptr, host->name().c_str(),
38     killTime, nullptr, 0));
39 }
40
41 void Actor::join() {
42   simcall_process_join(pimpl_, -1);
43 }
44
45 void Actor::setAutoRestart(bool autorestart) {
46   simcall_process_auto_restart_set(pimpl_,autorestart);
47 }
48
49 s4u::Host *Actor::getHost() {
50   return s4u::Host::by_name(sg_host_get_name(simcall_process_get_host(pimpl_)));
51 }
52
53 const char* Actor::getName() {
54   return simcall_process_get_name(pimpl_);
55 }
56
57 int Actor::getPid(){
58   return simcall_process_get_PID(pimpl_);
59 }
60
61 void Actor::setKillTime(double time) {
62   simcall_process_set_kill_time(pimpl_,time);
63 }
64
65 double Actor::getKillTime() {
66   return simcall_process_get_kill_time(pimpl_);
67 }
68
69 void Actor::kill(int pid) {
70   msg_process_t process = SIMIX_process_from_PID(pid);
71   if(process != nullptr) {
72     simcall_process_kill(process);
73   } else {
74     std::ostringstream oss;
75     oss << "kill: ("<< pid <<") - No such process" << std::endl;
76     throw std::runtime_error(oss.str());
77   }
78 }
79
80 smx_process_t Actor::getInferior() {
81   return pimpl_;
82 }
83
84
85 void Actor::kill() {
86   simcall_process_kill(pimpl_);
87 }
88
89 simgrid::s4u::Actor Actor::forPid(int pid)
90 {
91   // Should we throw if we did not find it?
92   smx_process_t process = SIMIX_process_from_PID(pid);
93   return simgrid::s4u::Actor(process);
94 }
95
96 // static stuff:
97
98 void Actor::killAll() {
99   simcall_process_killall(1);
100 }
101
102 namespace this_actor {
103
104 void sleep(double duration) {
105   simcall_process_sleep(duration);
106 }
107
108 e_smx_state_t execute(double flops) {
109   smx_synchro_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/);
110   return simcall_execution_wait(s);
111 }
112
113 void* recv(Mailbox &chan) {
114   void *res = nullptr;
115   Comm c = Comm::recv_init(chan);
116   c.setDstData(&res,sizeof(res));
117   c.wait();
118   return res;
119 }
120
121 void send(Mailbox &chan, void *payload, size_t simulatedSize) {
122   Comm c = Comm::send_init(chan);
123   c.setRemains(simulatedSize);
124   c.setSrcData(payload);
125   // c.start() is optional.
126   c.wait();
127 }
128
129 int getPid() {
130   return simcall_process_get_PID(SIMIX_process_self());
131 }
132
133 }
134 }
135 }