Logo AND Algorithmique Numérique Distribuée

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