Logo AND Algorithmique Numérique Distribuée

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