1 /* Copyright (c) 2009-2019. 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_SIMIX_THREAD_CONTEXT_HPP
9 #define SIMGRID_SIMIX_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, smx_actor_t actor, bool maestro);
24 ThreadContext(const ThreadContext&) = delete;
25 ThreadContext& operator=(const ThreadContext&) = delete;
26 ~ThreadContext() override;
28 void suspend() override;
29 void attach_start() override;
30 void attach_stop() override;
32 bool is_maestro() const { return is_maestro_; }
33 void release(); // unblock context's start()
34 void wait(); // wait for context's yield()
37 /** A portable thread */
38 std::thread* thread_ = nullptr;
39 /** Semaphore used to schedule/yield the actor (not needed when the maestro is in main, but harmless then) */
40 xbt::OsSemaphore begin_{0};
41 /** Semaphore used to schedule/unschedule (not needed when the maestro is in main, but harmless then) */
42 xbt::OsSemaphore end_{0};
45 void start(); // match a call to release()
46 void yield(); // match a call to yield()
47 virtual void start_hook() { /* empty placeholder, called after start(). Used in parallel mode and Java */}
48 virtual void yield_hook() { /* empty placeholder, called before yield(). Used in parallel mode */}
49 virtual void stop_hook() { /* empty placeholder, called at stop(). Used in Java */}
51 static void wrapper(ThreadContext* context);
54 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
56 SerialThreadContext(std::function<void()> code, smx_actor_t actor, bool maestro)
57 : ThreadContext(std::move(code), actor, maestro)
61 static void run_all();
64 class ParallelThreadContext : public ThreadContext {
66 ParallelThreadContext(std::function<void()> code, smx_actor_t actor, bool maestro)
67 : ThreadContext(std::move(code), actor, maestro)
71 static void initialize();
72 static void finalize();
73 static void run_all();
76 static xbt::OsSemaphore* thread_sem_;
78 void start_hook() override;
79 void yield_hook() override;
82 class ThreadContextFactory : public ContextFactory {
84 ThreadContextFactory();
85 ThreadContextFactory(const ThreadContextFactory&) = delete;
86 ThreadContextFactory& operator=(const ThreadContextFactory&) = delete;
87 ~ThreadContextFactory() override;
88 ThreadContext* create_context(std::function<void()> code, smx_actor_t actor) override
90 bool maestro = not code;
91 return create_context(std::move(code), actor, maestro);
93 void run_all() override;
96 ThreadContext* attach(smx_actor_t actor) override { return create_context(std::function<void()>(), actor, false); }
97 ThreadContext* create_maestro(std::function<void()> code, smx_actor_t actor) override
99 return create_context(std::move(code), actor, true);
105 ThreadContext* create_context(std::function<void()> code, smx_actor_t actor, bool maestro);