Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix: correct trace mask checking
[simgrid.git] / src / xbt / xbt_sg_synchro.c
1 /* $Id$ */
2
3 /* xbt_synchro -- Synchronization virtualized depending on whether we are   */
4 /*                in simulation or real life (act on simulated processes)   */
5
6 /* This is the simulation implementation, using simix.                      */
7
8 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
9  * All right reserved.                                                      */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14 #include "xbt/ex.h"
15
16 #include "xbt/synchro.h"        /* This module */
17
18 #include "simix/simix.h"        /* used implementation */
19 #include "simix/datatypes.h"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
22                                 "Synchronization mechanism");
23
24 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
25
26 typedef struct s_xbt_thread_ {
27   char *name;
28   smx_process_t s_process;
29   void_f_pvoid_t code;
30   void *userparam;
31   void *father_data;
32   /* stuff to allow other people to wait on me with xbt_thread_join */
33   int joinable:1,done:1;
34   xbt_cond_t cond;
35   xbt_mutex_t mutex;
36 } s_xbt_thread_t;
37
38 static int xbt_thread_create_wrapper(int argc, char *argv[])
39 {
40   xbt_thread_t t =
41     (xbt_thread_t) SIMIX_process_get_data(SIMIX_process_self());
42   SIMIX_process_set_data(SIMIX_process_self(), t->father_data);
43   (*t->code) (t->userparam);
44   if (t->joinable) {
45     t->done=1;
46     xbt_mutex_acquire(t->mutex);
47     xbt_cond_broadcast(t->cond);
48     xbt_mutex_release(t->mutex);
49   } else {
50     xbt_mutex_destroy(t->mutex);
51     xbt_cond_destroy(t->cond);
52     free(t->name);
53     free(t);
54   }
55   return 0;
56 }
57
58 xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code,
59                                void *param,int joinable)
60 {
61   xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1);
62   res->name = xbt_strdup(name);
63   res->userparam = param;
64   res->code = code;
65   res->father_data = SIMIX_process_get_data(SIMIX_process_self());
66   //   char*name = bprintf("%s#%p",SIMIX_process_get_name(SIMIX_process_self()), param);
67   res->s_process = SIMIX_process_create(name,
68                                         xbt_thread_create_wrapper, res,
69                                         SIMIX_host_get_name(SIMIX_host_self
70                                                             ()), 0, NULL,
71                                         /*props */ NULL);
72   res->joinable = joinable;
73   res->done = 0;
74   res->cond = xbt_cond_init();
75   res->mutex = xbt_mutex_init();
76   //   free(name);
77   return res;
78 }
79
80 const char *xbt_thread_name(xbt_thread_t t)
81 {
82   return t->name;
83 }
84
85 const char *xbt_thread_self_name(void)
86 {
87   xbt_thread_t me = xbt_thread_self();
88   return me ? me->name : "maestro";
89 }
90
91
92 void xbt_thread_join(xbt_thread_t thread)
93 {
94   xbt_mutex_acquire(thread->mutex);
95   xbt_assert1(thread->joinable,"Cannot join on %p: wasn't created joinable",thread);
96   if (!thread->done) {
97     xbt_cond_wait(thread->cond,thread->mutex);
98     xbt_mutex_release(thread->mutex);
99   }
100
101   xbt_mutex_destroy(thread->mutex);
102   xbt_cond_destroy(thread->cond);
103   free(thread->name);
104   free(thread);
105
106 }
107
108 void xbt_thread_cancel(xbt_thread_t thread)
109 {
110   SIMIX_process_kill(thread->s_process);
111   free(thread->name);
112   free(thread);
113 }
114
115 void xbt_thread_exit()
116 {
117   SIMIX_process_kill(SIMIX_process_self());
118 }
119
120 xbt_thread_t xbt_thread_self(void)
121 {
122   smx_process_t p = SIMIX_process_self();
123   return p ? SIMIX_process_get_data(p) : NULL;
124 }
125
126 void xbt_thread_yield(void) {
127   SIMIX_process_yield();
128 }
129
130 /****** mutex related functions ******/
131 struct s_xbt_mutex_ {
132
133   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
134   xbt_swag_t sleeping;          /* list of sleeping process */
135   int refcount;
136   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
137
138 };
139
140 xbt_mutex_t xbt_mutex_init(void)
141 {
142   return (xbt_mutex_t) SIMIX_mutex_init();
143 }
144
145 void xbt_mutex_acquire(xbt_mutex_t mutex)
146 {
147   SIMIX_mutex_lock((smx_mutex_t) mutex);
148 }
149
150 void xbt_mutex_release(xbt_mutex_t mutex)
151 {
152   SIMIX_mutex_unlock((smx_mutex_t) mutex);
153 }
154
155 void xbt_mutex_destroy(xbt_mutex_t mutex)
156 {
157   SIMIX_mutex_destroy((smx_mutex_t) mutex);
158 }
159
160 /***** condition related functions *****/
161 struct s_xbt_cond_ {
162
163   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
164   xbt_swag_t sleeping;          /* list of sleeping process */
165   smx_mutex_t mutex;
166   xbt_fifo_t actions;           /* list of actions */
167   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
168
169 };
170
171 xbt_cond_t xbt_cond_init(void)
172 {
173   return (xbt_cond_t) SIMIX_cond_init();
174 }
175
176 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
177 {
178   SIMIX_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex);
179 }
180
181 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
182 {
183   SIMIX_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay);
184 }
185
186 void xbt_cond_signal(xbt_cond_t cond)
187 {
188   SIMIX_cond_signal((smx_cond_t) cond);
189 }
190
191 void xbt_cond_broadcast(xbt_cond_t cond)
192 {
193   SIMIX_cond_broadcast((smx_cond_t) cond);
194 }
195
196 void xbt_cond_destroy(xbt_cond_t cond)
197 {
198   SIMIX_cond_destroy((smx_cond_t) cond);
199 }