Logo AND Algorithmique Numérique Distribuée

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