+SwappedContext::~SwappedContext()
+{
+ if (stack_ == nullptr) // maestro has no extra stack
+ return;
+
+#if HAVE_SANITIZER_THREAD_FIBER_SUPPORT
+ __tsan_destroy_fiber(tsan_fiber_);
+#endif
+#if HAVE_VALGRIND_H
+ if (valgrind_stack_id_ != 0)
+ VALGRIND_STACK_DEREGISTER(valgrind_stack_id_);
+#endif
+
+#ifndef _WIN32
+ if (smx_context_guard_size > 0 && not MC_is_active()) {
+ stack_ = stack_ - smx_context_guard_size;
+ if (mprotect(stack_, smx_context_guard_size, PROT_READ | PROT_WRITE) == -1) {
+ XBT_WARN("Failed to remove page protection: %s", strerror(errno));
+ /* try to pursue anyway */
+ }
+#if SIMGRID_HAVE_MC
+ /* Retrieve the saved pointer. See SIMIX_context_stack_new above. */
+ stack_ = reinterpret_cast<unsigned char**>(stack_)[-1];
+#endif
+ }
+#endif /* not windows */
+
+ xbt_free(stack_);
+}
+
+unsigned char* SwappedContext::get_stack_bottom() const
+{
+ // Depending on the stack direction, its bottom (that make_fcontext needs) may be the lower or higher end
+#if PTH_STACKGROWTH == 1
+ return stack_;
+#else
+ return stack_ + get_actor()->get_stacksize();
+#endif
+}
+
+void SwappedContext::stop()
+{
+ Context::stop();
+ /* We must cut the actor execution using an exception to properly free the C++ RAII variables */
+ throw ForcefulKillException();
+}
+
+void SwappedContext::swap_into(SwappedContext* to)
+{
+#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
+ void* fake_stack = nullptr;
+ to->asan_ctx_ = this;
+ __sanitizer_start_switch_fiber(this->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
+#endif
+#if HAVE_SANITIZER_THREAD_FIBER_SUPPORT
+ __tsan_switch_to_fiber(to->tsan_fiber_, 0);
+#endif
+
+ swap_into_for_real(to);
+
+#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
+ __sanitizer_finish_switch_fiber(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_);
+#endif
+}
+
+/** Maestro wants to run all ready actors */
+void SwappedContextFactory::run_all()
+{
+ const auto* engine = EngineImpl::get_instance();
+ /* 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 (SIMIX_context_is_parallel()) {
+ // We lazily create the parmap so that all options are actually processed when doing so.
+ if (parmap_ == nullptr)
+ parmap_ = std::make_unique<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(
+ [](const actor::ActorImpl* actor) {
+ auto* context = static_cast<SwappedContext*>(actor->context_.get());
+ context->resume();
+ },
+ engine->get_actors_to_run());
+ } else { // sequential execution
+ if (not engine->has_actors_to_run())
+ return;
+
+ /* maestro is already saved in the first slot of workers_context_ */
+ const actor::ActorImpl* first_actor = engine->get_first_actor_to_run();
+ 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.
+ */