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_ {
26 pvoid_f_pvoid_t *start_routine;
29 /* thread-specific data containing the xbt_thread_t structure */
30 static pthread_key_t xbt_self_thread_key;
32 /* frees the xbt_thread_t corresponding to the current thread */
33 static void xbt_thread_free_thread_data(void*d){
37 void xbt_thread_mod_init(void) {
40 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
41 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
43 void xbt_thread_mod_exit(void) {
44 /* FIXME: don't try to free our key on shutdown. Valgrind detects no leak if we don't, and whine if we try to */
47 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
48 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
51 static void * wrapper_start_routine(void *s) {
55 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
56 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
57 return t->start_routine(t->param);
60 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
64 xbt_thread_t res_thread=xbt_new(s_xbt_thread_t,1);
65 res_thread->start_routine = start_routine;
66 res_thread->param = param;
69 if ((errcode = pthread_create(&(res_thread->t), NULL, wrapper_start_routine, res_thread)))
70 THROW0(system_error,errcode, "pthread_create failed");
76 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
80 if ((errcode = pthread_join(thread->t,thread_return)))
81 THROW0(system_error,errcode, "pthread_join failed");
84 void xbt_thread_exit(int *retval) {
88 xbt_thread_t xbt_thread_self(void) {
89 return pthread_getspecific(xbt_self_thread_key);
93 void xbt_thread_yield(void) {
96 /****** mutex related functions ******/
97 typedef struct xbt_mutex_ {
101 xbt_mutex_t xbt_mutex_init(void) {
102 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
105 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
106 THROW0(system_error,errcode,"pthread_mutex_init() failed");
111 void xbt_mutex_lock(xbt_mutex_t mutex) {
114 if ((errcode=pthread_mutex_lock(&(mutex->m))))
115 THROW1(system_error,errcode,"pthread_mutex_lock(%p) failed",mutex);
118 void xbt_mutex_unlock(xbt_mutex_t mutex) {
121 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
122 THROW1(system_error,errcode,"pthread_mutex_unlock(%p) failed",mutex);
125 void xbt_mutex_destroy(xbt_mutex_t mutex) {
130 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
131 THROW1(system_error,errcode,"pthread_mutex_destroy(%p) failed",mutex);
135 /***** condition related functions *****/
136 typedef struct xbt_thcond_ {
140 xbt_thcond_t xbt_thcond_init(void) {
141 xbt_thcond_t res = xbt_new(s_xbt_thcond_t,1);
143 if ((errcode=pthread_cond_init(&(res->c),NULL)))
144 THROW0(system_error,errcode,"pthread_cond_init() failed");
149 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
151 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
152 THROW2(system_error,errcode,"pthread_cond_wait(%p,%p) failed",cond,mutex);
155 void xbt_thcond_signal(xbt_thcond_t cond) {
157 if ((errcode=pthread_cond_signal(&(cond->c))))
158 THROW1(system_error,errcode,"pthread_cond_signal(%p) failed",cond);
161 void xbt_thcond_broadcast(xbt_thcond_t cond){
163 if ((errcode=pthread_cond_broadcast(&(cond->c))))
164 THROW1(system_error,errcode,"pthread_cond_broadcast(%p) failed",cond);
166 void xbt_thcond_destroy(xbt_thcond_t cond){
171 if ((errcode=pthread_cond_destroy(&(cond->c))))
172 THROW1(system_error,errcode,"pthread_cond_destroy(%p) failed",cond);
176 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
180 typedef struct xbt_thread_ {
181 HANDLE handle; /* the win thread handle */
182 unsigned long id; /* the win thread id */
183 pvoid_f_pvoid_t *start_routine;
187 /* key to the TLS containing the xbt_thread_t structure */
188 static unsigned long xbt_self_thread_key;
190 void xbt_thread_mod_init(void) {
191 xbt_self_thread_key = TlsAlloc();
193 void xbt_thread_mod_exit(void) {
195 if (!TlsFree(xbt_self_thread_key))
196 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
199 static DWORD WINAPI wrapper_start_routine(void *s) {
200 xbt_thread_t t = (xbt_thread_t)s;
202 if(!TlsSetValue(xbt_self_thread_key,t))
203 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
205 return (DWORD)t->start_routine(t->param);
209 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
212 xbt_thread_t t = xbt_new(s_xbt_thread_t,1);
214 t->start_routine = start_routine ;
217 t->handle = CreateThread(NULL,0,
218 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
223 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
230 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
232 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
233 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
237 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
238 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
241 CloseHandle(thread->handle);
244 void xbt_thread_exit(int *retval) {
251 xbt_thread_t xbt_thread_self(void) {
252 return TlsGetValue(xbt_self_thread_key);
255 void xbt_thread_yield(void) {
259 /****** mutex related functions ******/
260 typedef struct xbt_mutex_ {
261 CRITICAL_SECTION lock;
264 xbt_mutex_t xbt_mutex_init(void) {
265 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
267 /* initialize the critical section object */
268 InitializeCriticalSection(&(res->lock));
273 void xbt_mutex_lock(xbt_mutex_t mutex) {
275 EnterCriticalSection(& mutex->lock);
278 void xbt_mutex_unlock(xbt_mutex_t mutex) {
280 LeaveCriticalSection (& mutex->lock);
284 void xbt_mutex_destroy(xbt_mutex_t mutex) {
288 DeleteCriticalSection(& mutex->lock);
292 /***** condition related functions *****/
299 typedef struct xbt_thcond_ {
300 HANDLE events[MAX_EVENTS];
302 unsigned int waiters_count; /* the number of waiters */
303 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
306 xbt_thcond_t xbt_thcond_init(void) {
308 xbt_thcond_t res = xbt_new0(s_xbt_thcond_t,1);
310 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
312 /* initialize the critical section object */
313 InitializeCriticalSection(& res->waiters_count_lock);
315 res->waiters_count = 0;
317 /* Create an auto-reset event */
318 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
320 if(!res->events[SIGNAL]){
321 DeleteCriticalSection(& res->waiters_count_lock);
323 THROW0(system_error,0,"CreateEvent failed for the signals");
326 /* Create a manual-reset event. */
327 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
329 if(!res->events[BROADCAST]){
331 DeleteCriticalSection(& res->waiters_count_lock);
332 CloseHandle(res->events[SIGNAL]);
334 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
340 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
342 unsigned long wait_result;
345 /* lock the threads counter and increment it */
346 EnterCriticalSection (& cond->waiters_count_lock);
347 cond->waiters_count++;
348 LeaveCriticalSection (& cond->waiters_count_lock);
350 /* unlock the mutex associate with the condition */
351 LeaveCriticalSection (& mutex->lock);
353 /* wait for a signal (broadcast or no) */
354 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
356 if(wait_result == WAIT_FAILED)
357 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
359 /* we have a signal lock the condition */
360 EnterCriticalSection (& cond->waiters_count_lock);
361 cond->waiters_count--;
363 /* it's the last waiter or it's a broadcast ? */
364 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
366 LeaveCriticalSection (& cond->waiters_count_lock);
368 /* yes it's the last waiter or it's a broadcast
369 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
373 if(!ResetEvent (cond->events[BROADCAST]))
374 THROW0(system_error,0,"ResetEvent failed");
376 /* relock the mutex associated with the condition in accordance with the posix thread specification */
377 EnterCriticalSection (& mutex->lock);
380 void xbt_thcond_signal(xbt_thcond_t cond) {
383 EnterCriticalSection (& cond->waiters_count_lock);
384 have_waiters = cond->waiters_count > 0;
385 LeaveCriticalSection (& cond->waiters_count_lock);
388 if(!SetEvent(cond->events[SIGNAL]))
389 THROW0(system_error,0,"SetEvent failed");
394 void xbt_thcond_broadcast(xbt_thcond_t cond){
397 EnterCriticalSection (& cond->waiters_count_lock);
398 have_waiters = cond->waiters_count > 0;
399 LeaveCriticalSection (& cond->waiters_count_lock);
402 SetEvent(cond->events[BROADCAST]);
405 void xbt_thcond_destroy(xbt_thcond_t cond){
410 if(!CloseHandle(cond->events[SIGNAL]))
413 if(!CloseHandle(cond->events[BROADCAST]))
416 DeleteCriticalSection(& cond->waiters_count_lock);
421 THROW0(system_error,0,"Error while destroying the condition");