Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / src / simix / smx_context_boost.cpp
1 /* Copyright (c) 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 /** @file smx_context_boost.cpp Userspace context switching implementation based on Boost.Context */
8
9 #include <cstdint>
10
11 #include <boost/context/all.hpp>
12
13 #include "xbt/log.h"
14 #include "smx_private.h"
15 #include "internal_config.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 typedef struct s_smx_ctx_boost {
20   s_smx_ctx_base_t super;       /* Fields of super implementation */
21 #if HAVE_BOOST_CONTEXT == 1
22   boost::context::fcontext_t* fc;
23 #else
24   boost::context::fcontext_t fc;
25 #endif
26   void* stack;
27 } s_smx_ctx_boost_t, *smx_ctx_boost_t;
28
29 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory);
30 static smx_context_t
31 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
32     void_pfn_smxprocess_t cleanup_func, smx_process_t process);
33 static void smx_ctx_boost_free(smx_context_t context);
34
35 static void smx_ctx_boost_wrapper(std::intptr_t arg);
36
37 static void smx_ctx_boost_stop_serial(smx_context_t context);
38 static void smx_ctx_boost_suspend_serial(smx_context_t context);
39 static void smx_ctx_boost_resume_serial(smx_process_t first_process);
40 static void smx_ctx_boost_runall_serial(void);
41
42 #ifdef CONTEXT_THREADS
43 static void smx_ctx_boost_stop_parallel(smx_context_t context);
44 static void smx_ctx_boost_suspend_parallel(smx_context_t context);
45 static void smx_ctx_boost_resume_parallel(smx_process_t first_process);
46 static void smx_ctx_boost_runall_parallel(void);
47 #endif
48
49 #ifdef CONTEXT_THREADS
50 static xbt_parmap_t boost_parmap;
51 static smx_ctx_boost_t* boost_workers_context;
52 static unsigned long boost_threads_working;
53 static xbt_os_thread_key_t boost_worker_id_key;
54 #endif
55
56 static unsigned long boost_process_index = 0;
57 static smx_ctx_boost_t boost_maestro_context;
58
59 void SIMIX_ctx_boost_factory_init(smx_context_factory_t *factory)
60 {
61   smx_ctx_base_factory_init(factory);
62   XBT_VERB("Activating boost context factory");
63
64   (*factory)->finalize = smx_ctx_boost_factory_finalize;
65   (*factory)->create_context = smx_ctx_boost_create_context;
66   /* Do not overload that method (*factory)->finalize */
67   (*factory)->free = smx_ctx_boost_free;
68   (*factory)->name = "smx_boost_context_factory";
69
70   if (SIMIX_context_is_parallel()) {
71 #ifndef CONTEXT_THREADS
72     THROWF(arg_error, 0, "No thread support for parallel context execution");
73 #else
74     int nthreads = SIMIX_context_get_nthreads();
75     boost_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
76     boost_workers_context = xbt_new(smx_ctx_boost_t, nthreads);
77     boost_maestro_context = NULL;
78
79     xbt_os_thread_key_create(&boost_worker_id_key);
80
81     (*factory)->stop = smx_ctx_boost_stop_parallel;
82     (*factory)->suspend = smx_ctx_boost_suspend_parallel;
83     (*factory)->runall = smx_ctx_boost_runall_parallel;
84 #endif
85   } else {
86     (*factory)->stop = smx_ctx_boost_stop_serial;
87     (*factory)->suspend = smx_ctx_boost_suspend_serial;
88     (*factory)->runall = smx_ctx_boost_runall_serial;
89   }
90 }
91
92 /* Initialization functions */
93
94 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory)
95 {
96 #ifdef CONTEXT_THREADS
97   if (boost_parmap) {
98     xbt_parmap_destroy(boost_parmap);
99     boost_parmap = nullptr;
100   }
101   xbt_free(boost_workers_context);
102   boost_workers_context = nullptr;
103 #endif
104   return smx_ctx_base_factory_finalize(factory);
105 }
106
107 static smx_context_t
108 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
109     void_pfn_smxprocess_t cleanup_func, smx_process_t process)
110 {
111   smx_ctx_boost_t context =
112       (smx_ctx_boost_t) smx_ctx_base_factory_create_context_sized(
113           sizeof(s_smx_ctx_boost_t),
114           code,
115           argc,
116           argv,
117           cleanup_func,
118           process);
119
120   /* if the user provided a function for the process then use it,
121      otherwise it is the context for maestro */
122   if (code) {
123     context->stack = SIMIX_context_stack_new();
124     // We need to pass the bottom of the stack to make_fcontext,
125     // depending on the stack direction it may be the lower or higher address:
126 #if PTH_STACKGROWTH == -1
127     void* stack = (char*) context->stack + smx_context_usable_stack_size - 1;
128 #else
129     void* stack = context->stack;
130 #endif
131     context->fc = boost::context::make_fcontext(
132                       stack,
133                       smx_context_usable_stack_size,
134                       smx_ctx_boost_wrapper);
135   } else {
136     context->stack = nullptr;
137 #if HAVE_BOOST_CONTEXT == 1
138     context->fc = new boost::context::fcontext_t();
139 #endif
140     if (boost_maestro_context == nullptr)
141       boost_maestro_context = context;
142   }
143
144   return (smx_context_t) context;
145 }
146
147 static void smx_ctx_boost_free(smx_context_t c)
148 {
149   smx_ctx_boost_t context = (smx_ctx_boost_t) c;
150   if (!context)
151     return;
152 #if HAVE_BOOST_CONTEXT == 1
153   if (!context->stack)
154     delete context->fc;
155 #endif
156   if ((smx_ctx_boost_t) c == boost_maestro_context)
157     boost_maestro_context = nullptr;
158   SIMIX_context_stack_delete(context->stack);
159   smx_ctx_base_free(c);
160 }
161
162 static void smx_ctx_boost_wrapper(std::intptr_t arg)
163 {
164   smx_context_t context = (smx_context_t) arg;
165   context->code(context->argc, context->argv);
166   smx_ctx_boost_stop_serial(context);
167 }
168
169 static void smx_ctx_boost_stop_serial(smx_context_t context)
170 {
171   smx_ctx_base_stop(context);
172   simix_global->context_factory->suspend(context);
173 }
174
175 static void smx_ctx_boost_suspend_serial(smx_context_t context)
176 {
177   /* determine the next context */
178   smx_ctx_boost_t next_context;
179   unsigned long int i = boost_process_index++;
180
181   if (i < xbt_dynar_length(simix_global->process_to_run)) {
182     /* execute the next process */
183     XBT_DEBUG("Run next process");
184     next_context = (smx_ctx_boost_t) xbt_dynar_get_as(
185         simix_global->process_to_run, i, smx_process_t)->context;
186   }
187   else {
188     /* all processes were run, return to maestro */
189     XBT_DEBUG("No more process to run");
190     next_context = (smx_ctx_boost_t) boost_maestro_context;
191   }
192   SIMIX_context_set_current((smx_context_t) next_context);
193 #if HAVE_BOOST_CONTEXT == 1
194   boost::context::jump_fcontext(
195     ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
196 #else
197   boost::context::jump_fcontext(
198     &((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
199 #endif
200 }
201
202 static void smx_ctx_boost_resume_serial(smx_process_t first_process)
203 {
204   smx_ctx_boost_t context = (smx_ctx_boost_t) first_process->context;
205   SIMIX_context_set_current((smx_context_t) context);
206 #if HAVE_BOOST_CONTEXT == 1
207   boost::context::jump_fcontext(boost_maestro_context->fc, context->fc,
208     (intptr_t)context);
209 #else
210   boost::context::jump_fcontext(&boost_maestro_context->fc, context->fc,
211     (intptr_t)context);
212 #endif
213 }
214
215 static void smx_ctx_boost_runall_serial(void)
216 {
217   smx_process_t first_process =
218       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
219   boost_process_index = 1;
220
221   /* execute the first process */
222   smx_ctx_boost_resume_serial(first_process);
223 }
224
225 // **** Parallel code
226
227 #ifdef CONTEXT_THREADS
228
229 static void smx_ctx_boost_stop_parallel(smx_context_t context)
230 {
231   smx_ctx_base_stop(context);
232   smx_ctx_boost_suspend_parallel(context);
233 }
234
235 static void smx_ctx_boost_suspend_parallel(smx_context_t context)
236 {
237   smx_process_t next_work = (smx_process_t) xbt_parmap_next(boost_parmap);
238   smx_ctx_boost_t next_context;
239
240   if (next_work != NULL) {
241     XBT_DEBUG("Run next process");
242     next_context = (smx_ctx_boost_t) next_work->context;
243   }
244   else {
245     XBT_DEBUG("No more processes to run");
246     unsigned long worker_id =
247         (unsigned long) xbt_os_thread_get_specific(boost_worker_id_key);
248     next_context = boost_workers_context[worker_id];
249   }
250
251   SIMIX_context_set_current((smx_context_t) next_context);
252 #if HAVE_BOOST_CONTEXT == 1
253   boost::context::jump_fcontext(
254     ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
255 #else
256   boost::context::jump_fcontext(
257     &((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
258 #endif
259 }
260
261 static void smx_ctx_boost_resume_parallel(smx_process_t process)
262 {
263   unsigned long worker_id = __sync_fetch_and_add(&boost_threads_working, 1);
264   xbt_os_thread_set_specific(boost_worker_id_key, (void*) worker_id);
265
266   smx_ctx_boost_t worker_context = (smx_ctx_boost_t)SIMIX_context_self();
267   boost_workers_context[worker_id] = worker_context;
268   smx_ctx_boost_t context = (smx_ctx_boost_t) process->context;
269
270   SIMIX_context_set_current((smx_context_t) context);
271 #if HAVE_BOOST_CONTEXT == 1
272   boost::context::jump_fcontext(worker_context->fc, context->fc,
273     (intptr_t)context);
274 #else
275   boost::context::jump_fcontext(&worker_context->fc, context->fc,
276     (intptr_t)context);
277 #endif
278 }
279
280 static void smx_ctx_boost_runall_parallel(void)
281 {
282   boost_threads_working = 0;
283   xbt_parmap_apply(boost_parmap, (void_f_pvoid_t) smx_ctx_boost_resume_parallel,
284     simix_global->process_to_run);
285 }
286
287 #endif