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_ && 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(),
92 watched_hosts.insert(actor_->host_->get_cname());
95 actor_->finished_ = true;
96 SIMIX_process_on_exit_runall(actor_);
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);
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 */
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.get(), comm->dst_proc.get());
111 comm->src_proc = nullptr;
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.get(), comm->dst_proc.get());
116 comm->dst_proc = nullptr;
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);
123 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
128 XBT_DEBUG("%s@%s(%ld) should not run anymore", actor_->get_cname(), actor_->iface()->get_host()->get_cname(),
131 if (this->cleanup_func_)
132 this->cleanup_func_(this->actor_);
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;
139 AttachContext::~AttachContext() = default;
141 StopRequest::~StopRequest() = default;
143 void StopRequest::do_throw()
148 bool StopRequest::try_n_catch(std::function<void(void)> try_block)
154 } catch (StopRequest const&) {
155 XBT_DEBUG("Caught a StopRequest");
162 /** @brief Executes all the processes to run (in parallel if possible). */
163 void SIMIX_context_runall()
165 simix_global->context_factory->run_all();