-namespace simgrid {
-namespace kernel {
-namespace context {
- class UContext;
- class SerialUContext;
- class ParallelUContext;
- class UContextFactory;
-}}}
-
-#if HAVE_THREAD_CONTEXTS
-static simgrid::xbt::Parmap<smx_actor_t>* sysv_parmap;
-static simgrid::kernel::context::UContext** sysv_workers_context; /* space to save the worker's context
- * in each thread */
-static uintptr_t sysv_threads_working; /* number of threads that have started their work */
-static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
-#endif
-static unsigned long sysv_process_index = 0; /* index of the next process to run in the
- * list of runnable processes */
-static simgrid::kernel::context::UContext* sysv_maestro_context;
-static bool sysv_parallel;
-
-// The name of this function is currently hardcoded in the code (as string).
-// Do not change it without fixing those references as well.
-static void smx_ctx_sysv_wrapper(int first, ...);
-
-namespace simgrid {
-namespace kernel {
-namespace context {
-
-class UContext : public Context {
-private:
- ucontext_t uc_; /* the ucontext that executes the code */
- char *stack_ = nullptr; /* the thread stack */
-public:
- friend UContextFactory;
- UContext(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
- ~UContext() override;
- void stop() override;
- static void swap(UContext* from, UContext* to) { swapcontext(&from->uc_, &to->uc_); }
-};
-
-class SerialUContext : public UContext {
-public:
- SerialUContext(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : UContext(std::move(code), cleanup_func, process)
- {}
- void suspend() override;
- void resume();
-};
-
-class ParallelUContext : public UContext {
-public:
- ParallelUContext(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : UContext(std::move(code), cleanup_func, process)
- {}
- void suspend() override;
- void resume();
-};
-
-class UContextFactory : public ContextFactory {
-public:
- friend UContext;
- friend SerialUContext;
- friend ParallelUContext;
-
- UContextFactory();
- ~UContextFactory() override;
- Context* create_context(std::function<void()> code,
- void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
- void run_all() override;
-};
-
-XBT_PRIVATE ContextFactory* sysv_factory()
-{
- XBT_VERB("Activating SYSV context factory");
- return new UContextFactory();
-}
-
-UContextFactory::UContextFactory() : ContextFactory("UContextFactory")
-{
- if (SIMIX_context_is_parallel()) {
- sysv_parallel = true;
-#if HAVE_THREAD_CONTEXTS /* To use parallel ucontexts a thread pool is needed */
- int nthreads = SIMIX_context_get_nthreads();
- sysv_parmap = nullptr;
- sysv_workers_context = new UContext*[nthreads];
- sysv_maestro_context = nullptr;
- xbt_os_thread_key_create(&sysv_worker_id_key);
-#else
- THROWF(arg_error, 0, "No thread support for parallel context execution");
-#endif
- } else {
- sysv_parallel = false;
- }
-}
-
-UContextFactory::~UContextFactory()
-{
-#if HAVE_THREAD_CONTEXTS
- delete sysv_parmap;
- delete[] sysv_workers_context;
-#endif
-}
-
-/* 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.
- */
-void UContextFactory::run_all()
-{
- if (sysv_parallel) {
-#if HAVE_THREAD_CONTEXTS
- sysv_threads_working = 0;
- // Parmap_apply ensures that every working thread get an index in the
- // process_to_run array (through an atomic fetch_and_add),
- // and runs the smx_ctx_sysv_resume_parallel function on that index
-
- // We lazily create the parmap because the parmap creates context
- // with simix_global->context_factory (which might not be initialized
- // when bootstrapping):
- if (sysv_parmap == nullptr)
- sysv_parmap =
- new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
-
- sysv_parmap->apply(
- [](smx_actor_t process) {
- ParallelUContext* context = static_cast<ParallelUContext*>(process->context);
- context->resume();
- },
- simix_global->process_to_run);
-#else
- xbt_die("You asked for a parallel execution, but you don't have any threads.");
-#endif
- } else {
- // Serial:
- if (simix_global->process_to_run.empty())
- return;
-
- smx_actor_t first_process = simix_global->process_to_run.front();
- sysv_process_index = 1;
- SerialUContext* context = static_cast<SerialUContext*>(first_process->context);
- context->resume();
- }
-}
-
-Context* UContextFactory::create_context(std::function<void()> code,
- void_pfn_smxprocess_t cleanup, smx_actor_t process)
-{
- if (sysv_parallel)
- return new_context<ParallelUContext>(std::move(code), cleanup, process);
- else
- return new_context<SerialUContext>(std::move(code), cleanup, process);
-}
-
-UContext::UContext(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
- : Context(std::move(code), cleanup_func, process)
-{
- /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
- if (has_code()) {
- this->stack_ = (char*) SIMIX_context_stack_new();
- getcontext(&this->uc_);
- this->uc_.uc_link = nullptr;
- this->uc_.uc_stack.ss_sp = sg_makecontext_stack_addr(this->stack_);
- this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
- simgrid_makecontext(&this->uc_, smx_ctx_sysv_wrapper, this);
- } else {
- if (process != nullptr && sysv_maestro_context == nullptr)
- sysv_maestro_context = this;
- }
-
-#if SIMGRID_HAVE_MC
- if (MC_is_active() && has_code()) {
- MC_register_stack_area(this->stack_, process,
- &(this->uc_), smx_context_usable_stack_size);
- }
-#endif
-}