- if (i < simix_global->process_to_run.size()) {
- /* execute the next process */
- XBT_DEBUG("Run next process");
- next_context = static_cast<SwappedContext*>(simix_global->process_to_run[i]->context_);
- } else {
- /* all processes were run, return to maestro */
- XBT_DEBUG("No more process to run");
- next_context = static_cast<SwappedContext*>(get_maestro());
+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 (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>>(get_nthreads(), 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();