1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_SIMIX_UNIX_CONTEXT_HPP
7 #define SIMGRID_SIMIX_UNIX_CONTEXT_HPP
9 #include <ucontext.h> /* context relative declarations */
15 #include <simgrid/simix.hpp>
16 #include <xbt/parmap.hpp>
17 #include <xbt/xbt_os_thread.h>
19 #include "Context.hpp"
20 #include "src/internal_config.h"
21 #include "src/simix/smx_private.hpp"
27 class UContext : public Context {
29 UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
32 virtual void resume() = 0;
34 static void swap(UContext* from, UContext* to) { swapcontext(&from->uc_, &to->uc_); }
35 static UContext* getMaestro() { return maestro_context_; }
36 static void setMaestro(UContext* maestro) { maestro_context_ = maestro; }
39 static UContext* maestro_context_;
40 void* stack_ = nullptr; /* the thread stack */
41 ucontext_t uc_; /* the ucontext that executes the code */
43 static void wrapper(int, int);
44 static void makecontext(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
47 class SerialUContext : public UContext {
49 SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
50 : UContext(std::move(code), cleanup_func, process)
53 void suspend() override;
54 void resume() override;
56 static void run_all();
59 static unsigned long process_index_;
62 #if HAVE_THREAD_CONTEXTS
63 class ParallelUContext : public UContext {
65 ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
66 : UContext(std::move(code), cleanup_func, process)
69 void suspend() override;
70 void resume() override;
72 static void initialize();
73 static void finalize();
74 static void run_all();
77 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
78 static std::vector<ParallelUContext*> workers_context_;
79 static uintptr_t threads_working_;
80 static xbt_os_thread_key_t worker_id_key_;
84 class UContextFactory : public ContextFactory {
87 ~UContextFactory() override;
88 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
89 void run_all() override;