Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : add stats in mmalloc
[simgrid.git] / src / xbt / xbt_rl_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 real life implementation, using xbt_os_thread to be portable */
5 /* to windows and linux.                                                    */
6
7 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "xbt/sysdep.h"
14 #include "xbt/ex.h"
15 #include "portable.h"
16
17 #include "xbt/synchro.h"        /* This module */
18 #include "xbt/xbt_os_thread.h"  /* The implementation we use */
19
20 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
22                                 "Synchronization mechanism");
23
24 typedef struct s_xbt_thread_ {
25   xbt_os_thread_t os_thread;
26   void_f_pvoid_t code;
27   void *userparam;
28 } s_xbt_thread_t;
29
30 static void *xbt_thread_create_wrapper(void *p)
31 {
32   xbt_thread_t t = (xbt_thread_t) p;
33   XBT_DEBUG("I'm thread %p", p);
34   t->code(t->userparam);
35   return NULL;
36 }
37
38
39 xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code,
40                                void *param, int joinable)
41 {
42
43   xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1);
44   res->userparam = param;
45   res->code = code;
46   XBT_DEBUG("Create thread %p", res);
47   res->os_thread =
48       xbt_os_thread_create(name, xbt_thread_create_wrapper, res, NULL);
49   return res;
50 }
51
52 const char *xbt_thread_name(xbt_thread_t t)
53 {
54   return xbt_os_thread_name(t->os_thread);
55 }
56
57 const char *xbt_thread_self_name(void)
58 {
59   return xbt_os_thread_self_name();
60 }
61
62 void xbt_thread_join(xbt_thread_t thread)
63 {
64   XBT_DEBUG("Join thread %p", thread);
65   xbt_os_thread_join(thread->os_thread, NULL);
66   xbt_free(thread);
67 }
68
69 void xbt_thread_exit()
70 {
71   XBT_DEBUG("Thread exits");
72   xbt_os_thread_exit(NULL);
73 }
74
75 xbt_thread_t xbt_thread_self(void)
76 {
77   return (xbt_thread_t) xbt_os_thread_self();
78 }
79
80 void xbt_thread_yield(void)
81 {
82   XBT_DEBUG("Thread yields");
83   xbt_os_thread_yield();
84 }
85
86 void xbt_thread_cancel(xbt_thread_t t)
87 {
88   XBT_DEBUG("Cancel thread %p", t);
89   xbt_os_thread_cancel(t->os_thread);
90 }
91
92 /****** mutex related functions ******/
93 struct xbt_mutex_ {
94   /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
95 #ifdef HAVE_PTHREAD_H
96   pthread_mutex_t m;
97 #elif defined(_XBT_WIN32)
98   CRITICAL_SECTION lock;
99 #endif
100 };
101
102 xbt_mutex_t xbt_mutex_init(void)
103 {
104   xbt_mutex_t res = (xbt_mutex_t) xbt_os_mutex_init();
105   XBT_DEBUG("Create mutex %p", res);
106   return res;
107 }
108
109 void xbt_mutex_acquire(xbt_mutex_t mutex)
110 {
111   XBT_DEBUG("Acquire mutex %p", mutex);
112   xbt_os_mutex_acquire((xbt_os_mutex_t) mutex);
113 }
114
115 void xbt_mutex_timedacquire(xbt_mutex_t mutex, double delay)
116 {
117   XBT_DEBUG("Acquire mutex %p with delay %lf", mutex, delay);
118   xbt_os_mutex_timedacquire((xbt_os_mutex_t) mutex, delay);
119 }
120
121 void xbt_mutex_release(xbt_mutex_t mutex)
122 {
123   XBT_DEBUG("Unlock mutex %p", mutex);
124   xbt_os_mutex_release((xbt_os_mutex_t) mutex);
125 }
126
127 void xbt_mutex_destroy(xbt_mutex_t mutex)
128 {
129   XBT_DEBUG("Destroy mutex %p", mutex);
130   xbt_os_mutex_destroy((xbt_os_mutex_t) mutex);
131 }
132
133 #ifdef _XBT_WIN32
134 enum {                          /* KEEP IT IN SYNC WITH OS IMPLEM */
135   SIGNAL = 0,
136   BROADCAST = 1,
137   MAX_EVENTS = 2
138 };
139 #endif
140
141 /***** condition related functions *****/
142 typedef struct xbt_cond_ {
143   /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
144 #ifdef HAVE_PTHREAD_H
145   pthread_cond_t c;
146 #elif defined(_XBT_WIN32)
147   HANDLE events[MAX_EVENTS];
148
149   unsigned int waiters_count;   /* the number of waiters                        */
150   CRITICAL_SECTION waiters_count_lock;  /* protect access to waiters_count  */
151 #endif
152 } s_xbt_cond_t;
153
154 xbt_cond_t xbt_cond_init(void)
155 {
156   xbt_cond_t res = (xbt_cond_t) xbt_os_cond_init();
157   XBT_DEBUG("Create cond %p", res);
158   return res;
159 }
160
161 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
162 {
163   XBT_DEBUG("Wait cond %p, mutex %p", cond, mutex);
164   xbt_os_cond_wait((xbt_os_cond_t) cond, (xbt_os_mutex_t) mutex);
165 }
166
167 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
168 {
169   XBT_DEBUG("Wait cond %p, mutex %p for %f sec", cond, mutex, delay);
170   xbt_os_cond_timedwait((xbt_os_cond_t) cond, (xbt_os_mutex_t) mutex,
171                         delay);
172   XBT_DEBUG("Done waiting cond %p, mutex %p for %f sec", cond, mutex, delay);
173 }
174
175 void xbt_cond_signal(xbt_cond_t cond)
176 {
177   XBT_DEBUG("Signal cond %p", cond);
178   xbt_os_cond_signal((xbt_os_cond_t) cond);
179 }
180
181 void xbt_cond_broadcast(xbt_cond_t cond)
182 {
183   XBT_DEBUG("Broadcast cond %p", cond);
184   xbt_os_cond_broadcast((xbt_os_cond_t) cond);
185 }
186
187 void xbt_cond_destroy(xbt_cond_t cond)
188 {
189   XBT_DEBUG("Destroy cond %p", cond);
190   xbt_os_cond_destroy((xbt_os_cond_t) cond);
191 }