Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce a portable xbt::OsSemaphore using C++11 threads
[simgrid.git] / src / kernel / context / ContextThread.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 /* \file ThreadContext.hpp Context switching with native threads */
7
8 #ifndef SIMGRID_SIMIX_THREAD_CONTEXT_HPP
9 #define SIMGRID_SIMIX_THREAD_CONTEXT_HPP
10
11 #include "simgrid/simix.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/xbt/OsSemaphore.hpp"
14 #include "xbt/xbt_os_thread.h"
15
16 namespace simgrid {
17 namespace kernel {
18 namespace context {
19
20 class XBT_PUBLIC ThreadContext : public AttachContext {
21 public:
22   ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process, bool maestro);
23   ~ThreadContext() override;
24   void stop() override;
25   void suspend() override;
26   void attach_start() override;
27   void attach_stop() override;
28
29   bool is_maestro() const { return is_maestro_; }
30   void release(); // unblock context's start()
31   void wait();    // wait for context's yield()
32
33 private:
34   /** A portable thread */
35   xbt_os_thread_t thread_ = nullptr;
36   /** Semaphore used to schedule/yield the process (not needed when the maestro is in main, but harmless then) */
37   xbt::OsSemaphore begin_{0};
38   /** Semaphore used to schedule/unschedule (not needed when the maestro is in main, but harmless then) */
39   xbt::OsSemaphore end_{0};
40   bool is_maestro_;
41
42   void start();                // match a call to release()
43   void yield();                // match a call to yield()
44   virtual void start_hook() { /* empty placeholder, called after start(). Used in parallel mode and Java */}
45   virtual void yield_hook() { /* empty placeholder, called before yield(). Used in parallel mode */}
46
47   static void* wrapper(void *param);
48 };
49
50 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
51 public:
52   SerialThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process, bool maestro)
53       : ThreadContext(std::move(code), cleanup_func, process, maestro)
54   {
55   }
56
57   static void run_all();
58 };
59
60 class ParallelThreadContext : public ThreadContext {
61 public:
62   ParallelThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
63                         bool maestro)
64       : ThreadContext(std::move(code), cleanup_func, process, maestro)
65   {
66   }
67
68   static void initialize();
69   static void finalize();
70   static void run_all();
71
72 private:
73   static xbt::OsSemaphore* thread_sem_;
74
75   void start_hook() override;
76   void yield_hook() override;
77 };
78
79 class ThreadContextFactory : public ContextFactory {
80 public:
81   ThreadContextFactory();
82   ~ThreadContextFactory() override;
83   ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
84                                 smx_actor_t process) override
85   {
86     bool maestro = not code;
87     return create_context(std::move(code), cleanup_func, process, maestro);
88   }
89   void run_all() override;
90   ThreadContext* self() override { return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data()); }
91
92   // Optional methods:
93   ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process) override
94   {
95     return create_context(std::function<void()>(), cleanup_func, process, false);
96   }
97   ThreadContext* create_maestro(std::function<void()> code, smx_actor_t process) override
98   {
99     return create_context(std::move(code), nullptr, process, true);
100   }
101
102 private:
103   bool parallel_;
104
105   ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
106                                 bool maestro);
107 };
108 }}} // namespace
109
110 #endif