Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cope with non-const SIGSTKSZ.
[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;
33   if (sigsegv_stack.size() == 0)
34     sigsegv_stack.resize(SIGSTKSZ);
35   stack_t stack;
36   stack.ss_sp    = sigsegv_stack.data();
37   stack.ss_size  = sigsegv_stack.size();
38   stack.ss_flags = enable ? 0 : SS_DISABLE;
39   return sigaltstack(&stack, old_stack);
40 }
41 #endif
42
43 Context* Context::self()
44 {
45   return current_context_;
46 }
47 void Context::set_current(Context* self)
48 {
49   current_context_ = self;
50 }
51
52 void Context::declare_context(std::size_t size)
53 {
54 #if SIMGRID_HAVE_MC
55   /* Store the address of the stack in heap to compare it apart of heap comparison */
56   if(MC_is_active())
57     MC_ignore_heap(this, size);
58 #endif
59 }
60
61 Context* ContextFactory::attach(actor::ActorImpl*)
62 {
63   xbt_die("Cannot attach with this ContextFactory.\n"
64     "Try using --cfg=contexts/factory:thread instead.\n");
65 }
66
67 Context* ContextFactory::create_maestro(std::function<void()>&&, actor::ActorImpl*)
68 {
69   xbt_die("Cannot create_maestro with this ContextFactory.\n"
70     "Try using --cfg=contexts/factory:thread instead.\n");
71 }
72
73 Context::Context(std::function<void()>&& code, actor::ActorImpl* actor) : code_(std::move(code)), actor_(actor)
74 {
75   /* If no function was provided, this is the context for maestro
76    * and we should set it as the current context */
77   if (not has_code())
78     set_current(this);
79 }
80
81 Context::~Context()
82 {
83   if (self() == this)
84     set_current(nullptr);
85 }
86
87 void Context::stop()
88 {
89   this->actor_->cleanup();
90 }
91
92 AttachContext::~AttachContext() = default;
93
94 } // namespace context
95 } // namespace kernel
96 } // namespace simgrid