Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: use cmakedefine01.
[simgrid.git] / src / kernel / context / ContextUnix.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_UNIX_CONTEXT_HPP
7 #define SIMGRID_SIMIX_UNIX_CONTEXT_HPP
8
9 #include <ucontext.h> /* context relative declarations */
10
11 #include <atomic>
12 #include <cstdint>
13 #include <functional>
14 #include <vector>
15
16 #include <simgrid/simix.hpp>
17 #include <xbt/parmap.hpp>
18 #include <xbt/xbt_os_thread.h>
19
20 #include "Context.hpp"
21 #include "src/internal_config.h"
22 #include "src/simix/smx_private.hpp"
23
24 namespace simgrid {
25 namespace kernel {
26 namespace context {
27
28 class UContext : public Context {
29 public:
30   UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
31   ~UContext() override;
32   void stop() override;
33   virtual void resume() = 0;
34
35   static void swap(UContext* from, UContext* to);
36   static UContext* getMaestro() { return maestro_context_; }
37   static void setMaestro(UContext* maestro) { maestro_context_ = maestro; }
38
39 private:
40   static UContext* maestro_context_;
41   void* stack_ = nullptr; /* the thread stack */
42   ucontext_t uc_;         /* the ucontext that executes the code */
43
44 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
45   const void* asan_stack_ = nullptr;
46   size_t asan_stack_size_ = 0;
47   UContext* asan_ctx_     = nullptr;
48   bool asan_stop_         = false;
49 #endif
50
51   static void smx_ctx_sysv_wrapper(int, int);
52   static void make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
53 };
54
55 class SerialUContext : public UContext {
56 public:
57   SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
58       : UContext(std::move(code), cleanup_func, process)
59   {
60   }
61   void suspend() override;
62   void resume() override;
63
64   static void run_all();
65
66 private:
67   static unsigned long process_index_;
68 };
69
70 #if HAVE_THREAD_CONTEXTS
71 class ParallelUContext : public UContext {
72 public:
73   ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
74       : UContext(std::move(code), cleanup_func, process)
75   {
76   }
77   void suspend() override;
78   void resume() override;
79
80   static void initialize();
81   static void finalize();
82   static void run_all();
83
84 private:
85   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
86   static std::vector<ParallelUContext*> workers_context_;
87   static std::atomic<uintptr_t> threads_working_;
88   static thread_local uintptr_t worker_id_;
89 };
90 #endif
91
92 class UContextFactory : public ContextFactory {
93 public:
94   UContextFactory();
95   ~UContextFactory() override;
96   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
97   void run_all() override;
98
99 private:
100   bool parallel_;
101 };
102 }}} // namespace
103
104 #endif