Logo AND Algorithmique Numérique Distribuée

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