Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modernize simcall process_cleanup.
[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   static void wrapper(void* arg);
44 };
45
46 class SerialRawContext : public RawContext {
47 public:
48   SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
49       : RawContext(std::move(code), cleanup_func, process)
50   {
51   }
52   void suspend() override;
53   void resume() override;
54
55   static void run_all();
56
57 private:
58   static unsigned long process_index_;
59 };
60
61 #if HAVE_THREAD_CONTEXTS
62 class ParallelRawContext : public RawContext {
63 public:
64   ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
65       : RawContext(std::move(code), cleanup_func, process)
66   {
67   }
68   void suspend() override;
69   void resume() override;
70
71   static void initialize();
72   static void finalize();
73   static void run_all();
74
75 private:
76   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
77   static std::vector<ParallelRawContext*> workers_context_;
78   static std::atomic<uintptr_t> threads_working_;
79   static xbt_os_thread_key_t worker_id_key_;
80 };
81 #endif
82
83 class RawContextFactory : public ContextFactory {
84 public:
85   RawContextFactory();
86   ~RawContextFactory() override;
87   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
88   void run_all() override;
89
90 private:
91   bool parallel_;
92 };
93 }}} // namespace
94
95 #endif