1 /* Copyright (c) 2009-2018. 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 */
16 #include <simgrid/simix.hpp>
17 #include <xbt/parmap.hpp>
18 #include <xbt/xbt_os_thread.h>
20 #include "Context.hpp"
21 #include "src/internal_config.h"
22 #include "src/simix/smx_private.hpp"
28 class UContext : public Context {
30 UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
33 virtual void resume() = 0;
35 static void swap(UContext* from, UContext* to);
36 static UContext* getMaestro() { return maestro_context_; }
37 static void setMaestro(UContext* maestro) { maestro_context_ = maestro; }
40 static UContext* maestro_context_;
41 void* stack_ = nullptr; /* the thread stack */
42 ucontext_t uc_; /* the ucontext that executes the code */
44 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
45 const void* asan_stack_ = nullptr;
46 size_t asan_stack_size_ = 0;
47 UContext* asan_ctx_ = nullptr;
48 bool asan_stop_ = false;
51 static void smx_ctx_sysv_wrapper(int, int);
52 static void make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
55 class SerialUContext : public UContext {
57 SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
58 : UContext(std::move(code), cleanup_func, process)
61 void suspend() override;
62 void resume() override;
64 static void run_all();
67 static unsigned long process_index_;
70 #if HAVE_THREAD_CONTEXTS
71 class ParallelUContext : public UContext {
73 ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
74 : UContext(std::move(code), cleanup_func, process)
77 void suspend() override;
78 void resume() override;
80 static void initialize();
81 static void finalize();
82 static void run_all();
85 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
86 static std::vector<ParallelUContext*> workers_context_;
87 static std::atomic<uintptr_t> threads_working_;
88 static thread_local uintptr_t worker_id_;
92 class UContextFactory : public ContextFactory {
95 ~UContextFactory() override;
96 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
97 void run_all() override;