Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
29393ea87ecb43fb6beaa44e1beab5e1f970b0a9
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdint>
8
9 #include <memory>
10 #include <functional>
11 #include <utility>
12
13 #include <simgrid/simix.hpp>
14
15 #include "mc/mc.h"
16
17 #include "src/kernel/context/Context.hpp"
18 #include "src/simix/smx_private.h"
19
20 void SIMIX_process_set_cleanup_function(
21   smx_process_t process, void_pfn_smxprocess_t cleanup)
22 {
23   process->context->set_cleanup(cleanup);
24 }
25
26 /**
27  * @brief creates a new context for a user level process
28  * @param code a main function
29  * @param cleanup_func the function to call when the context stops
30  */
31 smx_context_t SIMIX_context_new(
32   std::function<void()> code,
33   void_pfn_smxprocess_t cleanup_func,
34   smx_process_t simix_process)
35 {
36   if (!simix_global)
37     xbt_die("simix is not initialized, please call MSG_init first");
38   return simix_global->context_factory->create_context(
39     std::move(code), cleanup_func, simix_process);
40 }
41
42 namespace simgrid {
43 namespace kernel {
44 namespace context {
45
46 ContextFactoryInitializer factory_initializer = nullptr;
47
48 ContextFactory::~ContextFactory() {}
49
50 Context* ContextFactory::self()
51 {
52   return SIMIX_context_get_current();
53 }
54
55 void ContextFactory::declare_context(void* context, std::size_t size)
56 {
57 #if HAVE_MC
58   /* Store the address of the stack in heap to compare it apart of heap comparison */
59   if(MC_is_active())
60     MC_ignore_heap(context, size);
61 #endif
62 }
63
64 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process)
65 {
66   xbt_die("Cannot attach with this ContextFactory.\n"
67     "Try using --cfg=contexts/factory:thread instead.\n");
68 }
69
70 Context* ContextFactory::create_maestro(std::function<void()> code, smx_process_t process)
71 {
72   xbt_die("Cannot create_maestro with this ContextFactory.\n"
73     "Try using --cfg=contexts/factory:thread instead.\n");
74 }
75
76 Context::Context(std::function<void()> code,
77     void_pfn_smxprocess_t cleanup_func, smx_process_t process)
78   : code_(std::move(code)), process_(process), iwannadie(false)
79 {
80   /* If the user provided a function for the process then use it.
81      Otherwise, it is the context for maestro and we should set it as the
82      current context */
83   if (has_code())
84     this->cleanup_func_ = cleanup_func;
85   else
86     SIMIX_context_set_current(this);
87 }
88
89 Context::~Context()
90 {
91 }
92
93 void Context::stop()
94 {
95   if (this->cleanup_func_)
96     this->cleanup_func_(this->process_);
97   this->process_->suspended = 0;
98
99   this->iwannadie = false;
100   simcall_process_cleanup(this->process_);
101   this->iwannadie = true;
102 }
103
104 AttachContext::~AttachContext()
105 {
106 }
107
108 }}}
109
110 /** @brief Executes all the processes to run (in parallel if possible). */
111 void SIMIX_context_runall(void)
112 {
113   if (!xbt_dynar_is_empty(simix_global->process_to_run))
114     simix_global->context_factory->run_all();
115 }
116
117 /** @brief returns the current running context */
118 smx_context_t SIMIX_context_self(void)
119 {
120   if (simix_global && simix_global->context_factory)
121     return simix_global->context_factory->self();
122   else
123     return nullptr;
124 }
125