Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce a SwappedContextFactory to further reduce code duplication, working WIP
[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 #include "src/kernel/context/ContextSwapped.hpp"
18
19 namespace simgrid {
20 namespace kernel {
21 namespace context {
22
23 /** @brief Fast context switching inspired from SystemV ucontexts.
24   *
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.
27   */
28 class RawContext : public SwappedContext {
29 public:
30   RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
31              SwappedContextFactory* factory);
32   ~RawContext() override;
33   void stop() override;
34
35   void swap_into(SwappedContext* to) override;
36
37 private:
38   /** pointer to top the stack stack */
39   void* stack_top_ = nullptr;
40
41 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
42   const void* asan_stack_ = nullptr;
43   size_t asan_stack_size_ = 0;
44   RawContext* asan_ctx_   = nullptr;
45   bool asan_stop_         = false;
46 #endif
47
48   static void wrapper(void* arg);
49 };
50
51 class ParallelRawContext : public RawContext {
52 public:
53   ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
54                      SwappedContextFactory* factory)
55       : RawContext(std::move(code), cleanup_func, process, factory)
56   {
57   }
58   void suspend() override;
59   void resume() override;
60
61   static void run_all();
62 };
63
64 class RawContextFactory : public SwappedContextFactory {
65 public:
66   RawContextFactory();
67   ~RawContextFactory() override;
68   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
69 };
70 }}} // namespace
71
72 #endif