Logo AND Algorithmique Numérique Distribuée

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