1 /* Copyright (c) 2009-2022. The SimGrid Team. All rights reserved. */
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. */
6 /* \file ThreadContext.hpp Context switching with native threads */
8 #ifndef SIMGRID_KERNEL_CONTEXT_THREAD_CONTEXT_HPP
9 #define SIMGRID_KERNEL_CONTEXT_THREAD_CONTEXT_HPP
11 #include "simgrid/simix.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/xbt/OsSemaphore.hpp"
17 namespace simgrid::kernel::context {
19 class XBT_PUBLIC ThreadContext : public AttachContext {
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;
29 void release(); // unblock context's start()
30 void wait(); // wait for context's yield()
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};
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 */}
45 static void wrapper(ThreadContext* context);
48 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
50 SerialThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
51 : ThreadContext(std::move(code), actor, maestro)
55 static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
58 class ParallelThreadContext : public ThreadContext {
60 ParallelThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
61 : ThreadContext(std::move(code), actor, maestro)
65 static void initialize();
66 static void finalize();
67 static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
70 static xbt::OsSemaphore* thread_sem_;
72 void start_hook() override;
73 void yield_hook() override;
76 class ThreadContextFactory : public ContextFactory {
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
84 bool maestro = not code;
85 return create_context(std::move(code), actor, maestro);
87 void run_all(std::vector<actor::ActorImpl*> const& actors) override;
90 ThreadContext* attach(actor::ActorImpl* actor) override
92 return create_context(std::function<void()>(), actor, false);
94 ThreadContext* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor) override
96 return create_context(std::move(code), actor, true);
100 ThreadContext* create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
102 } // namespace simgrid::kernel::context