Logo AND Algorithmique Numérique Distribuée

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