Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
94608daac1cb86934fc40b61c3298c6ebb88de13
[simgrid.git] / src / kernel / context / ContextUnix.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_UNIX_CONTEXT_HPP
7 #define SIMGRID_SIMIX_UNIX_CONTEXT_HPP
8
9 #include <ucontext.h> /* context relative declarations */
10
11 #include <cstdint>
12 #include <functional>
13 #include <vector>
14
15 #include <simgrid/simix.hpp>
16 #include <xbt/parmap.hpp>
17 #include <xbt/xbt_os_thread.h>
18
19 #include "Context.hpp"
20 #include "src/internal_config.h"
21 #include "src/simix/smx_private.hpp"
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class UContext;
28 class SerialUContext;
29 class ParallelUContext;
30 class UContextFactory;
31
32 class UContext : public Context {
33 private:
34   ucontext_t uc_;         /* the ucontext that executes the code */
35   char* stack_ = nullptr; /* the thread stack */
36 public:
37   friend UContextFactory;
38   UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
39   ~UContext() override;
40   void stop() override;
41   static void swap(UContext* from, UContext* to) { swapcontext(&from->uc_, &to->uc_); }
42 };
43
44 class SerialUContext : public UContext {
45 public:
46   SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
47       : UContext(std::move(code), cleanup_func, process)
48   {
49   }
50   void suspend() override;
51   void resume();
52 };
53
54 class ParallelUContext : public UContext {
55 public:
56   ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
57       : UContext(std::move(code), cleanup_func, process)
58   {
59   }
60   void suspend() override;
61   void resume();
62 };
63
64 class UContextFactory : public ContextFactory {
65 public:
66   friend UContext;
67   friend SerialUContext;
68   friend ParallelUContext;
69
70   UContextFactory();
71   ~UContextFactory() override;
72   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
73   void run_all() override;
74 };
75 }}} // namespace
76
77 #endif