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. */
7 #ifndef _SIMIX_PRIVATE_H
8 #define _SIMIX_PRIVATE_H
12 #include <unordered_map>
14 #include <xbt/functional.hpp>
16 #include "src/internal_config.h"
17 #include "simgrid/simix.h"
18 #include "surf/surf.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"
40 #include <simgrid/simix.hpp>
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;
49 XBT_PRIVATE ContextFactory* thread_factory();
50 XBT_PRIVATE ContextFactory* sysv_factory();
51 XBT_PRIVATE ContextFactory* raw_factory();
52 XBT_PRIVATE ContextFactory* boost_factory();
57 typedef simgrid::simix::ContextFactory *smx_context_factory_t;
61 typedef struct s_smx_context_factory *smx_context_factory_t;
65 /********************************** Simix Global ******************************/
70 // What's executed as SIMIX actor code:
71 typedef std::function<void()> ActorCode;
73 // Create ActorCode based on argv:
74 typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
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;
85 // Maps function names to actor code:
86 std::unordered_map<std::string, simgrid::simix::ActorCodeFactory> registered_functions;
88 // This might be used when no corresponding function name is registered:
89 simgrid::simix::ActorCodeFactory default_function;
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;
103 XBT_PUBLIC_DATA(std::unique_ptr<simgrid::simix::Global>) simix_global;
105 extern XBT_PRIVATE unsigned long simix_process_maxpid;
107 XBT_PUBLIC(void) SIMIX_clean(void);
109 /******************************** Exceptions *********************************/
110 /** @brief Ask to the provided simix process to raise the provided exception */
111 #define SMX_EXCEPTION(issuer, cat, val, msg) \
113 smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \
120 _smx_throw_issuer->exception = std::make_exception_ptr(e); \
123 /* ******************************** File ************************************ */
124 typedef struct s_smx_file {
125 surf_file_t surf_file;
126 void* data; /**< @brief user data */
129 XBT_PRIVATE void SIMIX_context_mod_init(void);
130 XBT_PRIVATE void SIMIX_context_mod_exit(void);
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);
138 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
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. */
145 # define smx_context_usable_stack_size \
146 (smx_context_stack_size - sizeof(unsigned int)) /* for valgrind_stack_id */
148 # define smx_context_usable_stack_size smx_context_stack_size
151 /** @brief Executes all the processes to run (in parallel if possible). */
152 static inline void SIMIX_context_runall(void)
154 if (!xbt_dynar_is_empty(simix_global->process_to_run))
155 simix_global->context_factory->run_all();
158 /** @brief returns the current running context */
159 static inline smx_context_t SIMIX_context_self(void)
161 if (simix_global && simix_global->context_factory)
162 return simix_global->context_factory->self();
167 XBT_PRIVATE void *SIMIX_context_stack_new(void);
168 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
170 XBT_PRIVATE void SIMIX_context_set_current(smx_context_t context);
171 XBT_PRIVATE smx_context_t SIMIX_context_get_current(void);
173 XBT_PUBLIC(int) SIMIX_process_get_maxpid(void);
175 XBT_PRIVATE void SIMIX_post_create_environment(void);
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);
180 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);