Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7f6c139575b7d86d0151d402a18046eba8098811
[simgrid.git] / src / simix / smx_context_thread.c
1 /* $Id$ */
2
3 /* context_thread - implementation of context switching with native threads */
4
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/function_types.h"
11 #include "private.h"
12
13 #include "portable.h"           /* loads context system definitions */
14 #include "xbt/swag.h"
15 #include "xbt/xbt_os_thread.h"
16 #include "xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 typedef struct s_smx_ctx_thread {
21   SMX_CTX_BASE_T;
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, char** argv, 
29                                       void_f_pvoid_t cleanup_func, void* cleanup_arg);
30
31 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
32
33 static void smx_ctx_thread_free(smx_context_t context);
34
35 static void smx_ctx_thread_start(smx_context_t context);
36
37 static void smx_ctx_thread_stop(smx_context_t context);
38
39 static void smx_ctx_thread_suspend(smx_context_t context);
40
41 static void 
42   smx_ctx_thread_resume(smx_context_t old_context, smx_context_t new_context);
43
44 static void *smx_ctx_thread_wrapper(void *param);
45
46 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
47 {
48   /* Initialize the thread portability layer 
49   xbt_os_thread_mod_init();*/
50   
51   *factory = xbt_new0(s_smx_context_factory_t, 1);
52
53   (*factory)->create_context = smx_ctx_thread_factory_create_context;
54   (*factory)->finalize = smx_ctx_thread_factory_finalize;
55   (*factory)->free = smx_ctx_thread_free;
56   (*factory)->start = smx_ctx_thread_start;
57   (*factory)->stop = smx_ctx_thread_stop;
58   (*factory)->suspend = smx_ctx_thread_suspend;
59   (*factory)->resume = smx_ctx_thread_resume;
60   (*factory)->name = "ctx_thread_factory";
61 }
62
63 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
64 {
65   /* Stop the thread portability layer 
66   xbt_os_thread_mod_exit();*/
67   free(*factory);
68   *factory = NULL;
69   return 0;
70 }
71
72 static smx_context_t 
73 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
74                                       void_f_pvoid_t cleanup_func, void* cleanup_arg)
75 {
76   smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
77
78   /* If the user provided a function for the process then use it
79      otherwise is the context for maestro */
80   if(code){
81     context->code = code;
82     context->argc = argc;
83     context->argv = argv;
84     context->cleanup_func = cleanup_func;
85     context->cleanup_arg = cleanup_arg;
86     context->begin = xbt_os_sem_init(0);
87     context->end = xbt_os_sem_init(0);
88   }
89     
90   return (smx_context_t)context;
91 }
92
93 static void smx_ctx_thread_free(smx_context_t pcontext)
94 {
95   int i;
96   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
97
98   /* check if this is the context of maestro (it doesn't has a real thread) */  
99   if (context->thread) {
100     /* wait about the thread terminason */
101     xbt_os_thread_join(context->thread, NULL);
102     
103     /* destroy the synchronisation objects */
104     xbt_os_sem_destroy(context->begin);
105     xbt_os_sem_destroy(context->end);
106   }
107   
108   /* free argv */
109   if (context->argv) {
110     for (i = 0; i < context->argc; i++)
111       if (context->argv[i])
112         free(context->argv[i]);
113
114     free(context->argv);
115   }
116     
117   /* finally destroy the context */
118   free(context);
119 }
120
121 static void smx_ctx_thread_start(smx_context_t context)
122 {
123   smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
124
125   /* create and start the process */
126   /* NOTE: The first argument to xbt_os_thread_create used to be the process *
127    * name, but now the name is stored at SIMIX level, so we pass a null      */
128   ctx_thread->thread =
129     xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
130
131   /* wait the starting of the newly created process */
132   xbt_os_sem_acquire(ctx_thread->end);
133 }
134
135 static void smx_ctx_thread_stop(smx_context_t pcontext)
136 {
137
138   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
139   
140   /* please no debug here: our procdata was already free'd */
141   if (context->cleanup_func)
142     (*context->cleanup_func) (context->cleanup_arg);
143
144   /* signal to the maestro that it has finished */
145   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
146
147   /* exit */
148   /* We should provide return value in case other wants it */
149   xbt_os_thread_exit(NULL);     
150 }
151
152 static void *smx_ctx_thread_wrapper(void *param)
153 {
154   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
155
156   /* Tell the maestro we are starting, and wait for its green light */
157   xbt_os_sem_release(context->end);
158   xbt_os_sem_acquire(context->begin);
159
160   (context->code) (context->argc, context->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   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
169   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
170 }
171
172 static void smx_ctx_thread_resume(smx_context_t not_used, 
173                                   smx_context_t new_context)
174 {
175   xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
176   xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
177 }