Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Inline SIMIX_context_get_current()
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2018. 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 "src/kernel/context/Context.hpp"
9 #include "src/simix/smx_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
12
13 /**
14  * @brief creates a new context for a user level process
15  * @param code a main function
16  * @param cleanup_func the function to call when the context stops
17  * @param simix_process
18  */
19 smx_context_t SIMIX_context_new(
20   std::function<void()> code,
21   void_pfn_smxprocess_t cleanup_func,
22   smx_actor_t simix_process)
23 {
24   xbt_assert(simix_global, "simix is not initialized, please call MSG_init first");
25   return simix_global->context_factory->create_context(
26     std::move(code), cleanup_func, simix_process);
27 }
28
29 namespace simgrid {
30 namespace kernel {
31 namespace context {
32 static thread_local smx_context_t smx_current_context;
33
34 ContextFactoryInitializer factory_initializer = nullptr;
35
36 ContextFactory::~ContextFactory() = default;
37
38 Context* ContextFactory::self()
39 {
40   return smx_current_context;
41 }
42
43 void Context::declare_context(std::size_t size)
44 {
45 #if SIMGRID_HAVE_MC
46   /* Store the address of the stack in heap to compare it apart of heap comparison */
47   if(MC_is_active())
48     MC_ignore_heap(this, size);
49 #endif
50 }
51
52 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
53 {
54   xbt_die("Cannot attach with this ContextFactory.\n"
55     "Try using --cfg=contexts/factory:thread instead.\n");
56 }
57
58 Context* ContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
59 {
60   xbt_die("Cannot create_maestro with this ContextFactory.\n"
61     "Try using --cfg=contexts/factory:thread instead.\n");
62 }
63
64 Context::Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
65     : code_(std::move(code)), actor_(actor)
66 {
67   /* If the user provided a function for the process then use it.
68      Otherwise, it is the context for maestro and we should set it as the
69      current context */
70   if (has_code())
71     this->cleanup_func_ = cleanup_func;
72   else
73     SIMIX_context_set_current(this);
74 }
75
76 Context::~Context() = default;
77
78 void Context::stop()
79 {
80   if (this->cleanup_func_)
81     this->cleanup_func_(this->actor_);
82   this->actor_->suspended_ = 0;
83
84   this->iwannadie = false;
85   simgrid::simix::simcall([this] { SIMIX_process_cleanup(this->actor_); });
86   this->iwannadie = true;
87 }
88
89 AttachContext::~AttachContext() = default;
90
91 }}}
92
93 /** @brief Executes all the processes to run (in parallel if possible). */
94 void SIMIX_context_runall()
95 {
96   simix_global->context_factory->run_all();
97 }
98
99 /** @brief returns the current running context */
100 smx_context_t SIMIX_context_self()
101 {
102   if (simix_global && simix_global->context_factory)
103     return simix_global->context_factory->self();
104   else
105     return nullptr;
106 }
107
108 /**
109  * @brief Sets the current context of this thread.
110  * @param context the context to set
111  */
112 void SIMIX_context_set_current(smx_context_t context)
113 {
114   simgrid::kernel::context::smx_current_context = context;
115 }