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, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro);
24 ~ThreadContext() override;
26 void suspend() override;
27 void attach_start() override;
28 void attach_stop() override;
30 bool is_maestro() const { return is_maestro_; }
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};
43 void start(); // match a call to release()
44 void yield(); // match a call to yield()
45 virtual void start_hook() { /* empty placeholder, called after start(). Used in parallel mode and Java */}
46 virtual void yield_hook() { /* empty placeholder, called before yield(). Used in parallel mode */}
47 virtual void stop_hook() { /* empty placeholder, called at stop(). Used in Java */}
49 static void* wrapper(void *param);
52 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
54 SerialThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro)
55 : ThreadContext(std::move(code), cleanup_func, actor, maestro)
59 static void run_all();
62 class ParallelThreadContext : public ThreadContext {
64 ParallelThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor, bool maestro)
65 : ThreadContext(std::move(code), cleanup_func, actor, maestro)
69 static void initialize();
70 static void finalize();
71 static void run_all();
74 static xbt::OsSemaphore* thread_sem_;
76 void start_hook() override;
77 void yield_hook() override;
80 class ThreadContextFactory : public ContextFactory {
82 ThreadContextFactory();
83 ~ThreadContextFactory() override;
84 ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
85 smx_actor_t actor) override
87 bool maestro = not code;
88 return create_context(std::move(code), cleanup_func, actor, maestro);
90 void run_all() override;
93 ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t actor) override
95 return create_context(std::function<void()>(), cleanup_func, actor, false);
97 ThreadContext* create_maestro(std::function<void()> code, smx_actor_t actor) override
99 return create_context(std::move(code), nullptr, actor, true);
105 ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor,