Logo AND Algorithmique Numérique Distribuée

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