Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change a subclass into a superclass around contexts
[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 "src/internal_config.h"
21 #include "src/kernel/context/ContextSwapped.hpp"
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class UContext : public SwappedContext {
28 public:
29   UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
30   ~UContext() override;
31   void stop() override;
32
33   void swap_into(SwappedContext* to) override;
34
35 private:
36   void* stack_ = nullptr; /* the thread stack */
37   ucontext_t uc_;         /* the ucontext that executes the code */
38
39 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
40   const void* asan_stack_ = nullptr;
41   size_t asan_stack_size_ = 0;
42   UContext* asan_ctx_     = nullptr;
43   bool asan_stop_         = false;
44 #endif
45
46   static void smx_ctx_sysv_wrapper(int, int);
47   static void make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
48 };
49
50 class ParallelUContext : public UContext {
51 public:
52   ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
53       : UContext(std::move(code), cleanup_func, process)
54   {
55   }
56   void suspend() override;
57   void resume() override;
58
59   static void initialize();
60   static void finalize();
61   static void run_all();
62
63 private:
64   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
65   static std::vector<ParallelUContext*> workers_context_;
66   static std::atomic<uintptr_t> threads_working_;
67   static thread_local uintptr_t worker_id_;
68 };
69
70 class UContextFactory : public ContextFactory {
71 public:
72   UContextFactory();
73   ~UContextFactory() override;
74   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
75   void run_all() override;
76
77 private:
78   bool parallel_;
79 };
80 }}} // namespace
81
82 #endif