1 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef _SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
7 #define _SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
11 #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 "src/simix/smx_host_private.h"
30 #include "src/simix/smx_io_private.h"
31 #include "src/simix/smx_network_private.h"
32 #include "src/simix/popping_private.h"
33 #include "src/simix/smx_synchro_private.h"
36 #include "src/simix/ActorImpl.hpp"
40 #include <simgrid/simix.hpp>
49 XBT_PUBLIC_CLASS ContextFactory {
54 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
55 virtual ~ContextFactory();
56 virtual Context* create_context(std::function<void()> code,
57 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
59 // Optional methods for attaching main() as a context:
61 /** Creates a context from the current context of execution
63 * This will not work on all implementation of `ContextFactory`.
65 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
66 virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
68 virtual void run_all() = 0;
69 virtual Context* self();
70 std::string const& name() const
75 void declare_context(void* T, std::size_t size);
77 template<class T, class... Args>
78 T* new_context(Args&&... args)
80 T* context = new T(std::forward<Args>(args)...);
81 this->declare_context(context, sizeof(T));
86 XBT_PUBLIC_CLASS Context {
88 std::function<void()> code_;
89 void_pfn_smxprocess_t cleanup_func_ = nullptr;
90 smx_process_t process_ = nullptr;
94 Context(std::function<void()> code,
95 void_pfn_smxprocess_t cleanup_func,
96 smx_process_t process);
101 bool has_code() const
105 smx_process_t process()
107 return this->process_;
109 void set_cleanup(void_pfn_smxprocess_t cleanup)
111 cleanup_func_ = cleanup;
117 virtual void suspend() = 0;
120 XBT_PUBLIC_CLASS AttachContext : public Context {
123 AttachContext(std::function<void()> code,
124 void_pfn_smxprocess_t cleanup_func,
125 smx_process_t process)
126 : Context(std::move(code), cleanup_func, process)
129 ~AttachContext() override;
131 /** Called by the context when it is ready to give control
134 virtual void attach_start() = 0;
136 /** Called by the context when it has finished its job */
137 virtual void attach_stop() = 0;
140 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
141 typedef ContextFactory* (*ContextFactoryInitializer)(void);
142 XBT_PUBLIC_DATA(ContextFactoryInitializer) factory_initializer;
144 XBT_PRIVATE ContextFactory* thread_factory();
145 XBT_PRIVATE ContextFactory* sysv_factory();
146 XBT_PRIVATE ContextFactory* raw_factory();
147 XBT_PRIVATE ContextFactory* boost_factory();
151 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
155 typedef struct s_smx_context_factory *smx_context_factory_t;
162 XBT_PRIVATE void SIMIX_context_mod_init(void);
163 XBT_PRIVATE void SIMIX_context_mod_exit(void);
165 XBT_PRIVATE smx_context_t SIMIX_context_new(
166 std::function<void()> code,
167 void_pfn_smxprocess_t cleanup_func,
168 smx_process_t simix_process);
171 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
174 /* We are using the bottom of the stack to save some information, like the
175 * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
176 * size for the stack. */
178 # define smx_context_usable_stack_size \
179 (smx_context_stack_size - sizeof(unsigned int)) /* for valgrind_stack_id */
181 # define smx_context_usable_stack_size smx_context_stack_size
184 /** @brief Executes all the processes to run (in parallel if possible). */
185 XBT_PRIVATE void SIMIX_context_runall(void);
186 /** @brief returns the current running context */
187 XBT_PRIVATE smx_context_t SIMIX_context_self(void);
189 XBT_PRIVATE void *SIMIX_context_stack_new(void);
190 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
192 XBT_PRIVATE void SIMIX_context_set_current(smx_context_t context);
193 XBT_PRIVATE smx_context_t SIMIX_context_get_current(void);
195 XBT_PUBLIC(int) SIMIX_process_get_maxpid(void);
197 XBT_PRIVATE void SIMIX_post_create_environment(void);
201 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);