Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Fix SMPI+MSG
[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 <simgrid/simix.hpp>
8
9 #include "mc/mc.h"
10
11 #include <src/simix/smx_private.h>
12
13 void SIMIX_process_set_cleanup_function(
14   smx_process_t process, void_pfn_smxprocess_t cleanup)
15 {
16   process->context->set_cleanup(cleanup);
17 }
18
19 namespace simgrid {
20 namespace simix {
21
22 ContextFactoryInitializer factory_initializer = nullptr;
23
24 ContextFactory::~ContextFactory() {}
25
26 Context* ContextFactory::self()
27 {
28   return SIMIX_context_get_current();
29 }
30
31 void ContextFactory::declare_context(void* context, std::size_t size)
32 {
33 #ifdef HAVE_MC
34   /* Store the address of the stack in heap to compare it apart of heap comparison */
35   if(MC_is_active())
36     MC_ignore_heap(context, size);
37 #endif
38 }
39
40 Context::Context(xbt_main_func_t code,
41         int argc, char **argv,
42         void_pfn_smxprocess_t cleanup_func,
43         smx_process_t process)
44   : process_(process), iwannadie(false)
45 {
46   /* If the user provided a function for the process then use it.
47      Otherwise, it is the context for maestro and we should set it as the
48      current context */
49   if (code) {
50     this->cleanup_func_ = cleanup_func;
51     this->argc_ = argc;
52     this->argv_ = argv;
53     this->code_ = code;
54   } else {
55     SIMIX_context_set_current(this);
56   }
57 }
58
59 Context::~Context()
60 {
61   if (this->argv_) {
62     for (int i = 0; i < this->argc_; i++)
63       free(this->argv_[i]);
64     free(this->argv_);
65   }
66 }
67
68 void Context::stop()
69 {
70   if (this->cleanup_func_)
71     this->cleanup_func_(this->process_);
72   this->process_->suspended = 0;
73
74   this->iwannadie = false;
75   simcall_process_cleanup(this->process_);
76   this->iwannadie = true;
77 }
78
79 }
80 }