Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Remove sg_cabinet_cb
[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-2015. 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 #include "xbt/synchro_core.h"
14
15 #include "simgrid/simix.h"        /* used implementation */
16 #include "../simix/smx_private.h" /* FIXME */
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
19                                 "Synchronization mechanism");
20
21 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
22
23 typedef struct s_xbt_thread_ {
24   smx_process_t s_process; /* keep this first, gras_socket_im_the_server() does funky transtyping in sg_msg.c */
25   char *name;
26   void_f_pvoid_t code;
27   void *userparam;
28   void *father_data;
29   /* stuff to allow other people to wait on me with xbt_thread_join */
30   unsigned joinable:1, done:1;
31   xbt_cond_t cond;
32   xbt_mutex_t mutex;
33 } s_xbt_thread_t;
34
35 static int xbt_thread_create_wrapper(int argc, char *argv[])
36 {
37   smx_process_t self = SIMIX_process_self();
38   xbt_thread_t t =
39       (xbt_thread_t) SIMIX_process_self_get_data(self);
40   simcall_process_set_data(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_self_get_data(SIMIX_process_self());
64   /*   char*name = bprintf("%s#%p",SIMIX_process_self_get_name(), param); */
65   res->s_process = simcall_process_create(name,
66                            xbt_thread_create_wrapper, res,
67                            SIMIX_host_self_get_name(), -1.0, 0, NULL,
68                            /*props */ NULL,0);
69   res->joinable = joinable;
70   res->done = 0;
71   res->cond = xbt_cond_init();
72   res->mutex = xbt_mutex_init();
73   //   free(name);
74   return res;
75 }
76
77 const char *xbt_thread_name(xbt_thread_t t)
78 {
79   return t->name;
80 }
81
82 const char *xbt_thread_self_name(void)
83 {
84   xbt_thread_t me = xbt_thread_self();
85   return me ? me->name : "maestro";
86 }
87
88
89 void xbt_thread_join(xbt_thread_t thread)
90 {
91   xbt_mutex_acquire(thread->mutex);
92   xbt_assert(thread->joinable,
93               "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   simcall_process_kill(thread->s_process);
109   free(thread->name);
110   free(thread);
111 }
112
113 void xbt_thread_exit()
114 {
115   simcall_process_kill(SIMIX_process_self());
116 }
117
118 xbt_thread_t xbt_thread_self(void)
119 {
120   return SIMIX_process_self_get_data(SIMIX_process_self());
121 }
122
123 void xbt_thread_yield(void)
124 {
125   SIMIX_process_yield(SIMIX_process_self());
126 }
127
128 /****** mutex related functions ******/
129 struct s_xbt_mutex_ {
130   s_smx_mutex_t mutex;
131 };
132
133 xbt_mutex_t xbt_mutex_init(void)
134 {
135   return (xbt_mutex_t) simcall_mutex_init();
136 }
137
138 void xbt_mutex_acquire(xbt_mutex_t mutex)
139 {
140   simcall_mutex_lock((smx_mutex_t) mutex);
141 }
142
143 int xbt_mutex_try_acquire(xbt_mutex_t mutex)
144 {
145   return simcall_mutex_trylock((smx_mutex_t) mutex);
146 }
147
148 void xbt_mutex_release(xbt_mutex_t mutex)
149 {
150   simcall_mutex_unlock((smx_mutex_t) mutex);
151 }
152
153 void xbt_mutex_destroy(xbt_mutex_t mutex)
154 {
155   simcall_mutex_destroy((smx_mutex_t) mutex);
156 }
157
158 /***** condition related functions *****/
159 struct s_xbt_cond_ {
160   s_smx_cond_t cond;
161 };
162
163 xbt_cond_t xbt_cond_init(void)
164 {
165   return (xbt_cond_t) simcall_cond_init();
166 }
167
168 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
169 {
170   simcall_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex);
171 }
172
173 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
174 {
175   simcall_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay);
176 }
177
178 void xbt_cond_signal(xbt_cond_t cond)
179 {
180   simcall_cond_signal((smx_cond_t) cond);
181 }
182
183 void xbt_cond_broadcast(xbt_cond_t cond)
184 {
185   simcall_cond_broadcast((smx_cond_t) cond);
186 }
187
188 void xbt_cond_destroy(xbt_cond_t cond)
189 {
190   simcall_cond_destroy((smx_cond_t) cond);
191 }
192
193 /***** barrier related functions *****/
194 typedef struct s_xbt_bar_ {
195   xbt_mutex_t mutex;
196   xbt_cond_t cond;
197   unsigned int arrived_processes;
198   unsigned int expected_processes;
199 } s_xbt_bar_;
200
201 xbt_bar_t xbt_barrier_init(unsigned int count)
202 {
203   xbt_bar_t bar = xbt_new0(s_xbt_bar_, 1);
204   bar->expected_processes = count;
205   bar->arrived_processes = 0;
206   bar->mutex = xbt_mutex_init();
207   bar->cond = xbt_cond_init();
208   return bar;
209 }
210
211
212 int xbt_barrier_wait(xbt_bar_t bar)
213 {
214    int ret=0;
215    xbt_mutex_acquire(bar->mutex);
216    if (++bar->arrived_processes == bar->expected_processes) {
217      xbt_cond_broadcast(bar->cond);
218      xbt_mutex_release(bar->mutex);
219      ret=XBT_BARRIER_SERIAL_PROCESS;
220      bar->arrived_processes = 0;
221    } else {
222      xbt_cond_wait(bar->cond, bar->mutex);
223      xbt_mutex_release(bar->mutex);
224    }
225
226    return ret;
227 }
228
229 void xbt_barrier_destroy(xbt_bar_t bar)
230 {
231    xbt_mutex_destroy(bar->mutex);
232    xbt_cond_destroy(bar->cond);
233    xbt_free(bar);
234 }
235