Logo AND Algorithmique Numérique Distribuée

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