Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into actor-yield
[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 <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) { swapcontext(&from->uc_, &to->uc_); }
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   static void wrapper(int, int);
45   static void make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
46 };
47
48 class SerialUContext : public UContext {
49 public:
50   SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
51       : UContext(std::move(code), cleanup_func, process)
52   {
53   }
54   void suspend() override;
55   void resume() override;
56
57   static void run_all();
58
59 private:
60   static unsigned long process_index_;
61 };
62
63 #if HAVE_THREAD_CONTEXTS
64 class ParallelUContext : public UContext {
65 public:
66   ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
67       : UContext(std::move(code), cleanup_func, process)
68   {
69   }
70   void suspend() override;
71   void resume() override;
72
73   static void initialize();
74   static void finalize();
75   static void run_all();
76
77 private:
78   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
79   static std::vector<ParallelUContext*> workers_context_;
80   static std::atomic<uintptr_t> threads_working_;
81   static xbt_os_thread_key_t worker_id_key_;
82 };
83 #endif
84
85 class UContextFactory : public ContextFactory {
86 public:
87   UContextFactory();
88   ~UContextFactory() override;
89   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
90   void run_all() override;
91
92 private:
93   bool parallel_;
94 };
95 }}} // namespace
96
97 #endif