Logo AND Algorithmique Numérique Distribuée

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