Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix some compilation erros
[simgrid.git] / src / simix / smx_context_thread.c
1 /* context_thread - implementation of context switching with native threads */
2
3 /* Copyright (c) 2009-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/function_types.h"
10 #include "smx_private.h"
11 #include "portable.h"           /* loads context system definitions */
12 #include "xbt/swag.h"
13 #include "xbt/xbt_os_thread.h"
14 #include "xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
17
18 typedef struct s_smx_ctx_thread {
19   s_smx_ctx_base_t super;       /* Fields of super implementation */
20   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
21   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
22   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
23 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
24
25 static xbt_os_sem_t smx_ctx_thread_sem;
26
27 static smx_context_t
28 smx_ctx_thread_factory_create_context(xbt_main_func_t code,
29                                       int argc, char **argv,
30                                       void_pfn_smxprocess_t cleanup_func,
31                                       smx_process_t process);
32
33 static void smx_ctx_thread_free(smx_context_t context);
34 static void smx_ctx_thread_stop(smx_context_t context);
35 static void smx_ctx_thread_suspend(smx_context_t context);
36 static void smx_ctx_thread_runall_serial(void);
37 static void smx_ctx_thread_runall_parallel(void);
38 static smx_context_t smx_ctx_thread_self(void);
39
40 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory);
41 static void *smx_ctx_thread_wrapper(void *param);
42
43 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
44 {
45   smx_ctx_base_factory_init(factory);
46   XBT_VERB("Activating thread context factory");
47
48   (*factory)->finalize  = smx_ctx_thread_factory_finalize;
49   (*factory)->create_context = smx_ctx_thread_factory_create_context;
50   /* Do not overload that method (*factory)->finalize */
51   (*factory)->free = smx_ctx_thread_free;
52   (*factory)->stop = smx_ctx_thread_stop;
53   (*factory)->suspend = smx_ctx_thread_suspend;
54
55   if (SIMIX_context_is_parallel())
56     (*factory)->runall = smx_ctx_thread_runall_parallel;
57   else
58     (*factory)->runall = smx_ctx_thread_runall_serial;
59
60   (*factory)->self = smx_ctx_thread_self;
61   (*factory)->name = "ctx_thread_factory";
62
63   if (SIMIX_context_is_parallel()) {
64     smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
65   } else {
66     smx_ctx_thread_sem = NULL;
67   }
68 }
69
70 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory)
71 {
72   if (smx_ctx_thread_sem) {
73     xbt_os_sem_destroy(smx_ctx_thread_sem);
74     smx_ctx_thread_sem = NULL;
75   }
76   return smx_ctx_base_factory_finalize(factory);
77 }
78
79 static smx_context_t
80 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
81                                       char **argv,
82                                       void_pfn_smxprocess_t cleanup_func,
83                                       smx_process_t process)
84 {
85   smx_ctx_thread_t context = (smx_ctx_thread_t)
86       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
87                                                 code, argc, argv,
88                                                 cleanup_func, process);
89
90   /* If the user provided a function for the process then use it
91      otherwise is the context for maestro */
92   if (code) {
93     context->begin = xbt_os_sem_init(0);
94     context->end = xbt_os_sem_init(0);
95     if (smx_context_stack_size_was_set)
96       xbt_os_thread_setstacksize(smx_context_stack_size);
97     if (smx_context_guard_size_was_set)
98       xbt_os_thread_setguardsize(smx_context_guard_size);
99
100     /* create and start the process */
101     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
102     * name, but now the name is stored at SIMIX level, so we pass a null  */
103     context->thread =
104       xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
105
106
107     /* wait the starting of the newly created process */
108     xbt_os_sem_acquire(context->end);
109
110   } else {
111     xbt_os_thread_set_extra_data(context);
112   }
113
114   return (smx_context_t) context;
115 }
116
117 static void smx_ctx_thread_free(smx_context_t pcontext)
118 {
119   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
120
121   /* check if this is the context of maestro (it doesn't have a real thread) */
122   if (context->thread) {
123     /* wait about the thread terminason */
124     xbt_os_thread_join(context->thread, NULL);
125
126     /* destroy the synchronisation objects */
127     xbt_os_sem_destroy(context->begin);
128     xbt_os_sem_destroy(context->end);
129   }
130
131   smx_ctx_base_free(pcontext);
132 }
133
134 static void smx_ctx_thread_stop(smx_context_t pcontext)
135 {
136   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
137
138   /* please no debug here: our procdata was already free'd */
139   smx_ctx_base_stop(pcontext);
140
141   if (smx_ctx_thread_sem)       /* parallel run */
142     xbt_os_sem_release(smx_ctx_thread_sem);
143
144   /* signal to the maestro that it has finished */
145   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
146
147   /* exit */
148   /* We should provide return value in case other wants it */
149   xbt_os_thread_exit(NULL);
150 }
151
152 static void *smx_ctx_thread_wrapper(void *param)
153 {
154   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
155 #ifndef WIN32
156   /* Install alternate signal stack, for SIGSEGV handler. */
157   stack_t stack;
158   stack.ss_sp = sigsegv_stack;
159   stack.ss_size = sizeof sigsegv_stack;
160   stack.ss_flags = 0;
161   sigaltstack(&stack, NULL);
162 #endif
163   /* Tell the maestro we are starting, and wait for its green light */
164   xbt_os_sem_release(context->end);
165   xbt_os_sem_acquire(context->begin);
166   if (smx_ctx_thread_sem)       /* parallel run */
167     xbt_os_sem_acquire(smx_ctx_thread_sem);
168
169   (context->super.code) (context->super.argc, context->super.argv);
170
171   smx_ctx_thread_stop((smx_context_t) context);
172   return NULL;
173 }
174
175 static void smx_ctx_thread_suspend(smx_context_t context)
176 {
177   if (smx_ctx_thread_sem)       /* parallel run */
178     xbt_os_sem_release(smx_ctx_thread_sem);
179   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
180   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
181   if (smx_ctx_thread_sem)       /* parallel run */
182     xbt_os_sem_acquire(smx_ctx_thread_sem);
183 }
184
185 static void smx_ctx_thread_runall_serial(void)
186 {
187   smx_process_t process;
188   unsigned int cursor;
189
190   xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
191     XBT_DEBUG("Handling %p",process);
192     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
193     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
194   }
195 }
196
197 static void smx_ctx_thread_runall_parallel(void)
198 {
199   unsigned int index;
200   smx_process_t process;
201
202   xbt_dynar_foreach(simix_global->process_to_run, index, process)
203     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
204
205   xbt_dynar_foreach(simix_global->process_to_run, index, process) {
206      xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
207   }
208 }
209
210 static smx_context_t smx_ctx_thread_self(void)
211 {
212   return (smx_context_t) xbt_os_thread_get_extra_data();
213 }