Logo AND Algorithmique Numérique Distribuée

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