Logo AND Algorithmique Numérique Distribuée

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