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/simix/smx_private.h"
19 void SIMIX_process_set_cleanup_function(
20 smx_process_t process, void_pfn_smxprocess_t cleanup)
22 process->context->set_cleanup(cleanup);
26 * \brief creates a new context for a user level process
27 * \param code a main function
28 * \param argc the number of arguments of the main function
29 * \param argv the vector of arguments of the main function
30 * \param cleanup_func the function to call when the context stops
31 * \param cleanup_arg the argument of the cleanup_func function
33 smx_context_t SIMIX_context_new(
34 xbt_main_func_t code, int argc, char **argv,
35 void_pfn_smxprocess_t cleanup_func,
36 smx_process_t simix_process)
39 xbt_die("simix is not initialized, please call MSG_init first");
40 return simix_global->context_factory->create_context(
41 simgrid::simix::wrap_main(code, argc, argv), cleanup_func, simix_process);
47 ContextFactoryInitializer factory_initializer = nullptr;
49 ContextFactory::~ContextFactory() {}
51 Context* ContextFactory::self()
53 return SIMIX_context_get_current();
56 void ContextFactory::declare_context(void* context, std::size_t size)
59 /* Store the address of the stack in heap to compare it apart of heap comparison */
61 MC_ignore_heap(context, size);
65 Context* ContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process)
67 xbt_die("Cannot attach with this ContextFactory.\n"
68 "Try using --cfg=contexts/factory:thread instead.\n");
71 Context* ContextFactory::create_maestro(std::function<void()> code, smx_process_t process)
73 xbt_die("Cannot create_maestro with this ContextFactory.\n"
74 "Try using --cfg=contexts/factory:thread instead.\n");
77 Context::Context(std::function<void()> code,
78 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
79 : code_(std::move(code)), process_(process), iwannadie(false)
81 /* If the user provided a function for the process then use it.
82 Otherwise, it is the context for maestro and we should set it as the
85 this->cleanup_func_ = cleanup_func;
87 SIMIX_context_set_current(this);
96 if (this->cleanup_func_)
97 this->cleanup_func_(this->process_);
98 this->process_->suspended = 0;
100 this->iwannadie = false;
101 simcall_process_cleanup(this->process_);
102 this->iwannadie = true;
105 AttachContext::~AttachContext()