Logo AND Algorithmique Numérique Distribuée

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