Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix another race in log initializations.
[simgrid.git] / src / simix / smx_context_thread.c
1 /* context_thread - implementation of context switching with native threads */
2
3 /* Copyright (c) 2009, 2010. 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 #include "simix/context.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 typedef struct s_smx_ctx_thread {
20   s_smx_ctx_base_t super;       /* Fields of super implementation */
21   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
22   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
23   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
24 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
25
26 static xbt_os_sem_t smx_ctx_thread_sem;
27
28 static smx_context_t
29 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
30                                       char **argv,
31                                       void_pfn_smxprocess_t cleanup_func,
32                                       void *data);
33
34 static void smx_ctx_thread_free(smx_context_t context);
35 static void smx_ctx_thread_stop(smx_context_t context);
36 static void smx_ctx_thread_suspend(smx_context_t context);
37 static void smx_ctx_thread_runall_serial(void);
38 static void smx_ctx_thread_runall_parallel(void);
39 static smx_context_t smx_ctx_thread_self(void);
40
41 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory);
42 static void *smx_ctx_thread_wrapper(void *param);
43
44 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
45 {
46   smx_ctx_base_factory_init(factory);
47   XBT_VERB("Activating thread context factory");
48
49   (*factory)->finalize  = smx_ctx_thread_factory_finalize;
50   (*factory)->create_context = smx_ctx_thread_factory_create_context;
51   /* Do not overload that method (*factory)->finalize */
52   (*factory)->free = smx_ctx_thread_free;
53   (*factory)->stop = smx_ctx_thread_stop;
54   (*factory)->suspend = smx_ctx_thread_suspend;
55
56   if (SIMIX_context_is_parallel())
57     (*factory)->runall = smx_ctx_thread_runall_parallel;
58   else
59     (*factory)->runall = smx_ctx_thread_runall_serial;
60
61   (*factory)->self = smx_ctx_thread_self;
62   (*factory)->name = "ctx_thread_factory";
63
64   if (SIMIX_context_is_parallel()) {
65     smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
66   } else {
67     smx_ctx_thread_sem = NULL;
68   }
69 }
70
71 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory)
72 {
73   if (smx_ctx_thread_sem) {
74     xbt_os_sem_destroy(smx_ctx_thread_sem);
75     smx_ctx_thread_sem = NULL;
76   }
77   return smx_ctx_base_factory_finalize(factory);
78 }
79
80 static smx_context_t
81 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
82                                       char **argv,
83                                       void_pfn_smxprocess_t cleanup_func,
84                                       void *data)
85 {
86   smx_ctx_thread_t context = (smx_ctx_thread_t)
87       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
88                                                 code, argc, argv,
89                                                 cleanup_func, data);
90
91   /* If the user provided a function for the process then use it
92      otherwise is the context for maestro */
93   if (code) {
94     context->begin = xbt_os_sem_init(0);
95     context->end = xbt_os_sem_init(0);
96     /* create and start the process */
97     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
98     * name, but now the name is stored at SIMIX level, so we pass a null  */
99     context->thread =
100       xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
101
102     /* wait the starting of the newly created process */
103     xbt_os_sem_acquire(context->end);
104
105   } else {
106     xbt_os_thread_set_extra_data(context);
107   }
108
109   return (smx_context_t) context;
110 }
111
112 static void smx_ctx_thread_free(smx_context_t pcontext)
113 {
114   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
115
116   /* check if this is the context of maestro (it doesn't has a real thread) */
117   if (context->thread) {
118     /* wait about the thread terminason */
119     xbt_os_thread_join(context->thread, NULL);
120
121     /* destroy the synchronisation objects */
122     xbt_os_sem_destroy(context->begin);
123     xbt_os_sem_destroy(context->end);
124   }
125
126   smx_ctx_base_free(pcontext);
127 }
128
129 static void smx_ctx_thread_stop(smx_context_t pcontext)
130 {
131   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
132
133   /* please no debug here: our procdata was already free'd */
134   smx_ctx_base_stop(pcontext);
135
136   if (smx_ctx_thread_sem)       /* parallel run */
137     xbt_os_sem_release(smx_ctx_thread_sem);
138
139   /* signal to the maestro that it has finished */
140   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
141
142   /* exit */
143   /* We should provide return value in case other wants it */
144   xbt_os_thread_exit(NULL);
145 }
146
147 static void *smx_ctx_thread_wrapper(void *param)
148 {
149   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
150
151   /* Tell the maestro we are starting, and wait for its green light */
152   xbt_os_sem_release(context->end);
153   xbt_os_sem_acquire(context->begin);
154   if (smx_ctx_thread_sem)       /* parallel run */
155     xbt_os_sem_acquire(smx_ctx_thread_sem);
156
157   (context->super.code) (context->super.argc, context->super.argv);
158
159   smx_ctx_thread_stop((smx_context_t) context);
160   return NULL;
161 }
162
163 static void smx_ctx_thread_suspend(smx_context_t context)
164 {
165   if (smx_ctx_thread_sem)       /* parallel run */
166     xbt_os_sem_release(smx_ctx_thread_sem);
167   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
168   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
169   if (smx_ctx_thread_sem)       /* parallel run */
170     xbt_os_sem_acquire(smx_ctx_thread_sem);
171 }
172
173 static void smx_ctx_thread_runall_serial(void)
174 {
175   smx_process_t process;
176   unsigned int cursor;
177
178   xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
179     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
180     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
181   }
182 }
183
184 static void smx_ctx_thread_runall_parallel(void)
185 {
186   unsigned int index;
187   smx_process_t process;
188
189   xbt_dynar_foreach(simix_global->process_to_run, index, process)
190     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
191
192   xbt_dynar_foreach(simix_global->process_to_run, index, process) {
193      xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
194   }
195 }
196
197 static smx_context_t smx_ctx_thread_self(void)
198 {
199   return (smx_context_t) xbt_os_thread_get_extra_data();
200 }