1 /* xbt_synchro -- Synchronization virtualized depending on whether we are */
2 /* in simulation or real life (act on simulated processes) */
4 /* This is the real life implementation, using xbt_os_thread to be portable */
5 /* to windows and linux. */
7 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
8 * All rights reserved. */
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. */
13 #include "xbt/sysdep.h"
17 #include "xbt/synchro.h" /* This module */
18 #include "xbt/xbt_os_thread.h" /* The implementation we use */
20 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
22 "Synchronization mechanism");
24 typedef struct s_xbt_thread_ {
25 xbt_os_thread_t os_thread;
30 static void *xbt_thread_create_wrapper(void *p)
32 xbt_thread_t t = (xbt_thread_t) p;
33 DEBUG1("I'm thread %p", p);
34 (*t->code) (t->userparam);
39 xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code,
40 void *param, int joinable)
43 xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1);
44 res->userparam = param;
46 DEBUG1("Create thread %p", res);
48 xbt_os_thread_create(name, xbt_thread_create_wrapper, res);
52 const char *xbt_thread_name(xbt_thread_t t)
54 return xbt_os_thread_name(t->os_thread);
57 const char *xbt_thread_self_name(void)
59 return xbt_os_thread_self_name();
62 void xbt_thread_join(xbt_thread_t thread)
64 DEBUG1("Join thread %p", thread);
65 xbt_os_thread_join(thread->os_thread, NULL);
69 void xbt_thread_exit()
71 DEBUG0("Thread exits");
72 xbt_os_thread_exit(NULL);
75 xbt_thread_t xbt_thread_self(void)
77 return (xbt_thread_t) xbt_os_thread_self();
80 void xbt_thread_yield(void)
82 DEBUG0("Thread yields");
83 xbt_os_thread_yield();
86 void xbt_thread_cancel(xbt_thread_t t)
88 DEBUG1("Cancel thread %p", t);
89 xbt_os_thread_cancel(t->os_thread);
92 /****** mutex related functions ******/
94 /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
97 #elif defined(_XBT_WIN32)
98 CRITICAL_SECTION lock;
102 xbt_mutex_t xbt_mutex_init(void)
104 xbt_mutex_t res = (xbt_mutex_t) xbt_os_mutex_init();
105 DEBUG1("Create mutex %p", res);
109 void xbt_mutex_acquire(xbt_mutex_t mutex)
111 DEBUG1("Acquire mutex %p", mutex);
112 xbt_os_mutex_acquire((xbt_os_mutex_t) mutex);
115 void xbt_mutex_timedacquire(xbt_mutex_t mutex, double delay)
117 DEBUG2("Acquire mutex %p with delay %lf", mutex, delay);
118 xbt_os_mutex_timedacquire((xbt_os_mutex_t) mutex, delay);
121 void xbt_mutex_release(xbt_mutex_t mutex)
123 DEBUG1("Unlock mutex %p", mutex);
124 xbt_os_mutex_release((xbt_os_mutex_t) mutex);
127 void xbt_mutex_destroy(xbt_mutex_t mutex)
129 DEBUG1("Destroy mutex %p", mutex);
130 xbt_os_mutex_destroy((xbt_os_mutex_t) mutex);
134 enum { /* KEEP IT IN SYNC WITH OS IMPLEM */
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
146 #elif defined(_XBT_WIN32)
147 HANDLE events[MAX_EVENTS];
149 unsigned int waiters_count; /* the number of waiters */
150 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
154 xbt_cond_t xbt_cond_init(void)
156 xbt_cond_t res = (xbt_cond_t) xbt_os_cond_init();
157 DEBUG1("Create cond %p", res);
161 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
163 DEBUG2("Wait cond %p, mutex %p", cond, mutex);
164 xbt_os_cond_wait((xbt_os_cond_t) cond, (xbt_os_mutex_t) mutex);
167 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
169 DEBUG3("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,
172 DEBUG3("Done waiting cond %p, mutex %p for %f sec", cond, mutex, delay);
175 void xbt_cond_signal(xbt_cond_t cond)
177 DEBUG1("Signal cond %p", cond);
178 xbt_os_cond_signal((xbt_os_cond_t) cond);
181 void xbt_cond_broadcast(xbt_cond_t cond)
183 DEBUG1("Broadcast cond %p", cond);
184 xbt_os_cond_broadcast((xbt_os_cond_t) cond);
187 void xbt_cond_destroy(xbt_cond_t cond)
189 DEBUG1("Destroy cond %p", cond);
190 xbt_os_cond_destroy((xbt_os_cond_t) cond);