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"
14 #include "xbt/xbt_os_thread.h"
20 class XBT_PUBLIC ThreadContext : public AttachContext {
22 ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process, bool maestro);
23 ~ThreadContext() override;
25 void suspend() override;
26 void attach_start() override;
27 void attach_stop() override;
29 bool is_maestro() const { return is_maestro_; }
30 void release(); // unblock context's start()
31 void wait(); // wait for context's yield()
34 /** A portable thread */
35 xbt_os_thread_t thread_ = nullptr;
36 /** Semaphore used to schedule/yield the process (not needed when the maestro is in main, but harmless then) */
37 xbt::OsSemaphore begin_{0};
38 /** Semaphore used to schedule/unschedule (not needed when the maestro is in main, but harmless then) */
39 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(void *param);
50 class XBT_PUBLIC SerialThreadContext : public ThreadContext {
52 SerialThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process, bool maestro)
53 : ThreadContext(std::move(code), cleanup_func, process, maestro)
57 static void run_all();
60 class ParallelThreadContext : public ThreadContext {
62 ParallelThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
64 : ThreadContext(std::move(code), cleanup_func, process, maestro)
68 static void initialize();
69 static void finalize();
70 static void run_all();
73 static xbt::OsSemaphore* thread_sem_;
75 void start_hook() override;
76 void yield_hook() override;
79 class ThreadContextFactory : public ContextFactory {
81 ThreadContextFactory();
82 ~ThreadContextFactory() override;
83 ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
84 smx_actor_t process) override
86 bool maestro = not code;
87 return create_context(std::move(code), cleanup_func, process, maestro);
89 void run_all() override;
92 ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process) override
94 return create_context(std::function<void()>(), cleanup_func, process, false);
96 ThreadContext* create_maestro(std::function<void()> code, smx_actor_t process) override
98 return create_context(std::move(code), nullptr, process, true);
104 ThreadContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,