Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s/process/actor/ A lot remains TBD about it
[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
15 #include <thread>
16
17 namespace simgrid {
18 namespace kernel {
19 namespace context {
20
21 class XBT_PUBLIC ThreadContext : public AttachContext {
22 public:
23   ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro);
24   ~ThreadContext() override;
25   void stop() override;
26   void suspend() override;
27   void attach_start() override;
28   void attach_stop() override;
29
30   bool is_maestro() const { return is_maestro_; }
31   void release(); // unblock context's start()
32   void wait();    // wait for context's yield()
33
34 private:
35   /** A portable thread */
36   std::thread* thread_ = nullptr;
37   /** Semaphore used to schedule/yield the actor (not needed when the maestro is in main, but harmless then) */
38   xbt::OsSemaphore begin_{0};
39   /** Semaphore used to schedule/unschedule (not needed when the maestro is in main, but harmless then) */
40   xbt::OsSemaphore end_{0};
41   bool is_maestro_;
42
43   void start();                // match a call to release()
44   void yield();                // match a call to yield()
45   virtual void start_hook() { /* empty placeholder, called after start(). Used in parallel mode and Java */}
46   virtual void yield_hook() { /* empty placeholder, called before yield(). Used in parallel mode */}
47
48   static void* wrapper(void *param);
49 };
50
51 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
52 public:
53   SerialThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro)
54       : ThreadContext(std::move(code), cleanup_func, actor, maestro)
55   {
56   }
57
58   static void run_all();
59 };
60
61 class ParallelThreadContext : public ThreadContext {
62 public:
63   ParallelThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro)
64       : ThreadContext(std::move(code), cleanup_func, actor, 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 actor) override
85   {
86     bool maestro = not code;
87     return create_context(std::move(code), cleanup_func, actor, maestro);
88   }
89   void run_all() override;
90
91   // Optional methods:
92   ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t actor) override
93   {
94     return create_context(std::function<void()>(), cleanup_func, actor, false);
95   }
96   ThreadContext* create_maestro(std::function<void()> code, smx_actor_t actor) override
97   {
98     return create_context(std::move(code), nullptr, actor, true);
99   }
100
101 private:
102   bool parallel_;
103
104   ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor,
105                                 bool maestro);
106 };
107 }}} // namespace
108
109 #endif