1 /* Copyright (c) 2009-2017. 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 <simgrid/simix.hpp>
15 #include <xbt/parmap.hpp>
16 #include <xbt/xbt_os_thread.h>
18 #include "Context.hpp"
19 #include "src/internal_config.h"
20 #include "src/simix/smx_private.hpp"
26 /** @brief Fast context switching inspired from SystemV ucontexts.
28 * The main difference to the System V context is that Raw Contexts are much faster because they don't
29 * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
31 class RawContext : public Context {
33 RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
34 ~RawContext() override;
36 virtual void resume() = 0;
38 static void swap(RawContext* from, RawContext* to);
39 static RawContext* getMaestro() { return maestro_context_; }
40 static void setMaestro(RawContext* maestro) { maestro_context_ = maestro; }
43 static RawContext* maestro_context_;
44 void* stack_ = nullptr;
45 /** pointer to top the stack stack */
46 void* stack_top_ = nullptr;
48 static void wrapper(void* arg);
51 class SerialRawContext : public RawContext {
53 SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
54 : RawContext(std::move(code), cleanup_func, process)
57 void suspend() override;
58 void resume() override;
60 static void run_all();
63 static unsigned long process_index_;
66 #if HAVE_THREAD_CONTEXTS
67 class ParallelRawContext : public RawContext {
69 ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
70 : RawContext(std::move(code), cleanup_func, process)
73 void suspend() override;
74 void resume() override;
76 static void initialize();
77 static void finalize();
78 static void run_all();
81 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
82 static std::vector<ParallelRawContext*> workers_context_;
83 static std::atomic<uintptr_t> threads_working_;
84 static xbt_os_thread_key_t worker_id_key_;
88 class RawContextFactory : public ContextFactory {
91 ~RawContextFactory() override;
92 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
93 void run_all() override;