Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed some issues in logs logic due to recent changes [Cristian]
[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
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 typedef struct s_smx_ctx_thread {
20   SMX_CTX_BASE_T;
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, char** argv, 
28                                       void_f_pvoid_t cleanup_func, void* cleanup_arg);
29
30 static smx_context_t smx_ctx_thread_factory_create_master_context(void);
31
32 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
33
34 static void smx_ctx_thread_free(smx_context_t context);
35
36 static void smx_ctx_thread_start(smx_context_t context);
37
38 static void smx_ctx_thread_stop(smx_context_t context);
39
40 static void smx_ctx_thread_suspend(smx_context_t context);
41
42 static void 
43   smx_ctx_thread_resume(smx_context_t old_context, smx_context_t new_context);
44
45 static void *smx_ctx_thread_wrapper(void *param);
46
47 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
48 {
49   *factory = xbt_new0(s_smx_context_factory_t, 1);
50
51   (*factory)->create_context = smx_ctx_thread_factory_create_context;
52   (*factory)->finalize = smx_ctx_thread_factory_finalize;
53   (*factory)->create_maestro_context = smx_ctx_thread_factory_create_master_context;
54   (*factory)->free = smx_ctx_thread_free;
55   (*factory)->start = smx_ctx_thread_start;
56   (*factory)->stop = smx_ctx_thread_stop;
57   (*factory)->suspend = smx_ctx_thread_suspend;
58   (*factory)->resume = smx_ctx_thread_resume;
59   (*factory)->name = "ctx_thread_factory";
60 }
61
62 static smx_context_t smx_ctx_thread_factory_create_master_context(void)
63 {
64   return (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
65 }
66
67 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
68 {
69   free(*factory);
70   *factory = NULL;
71   return 0;
72 }
73
74 static smx_context_t 
75 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
76                                       void_f_pvoid_t cleanup_func, void* cleanup_arg)
77 {
78   smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
79
80   context->code = code;
81   context->argc = argc;
82   context->argv = argv;
83   context->cleanup_func = cleanup_func;
84   context->cleanup_arg = cleanup_arg;
85   context->begin = xbt_os_sem_init(0);
86   context->end = xbt_os_sem_init(0);
87
88   return (smx_context_t)context;
89 }
90
91 static void smx_ctx_thread_free(smx_context_t pcontext)
92 {
93   int i;
94   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
95
96   /* check if this is the context of maestro (it doesn't has a real thread) */  
97   if (context->thread) {
98     /* wait about the thread terminason */
99     xbt_os_thread_join(context->thread, NULL);
100     
101     /* destroy the synchronisation objects */
102     xbt_os_sem_destroy(context->begin);
103     xbt_os_sem_destroy(context->end);
104   }
105   
106   /* free argv */
107   if (context->argv) {
108     for (i = 0; i < context->argc; i++)
109       if (context->argv[i])
110         free(context->argv[i]);
111
112     free(context->argv);
113   }
114     
115   /* finally destroy the context */
116   free(context);
117 }
118
119 static void smx_ctx_thread_start(smx_context_t context)
120 {
121   smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
122
123   /* create and start the process */
124   /* NOTE: The first argument to xbt_os_thread_create used to be the process *
125    * name, but now the name is stored at SIMIX level, so we pass a null      */
126   ctx_thread->thread =
127     xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
128
129   /* wait the starting of the newly created process */
130   xbt_os_sem_acquire(ctx_thread->end);
131 }
132
133 static void smx_ctx_thread_stop(smx_context_t pcontext)
134 {
135
136   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
137   
138   /* please no debug here: our procdata was already free'd */
139   if (context->cleanup_func)
140     (*context->cleanup_func) (context->cleanup_arg);
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
158   (context->code) (context->argc, context->argv);
159
160   smx_ctx_thread_stop((smx_context_t)context);
161   return NULL;
162 }
163
164 static void smx_ctx_thread_suspend(smx_context_t context)
165 {
166   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
167   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
168 }
169
170 static void smx_ctx_thread_resume(smx_context_t not_used, 
171                                   smx_context_t new_context)
172 {
173   xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
174   xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
175 }