Logo AND Algorithmique Numérique Distribuée

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