Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2021. 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 #include "mc/mc.h"
7
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/context/Context.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 #include <vector>
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
17
18 namespace simgrid {
19 namespace kernel {
20 namespace context {
21
22 ContextFactoryInitializer factory_initializer = nullptr;
23
24 ContextFactory::~ContextFactory() = default;
25
26 thread_local Context* Context::current_context_ = nullptr;
27
28 #ifndef WIN32
29 /* Install or disable alternate signal stack, for SIGSEGV handler. */
30 int Context::install_sigsegv_stack(stack_t* old_stack, bool enable)
31 {
32   static std::vector<unsigned char> sigsegv_stack(SIGSTKSZ);
33   stack_t stack;
34   stack.ss_sp    = sigsegv_stack.data();
35   stack.ss_size  = sigsegv_stack.size();
36   stack.ss_flags = enable ? 0 : SS_DISABLE;
37   return sigaltstack(&stack, old_stack);
38 }
39 #endif
40
41 Context* Context::self()
42 {
43   return current_context_;
44 }
45 void Context::set_current(Context* self)
46 {
47   current_context_ = self;
48 }
49
50 void Context::declare_context(std::size_t size)
51 {
52 #if SIMGRID_HAVE_MC
53   /* Store the address of the stack in heap to compare it apart of heap comparison */
54   if(MC_is_active())
55     MC_ignore_heap(this, size);
56 #endif
57 }
58
59 Context* ContextFactory::attach(actor::ActorImpl*)
60 {
61   xbt_die("Cannot attach with this ContextFactory.\n"
62     "Try using --cfg=contexts/factory:thread instead.\n");
63 }
64
65 Context* ContextFactory::create_maestro(std::function<void()>&&, actor::ActorImpl*)
66 {
67   xbt_die("Cannot create_maestro with this ContextFactory.\n"
68     "Try using --cfg=contexts/factory:thread instead.\n");
69 }
70
71 Context::Context(std::function<void()>&& code, actor::ActorImpl* actor) : code_(std::move(code)), actor_(actor)
72 {
73   /* If no function was provided, this is the context for maestro
74    * and we should set it as the current context */
75   if (not has_code())
76     set_current(this);
77 }
78
79 Context::~Context()
80 {
81   if (self() == this)
82     set_current(nullptr);
83 }
84
85 void Context::stop()
86 {
87   this->actor_->cleanup();
88 }
89
90 AttachContext::~AttachContext() = default;
91
92 } // namespace context
93 } // namespace kernel
94 } // namespace simgrid