3 /* xbt_thread -- portability layer over the pthread API */
5 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
6 * All right reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
14 #include "xbt/xbt_thread.h" /* This module */
15 #include "xbt_modinter.h" /* Initialization/finalization of this module */
19 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
23 typedef struct xbt_thread_ {
27 /* thread-specific data containing the xbt_thread_t structure */
28 static pthread_key_t xbt_self_thread_key;
30 /* frees the xbt_thread_t corresponding to the current thread */
31 static void xbt_thread_free_thread_data(void*d){
35 void xbt_thread_mod_init(void) {
38 if ((errcode=pthread_key_create(&xbt_self_thread_key, &xbt_thread_free_thread_data)))
39 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
41 void xbt_thread_mod_exit(void) {
42 /* FIXME: don't try to free our key on shutdown. Valgrind detects no leak if we don't, and whine if we try to */
45 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
46 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
50 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
52 xbt_thread_t res = xbt_new(s_xbt_thread_t,1);
55 if ((errcode=pthread_setspecific(xbt_self_thread_key,res)))
56 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
58 if ((errcode = pthread_create(&(res->t), NULL, start_routine, param)))
59 THROW0(system_error,errcode, "pthread_create failed");
63 void xbt_thread_exit(int *retval) {
66 xbt_thread_t xbt_thread_self(void) {
67 return pthread_getspecific(xbt_self_thread_key);
71 void xbt_thread_yield(void) {
74 /****** mutex related functions ******/
75 typedef struct xbt_mutex_ {
79 xbt_mutex_t xbt_mutex_init(void) {
80 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
83 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
84 THROW0(system_error,errcode,"pthread_mutex_init() failed");
89 void xbt_mutex_lock(xbt_mutex_t mutex) {
92 if ((errcode=pthread_mutex_lock(&(mutex->m))))
93 THROW1(system_error,errcode,"pthread_mutex_lock(%p) failed",mutex);
96 void xbt_mutex_unlock(xbt_mutex_t mutex) {
99 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
100 THROW1(system_error,errcode,"pthread_mutex_unlock(%p) failed",mutex);
103 void xbt_mutex_destroy(xbt_mutex_t mutex) {
108 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
109 THROW1(system_error,errcode,"pthread_mutex_destroy(%p) failed",mutex);
113 /***** condition related functions *****/
114 typedef struct xbt_thcond_ {
118 xbt_thcond_t xbt_thcond_init(void) {
119 xbt_thcond_t res = xbt_new(s_xbt_thcond_t,1);
121 if ((errcode=pthread_cond_init(&(res->c),NULL)))
122 THROW0(system_error,errcode,"pthread_cond_init() failed");
127 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
129 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
130 THROW2(system_error,errcode,"pthread_cond_wait(%p,%p) failed",cond,mutex);
133 void xbt_thcond_signal(xbt_thcond_t cond) {
135 if ((errcode=pthread_cond_signal(&(cond->c))))
136 THROW1(system_error,errcode,"pthread_cond_signal(%p) failed",cond);
139 void xbt_thcond_broadcast(xbt_thcond_t cond){
141 if ((errcode=pthread_cond_broadcast(&(cond->c))))
142 THROW1(system_error,errcode,"pthread_cond_broadcast(%p) failed",cond);
144 void xbt_thcond_destroy(xbt_thcond_t cond){
149 if ((errcode=pthread_cond_destroy(&(cond->c))))
150 THROW1(system_error,errcode,"pthread_cond_destroy(%p) failed",cond);
154 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
158 typedef struct xbt_thread_ {
159 HANDLE handle; /* the win thread handle */
160 unsigned long id; /* the win thread id */
163 /* key to the TLS containing the xbt_thread_t structure */
164 static unsigned long xbt_self_thread_key;
166 void xbt_thread_mod_init(void) {
167 xbt_self_thread_key = TlsAlloc();
169 void xbt_thread_mod_exit(void) {
171 if (!(errcode = TlsFree(xbt_self_thread_key)))
172 THROW0(system_error,errcode,"TlsFree() failed to cleanup the thread submodule");
175 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
178 xbt_thread_t res = xbt_new(s_xbt_thread_t,1);
180 res->handle = CreateThread(NULL,0,
181 (LPTHREAD_START_ROUTINE)start_routine,
186 THROW0(system_error,0,"CreateThread failed");
189 if(!TlsSetValue(xbt_self_thread_key,res))
190 THROW0(system_error,0,"TlsSetValue of data describing the created thread failed");
195 void xbt_thread_exit(int *retval) {
196 xbt_thread_t self = xbt_thread_self();
198 CloseHandle(self->handle);
204 xbt_thread_t xbt_thread_self(void) {
205 return TlsGetValue(xbt_self_thread_key);
208 void xbt_thread_yield(void) {
212 /****** mutex related functions ******/
213 typedef struct xbt_mutex_ {
214 CRITICAL_SECTION lock;
217 xbt_mutex_t xbt_mutex_init(void) {
218 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
220 /* initialize the critical section object */
221 InitializeCriticalSection(&(res->lock));
226 void xbt_mutex_lock(xbt_mutex_t mutex) {
228 EnterCriticalSection(& mutex->lock);
231 void xbt_mutex_unlock(xbt_mutex_t mutex) {
233 LeaveCriticalSection (& mutex->lock);
237 void xbt_mutex_destroy(xbt_mutex_t mutex) {
241 DeleteCriticalSection(& mutex->lock);
245 /***** condition related functions *****/
252 typedef struct xbt_thcond_ {
253 HANDLE events[MAX_EVENTS];
255 unsigned int waiters_count; /* the number of waiters */
256 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
259 xbt_thcond_t xbt_thcond_init(void) {
261 xbt_thcond_t res = xbt_new0(s_xbt_thcond_t,1);
263 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
265 /* initialize the critical section object */
266 InitializeCriticalSection(& res->waiters_count_lock);
268 res->waiters_count = 0;
270 /* Create an auto-reset event */
271 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
273 if(!res->events[SIGNAL]){
274 DeleteCriticalSection(& res->waiters_count_lock);
276 THROW0(system_error,0,"CreateEvent failed for the signals");
279 /* Create a manual-reset event. */
280 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
282 if(!res->events[BROADCAST]){
284 DeleteCriticalSection(& res->waiters_count_lock);
285 CloseHandle(res->events[SIGNAL]);
287 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
293 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
295 unsigned long wait_result;
298 /* lock the threads counter and increment it */
299 EnterCriticalSection (& cond->waiters_count_lock);
300 cond->waiters_count++;
301 LeaveCriticalSection (& cond->waiters_count_lock);
303 /* unlock the mutex associate with the condition */
304 LeaveCriticalSection (& mutex->lock);
306 /* wait for a signal (broadcast or no) */
307 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
309 if(wait_result == WAIT_FAILED)
310 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
312 /* we have a signal lock the condition */
313 EnterCriticalSection (& cond->waiters_count_lock);
314 cond->waiters_count--;
316 /* it's the last waiter or it's a broadcast ? */
317 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
319 LeaveCriticalSection (& cond->waiters_count_lock);
321 /* yes it's the last waiter or it's a broadcast
322 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
326 if(!ResetEvent (cond->events[BROADCAST]))
327 THROW0(system_error,0,"ResetEvent failed");
329 /* relock the mutex associated with the condition in accordance with the posix thread specification */
330 EnterCriticalSection (& mutex->lock);
333 void xbt_thcond_signal(xbt_thcond_t cond) {
336 EnterCriticalSection (& cond->waiters_count_lock);
337 have_waiters = cond->waiters_count > 0;
338 LeaveCriticalSection (& cond->waiters_count_lock);
341 if(!SetEvent(cond->events[SIGNAL]))
342 THROW0(system_error,0,"SetEvent failed");
345 void xbt_thcond_broadcast(xbt_thcond_t cond){
348 EnterCriticalSection (& cond->waiters_count_lock);
349 have_waiters = cond->waiters_count > 0;
350 LeaveCriticalSection (& cond->waiters_count_lock);
353 SetEvent(cond->events[BROADCAST]);
356 void xbt_thcond_destroy(xbt_thcond_t cond){
361 if(!CloseHandle(cond->events[SIGNAL]))
364 if(!CloseHandle(cond->events[BROADCAST]))
367 DeleteCriticalSection(& cond->waiters_count_lock);
372 THROW0(system_error,0,"Error while destroying the condition");