Logo AND Algorithmique Numérique Distribuée

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