Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d7d48eb8f8b3e600c987afc8f97038138bfc2f57
[simgrid.git] / src / kernel / context / ContextRaw.hpp
1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_SIMIX_RAW_CONTEXT_HPP
7 #define SIMGRID_SIMIX_RAW_CONTEXT_HPP
8
9 #include <atomic>
10 #include <cstdint>
11 #include <functional>
12 #include <vector>
13
14 #include <xbt/parmap.hpp>
15 #include <xbt/xbt_os_thread.h>
16
17 namespace simgrid {
18 namespace kernel {
19 namespace context {
20
21 /** @brief Fast context switching inspired from SystemV ucontexts.
22   *
23   * The main difference to the System V context is that Raw Contexts are much faster because they don't
24   * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
25   */
26 class RawContext : public Context {
27 public:
28   RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
29   ~RawContext() override;
30   void stop() override;
31   virtual void resume() = 0;
32
33   static void swap(RawContext* from, RawContext* to);
34   static RawContext* getMaestro() { return maestro_context_; }
35   static void setMaestro(RawContext* maestro) { maestro_context_ = maestro; }
36
37 private:
38   static RawContext* maestro_context_;
39   void* stack_ = nullptr;
40   /** pointer to top the stack stack */
41   void* stack_top_ = nullptr;
42
43 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
44   const void* asan_stack_ = nullptr;
45   size_t asan_stack_size_ = 0;
46   RawContext* asan_ctx_   = nullptr;
47   bool asan_stop_         = false;
48 #endif
49
50   static void wrapper(void* arg);
51 };
52
53 class SerialRawContext : public RawContext {
54 public:
55   SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
56       : RawContext(std::move(code), cleanup_func, process)
57   {
58   }
59   void suspend() override;
60   void resume() override;
61
62   static void run_all();
63
64 private:
65   static unsigned long process_index_;
66 };
67
68 #if HAVE_THREAD_CONTEXTS
69 class ParallelRawContext : public RawContext {
70 public:
71   ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
72       : RawContext(std::move(code), cleanup_func, process)
73   {
74   }
75   void suspend() override;
76   void resume() override;
77
78   static void initialize();
79   static void finalize();
80   static void run_all();
81
82 private:
83   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
84   static std::vector<ParallelRawContext*> workers_context_;
85   static std::atomic<uintptr_t> threads_working_;
86   static uintptr_t thread_local worker_id_;
87 };
88 #endif
89
90 class RawContextFactory : public ContextFactory {
91 public:
92   RawContextFactory();
93   ~RawContextFactory() override;
94   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
95   void run_all() override;
96
97 private:
98   bool parallel_;
99 };
100 }}} // namespace
101
102 #endif