1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
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"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
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
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)
27 return simix_global->context_factory->create_context(
28 std::move(code), cleanup_func, simix_process);
35 ContextFactoryInitializer factory_initializer = nullptr;
37 ContextFactory::~ContextFactory() = default;
39 static thread_local smx_context_t smx_current_context = nullptr;
40 Context* Context::self()
42 return smx_current_context;
44 void Context::set_current(Context* self)
46 smx_current_context = self;
49 void Context::declare_context(std::size_t size)
52 /* Store the address of the stack in heap to compare it apart of heap comparison */
54 MC_ignore_heap(this, size);
58 Context* ContextFactory::attach(void_pfn_smxprocess_t, smx_actor_t)
60 xbt_die("Cannot attach with this ContextFactory.\n"
61 "Try using --cfg=contexts/factory:thread instead.\n");
64 Context* ContextFactory::create_maestro(std::function<void()>, smx_actor_t)
66 xbt_die("Cannot create_maestro with this ContextFactory.\n"
67 "Try using --cfg=contexts/factory:thread instead.\n");
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)
73 /* If no function was provided, this is the context for maestro
74 * and we should set it as the current context */
87 actor_->finished_ = true;
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(),
92 watched_hosts.insert(actor_->host_->get_name());
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);
103 /* cancel non-blocking activities */
104 for (auto activity : actor_->comms)
105 boost::static_pointer_cast<activity::CommImpl>(activity)->cancel();
106 actor_->comms.clear();
108 XBT_DEBUG("%s@%s(%ld) should not run anymore", actor_->get_cname(), actor_->iface()->get_host()->get_cname(),
111 if (this->cleanup_func_)
112 this->cleanup_func_(this->actor_);
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());
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;
124 SIMIX_process_cleanup(actor_);
126 this->iwannadie = true;
129 AttachContext::~AttachContext() = default;
131 StopRequest::~StopRequest() = default;
133 void StopRequest::do_throw()
138 bool StopRequest::try_n_catch(std::function<void(void)> try_block)
144 } catch (StopRequest const&) {
145 XBT_DEBUG("Caught a StopRequest");
152 /** @brief Executes all the processes to run (in parallel if possible). */
153 void SIMIX_context_runall()
155 simix_global->context_factory->run_all();