Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not load internal_config.h from mc.h
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2017. 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.h"
10 #include "src/mc/mc_ignore.h"
11
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  */
18 smx_context_t SIMIX_context_new(
19   std::function<void()> code,
20   void_pfn_smxprocess_t cleanup_func,
21   smx_actor_t simix_process)
22 {
23   xbt_assert(simix_global, "simix is not initialized, please call MSG_init first");
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 Context* ContextFactory::self()
37 {
38   return SIMIX_context_get_current();
39 }
40
41 void ContextFactory::declare_context(void* context, std::size_t size)
42 {
43 #if SIMGRID_HAVE_MC
44   /* Store the address of the stack in heap to compare it apart of heap comparison */
45   if(MC_is_active())
46     MC_ignore_heap(context, size);
47 #endif
48 }
49
50 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
51 {
52   xbt_die("Cannot attach with this ContextFactory.\n"
53     "Try using --cfg=contexts/factory:thread instead.\n");
54 }
55
56 Context* ContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
57 {
58   xbt_die("Cannot create_maestro with this ContextFactory.\n"
59     "Try using --cfg=contexts/factory:thread instead.\n");
60 }
61
62 Context::Context(std::function<void()> code,
63     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
64   : code_(std::move(code)), process_(process), iwannadie(false)
65 {
66   /* If the user provided a function for the process then use it.
67      Otherwise, it is the context for maestro and we should set it as the
68      current context */
69   if (has_code())
70     this->cleanup_func_ = cleanup_func;
71   else
72     SIMIX_context_set_current(this);
73 }
74
75 Context::~Context() = default;
76
77 void Context::stop()
78 {
79   if (this->cleanup_func_)
80     this->cleanup_func_(this->process_);
81   this->process_->suspended = 0;
82
83   this->iwannadie = false;
84   simcall_process_cleanup(this->process_);
85   this->iwannadie = true;
86 }
87
88 AttachContext::~AttachContext() = default;
89
90 }}}
91
92 /** @brief Executes all the processes to run (in parallel if possible). */
93 void SIMIX_context_runall()
94 {
95   simix_global->context_factory->run_all();
96 }
97
98 /** @brief returns the current running context */
99 smx_context_t SIMIX_context_self()
100 {
101   if (simix_global && simix_global->context_factory)
102     return simix_global->context_factory->self();
103   else
104     return nullptr;
105 }
106