Logo AND Algorithmique Numérique Distribuée

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