Logo AND Algorithmique Numérique Distribuée

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