Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change some calls to get_cname to calls to get_name.
[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_ && not actor_->host_->is_on()) {
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_name());
93   }
94
95   // Execute the termination callbacks
96   smx_process_exit_status_t exit_status = (actor_->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
97   while (not actor_->on_exit.empty()) {
98     s_smx_process_exit_fun_t exit_fun = actor_->on_exit.back();
99     actor_->on_exit.pop_back();
100     (exit_fun.fun)(exit_status, exit_fun.arg);
101   }
102
103   /* cancel non-blocking activities */
104   for (auto activity : actor_->comms)
105     boost::static_pointer_cast<activity::CommImpl>(activity)->cancel();
106   actor_->comms.clear();
107
108   XBT_DEBUG("%s@%s(%ld) should not run anymore", actor_->get_cname(), actor_->iface()->get_host()->get_cname(),
109             actor_->pid_);
110
111   if (this->cleanup_func_)
112     this->cleanup_func_(this->actor_);
113
114   this->iwannadie = false; // don't let the simcall's yield() do a Context::stop(), because that's me
115   simgrid::simix::simcall([this] {
116     simgrid::s4u::Actor::on_destruction(actor_->iface());
117
118     /* Unregister from the kill timer if any */
119     if (actor_->kill_timer != nullptr) {
120       SIMIX_timer_remove(actor_->kill_timer);
121       actor_->kill_timer = nullptr;
122     }
123
124     SIMIX_process_cleanup(actor_);
125   });
126   this->iwannadie = true;
127 }
128
129 AttachContext::~AttachContext() = default;
130
131 StopRequest::~StopRequest() = default;
132
133 void StopRequest::do_throw()
134 {
135   throw StopRequest();
136 }
137
138 bool StopRequest::try_n_catch(std::function<void(void)> try_block)
139 {
140   bool res;
141   try {
142     try_block();
143     res = true;
144   } catch (StopRequest const&) {
145     XBT_DEBUG("Caught a StopRequest");
146     res = false;
147   }
148   return res;
149 }
150 }}}
151
152 /** @brief Executes all the processes to run (in parallel if possible). */
153 void SIMIX_context_runall()
154 {
155   simix_global->context_factory->run_all();
156 }