Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge get-pid
[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
14 #include <xbt/functional.hpp>
15
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
93 }
94 }
95
96 SG_BEGIN_DECL()
97
98 XBT_PUBLIC_DATA(std::unique_ptr<simgrid::simix::Global>) simix_global;
99
100 extern XBT_PRIVATE unsigned long simix_process_maxpid;
101
102 XBT_PUBLIC(void) SIMIX_clean(void);
103
104 /******************************** Exceptions *********************************/
105 /** @brief Ask to the provided simix process to raise the provided exception */
106 #define SMX_EXCEPTION(issuer, cat, val, msg) \
107   if (1) { \
108   smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \
109   xbt_ex e(msg); \
110   e.category = cat; \
111   e.value = val; \
112   e.file = __FILE__; \
113   e.line = __LINE__; \
114   e.func = __func__; \
115   _smx_throw_issuer->exception = std::make_exception_ptr(e); \
116   } else ((void)0)
117
118 /* ******************************** File ************************************ */
119 typedef struct s_smx_file {
120   surf_file_t surf_file;
121   void* data;                   /**< @brief user data */
122 } s_smx_file_t;
123
124 XBT_PRIVATE void SIMIX_context_mod_init(void);
125 XBT_PRIVATE void SIMIX_context_mod_exit(void);
126
127 XBT_PRIVATE smx_context_t SIMIX_context_new(
128   std::function<void()> code,
129   void_pfn_smxprocess_t cleanup_func,
130   smx_process_t simix_process);
131
132 #ifndef WIN32
133 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
134 #endif
135
136 /* We are using the bottom of the stack to save some information, like the
137  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
138  * size for the stack. */
139 #if HAVE_VALGRIND_H
140 # define smx_context_usable_stack_size                                  \
141   (smx_context_stack_size - sizeof(unsigned int)) /* for valgrind_stack_id */
142 #else
143 # define smx_context_usable_stack_size smx_context_stack_size
144 #endif
145
146 /** @brief Executes all the processes to run (in parallel if possible). */
147 static inline void SIMIX_context_runall(void)
148 {
149   if (!xbt_dynar_is_empty(simix_global->process_to_run))
150     simix_global->context_factory->run_all();
151 }
152
153 /** @brief returns the current running context */
154 static inline smx_context_t SIMIX_context_self(void)
155 {
156   if (simix_global && simix_global->context_factory)
157     return simix_global->context_factory->self();
158   else
159     return nullptr;
160 }
161
162 XBT_PRIVATE void *SIMIX_context_stack_new(void);
163 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
164
165 XBT_PRIVATE void SIMIX_context_set_current(smx_context_t context);
166 XBT_PRIVATE smx_context_t SIMIX_context_get_current(void);
167
168 XBT_PUBLIC(int) SIMIX_process_get_maxpid(void);
169
170 XBT_PRIVATE void SIMIX_post_create_environment(void);
171
172 // FIXME, Dirty hack for SMPI+MSG
173 XBT_PRIVATE void SIMIX_process_set_cleanup_function(smx_process_t process, void_pfn_smxprocess_t cleanup);
174
175 SG_END_DECL()
176
177 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);
178
179 #endif