Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Workaround problem with excess precision on i387 FPU.
[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 #include "../simix/smx_private.h" /* FIXME */
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
21                                 "Synchronization mechanism");
22
23 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
24
25 typedef struct s_xbt_thread_ {
26   smx_process_t s_process; /* keep this first, gras_socket_im_the_server() does funky transtyping in sg_msg.c */
27   char *name;
28   void_f_pvoid_t code;
29   void *userparam;
30   void *father_data;
31   /* stuff to allow other people to wait on me with xbt_thread_join */
32   int joinable:1, done:1;
33   xbt_cond_t cond;
34   xbt_mutex_t mutex;
35 } s_xbt_thread_t;
36
37 static int xbt_thread_create_wrapper(int argc, char *argv[])
38 {
39   smx_process_t self = SIMIX_process_self();
40   xbt_thread_t t =
41       (xbt_thread_t) SIMIX_process_self_get_data(self);
42   simcall_process_set_data(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_self_get_data(SIMIX_process_self());
66   /*   char*name = bprintf("%s#%p",SIMIX_process_self_get_name(), param); */
67   simcall_process_create(&res->s_process, name,
68                            xbt_thread_create_wrapper, res,
69                            SIMIX_host_self_get_name(), -1.0, 0, NULL,
70                            /*props */ NULL);
71   res->joinable = joinable;
72   res->done = 0;
73   res->cond = xbt_cond_init();
74   res->mutex = xbt_mutex_init();
75   //   free(name);
76   return res;
77 }
78
79 const char *xbt_thread_name(xbt_thread_t t)
80 {
81   return t->name;
82 }
83
84 const char *xbt_thread_self_name(void)
85 {
86   xbt_thread_t me = xbt_thread_self();
87   return me ? me->name : "maestro";
88 }
89
90
91 void xbt_thread_join(xbt_thread_t thread)
92 {
93   xbt_mutex_acquire(thread->mutex);
94   xbt_assert(thread->joinable,
95               "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   simcall_process_kill(thread->s_process);
111   free(thread->name);
112   free(thread);
113 }
114
115 void xbt_thread_exit()
116 {
117   simcall_process_kill(SIMIX_process_self());
118 }
119
120 xbt_thread_t xbt_thread_self(void)
121 {
122   return SIMIX_process_self_get_data(SIMIX_process_self());
123 }
124
125 void xbt_thread_yield(void)
126 {
127   SIMIX_process_yield(SIMIX_process_self());
128 }
129
130 /****** mutex related functions ******/
131 struct s_xbt_mutex_ {
132   s_smx_mutex_t mutex;
133 };
134
135 xbt_mutex_t xbt_mutex_init(void)
136 {
137   return (xbt_mutex_t) simcall_mutex_init();
138 }
139
140 void xbt_mutex_acquire(xbt_mutex_t mutex)
141 {
142   simcall_mutex_lock((smx_mutex_t) mutex);
143 }
144
145 void xbt_mutex_release(xbt_mutex_t mutex)
146 {
147   simcall_mutex_unlock((smx_mutex_t) mutex);
148 }
149
150 void xbt_mutex_destroy(xbt_mutex_t mutex)
151 {
152   simcall_mutex_destroy((smx_mutex_t) mutex);
153 }
154
155 /***** condition related functions *****/
156 struct s_xbt_cond_ {
157   s_smx_cond_t cond;
158 };
159
160 xbt_cond_t xbt_cond_init(void)
161 {
162   return (xbt_cond_t) simcall_cond_init();
163 }
164
165 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
166 {
167   simcall_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex);
168 }
169
170 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
171 {
172   simcall_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay);
173 }
174
175 void xbt_cond_signal(xbt_cond_t cond)
176 {
177   simcall_cond_signal((smx_cond_t) cond);
178 }
179
180 void xbt_cond_broadcast(xbt_cond_t cond)
181 {
182   simcall_cond_broadcast((smx_cond_t) cond);
183 }
184
185 void xbt_cond_destroy(xbt_cond_t cond)
186 {
187   simcall_cond_destroy((smx_cond_t) cond);
188 }