Logo AND Algorithmique Numérique Distribuée

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