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_RAW_CONTEXT_HPP
7 #define SIMGRID_SIMIX_RAW_CONTEXT_HPP
14 #include <xbt/parmap.hpp>
15 #include <xbt/xbt_os_thread.h>
17 #include "src/kernel/context/ContextSwapped.hpp"
23 /** @brief Fast context switching inspired from SystemV ucontexts.
25 * The main difference to the System V context is that Raw Contexts are much faster because they don't
26 * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
28 class RawContext : public SwappedContext {
30 RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
31 ~RawContext() override;
34 void swap_into(SwappedContext* to) override;
37 /** pointer to top the stack stack */
38 void* stack_top_ = nullptr;
40 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
41 const void* asan_stack_ = nullptr;
42 size_t asan_stack_size_ = 0;
43 RawContext* asan_ctx_ = nullptr;
44 bool asan_stop_ = false;
47 static void wrapper(void* arg);
50 class ParallelRawContext : public RawContext {
52 ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
53 : RawContext(std::move(code), cleanup_func, process)
56 void suspend() override;
57 void resume() override;
59 static void initialize();
60 static void finalize();
61 static void run_all();
64 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
65 static std::vector<ParallelRawContext*> workers_context_;
66 static std::atomic<uintptr_t> threads_working_;
67 static uintptr_t thread_local worker_id_;
70 class RawContextFactory : public ContextFactory {
73 ~RawContextFactory() override;
74 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
75 void run_all() override;