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"
21 class XBT_PUBLIC ThreadContext : public AttachContext {
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;
31 void release(); // unblock context's start()
32 void wait(); // wait for context's yield()
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};
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 */}
47 static void wrapper(ThreadContext* context);
50 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
52 SerialThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
53 : ThreadContext(std::move(code), actor, maestro)
57 static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
60 class ParallelThreadContext : public ThreadContext {
62 ParallelThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
63 : ThreadContext(std::move(code), actor, maestro)
67 static void initialize();
68 static void finalize();
69 static void run_all(std::vector<actor::ActorImpl*> const& actors_list);
72 static xbt::OsSemaphore* thread_sem_;
74 void start_hook() override;
75 void yield_hook() override;
78 class ThreadContextFactory : public ContextFactory {
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
86 bool maestro = not code;
87 return create_context(std::move(code), actor, maestro);
89 void run_all(std::vector<actor::ActorImpl*> const& actors) override;
92 ThreadContext* attach(actor::ActorImpl* actor) override
94 return create_context(std::function<void()>(), actor, false);
96 ThreadContext* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor) override
98 return create_context(std::move(code), actor, true);
102 ThreadContext* create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
104 } // namespace context
105 } // namespace kernel
106 } // namespace simgrid