Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Port UContext to C++
[simgrid.git] / src / simix / smx_private.hpp
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_SIMIX_PRIVATE_HPP
8 #define SIMGRID_SIMIX_PRIVATE_HPP
9
10 #include <simgrid/simix.hpp>
11 #include "smx_private.h"
12
13 /**
14  * \brief creates a new context for a user level process
15  * \param code a main function
16  * \param argc the number of arguments of the main function
17  * \param argv the vector of arguments of the main function
18  * \param cleanup_func the function to call when the context stops
19  * \param cleanup_arg the argument of the cleanup_func function
20  */
21 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code,
22                                                   int argc, char **argv,
23                                                   void_pfn_smxprocess_t cleanup_func,
24                                                   smx_process_t simix_process)
25 {
26   if (!simix_global)
27     xbt_die("simix is not initialized, please call MSG_init first");
28   return simix_global->context_factory->create_context(
29     code, argc, argv, cleanup_func, simix_process);
30 }
31
32 /**
33  * \brief destroy a context
34  * \param context the context to destroy
35  * Argument must be stopped first -- runs in maestro context
36  */
37 static XBT_INLINE void SIMIX_context_free(smx_context_t context)
38 {
39   delete context;
40 }
41
42 /**
43  * \brief stops the execution of a context
44  * \param context to stop
45  */
46 static XBT_INLINE void SIMIX_context_stop(smx_context_t context)
47 {
48   context->stop();
49 }
50
51 /**
52  \brief suspends a context and return the control back to the one which
53         scheduled it
54  \param context the context to be suspended (it must be the running one)
55  */
56 static XBT_INLINE void SIMIX_context_suspend(smx_context_t context)
57 {
58   context->suspend();
59 }
60
61 /**
62  \brief Executes all the processes to run (in parallel if possible).
63  */
64 static XBT_INLINE void SIMIX_context_runall(void)
65 {
66   if (!xbt_dynar_is_empty(simix_global->process_to_run))
67     simix_global->context_factory->run_all();
68 }
69
70 /**
71  \brief returns the current running context
72  */
73 static XBT_INLINE smx_context_t SIMIX_context_self(void)
74 {
75   if (simix_global && simix_global->context_factory)
76     return simix_global->context_factory->self();
77   else
78     return nullptr;
79 }
80
81 /**
82  \brief returns the SIMIX process associated to a context
83  \param context The context
84  \return The SIMIX process
85  */
86 static XBT_INLINE smx_process_t SIMIX_context_get_process(smx_context_t context)
87 {
88   return context->process();
89 }
90
91 namespace simgrid {
92 namespace simix {
93
94 XBT_PRIVATE ContextFactory* thread_factory();
95 XBT_PRIVATE ContextFactory* sysv_factory();
96 XBT_PRIVATE ContextFactory* raw_factory();
97 XBT_PRIVATE ContextFactory* boost_factory();
98
99 }
100 }
101
102 #endif