Logo AND Algorithmique Numérique Distribuée

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