1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <simgrid/simix.hpp>
17 #include "src/kernel/context/Context.hpp"
18 #include "src/simix/smx_private.h"
21 * @brief creates a new context for a user level process
22 * @param code a main function
23 * @param cleanup_func the function to call when the context stops
25 smx_context_t SIMIX_context_new(
26 std::function<void()> code,
27 void_pfn_smxprocess_t cleanup_func,
28 smx_process_t simix_process)
31 xbt_die("simix is not initialized, please call MSG_init first");
32 return simix_global->context_factory->create_context(
33 std::move(code), cleanup_func, simix_process);
40 ContextFactoryInitializer factory_initializer = nullptr;
42 ContextFactory::~ContextFactory() {}
44 Context* ContextFactory::self()
46 return SIMIX_context_get_current();
49 void ContextFactory::declare_context(void* 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(context, size);
58 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process)
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()> code, smx_process_t process)
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,
71 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
72 : code_(std::move(code)), process_(process), iwannadie(false)
74 /* If the user provided a function for the process then use it.
75 Otherwise, it is the context for maestro and we should set it as the
78 this->cleanup_func_ = cleanup_func;
80 SIMIX_context_set_current(this);
89 if (this->cleanup_func_)
90 this->cleanup_func_(this->process_);
91 this->process_->suspended = 0;
93 this->iwannadie = false;
94 simcall_process_cleanup(this->process_);
95 this->iwannadie = true;
98 AttachContext::~AttachContext()
104 /** @brief Executes all the processes to run (in parallel if possible). */
105 void SIMIX_context_runall(void)
107 if (!xbt_dynar_is_empty(simix_global->process_to_run))
108 simix_global->context_factory->run_all();
111 /** @brief returns the current running context */
112 smx_context_t SIMIX_context_self(void)
114 if (simix_global && simix_global->context_factory)
115 return simix_global->context_factory->self();