Logo AND Algorithmique Numérique Distribuée

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