1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
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"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
22 ContextFactoryInitializer factory_initializer = nullptr;
24 ContextFactory::~ContextFactory() = default;
26 thread_local Context* Context::current_context_ = nullptr;
29 /* Install or disable alternate signal stack, for SIGSEGV handler. */
30 int Context::install_sigsegv_stack(stack_t* old_stack, bool enable)
32 static std::vector<unsigned char> sigsegv_stack(SIGSTKSZ);
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);
41 Context* Context::self()
43 return current_context_;
45 void Context::set_current(Context* self)
47 current_context_ = self;
50 void Context::declare_context(std::size_t size)
53 /* Store the address of the stack in heap to compare it apart of heap comparison */
55 MC_ignore_heap(this, size);
59 Context* ContextFactory::attach(actor::ActorImpl*)
61 xbt_die("Cannot attach with this ContextFactory.\n"
62 "Try using --cfg=contexts/factory:thread instead.\n");
65 Context* ContextFactory::create_maestro(std::function<void()>&&, actor::ActorImpl*)
67 xbt_die("Cannot create_maestro with this ContextFactory.\n"
68 "Try using --cfg=contexts/factory:thread instead.\n");
71 Context::Context(std::function<void()>&& code, actor::ActorImpl* actor) : code_(std::move(code)), actor_(actor)
73 /* If no function was provided, this is the context for maestro
74 * and we should set it as the current context */
87 this->actor_->cleanup();
90 AttachContext::~AttachContext() = default;
92 } // namespace context
94 } // namespace simgrid