Logo AND Algorithmique Numérique Distribuée

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