Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compile fix: pthread should respect the context factory interface
[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 "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/smx_context_private.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 smx_context_t
27 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
28                                       char **argv,
29                                       void_pfn_smxprocess_t cleanup_func,
30                                       void *data);
31
32 static void smx_ctx_thread_free(smx_context_t context);
33 static void smx_ctx_thread_stop(smx_context_t context);
34 static void smx_ctx_thread_suspend(smx_context_t context);
35 static void smx_ctx_thread_resume(smx_context_t new_context);
36 static void smx_ctx_thread_runall_serial(xbt_swag_t processes);
37 static void smx_ctx_thread_runall_parallel(xbt_swag_t processes);
38 static smx_context_t smx_ctx_thread_self(void);
39
40 static void *smx_ctx_thread_wrapper(void *param);
41
42 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
43 {
44   smx_ctx_base_factory_init(factory);
45
46   (*factory)->create_context = smx_ctx_thread_factory_create_context;
47   /* Do not overload that method (*factory)->finalize */
48   (*factory)->free = smx_ctx_thread_free;
49   (*factory)->stop = smx_ctx_thread_stop;
50   (*factory)->suspend = smx_ctx_thread_suspend;
51
52   if(_surf_parallel_contexts)
53     (*factory)->runall = smx_ctx_thread_runall_parallel;
54   else
55     (*factory)->runall = smx_ctx_thread_runall_serial;
56
57   (*factory)->self = smx_ctx_thread_self;
58   (*factory)->name = "ctx_thread_factory";
59 }
60
61 static smx_context_t
62 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
63                                       char **argv,
64                                       void_pfn_smxprocess_t cleanup_func,
65                                       void *data)
66 {
67   smx_ctx_thread_t context = (smx_ctx_thread_t)
68       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
69                                                 code, argc, argv,
70                                                 cleanup_func, data);
71
72   /* If the user provided a function for the process then use it
73      otherwise is the context for maestro */
74   if (code) {
75     context->begin = xbt_os_sem_init(0);
76     context->end = xbt_os_sem_init(0);
77
78     /* delay the thread creation until first run */
79     context->thread = NULL;
80
81   } else {
82     xbt_os_thread_set_extra_data(context);
83   }
84
85   return (smx_context_t) context;
86 }
87
88 static void smx_ctx_thread_free(smx_context_t pcontext)
89 {
90   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
91
92   /* check if the context has a thread or not */
93   /* if it doesn't, it is maestro's context or the context never run */
94   if (context->thread) {
95     /* wait about the thread terminason */
96     xbt_os_thread_join(context->thread, NULL);
97   }
98
99   /* destroy the synchronisation objects */
100   if (context->begin)
101     xbt_os_sem_destroy(context->begin);
102
103   if (context->end)
104     xbt_os_sem_destroy(context->end);
105
106   smx_ctx_base_free(pcontext);
107 }
108
109 static void smx_ctx_thread_stop(smx_context_t pcontext)
110 {
111   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
112
113   /* please no debug here: our procdata was already free'd */
114   smx_ctx_base_stop(pcontext);
115
116   /* signal to the maestro that it has finished */
117   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
118
119   /* exit */
120   /* We should provide return value in case other wants it */
121   xbt_os_thread_exit(NULL);
122 }
123
124 static void *smx_ctx_thread_wrapper(void *param)
125 {
126   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
127
128   /* Tell the maestro we are starting, and wait for its green light */
129   xbt_os_sem_release(context->end);
130   xbt_os_sem_acquire(context->begin);
131
132   (context->super.code) (context->super.argc, context->super.argv);
133
134   smx_ctx_thread_stop((smx_context_t) context);
135   return NULL;
136 }
137
138 static void smx_ctx_thread_suspend(smx_context_t context)
139 {
140   if (((smx_ctx_thread_t) context)->thread) {
141     xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
142     xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
143   }
144 }
145
146 static void smx_ctx_thread_runall_serial(xbt_swag_t processes)
147 {
148   smx_process_t process;
149   while ((process = xbt_swag_extract(processes))) {
150     /* if the context has no thread associated, create one for it (first run) */
151     if (!(((smx_ctx_thread_t)process->context)->thread)) {
152       ((smx_ctx_thread_t) process->context)->thread =
153         xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, process->context, process->context);
154       xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
155     }
156     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
157     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
158   }
159 }
160
161 static void smx_ctx_thread_runall_parallel(xbt_swag_t processes)
162 {
163   smx_process_t process, p_next;
164   xbt_swag_foreach_safe(process, p_next, processes) {
165     /* if the context has no thread associated, create one for it (first run) */
166     if (!(((smx_ctx_thread_t) process->context)->thread)) {
167       ((smx_ctx_thread_t)process->context)->thread =
168         xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, process->context, process->context);
169       xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
170     }
171     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
172   }
173
174   while ((process = xbt_swag_extract(processes))) {
175     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
176   }
177 }
178
179 static smx_context_t smx_ctx_thread_self(void)
180 {
181   return (smx_context_t) xbt_os_thread_get_extra_data();
182 }