Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7e3ce088131f8ca19ec1211594a22c165784a588
[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 "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   return simix_global->context_factory->create_context(
25     std::move(code), cleanup_func, simix_process);
26 }
27
28 namespace simgrid {
29 namespace kernel {
30 namespace context {
31
32 ContextFactoryInitializer factory_initializer = nullptr;
33
34 ContextFactory::~ContextFactory() = default;
35
36 static thread_local smx_context_t smx_current_context = nullptr;
37 Context* Context::self()
38 {
39   return smx_current_context;
40 }
41 void Context::set_current(Context* self)
42 {
43   smx_current_context = self;
44 }
45
46 void Context::declare_context(std::size_t size)
47 {
48 #if SIMGRID_HAVE_MC
49   /* Store the address of the stack in heap to compare it apart of heap comparison */
50   if(MC_is_active())
51     MC_ignore_heap(this, size);
52 #endif
53 }
54
55 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
56 {
57   xbt_die("Cannot attach with this ContextFactory.\n"
58     "Try using --cfg=contexts/factory:thread instead.\n");
59 }
60
61 Context* ContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
62 {
63   xbt_die("Cannot create_maestro with this ContextFactory.\n"
64     "Try using --cfg=contexts/factory:thread instead.\n");
65 }
66
67 Context::Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
68     : code_(std::move(code)), cleanup_func_(cleanup_func), actor_(actor)
69 {
70   /* If no function was provided, this is the context for maestro
71    * and we should set it as the current context */
72   if (not has_code())
73     set_current(this);
74 }
75
76 Context::~Context()
77 {
78   if (self() == this)
79     set_current(nullptr);
80 }
81
82 void Context::stop()
83 {
84   if (this->cleanup_func_)
85     this->cleanup_func_(this->actor_);
86   this->actor_->suspended_ = 0;
87
88   this->iwannadie = false;
89   simgrid::simix::simcall([this] { SIMIX_process_cleanup(this->actor_); });
90   this->iwannadie = true;
91 }
92
93 AttachContext::~AttachContext() = default;
94
95 }}}
96
97 /** @brief Executes all the processes to run (in parallel if possible). */
98 void SIMIX_context_runall()
99 {
100   simix_global->context_factory->run_all();
101 }