Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Useless test; TODO--.
[simgrid.git] / src / s4u / s4u_Actor.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/actor.h"
8 #include "simgrid/modelchecker.h"
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/Exec.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/VirtualMachine.hpp"
13 #include "src/include/mc/mc.h"
14 #include "src/kernel/EngineImpl.hpp"
15 #include "src/kernel/activity/ExecImpl.hpp"
16 #include "src/mc/mc_replay.hpp"
17 #include "src/surf/HostImpl.hpp"
18
19 #include <algorithm>
20 #include <sstream>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
23
24 namespace simgrid {
25
26 template class xbt::Extendable<s4u::Actor>;
27
28 namespace s4u {
29
30 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
31 xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
32 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
33 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
34 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
35 #ifndef DOXYGEN
36 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start; // XBT_ATTRIB_DEPRECATED_v329
37 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end;   // XBT_ATTRIB_DEPRECATED_v329
38 #endif
39 xbt::signal<void(Actor const&, Host const& previous_location)> s4u::Actor::on_host_change;
40 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
41 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
42
43 // ***** Actor creation *****
44 Actor* Actor::self()
45 {
46   const kernel::context::Context* self_context = kernel::context::Context::self();
47   if (self_context == nullptr)
48     return nullptr;
49
50   return self_context->get_actor()->get_ciface();
51 }
52
53 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
54 {
55   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
56   kernel::actor::ActorImpl* actor =
57       kernel::actor::simcall([self, &name, host] { return self->init(name, host).get(); });
58   return actor->get_iface();
59 }
60
61 /** Set a non-default stack size for this context (in Kb)
62  *
63  * This must be done before starting the actor, and it won't work with the thread factory. */
64 ActorPtr Actor::set_stacksize(unsigned stacksize)
65 {
66   pimpl_->set_stacksize(stacksize * 1024);
67   return this;
68 }
69
70 ActorPtr Actor::start(const std::function<void()>& code)
71 {
72   simgrid::kernel::actor::simcall([this, &code] { pimpl_->start(code); });
73   return this;
74 }
75
76 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
77 {
78   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
79   kernel::actor::ActorImpl* actor =
80       kernel::actor::simcall([self, &name, host, &code] { return self->init(name, host)->start(code); });
81
82   return actor->get_iface();
83 }
84
85 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
86                        std::vector<std::string> args)
87 {
88   const simgrid::kernel::actor::ActorCodeFactory& factory =
89       simgrid::kernel::EngineImpl::get_instance()->get_function(function);
90   return create(name, host, factory(std::move(args)));
91 }
92
93 void intrusive_ptr_add_ref(const Actor* actor)
94 {
95   intrusive_ptr_add_ref(actor->pimpl_);
96 }
97 void intrusive_ptr_release(const Actor* actor)
98 {
99   intrusive_ptr_release(actor->pimpl_);
100 }
101 int Actor::get_refcount() const
102 {
103   return pimpl_->get_refcount();
104 }
105
106 // ***** Actor methods *****
107
108 void Actor::join() const
109 {
110   join(-1);
111 }
112
113 void Actor::join(double timeout) const
114 {
115   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
116   const kernel::actor::ActorImpl* target = pimpl_;
117   kernel::actor::simcall_blocking([issuer, target, timeout] {
118     if (target->finished_) {
119       // The joined actor is already finished, just wake up the issuer right away
120       issuer->simcall_answer();
121     } else {
122       kernel::activity::ActivityImplPtr sync = issuer->join(target, timeout);
123       sync->register_simcall(&issuer->simcall_);
124     }
125   });
126 }
127
128 void Actor::set_auto_restart(bool autorestart)
129 {
130   kernel::actor::simcall([this, autorestart]() {
131     xbt_assert(autorestart && not pimpl_->has_to_auto_restart()); // FIXME: handle all cases
132     pimpl_->set_auto_restart(autorestart);
133
134     auto* arg = new kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
135     XBT_DEBUG("Adding %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
136     pimpl_->get_host()->get_impl()->add_actor_at_boot(arg);
137   });
138 }
139
140 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
141 {
142   kernel::actor::simcall([this, &fun] { pimpl_->on_exit->emplace_back(fun); });
143 }
144
145 void Actor::set_host(Host* new_host)
146 {
147   if (s4u::Actor::on_migration_start.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
148     static bool already_warned = false;
149     if (not already_warned) {
150       XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_start. This will be removed "
151                "in v3.29");
152       already_warned = true;
153     }
154     s4u::Actor::on_migration_start(*this);
155   }
156
157   const s4u::Host* previous_location = get_host();
158
159   kernel::actor::simcall([this, new_host]() {
160     for (auto const& activity : pimpl_->activities_) {
161       // FIXME: implement the migration of other kinds of activities
162       if (auto exec = boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(activity))
163         exec->migrate(new_host);
164     }
165     this->pimpl_->set_host(new_host);
166   });
167
168   if (s4u::Actor::on_migration_end.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
169     static bool already_warned = false;
170     if (not already_warned) {
171       XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_end. This will be removed in "
172                "v3.29");
173       already_warned = true;
174     }
175     s4u::Actor::on_migration_end(*this);
176   }
177
178   s4u::Actor::on_host_change(*this, *previous_location);
179 }
180
181 s4u::Host* Actor::get_host() const
182 {
183   return this->pimpl_->get_host();
184 }
185
186 void Actor::daemonize()
187 {
188   kernel::actor::simcall([this]() { pimpl_->daemonize(); });
189 }
190
191 bool Actor::is_daemon() const
192 {
193   return this->pimpl_->is_daemon();
194 }
195
196 const simgrid::xbt::string& Actor::get_name() const
197 {
198   return this->pimpl_->get_name();
199 }
200
201 const char* Actor::get_cname() const
202 {
203   return this->pimpl_->get_cname();
204 }
205
206 aid_t Actor::get_pid() const
207 {
208   return this->pimpl_->get_pid();
209 }
210
211 aid_t Actor::get_ppid() const
212 {
213   return this->pimpl_->get_ppid();
214 }
215
216 void Actor::suspend()
217 {
218   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
219   kernel::actor::ActorImpl* target = pimpl_;
220   s4u::Actor::on_suspend(*this);
221   kernel::actor::simcall_blocking([issuer, target]() {
222     target->suspend();
223     if (target != issuer) {
224       /* If we are suspending ourselves, then just do not finish the simcall now */
225       issuer->simcall_answer();
226     }
227   });
228 }
229
230 void Actor::resume()
231 {
232   kernel::actor::simcall([this] { pimpl_->resume(); });
233   s4u::Actor::on_resume(*this);
234 }
235
236 bool Actor::is_suspended() const
237 {
238   return pimpl_->is_suspended();
239 }
240
241 void Actor::set_kill_time(double kill_time)
242 {
243   kernel::actor::simcall([this, kill_time] { pimpl_->set_kill_time(kill_time); });
244 }
245
246 /** @brief Get the kill time of an actor(or 0 if unset). */
247 double Actor::get_kill_time() const
248 {
249   return pimpl_->get_kill_time();
250 }
251
252 void Actor::kill()
253 {
254   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
255   kernel::actor::simcall([this, self] { self->kill(pimpl_); });
256 }
257
258 // ***** Static functions *****
259
260 ActorPtr Actor::by_pid(aid_t pid)
261 {
262   kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(pid);
263   if (actor != nullptr)
264     return actor->get_iface();
265   else
266     return ActorPtr();
267 }
268
269 void Actor::kill_all()
270 {
271   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
272   kernel::actor::simcall([self] { self->kill_all(); });
273 }
274
275 const std::unordered_map<std::string, std::string>* Actor::get_properties() const
276 {
277   return pimpl_->get_properties();
278 }
279
280 /** Retrieve the property value (or nullptr if not set) */
281 const char* Actor::get_property(const std::string& key) const
282 {
283   return pimpl_->get_property(key);
284 }
285
286 void Actor::set_property(const std::string& key, const std::string& value)
287 {
288   kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
289 }
290
291 Actor* Actor::restart()
292 {
293   return kernel::actor::simcall([this]() { return pimpl_->restart(); });
294 }
295
296 // ***** this_actor *****
297
298 namespace this_actor {
299
300 /** Returns true if run from the kernel mode, and false if run from a real actor
301  *
302  * Everything that is run out of any actor (simulation setup before the engine is run,
303  * computing the model evolutions as a result to the actors' action, etc) is run in
304  * kernel mode, just as in any operating systems.
305  *
306  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
307  * because it is the one scheduling when the others should move or wait.
308  */
309 bool is_maestro()
310 {
311   return SIMIX_is_maestro();
312 }
313
314 void sleep_for(double duration)
315 {
316   xbt_assert(std::isfinite(duration), "duration is not finite!");
317
318   if (duration <= 0) /* that's a no-op */
319     return;
320
321   if (duration < sg_surf_precision) {
322     static unsigned int warned = 0; // At most 20 such warnings
323     warned++;
324     if (warned <= 20)
325       XBT_INFO("The parameter to sleep_for() is smaller than the SimGrid numerical accuracy (%g < %g). "
326                "Please refer to https://simgrid.org/doc/latest/Configuring_SimGrid.html#numerical-precision",
327                duration, sg_surf_precision);
328     if (warned == 20)
329       XBT_VERB("(further warnings about the numerical accuracy of sleep_for() will be omitted).");
330   }
331
332   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
333   Actor::on_sleep(*issuer->get_ciface());
334
335   kernel::actor::simcall_blocking([issuer, duration]() {
336     if (MC_is_active() || MC_record_replay_is_active()) {
337       MC_process_clock_add(issuer, duration);
338       issuer->simcall_answer();
339       return;
340     }
341     kernel::activity::ActivityImplPtr sync = issuer->sleep(duration);
342     sync->register_simcall(&issuer->simcall_);
343   });
344
345   Actor::on_wake_up(*issuer->get_ciface());
346 }
347
348 void yield()
349 {
350   kernel::actor::simcall([] { /* do nothing*/ });
351 }
352
353 XBT_PUBLIC void sleep_until(double wakeup_time)
354 {
355   double now = s4u::Engine::get_clock();
356   if (wakeup_time > now)
357     sleep_for(wakeup_time - now);
358 }
359
360 void execute(double flops)
361 {
362   execute(flops, 1.0 /* priority */);
363 }
364
365 void execute(double flops, double priority)
366 {
367   exec_init(flops)->set_priority(priority)->vetoable_start()->wait();
368 }
369
370 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
371                       const std::vector<double>& bytes_amounts)
372 {
373   exec_init(hosts, flops_amounts, bytes_amounts)->wait();
374 }
375
376 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
377                       const std::vector<double>& bytes_amounts, double timeout) // XBT_ATTRIB_DEPRECATED_v329
378 {
379   exec_init(hosts, flops_amounts, bytes_amounts)->wait_for(timeout);
380 }
381
382 ExecPtr exec_init(double flops_amount)
383 {
384   return Exec::init()->set_flops_amount(flops_amount)->set_host(get_host());
385 }
386
387 ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
388                   const std::vector<double>& bytes_amounts)
389 {
390   xbt_assert(hosts.size() > 0, "Your parallel executions must span over at least one host.");
391   xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
392              "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
393   xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
394              "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
395              hosts.size(), hosts.size(), bytes_amounts.size());
396   /* Check that we are not mixing VMs and PMs in the parallel task */
397   bool is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(hosts.front()));
398   xbt_assert(std::all_of(hosts.begin(), hosts.end(),
399                          [is_a_vm](s4u::Host* elm) {
400                            bool tmp_is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(elm));
401                            return is_a_vm == tmp_is_a_vm;
402                          }),
403              "parallel_execute: mixing VMs and PMs is not supported (yet).");
404   /* checking for infinite values */
405   xbt_assert(std::all_of(flops_amounts.begin(), flops_amounts.end(), [](double elm) { return std::isfinite(elm); }),
406              "flops_amounts comprises infinite values!");
407   xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
408              "flops_amounts comprises infinite values!");
409
410   return Exec::init()->set_flops_amounts(flops_amounts)->set_bytes_amounts(bytes_amounts)->set_hosts(hosts);
411 }
412
413 ExecPtr exec_async(double flops)
414 {
415   ExecPtr res = exec_init(flops);
416   res->vetoable_start();
417   return res;
418 }
419
420 aid_t get_pid()
421 {
422   return simgrid::kernel::actor::ActorImpl::self()->get_pid();
423 }
424
425 aid_t get_ppid()
426 {
427   return simgrid::kernel::actor::ActorImpl::self()->get_ppid();
428 }
429
430 std::string get_name()
431 {
432   return simgrid::kernel::actor::ActorImpl::self()->get_name();
433 }
434
435 const char* get_cname()
436 {
437   return simgrid::kernel::actor::ActorImpl::self()->get_cname();
438 }
439
440 Host* get_host()
441 {
442   return simgrid::kernel::actor::ActorImpl::self()->get_host();
443 }
444
445 void suspend()
446 {
447   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
448   s4u::Actor::on_suspend(*self->get_ciface());
449   kernel::actor::simcall_blocking([self] { self->suspend(); });
450 }
451
452 void exit()
453 {
454   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
455   simgrid::kernel::actor::simcall([self] { self->exit(); });
456 }
457
458 void on_exit(const std::function<void(bool)>& fun)
459 {
460   simgrid::kernel::actor::ActorImpl::self()->get_iface()->on_exit(fun);
461 }
462
463 /** @brief Moves the current actor to another host
464  *
465  * @see simgrid::s4u::Actor::migrate() for more information
466  */
467 void set_host(Host* new_host)
468 {
469   simgrid::kernel::actor::ActorImpl::self()->get_iface()->set_host(new_host);
470 }
471 void migrate(Host* new_host) // XBT_ATTRIB_DEPRECATED_v329
472 {
473   set_host(new_host);
474 }
475
476 } // namespace this_actor
477 } // namespace s4u
478 } // namespace simgrid
479
480 /* **************************** Public C interface *************************** */
481 size_t sg_actor_count()
482 {
483   return simgrid::s4u::Engine::get_instance()->get_actor_count();
484 }
485
486 sg_actor_t* sg_actor_list()
487 {
488   const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
489   size_t actor_count      = e->get_actor_count();
490   xbt_assert(actor_count > 0, "There is no actor!");
491   std::vector<simgrid::s4u::ActorPtr> actors = e->get_all_actors();
492
493   sg_actor_t* res = xbt_new(sg_actor_t, actors.size());
494   for (size_t i = 0; i < actor_count; i++)
495     res[i] = actors[i].get();
496   return res;
497 }
498
499 sg_actor_t sg_actor_init(const char* name, sg_host_t host)
500 {
501   return simgrid::s4u::Actor::init(name, host).get();
502 }
503
504 void sg_actor_start_(sg_actor_t actor, xbt_main_func_t code, int argc, const char* const* argv)
505 {
506   simgrid::kernel::actor::ActorCode function;
507   if (code)
508     function = simgrid::xbt::wrap_main(code, argc, argv);
509   actor->start(function);
510 }
511
512 sg_actor_t sg_actor_create_(const char* name, sg_host_t host, xbt_main_func_t code, int argc, const char* const* argv)
513 {
514   simgrid::kernel::actor::ActorCode function = simgrid::xbt::wrap_main(code, argc, argv);
515   return simgrid::s4u::Actor::init(name, host)->start(function).get();
516 }
517
518 void sg_actor_set_stacksize(sg_actor_t actor, unsigned size)
519 {
520   actor->set_stacksize(size);
521 }
522
523 void sg_actor_exit()
524 {
525   simgrid::s4u::this_actor::exit();
526 }
527
528 /**
529  * @brief Returns the process ID of @a actor.
530  *
531  * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
532  */
533
534 aid_t sg_actor_get_pid(const_sg_actor_t actor)
535 {
536   /* Do not raise an exception here: this function is called by the logs
537    * and the exceptions, so it would be called back again and again */
538   if (actor == nullptr || actor->get_impl() == nullptr)
539     return 0;
540   return actor->get_pid();
541 }
542
543 /**
544  * @brief Returns the process ID of the parent of @a actor.
545  *
546  * This function checks whether @a actor is a valid pointer and return its parent's PID.
547  * Returns -1 if the actor has not been created by any other actor.
548  */
549 aid_t sg_actor_get_ppid(const_sg_actor_t actor)
550 {
551   return actor->get_ppid();
552 }
553
554 /**
555  * @brief Return a #sg_actor_t given its PID.
556  *
557  * This function search in the list of all the created sg_actor_t for a sg_actor_t  whose PID is equal to @a PID.
558  * If none is found, @c nullptr is returned.
559    Note that the PID are unique in the whole simulation, not only on a given host.
560  */
561 sg_actor_t sg_actor_by_pid(aid_t pid)
562 {
563   return simgrid::s4u::Actor::by_pid(pid).get();
564 }
565
566 aid_t sg_actor_get_PID(const_sg_actor_t actor) // XBT_ATTRIB_DEPRECATED_v331
567 {
568   return sg_actor_get_pid(actor);
569 }
570
571 aid_t sg_actor_get_PPID(const_sg_actor_t actor) // XBT_ATTRIB_DEPRECATED_v331
572 {
573   return sg_actor_get_ppid(actor);
574 }
575
576 sg_actor_t sg_actor_by_PID(aid_t pid) // XBT_ATTRIB_DEPRECATED_v331
577 {
578   return sg_actor_by_pid(pid);
579 }
580
581 /** @brief Return the name of an actor. */
582 const char* sg_actor_get_name(const_sg_actor_t actor)
583 {
584   return actor->get_cname();
585 }
586
587 sg_host_t sg_actor_get_host(const_sg_actor_t actor)
588 {
589   return actor->get_host();
590 }
591
592 /**
593  * @brief Returns the value of a given actor property
594  *
595  * @param actor an actor
596  * @param name a property name
597  * @return value of a property (or nullptr if the property is not set)
598  */
599 const char* sg_actor_get_property_value(const_sg_actor_t actor, const char* name)
600 {
601   return actor->get_property(name);
602 }
603
604 /**
605  * @brief Return the list of properties
606  *
607  * This function returns all the parameters associated with an actor
608  */
609 xbt_dict_t sg_actor_get_properties(const_sg_actor_t actor)
610 {
611   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
612   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
613   const std::unordered_map<std::string, std::string>* props = actor->get_properties();
614   if (props == nullptr)
615     return nullptr;
616   for (auto const& kv : *props) {
617     xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()));
618   }
619   return as_dict;
620 }
621
622 /**
623  * @brief Suspend the actor.
624  *
625  * This function suspends the actor by suspending the task on which it was waiting for the completion.
626  */
627 void sg_actor_suspend(sg_actor_t actor)
628 {
629   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
630   actor->suspend();
631 }
632
633 /**
634  * @brief Resume a suspended actor.
635  *
636  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
637  */
638 void sg_actor_resume(sg_actor_t actor)
639 {
640   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
641   actor->resume();
642 }
643
644 /**
645  * @brief Returns true if the actor is suspended .
646  *
647  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
648  */
649 int sg_actor_is_suspended(const_sg_actor_t actor)
650 {
651   return actor->is_suspended();
652 }
653
654 /** @brief Restarts an actor from the beginning. */
655 sg_actor_t sg_actor_restart(sg_actor_t actor)
656 {
657   return actor->restart();
658 }
659
660 /**
661  * @brief Sets the "auto-restart" flag of the actor.
662  * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
663  */
664 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
665 {
666   actor->set_auto_restart(auto_restart);
667 }
668
669 /** @brief This actor will be terminated automatically when the last non-daemon actor finishes */
670 void sg_actor_daemonize(sg_actor_t actor)
671 {
672   actor->daemonize();
673 }
674
675 /** Returns whether or not this actor has been daemonized or not */
676 int sg_actor_is_daemon(const_sg_actor_t actor)
677 {
678   return actor->is_daemon();
679 }
680
681 /**
682  * @brief Migrates an actor to another location.
683  *
684  * This function changes the value of the #sg_host_t on  which @a actor is running.
685  */
686 void sg_actor_set_host(sg_actor_t actor, sg_host_t host)
687 {
688   actor->set_host(host);
689 }
690 void sg_actor_migrate(sg_actor_t actor, sg_host_t host) // XBT_ATTRIB_DEPRECATED_v329
691 {
692   actor->set_host(host);
693 }
694
695 /**
696  * @brief Wait for the completion of a #sg_actor_t.
697  *
698  * @param actor the actor to wait for
699  * @param timeout wait until the actor is over, or the timeout expires
700  */
701 void sg_actor_join(const_sg_actor_t actor, double timeout)
702 {
703   actor->join(timeout);
704 }
705
706 void sg_actor_kill(sg_actor_t actor)
707 {
708   actor->kill();
709 }
710
711 void sg_actor_kill_all()
712 {
713   simgrid::s4u::Actor::kill_all();
714 }
715
716 /**
717  * @brief Set the kill time of an actor.
718  *
719  * @param actor an actor
720  * @param kill_time the time when the actor is killed.
721  */
722 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
723 {
724   actor->set_kill_time(kill_time);
725 }
726
727 /** Yield the current actor; let the other actors execute first */
728 void sg_actor_yield()
729 {
730   simgrid::s4u::this_actor::yield();
731 }
732
733 void sg_actor_sleep_for(double duration)
734 {
735   simgrid::s4u::this_actor::sleep_for(duration);
736 }
737
738 void sg_actor_sleep_until(double wakeup_time)
739 {
740   simgrid::s4u::this_actor::sleep_until(wakeup_time);
741 }
742
743 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
744 {
745   xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
746   std::unordered_map<std::string, std::string> props;
747   xbt_dict_cursor_t cursor = nullptr;
748   char* key;
749   char* value;
750   xbt_dict_foreach (properties, cursor, key, value)
751     props[key] = value;
752   xbt_dict_free(&properties);
753
754   /* Let's create the actor: SIMIX may decide to start it right now, even before returning the flow control to us */
755   smx_actor_t actor = nullptr;
756   try {
757     actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host).get();
758     actor->set_properties(props);
759   } catch (simgrid::HostFailureException const&) {
760     xbt_die("Could not attach");
761   }
762
763   simgrid::s4u::this_actor::yield();
764   return actor->get_ciface();
765 }
766
767 void sg_actor_detach()
768 {
769   simgrid::kernel::actor::ActorImpl::detach();
770 }
771
772 aid_t sg_actor_self_get_pid()
773 {
774   return simgrid::s4u::this_actor::get_pid();
775 }
776
777 aid_t sg_actor_self_get_ppid()
778 {
779   return simgrid::s4u::this_actor::get_ppid();
780 }
781
782 const char* sg_actor_self_get_name()
783 {
784   return simgrid::s4u::this_actor::get_cname();
785 }
786
787 void* sg_actor_self_get_data()
788 {
789   return simgrid::s4u::Actor::self()->get_data();
790 }
791
792 void sg_actor_self_set_data(void* userdata)
793 {
794   simgrid::s4u::Actor::self()->set_data(userdata);
795 }
796
797 void* sg_actor_self_data() // XBT_ATTRIB_DEPRECATED_v330
798 {
799   return sg_actor_self_get_data();
800 }
801
802 void sg_actor_self_data_set(void* userdata) // XBT_ATTRIB_DEPRECATED_v330
803 {
804   sg_actor_self_set_data(userdata);
805 }
806
807 sg_actor_t sg_actor_self()
808 {
809   return simgrid::s4u::Actor::self();
810 }
811
812 void sg_actor_self_execute(double flops) // XBT_ATTRIB_DEPRECATED_v330
813 {
814   simgrid::s4u::this_actor::execute(flops);
815 }
816
817 void sg_actor_execute(double flops)
818 {
819   simgrid::s4u::this_actor::execute(flops);
820 }
821 void sg_actor_execute_with_priority(double flops, double priority)
822 {
823   simgrid::s4u::this_actor::exec_init(flops)->set_priority(priority)->wait();
824 }
825
826 void sg_actor_parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
827 {
828   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
829   std::vector<double> flops;
830   std::vector<double> bytes;
831   if (flops_amount != nullptr)
832     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
833   if (bytes_amount != nullptr)
834     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
835
836   simgrid::s4u::this_actor::parallel_execute(hosts, flops, bytes);
837 }
838
839 /** @brief Take an extra reference on that actor to prevent it to be garbage-collected */
840 void sg_actor_ref(const_sg_actor_t actor)
841 {
842   intrusive_ptr_add_ref(actor);
843 }
844 /** @brief Release a reference on that actor so that it can get be garbage-collected */
845 void sg_actor_unref(const_sg_actor_t actor)
846 {
847   intrusive_ptr_release(actor);
848 }
849
850 /** @brief Return the user data of a #sg_actor_t */
851 void* sg_actor_get_data(const_sg_actor_t actor)
852 {
853   return actor->get_data();
854 }
855
856 /** @brief Set the user data of a #sg_actor_t */
857 void sg_actor_set_data(sg_actor_t actor, void* userdata)
858 {
859   actor->set_data(userdata);
860 }
861
862 void* sg_actor_data(const_sg_actor_t actor) // XBT_ATTRIB_DEPRECATED_v330
863 {
864   return sg_actor_get_data(actor);
865 }
866
867 void sg_actor_data_set(sg_actor_t actor, void* userdata) // XBT_ATTRIB_DEPRECATED_v330
868 {
869   sg_actor_set_data(actor, userdata);
870 }
871
872 /** @brief Add a function to the list of "on_exit" functions for the current actor.
873  *  The on_exit functions are the functions executed when your actor is killed.
874  *  You should use them to free the data used by your actor.
875  */
876 void sg_actor_on_exit(void_f_int_pvoid_t fun, void* data)
877 {
878   simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); });
879 }
880
881 sg_exec_t sg_actor_exec_init(double computation_amount)
882 {
883   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(computation_amount);
884   exec->add_ref();
885   return exec.get();
886 }
887
888 sg_exec_t sg_actor_parallel_exec_init(int host_nb, const sg_host_t* host_list, double* flops_amount,
889                                       double* bytes_amount)
890 {
891   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
892   std::vector<double> flops;
893   std::vector<double> bytes;
894   if (flops_amount != nullptr)
895     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
896   if (bytes_amount != nullptr)
897     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
898
899   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(hosts, flops, bytes);
900   exec->add_ref();
901   return exec.get();
902 }
903
904 sg_exec_t sg_actor_exec_async(double computation_amount)
905 {
906   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_async(computation_amount);
907   exec->add_ref();
908   return exec.get();
909 }