Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Execute future callbacks in the main loop
[simgrid.git] / src / simix / smx_private.h
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 _SIMIX_PRIVATE_H
8 #define _SIMIX_PRIVATE_H
9
10 #include <functional>
11 #include <memory>
12 #include <unordered_map>
13 #include <vector>
14
15 #include <xbt/functional.hpp>
16
17 #include "src/internal_config.h"
18 #include "simgrid/simix.h"
19 #include "surf/surf.h"
20 #include "xbt/base.h"
21 #include "xbt/fifo.h"
22 #include "xbt/swag.h"
23 #include "xbt/dict.h"
24 #include "xbt/mallocator.h"
25 #include "xbt/config.h"
26 #include "xbt/xbt_os_time.h"
27 #include "xbt/function_types.h"
28 #include "src/xbt/ex_interface.h"
29 #include "src/instr/instr_private.h"
30 #include "smx_process_private.h"
31 #include "smx_host_private.h"
32 #include "smx_io_private.h"
33 #include "smx_network_private.h"
34 #include "popping_private.h"
35 #include "smx_synchro_private.h"
36
37 #include <signal.h>
38
39 #ifdef __cplusplus
40
41 #include <simgrid/simix.hpp>
42
43 namespace simgrid {
44 namespace simix {
45
46 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
47 typedef ContextFactory* (*ContextFactoryInitializer)(void);
48 XBT_PUBLIC_DATA(ContextFactoryInitializer) factory_initializer;
49
50 XBT_PRIVATE ContextFactory* thread_factory();
51 XBT_PRIVATE ContextFactory* sysv_factory();
52 XBT_PRIVATE ContextFactory* raw_factory();
53 XBT_PRIVATE ContextFactory* boost_factory();
54
55 }
56 }
57
58 typedef simgrid::simix::ContextFactory *smx_context_factory_t;
59
60 #else
61
62 typedef struct s_smx_context_factory *smx_context_factory_t;
63
64 #endif
65
66 /********************************** Simix Global ******************************/
67
68 namespace simgrid {
69 namespace simix {
70
71 class Global {
72 public:
73   smx_context_factory_t context_factory = nullptr;
74   xbt_dynar_t process_to_run = nullptr;
75   xbt_dynar_t process_that_ran = nullptr;
76   xbt_swag_t process_list = nullptr;
77   xbt_swag_t process_to_destroy = nullptr;
78   smx_process_t maestro_process = nullptr;
79
80   // Maps function names to actor code:
81   std::unordered_map<std::string, simgrid::simix::ActorCodeFactory> registered_functions;
82
83   // This might be used when no corresponding function name is registered:
84   simgrid::simix::ActorCodeFactory default_function;
85
86   smx_creation_func_t create_process_function = nullptr;
87   void_pfn_smxprocess_t kill_process_function = nullptr;
88   /** Callback used when killing a SMX_process */
89   void_pfn_smxprocess_t cleanup_process_function = nullptr;
90   xbt_os_mutex_t mutex = nullptr;
91
92   std::vector<simgrid::xbt::Task<void()>> tasks;
93   std::vector<simgrid::xbt::Task<void()>> tasksTemp;
94 };
95
96 }
97 }
98
99 SG_BEGIN_DECL()
100
101 XBT_PUBLIC_DATA(std::unique_ptr<simgrid::simix::Global>) simix_global;
102
103 extern XBT_PRIVATE unsigned long simix_process_maxpid;
104
105 XBT_PUBLIC(void) SIMIX_clean(void);
106
107 /******************************** Exceptions *********************************/
108 /** @brief Ask to the provided simix process to raise the provided exception */
109 #define SMX_EXCEPTION(issuer, cat, val, msg) \
110   if (1) { \
111   smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \
112   xbt_ex e(msg); \
113   e.category = cat; \
114   e.value = val; \
115   e.file = __FILE__; \
116   e.line = __LINE__; \
117   e.func = __func__; \
118   _smx_throw_issuer->exception = std::make_exception_ptr(e); \
119   } else ((void)0)
120
121 /* ******************************** File ************************************ */
122 typedef struct s_smx_file {
123   surf_file_t surf_file;
124   void* data;                   /**< @brief user data */
125 } s_smx_file_t;
126
127 XBT_PRIVATE void SIMIX_context_mod_init(void);
128 XBT_PRIVATE void SIMIX_context_mod_exit(void);
129
130 XBT_PRIVATE smx_context_t SIMIX_context_new(
131   std::function<void()> code,
132   void_pfn_smxprocess_t cleanup_func,
133   smx_process_t simix_process);
134
135 #ifndef WIN32
136 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
137 #endif
138
139 /* We are using the bottom of the stack to save some information, like the
140  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
141  * size for the stack. */
142 #if HAVE_VALGRIND_H
143 # define smx_context_usable_stack_size                                  \
144   (smx_context_stack_size - sizeof(unsigned int)) /* for valgrind_stack_id */
145 #else
146 # define smx_context_usable_stack_size smx_context_stack_size
147 #endif
148
149 /** @brief Executes all the processes to run (in parallel if possible). */
150 static inline void SIMIX_context_runall(void)
151 {
152   if (!xbt_dynar_is_empty(simix_global->process_to_run))
153     simix_global->context_factory->run_all();
154 }
155
156 /** @brief returns the current running context */
157 static inline smx_context_t SIMIX_context_self(void)
158 {
159   if (simix_global && simix_global->context_factory)
160     return simix_global->context_factory->self();
161   else
162     return nullptr;
163 }
164
165 XBT_PRIVATE void *SIMIX_context_stack_new(void);
166 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
167
168 XBT_PRIVATE void SIMIX_context_set_current(smx_context_t context);
169 XBT_PRIVATE smx_context_t SIMIX_context_get_current(void);
170
171 XBT_PUBLIC(int) SIMIX_process_get_maxpid(void);
172
173 XBT_PRIVATE void SIMIX_post_create_environment(void);
174
175 // FIXME, Dirty hack for SMPI+MSG
176 XBT_PRIVATE void SIMIX_process_set_cleanup_function(smx_process_t process, void_pfn_smxprocess_t cleanup);
177
178 SG_END_DECL()
179
180 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);
181
182 #endif