Logo AND Algorithmique Numérique Distribuée

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