Logo AND Algorithmique Numérique Distribuée

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