1 /* Copyright (c) 2009-2018. 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 #ifndef SIMGRID_SIMIX_RAW_CONTEXT_HPP
7 #define SIMGRID_SIMIX_RAW_CONTEXT_HPP
14 #include <xbt/parmap.hpp>
15 #include <xbt/xbt_os_thread.h>
21 /** @brief Fast context switching inspired from SystemV ucontexts.
23 * The main difference to the System V context is that Raw Contexts are much faster because they don't
24 * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
26 class RawContext : public Context {
28 RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
29 ~RawContext() override;
31 virtual void resume() = 0;
33 static void swap(RawContext* from, RawContext* to);
34 static RawContext* getMaestro() { return maestro_context_; }
35 static void setMaestro(RawContext* maestro) { maestro_context_ = maestro; }
38 static RawContext* maestro_context_;
39 void* stack_ = nullptr;
40 /** pointer to top the stack stack */
41 void* stack_top_ = nullptr;
43 static void wrapper(void* arg);
46 class SerialRawContext : public RawContext {
48 SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
49 : RawContext(std::move(code), cleanup_func, process)
52 void suspend() override;
53 void resume() override;
55 static void run_all();
58 static unsigned long process_index_;
61 #if HAVE_THREAD_CONTEXTS
62 class ParallelRawContext : public RawContext {
64 ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
65 : RawContext(std::move(code), cleanup_func, process)
68 void suspend() override;
69 void resume() override;
71 static void initialize();
72 static void finalize();
73 static void run_all();
76 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
77 static std::vector<ParallelRawContext*> workers_context_;
78 static std::atomic<uintptr_t> threads_working_;
79 static xbt_os_thread_key_t worker_id_key_;
83 class RawContextFactory : public ContextFactory {
86 ~RawContextFactory() override;
87 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
88 void run_all() override;