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
13 #include <simgrid/simix.hpp>
14 #include <xbt/parmap.hpp>
15 #include <xbt/xbt_os_thread.h>
17 #include "Context.hpp"
18 #include "src/internal_config.h"
19 #include "src/simix/smx_private.hpp"
25 /** @brief Fast context switching inspired from SystemV ucontexts.
27 * The main difference to the System V context is that Raw Contexts are much faster because they don't
28 * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
30 class RawContext : public Context {
32 RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
33 ~RawContext() override;
35 virtual void resume() = 0;
37 static void swap(RawContext* from, RawContext* to);
38 static RawContext* getMaestro() { return maestro_context_; }
39 static void setMaestro(RawContext* maestro) { maestro_context_ = maestro; }
42 static RawContext* maestro_context_;
43 void* stack_ = nullptr;
44 /** pointer to top the stack stack */
45 void* stack_top_ = nullptr;
47 static void wrapper(void* arg);
50 class SerialRawContext : public RawContext {
52 SerialRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
53 : RawContext(std::move(code), cleanup_func, process)
56 void suspend() override;
57 void resume() override;
59 static void run_all();
62 static unsigned long process_index_;
65 #if HAVE_THREAD_CONTEXTS
66 class ParallelRawContext : public RawContext {
68 ParallelRawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
69 : RawContext(std::move(code), cleanup_func, process)
72 void suspend() override;
73 void resume() override;
75 static void initialize();
76 static void finalize();
77 static void run_all();
80 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
81 static std::vector<ParallelRawContext*> workers_context_;
82 static uintptr_t threads_working_;
83 static xbt_os_thread_key_t worker_id_key_;
87 class RawContextFactory : public ContextFactory {
90 ~RawContextFactory() override;
91 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
92 void run_all() override;