Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement xbt_thread_join and xbt_thread_yield in SG also
[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_sg, xbt,
22                                 "Synchronization mechanism (SG)");
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;
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     xbt_mutex_acquire(t->mutex);
46     xbt_cond_broadcast(t->cond);
47     xbt_mutex_release(t->mutex);
48   } else {
49     xbt_mutex_destroy(t->mutex);
50     xbt_cond_destroy(t->cond);
51     free(t->name);
52     free(t);
53   }
54   return 0;
55 }
56
57 xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code,
58                                void *param,int joinable)
59 {
60   xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1);
61   res->name = xbt_strdup(name);
62   res->userparam = param;
63   res->code = code;
64   res->father_data = SIMIX_process_get_data(SIMIX_process_self());
65   //   char*name = bprintf("%s#%p",SIMIX_process_get_name(SIMIX_process_self()), param);
66   res->s_process = SIMIX_process_create(name,
67                                         xbt_thread_create_wrapper, res,
68                                         SIMIX_host_get_name(SIMIX_host_self
69                                                             ()), 0, NULL,
70                                         /*props */ NULL);
71   res->joinable = joinable;
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   xbt_cond_wait(thread->cond,thread->mutex);
95   xbt_mutex_release(thread->mutex);
96
97   xbt_mutex_destroy(thread->mutex);
98   xbt_cond_destroy(thread->cond);
99   free(thread->name);
100   free(thread);
101
102 }
103
104 void xbt_thread_cancel(xbt_thread_t thread)
105 {
106   SIMIX_process_kill(thread->s_process);
107   free(thread->name);
108   free(thread);
109 }
110
111 void xbt_thread_exit()
112 {
113   SIMIX_process_kill(SIMIX_process_self());
114 }
115
116 xbt_thread_t xbt_thread_self(void)
117 {
118   smx_process_t p = SIMIX_process_self();
119   return p ? SIMIX_process_get_data(p) : NULL;
120 }
121
122 void xbt_thread_yield(void) {
123   SIMIX_process_yield();
124 }
125
126 /****** mutex related functions ******/
127 struct s_xbt_mutex_ {
128
129   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
130   xbt_swag_t sleeping;          /* list of sleeping process */
131   int refcount;
132   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
133
134 };
135
136 xbt_mutex_t xbt_mutex_init(void)
137 {
138   return (xbt_mutex_t) SIMIX_mutex_init();
139 }
140
141 void xbt_mutex_acquire(xbt_mutex_t mutex)
142 {
143   SIMIX_mutex_lock((smx_mutex_t) mutex);
144 }
145
146 void xbt_mutex_release(xbt_mutex_t mutex)
147 {
148   SIMIX_mutex_unlock((smx_mutex_t) mutex);
149 }
150
151 void xbt_mutex_destroy(xbt_mutex_t mutex)
152 {
153   SIMIX_mutex_destroy((smx_mutex_t) mutex);
154 }
155
156 /***** condition related functions *****/
157 struct s_xbt_cond_ {
158
159   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
160   xbt_swag_t sleeping;          /* list of sleeping process */
161   smx_mutex_t mutex;
162   xbt_fifo_t actions;           /* list of actions */
163   /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
164
165 };
166
167 xbt_cond_t xbt_cond_init(void)
168 {
169   return (xbt_cond_t) SIMIX_cond_init();
170 }
171
172 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
173 {
174   SIMIX_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex);
175 }
176
177 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
178 {
179   SIMIX_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay);
180 }
181
182 void xbt_cond_signal(xbt_cond_t cond)
183 {
184   SIMIX_cond_signal((smx_cond_t) cond);
185 }
186
187 void xbt_cond_broadcast(xbt_cond_t cond)
188 {
189   SIMIX_cond_broadcast((smx_cond_t) cond);
190 }
191
192 void xbt_cond_destroy(xbt_cond_t cond)
193 {
194   SIMIX_cond_destroy((smx_cond_t) cond);
195 }