Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify parameter passing between EngineImpl and ContextFactory
[simgrid.git] / src / kernel / context / ContextThread.hpp
1 /* Copyright (c) 2009-2022. 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_KERNEL_CONTEXT_THREAD_CONTEXT_HPP
9 #define SIMGRID_KERNEL_CONTEXT_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, actor::ActorImpl* actor, bool maestro);
24   ThreadContext(const ThreadContext&) = delete;
25   ThreadContext& operator=(const ThreadContext&) = delete;
26   ~ThreadContext() override;
27   void suspend() override;
28   void attach_start() override;
29   void attach_stop() override;
30
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
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(ThreadContext* context);
48 };
49
50 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
51 public:
52   SerialThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
53       : ThreadContext(std::move(code), actor, maestro)
54   {
55   }
56
57   static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
58 };
59
60 class ParallelThreadContext : public ThreadContext {
61 public:
62   ParallelThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
63       : ThreadContext(std::move(code), actor, maestro)
64   {
65   }
66
67   static void initialize();
68   static void finalize();
69   static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
70
71 private:
72   static xbt::OsSemaphore* thread_sem_;
73
74   void start_hook() override;
75   void yield_hook() override;
76 };
77
78 class ThreadContextFactory : public ContextFactory {
79 public:
80   ThreadContextFactory();
81   ThreadContextFactory(const ThreadContextFactory&) = delete;
82   ThreadContextFactory& operator=(const ThreadContextFactory&) = delete;
83   ~ThreadContextFactory() override;
84   ThreadContext* create_context(std::function<void()>&& code, actor::ActorImpl* actor) override
85   {
86     bool maestro = not code;
87     return create_context(std::move(code), actor, maestro);
88   }
89   void run_all(std::vector<actor::ActorImpl*> const& actors) override;
90
91   // Optional methods:
92   ThreadContext* attach(actor::ActorImpl* actor) override
93   {
94     return create_context(std::function<void()>(), actor, false);
95   }
96   ThreadContext* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor) override
97   {
98     return create_context(std::move(code), actor, true);
99   }
100
101 private:
102   ThreadContext* create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
103 };
104 } // namespace context
105 } // namespace kernel
106 } // namespace simgrid
107
108 #endif