-namespace simgrid {
-namespace kernel {
-namespace context {
-
-/* Sequential execution */
-unsigned long SwappedContext::process_index_;
-
-/* Parallel execution */
-simgrid::xbt::Parmap<smx_actor_t>* SwappedContext::parmap_;
-std::atomic<uintptr_t> SwappedContext::threads_working_; /* number of threads that have started their work */
-thread_local uintptr_t SwappedContext::worker_id_; /* thread-specific storage for the thread id */
-std::vector<SwappedContext*> SwappedContext::workers_context_; /* space to save the worker's context in each thread */
-
-SwappedContextFactory::SwappedContextFactory(std::string name)
- : ContextFactory(name), parallel_(SIMIX_context_is_parallel())
-{
- SwappedContext::set_maestro(nullptr);
- SwappedContext::initialize(parallel_);
-}
-SwappedContextFactory::~SwappedContextFactory()
-{
- SwappedContext::finalize();
-}
-/** 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_) {
- SwappedContext::threads_working_ = 0;
-
- // We lazily create the parmap so that all options are actually processed when doing so.
- if (SwappedContext::parmap_ == nullptr)
- SwappedContext::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.
- SwappedContext::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;
- smx_actor_t first_process = simix_global->process_to_run.front();
- SwappedContext::process_index_ = 1;
- /* execute the first process */
- static_cast<SwappedContext*>(first_process->context_)->resume();
- }
-}
-
-void SwappedContext::initialize(bool parallel)