+void SwappedContext::stop()
+{
+ Context::stop();
+ /* We must cut the actor execution using an exception to properly free the C++ RAII variables */
+ throw ForcefulKillException();
+}
+
+/** Maestro wants to run all ready actors */
+void SwappedContextFactory::run_all()
+{
+ /* This function is called by maestro at the beginning of a scheduling round to get all working threads executing some
+ * stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul
+ * for the ones of the simulated processes that must run.
+ */
+ if (parallel_) {
+ threads_working_ = 0;
+
+ // We lazily create the parmap so that all options are actually processed when doing so.
+ if (parmap_ == nullptr)
+ parmap_.reset(
+ new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode()));
+
+ // Usually, Parmap::apply() executes the provided function on all elements of the array.
+ // Here, the executed function does not return the control to the parmap before all the array is processed:
+ // - suspend() should switch back to the worker_context (either maestro or one of its minions) to return
+ // the control to the parmap. Instead, it uses parmap_->next() to steal another work, and does it directly.
+ // It only yields back to worker_context when the work array is exhausted.
+ // - So, resume() is only launched from the parmap for the first job of each minion.
+ parmap_->apply(
+ [](smx_actor_t process) {
+ SwappedContext* context = static_cast<SwappedContext*>(process->context_.get());
+ context->resume();
+ },
+ simix_global->actors_to_run);
+ } else { // sequential execution
+ if (simix_global->actors_to_run.empty())
+ return;
+
+ /* maestro is already saved in the first slot of workers_context_ */
+ smx_actor_t first_actor = simix_global->actors_to_run.front();
+ process_index_ = 1;
+ /* execute the first actor; it will chain to the others when using suspend() */
+ static_cast<SwappedContext*>(first_actor->context_.get())->resume();
+ }
+}
+
+/** Maestro wants to yield back to a given actor, so awake it on the current thread
+ *
+ * In parallel, it is only applied to the N first elements of the parmap array,
+ * where N is the amount of worker threads in the parmap.
+ * See SwappedContextFactory::run_all for details.
+ */