Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
82ff4462ec323f480f7e34aeb25ebf1786c5714b
[simgrid.git] / src / kernel / context / ContextRaw.hpp
1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_SIMIX_BOOST_CONTEXT_HPP
7 #define SIMGRID_SIMIX_BOOST_CONTEXT_HPP
8
9 #include <functional>
10
11 #include "Context.hpp"
12 #include "src/internal_config.h"
13 #include "src/simix/smx_private.hpp"
14
15 namespace simgrid {
16 namespace kernel {
17 namespace context {
18
19 class RawContext;
20 class RawContextFactory;
21
22 /** @brief Fast context switching inspired from SystemV ucontexts.
23   *
24   * The main difference to the System V context is that Raw Contexts are much faster because they don't
25   * preserve the signal mask when switching. This saves a system call (at least on Linux) on each context switch.
26   */
27 class RawContext : public Context {
28 private:
29   void* stack_ = nullptr;
30   /** pointer to top the stack stack */
31   void* stack_top_ = nullptr;
32
33 public:
34   friend class RawContextFactory;
35   RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
36   ~RawContext() override;
37
38   static void wrapper(void* arg);
39   void stop() override;
40   void suspend() override;
41   void resume();
42
43 private:
44   void suspend_serial();
45   void suspend_parallel();
46   void resume_serial();
47   void resume_parallel();
48 };
49
50 class RawContextFactory : public ContextFactory {
51 public:
52   RawContextFactory();
53   ~RawContextFactory() override;
54   RawContext* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
55   void run_all() override;
56
57 private:
58   void run_all_serial();
59   void run_all_parallel();
60 };
61 }}} // namespace
62
63 #endif