Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c2ea68ac9f8ae67bd4bd22c663cb0538c4b8299b
[simgrid.git] / src / kernel / context / Context.cpp
1 /* Copyright (c) 2007-2019. 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 "simgrid/s4u/Host.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/context/Context.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
15
16 /**
17  * @brief creates a new context for a user level process
18  * @param code a main function
19  * @param cleanup_func the function to call when the context stops
20  * @param simix_process
21  */
22 smx_context_t SIMIX_context_new(
23   std::function<void()> code,
24   void_pfn_smxprocess_t cleanup_func,
25   smx_actor_t simix_process)
26 {
27   return simix_global->context_factory->create_context(
28     std::move(code), cleanup_func, simix_process);
29 }
30
31 namespace simgrid {
32 namespace kernel {
33 namespace context {
34
35 ContextFactoryInitializer factory_initializer = nullptr;
36
37 ContextFactory::~ContextFactory() = default;
38
39 static thread_local smx_context_t smx_current_context = nullptr;
40 Context* Context::self()
41 {
42   return smx_current_context;
43 }
44 void Context::set_current(Context* self)
45 {
46   smx_current_context = self;
47 }
48
49 void Context::declare_context(std::size_t size)
50 {
51 #if SIMGRID_HAVE_MC
52   /* Store the address of the stack in heap to compare it apart of heap comparison */
53   if(MC_is_active())
54     MC_ignore_heap(this, size);
55 #endif
56 }
57
58 Context* ContextFactory::attach(void_pfn_smxprocess_t, smx_actor_t)
59 {
60   xbt_die("Cannot attach with this ContextFactory.\n"
61     "Try using --cfg=contexts/factory:thread instead.\n");
62 }
63
64 Context* ContextFactory::create_maestro(std::function<void()>, smx_actor_t)
65 {
66   xbt_die("Cannot create_maestro with this ContextFactory.\n"
67     "Try using --cfg=contexts/factory:thread instead.\n");
68 }
69
70 Context::Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
71     : code_(std::move(code)), cleanup_func_(cleanup_func), actor_(actor)
72 {
73   /* If no function was provided, this is the context for maestro
74    * and we should set it as the current context */
75   if (not has_code())
76     set_current(this);
77 }
78
79 Context::~Context()
80 {
81   if (self() == this)
82     set_current(nullptr);
83 }
84
85 void Context::stop()
86 {
87   actor_->finished_ = true;
88
89   if (actor_->auto_restart_ && actor_->host_->is_off()) {
90     XBT_DEBUG("Insert host %s to watched_hosts because it's off and %s needs to restart", actor_->host_->get_cname(),
91               actor_->get_cname());
92     watched_hosts.insert(actor_->host_->get_cname());
93   }
94
95   actor_->finished_ = true;
96   SIMIX_process_on_exit_runall(actor_);
97
98   /* cancel non-blocking activities */
99   while (not actor_->comms.empty()) {
100     smx_activity_t synchro = actor_->comms.front();
101     actor_->comms.pop_front();
102     simgrid::kernel::activity::CommImplPtr comm =
103         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
104
105     /* make sure no one will finish the comm after this process is destroyed,
106      * because src_proc or dst_proc would be an invalid pointer */
107
108     if (comm->src_proc == actor_) {
109       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
110                 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
111       comm->src_proc = nullptr;
112
113     } else if (comm->dst_proc == actor_) {
114       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
115                 comm->src_proc, comm->dst_proc);
116       comm->dst_proc = nullptr;
117
118       if (comm->detached && comm->src_proc != nullptr) {
119         /* the comm will be freed right now, remove it from the sender */
120         comm->src_proc->comms.remove(comm);
121       }
122     } else {
123       xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
124     }
125     comm->cancel();
126   }
127
128   XBT_DEBUG("%s@%s(%ld) should not run anymore", actor_->get_cname(), actor_->iface()->get_host()->get_cname(),
129             actor_->pid_);
130
131   if (this->cleanup_func_)
132     this->cleanup_func_(this->actor_);
133
134   this->iwannadie = false; // don't let the simcall's yield() do a Context::stop(), because that's me
135   simgrid::simix::simcall([this] { SIMIX_process_cleanup(this->actor_); });
136   this->iwannadie = true;
137 }
138
139 AttachContext::~AttachContext() = default;
140
141 StopRequest::~StopRequest() = default;
142
143 void StopRequest::do_throw()
144 {
145   throw StopRequest();
146 }
147
148 bool StopRequest::try_n_catch(std::function<void(void)> try_block)
149 {
150   bool res;
151   try {
152     try_block();
153     res = true;
154   } catch (StopRequest const&) {
155     XBT_DEBUG("Caught a StopRequest");
156     res = false;
157   }
158   return res;
159 }
160 }}}
161
162 /** @brief Executes all the processes to run (in parallel if possible). */
163 void SIMIX_context_runall()
164 {
165   simix_global->context_factory->run_all();
166 }