- /* determine the next context */
- SwappedContext* next_context;
- unsigned long int i = process_index_;
- process_index_++;
-
- 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());
+ if (factory_->threads_working_ == 0) // Don't save the soul of minions, only the one of maestro
+ factory_->workers_context_[0] = ctx;
+}
+
+void SwappedContext::stop()
+{
+ Context::stop();
+ /* We must cut the actor execution using an exception to properly free the C++ RAII variables */
+ throw StopRequest();
+}
+
+/** 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_ = 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_);
+ context->resume();
+ },
+ simix_global->process_to_run);
+ } else { // sequential execution
+ if (simix_global->process_to_run.empty())
+ return;
+
+ /* maestro is already saved in the first slot of workers_context_ */
+ smx_actor_t first_actor = simix_global->process_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_)->resume();