Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
145e1e1cfe7cea14b64b02e800d1b120d690cb45
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2019. 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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
15
16
17 namespace simgrid {
18 namespace kernel {
19 namespace context {
20
21 ContextFactoryInitializer factory_initializer = nullptr;
22
23 ContextFactory::~ContextFactory() = default;
24
25 static thread_local Context* smx_current_context = nullptr;
26 Context* Context::self()
27 {
28   return smx_current_context;
29 }
30 void Context::set_current(Context* self)
31 {
32   smx_current_context = self;
33 }
34
35 void Context::declare_context(std::size_t size)
36 {
37 #if SIMGRID_HAVE_MC
38   /* Store the address of the stack in heap to compare it apart of heap comparison */
39   if(MC_is_active())
40     MC_ignore_heap(this, size);
41 #endif
42 }
43
44 Context* ContextFactory::attach(actor::ActorImpl*)
45 {
46   xbt_die("Cannot attach with this ContextFactory.\n"
47     "Try using --cfg=contexts/factory:thread instead.\n");
48 }
49
50 Context* ContextFactory::create_maestro(std::function<void()>&&, actor::ActorImpl*)
51 {
52   xbt_die("Cannot create_maestro with this ContextFactory.\n"
53     "Try using --cfg=contexts/factory:thread instead.\n");
54 }
55
56 Context::Context(std::function<void()>&& code, actor::ActorImpl* actor) : code_(std::move(code)), actor_(actor)
57 {
58   /* If no function was provided, this is the context for maestro
59    * and we should set it as the current context */
60   if (not has_code())
61     set_current(this);
62 }
63
64 Context::~Context()
65 {
66   if (self() == this)
67     set_current(nullptr);
68 }
69
70 void Context::stop()
71 {
72   this->actor_->cleanup();
73 }
74
75 AttachContext::~AttachContext() = default;
76
77 } // namespace context
78 } // namespace kernel
79 } // namespace simgrid
80
81 /** @brief Executes all the processes to run (in parallel if possible). */
82 void SIMIX_context_runall()
83 {
84   simix_global->context_factory->run_all();
85 }