Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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
12 #include "portable.h"           /* loads context system definitions */
13 #include "xbt/swag.h"
14 #include "xbt/xbt_os_thread.h"
15 #include "xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
16 #include "simix/smx_context_private.h"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 typedef struct s_smx_ctx_thread {
21   s_smx_ctx_base_t super;       /* Fields of super implementation */
22   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
23   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
24   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
25 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
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                                       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_resume(smx_context_t new_context);
37 static void smx_ctx_thread_runall_serial(xbt_swag_t processes);
38 static void smx_ctx_thread_runall_parallel(xbt_swag_t processes);
39 static smx_process_t smx_ctx_thread_self(void);
40
41 static void *smx_ctx_thread_wrapper(void *param);
42
43 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
44 {
45
46   smx_ctx_base_factory_init(factory);
47
48   (*factory)->create_context = smx_ctx_thread_factory_create_context;
49   /* Do not overload that method (*factory)->finalize */
50   (*factory)->free = smx_ctx_thread_free;
51   (*factory)->stop = smx_ctx_thread_stop;
52   (*factory)->suspend = smx_ctx_thread_suspend;
53
54   if(_surf_parallel_contexts)
55     (*factory)->runall = smx_ctx_thread_runall_parallel;
56   else
57     (*factory)->runall = smx_ctx_thread_runall_serial;
58   
59   (*factory)->self = smx_ctx_thread_self;
60   (*factory)->name = "ctx_thread_factory";
61 }
62
63 static smx_context_t
64 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
65                                       char **argv,
66                                       void_pfn_smxprocess_t cleanup_func,
67                                       smx_process_t process)
68 {
69   smx_ctx_thread_t context = (smx_ctx_thread_t)
70       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
71                                                 code, argc, argv,
72                                                 cleanup_func, process);
73
74   /* If the user provided a function for the process then use it
75      otherwise is the context for maestro */
76   if (code) {
77     context->begin = xbt_os_sem_init(0);
78     context->end = xbt_os_sem_init(0);
79
80
81     /* delay the thread creation until first run */
82     context->thread = NULL;
83  
84   } else {
85     xbt_os_thread_set_extra_data(process);
86   }
87
88   return (smx_context_t) context;
89 }
90
91 static void smx_ctx_thread_free(smx_context_t pcontext)
92 {
93   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
94
95   /* check if the context has a thread or not */
96   /* if it doesn't, it is maestro's context or the context never run */
97   if (context->thread) {
98     /* wait about the thread terminason */
99     xbt_os_thread_join(context->thread, NULL);
100   }
101   
102   /* destroy the synchronisation objects */
103   if(context->begin)
104     xbt_os_sem_destroy(context->begin);
105
106   if(context->end)
107     xbt_os_sem_destroy(context->end);
108   
109   smx_ctx_base_free(pcontext);
110 }
111
112 static void smx_ctx_thread_stop(smx_context_t pcontext)
113 {
114
115   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
116
117   /* please no debug here: our procdata was already free'd */
118   smx_ctx_base_stop(pcontext);
119
120   /* signal to the maestro that it has finished */
121   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
122
123   /* exit */
124   /* We should provide return value in case other wants it */
125   xbt_os_thread_exit(NULL);
126 }
127
128 static void *smx_ctx_thread_wrapper(void *param)
129 {
130   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
131
132   /* Tell the maestro we are starting, and wait for its green light */
133   xbt_os_sem_release(context->end);
134   xbt_os_sem_acquire(context->begin);
135
136   (context->super.code) (context->super.argc, context->super.argv);
137
138   smx_ctx_thread_stop((smx_context_t) context);
139   return NULL;
140 }
141
142 static void smx_ctx_thread_suspend(smx_context_t context)
143 {
144   if(((smx_ctx_thread_t) context)->thread){
145     xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
146     xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
147   }
148 }
149
150 static void smx_ctx_thread_runall_serial(xbt_swag_t processes)
151 {
152   smx_process_t process;
153   while((process = xbt_swag_extract(processes))){
154     /* if the context has no thread associated, create one for it (first run) */
155     if(!(((smx_ctx_thread_t)process->context)->thread)){
156       ((smx_ctx_thread_t)process->context)->thread =
157         xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, process->context, process);
158       xbt_os_sem_acquire(((smx_ctx_thread_t)process->context)->end);
159     }
160     xbt_os_sem_release(((smx_ctx_thread_t) (process->context))->begin);
161     xbt_os_sem_acquire(((smx_ctx_thread_t) (process->context))->end);
162   }  
163 }
164
165 static void smx_ctx_thread_runall_parallel(xbt_swag_t processes){
166   smx_process_t process, p_next;
167   xbt_swag_foreach_safe(process, p_next, processes){
168     /* if the context has no thread associated, create one for it (first run) */
169     if(!(((smx_ctx_thread_t)process->context)->thread)){
170       ((smx_ctx_thread_t)process->context)->thread =
171         xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, process->context, process);
172       xbt_os_sem_acquire(((smx_ctx_thread_t)process->context)->end);
173     }
174     xbt_os_sem_release(((smx_ctx_thread_t) (process->context))->begin);
175   }
176
177   while((process = xbt_swag_extract(processes))){
178     xbt_os_sem_acquire(((smx_ctx_thread_t) (process->context))->end);
179   }
180 }
181
182 static smx_process_t smx_ctx_thread_self(void)
183 {
184   return (smx_process_t) xbt_os_thread_get_extra_data();
185 }