Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use %zu for type size_t.
[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
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, int argc,
29                                       char **argv,
30                                       void_pfn_smxprocess_t cleanup_func,
31                                       void *data);
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                                       void *data)
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, data);
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
98     /* create and start the process */
99     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
100     * name, but now the name is stored at SIMIX level, so we pass a null  */
101     context->thread =
102       xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
103
104
105     /* wait the starting of the newly created process */
106     xbt_os_sem_acquire(context->end);
107
108   } else {
109     xbt_os_thread_set_extra_data(context);
110   }
111
112   return (smx_context_t) context;
113 }
114
115 static void smx_ctx_thread_free(smx_context_t pcontext)
116 {
117   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
118
119   /* check if this is the context of maestro (it doesn't have a real thread) */
120   if (context->thread) {
121     /* wait about the thread terminason */
122     xbt_os_thread_join(context->thread, NULL);
123
124     /* destroy the synchronisation objects */
125     xbt_os_sem_destroy(context->begin);
126     xbt_os_sem_destroy(context->end);
127   }
128
129   smx_ctx_base_free(pcontext);
130 }
131
132 static void smx_ctx_thread_stop(smx_context_t pcontext)
133 {
134   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
135
136   /* please no debug here: our procdata was already free'd */
137   smx_ctx_base_stop(pcontext);
138
139   if (smx_ctx_thread_sem)       /* parallel run */
140     xbt_os_sem_release(smx_ctx_thread_sem);
141
142   /* signal to the maestro that it has finished */
143   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
144
145   /* exit */
146   /* We should provide return value in case other wants it */
147   xbt_os_thread_exit(NULL);
148 }
149
150 static void *smx_ctx_thread_wrapper(void *param)
151 {
152   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
153
154   /* Tell the maestro we are starting, and wait for its green light */
155   xbt_os_sem_release(context->end);
156   xbt_os_sem_acquire(context->begin);
157   if (smx_ctx_thread_sem)       /* parallel run */
158     xbt_os_sem_acquire(smx_ctx_thread_sem);
159
160   (context->super.code) (context->super.argc, context->super.argv);
161
162   smx_ctx_thread_stop((smx_context_t) context);
163   return NULL;
164 }
165
166 static void smx_ctx_thread_suspend(smx_context_t context)
167 {
168   if (smx_ctx_thread_sem)       /* parallel run */
169     xbt_os_sem_release(smx_ctx_thread_sem);
170   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
171   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
172   if (smx_ctx_thread_sem)       /* parallel run */
173     xbt_os_sem_acquire(smx_ctx_thread_sem);
174 }
175
176 static void smx_ctx_thread_runall_serial(void)
177 {
178   smx_process_t process;
179   unsigned int cursor;
180
181   xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
182     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
183     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
184   }
185 }
186
187 static void smx_ctx_thread_runall_parallel(void)
188 {
189   unsigned int index;
190   smx_process_t process;
191
192   xbt_dynar_foreach(simix_global->process_to_run, index, process)
193     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
194
195   xbt_dynar_foreach(simix_global->process_to_run, index, process) {
196      xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
197   }
198 }
199
200 static smx_context_t smx_ctx_thread_self(void)
201 {
202   return (smx_context_t) xbt_os_thread_get_extra_data();
203 }