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, NULL)))
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");
49 typedef struct s_xbt_thread_wrapper_for_restart__ {
50 pvoid_f_pvoid_t *start_routine;
53 } s_xbt_thread_wrapper_for_restart_t, *xbt_thread_wrapper_for_restart_t;
55 static void * wrapper_start_routine(void *s) {
56 xbt_thread_wrapper_for_restart_t stub = s;
59 if ((errcode=pthread_setspecific(xbt_self_thread_key,stub->res)))
60 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
61 return stub->start_routine(stub->param);
64 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
67 xbt_thread_wrapper_for_restart_t stub = xbt_new0(s_xbt_thread_wrapper_for_restart_t,1);
69 stub->start_routine = start_routine ;
71 stub->res = xbt_new(s_xbt_thread_t,1);
74 if ((errcode = pthread_create(&(stub->res->t), NULL, wrapper_start_routine, stub)))
75 THROW0(system_error,errcode, "pthread_create failed");
81 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
85 if ((errcode = pthread_join(thread->t,thread_return)))
86 THROW0(system_error,errcode, "pthread_join failed");
89 void xbt_thread_exit(int *retval) {
93 xbt_thread_t xbt_thread_self(void) {
94 return pthread_getspecific(xbt_self_thread_key);
98 void xbt_thread_yield(void) {
101 /****** mutex related functions ******/
102 typedef struct xbt_mutex_ {
106 xbt_mutex_t xbt_mutex_init(void) {
107 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
110 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
111 THROW0(system_error,errcode,"pthread_mutex_init() failed");
116 void xbt_mutex_lock(xbt_mutex_t mutex) {
119 if ((errcode=pthread_mutex_lock(&(mutex->m))))
120 THROW1(system_error,errcode,"pthread_mutex_lock(%p) failed",mutex);
123 void xbt_mutex_unlock(xbt_mutex_t mutex) {
126 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
127 THROW1(system_error,errcode,"pthread_mutex_unlock(%p) failed",mutex);
130 void xbt_mutex_destroy(xbt_mutex_t mutex) {
135 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
136 THROW1(system_error,errcode,"pthread_mutex_destroy(%p) failed",mutex);
140 /***** condition related functions *****/
141 typedef struct xbt_thcond_ {
145 xbt_thcond_t xbt_thcond_init(void) {
146 xbt_thcond_t res = xbt_new(s_xbt_thcond_t,1);
148 if ((errcode=pthread_cond_init(&(res->c),NULL)))
149 THROW0(system_error,errcode,"pthread_cond_init() failed");
154 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
156 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
157 THROW2(system_error,errcode,"pthread_cond_wait(%p,%p) failed",cond,mutex);
160 void xbt_thcond_signal(xbt_thcond_t cond) {
162 if ((errcode=pthread_cond_signal(&(cond->c))))
163 THROW1(system_error,errcode,"pthread_cond_signal(%p) failed",cond);
166 void xbt_thcond_broadcast(xbt_thcond_t cond){
168 if ((errcode=pthread_cond_broadcast(&(cond->c))))
169 THROW1(system_error,errcode,"pthread_cond_broadcast(%p) failed",cond);
171 void xbt_thcond_destroy(xbt_thcond_t cond){
176 if ((errcode=pthread_cond_destroy(&(cond->c))))
177 THROW1(system_error,errcode,"pthread_cond_destroy(%p) failed",cond);
181 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
185 typedef struct xbt_thread_ {
186 HANDLE handle; /* the win thread handle */
187 unsigned long id; /* the win thread id */
190 typedef struct s_xbt_thread_wrapper_for_restart__ {
191 pvoid_f_pvoid_t *start_routine;
194 } s_xbt_thread_wrapper_for_restart_t, *xbt_thread_wrapper_for_restart_t;
196 /* key to the TLS containing the xbt_thread_t structure */
197 static unsigned long xbt_self_thread_key;
199 void xbt_thread_mod_init(void) {
200 xbt_self_thread_key = TlsAlloc();
202 void xbt_thread_mod_exit(void) {
204 if (!TlsFree(xbt_self_thread_key))
205 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
208 DWORD WINAPI wrapper_start_routine(void *s) {
209 xbt_thread_wrapper_for_restart_t stub = (xbt_thread_wrapper_for_restart_t)s;
211 if(!TlsSetValue(xbt_self_thread_key,stub->res))
212 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
214 return (DWORD)stub->start_routine(stub->param);
218 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
221 xbt_thread_wrapper_for_restart_t stub = xbt_new0(s_xbt_thread_wrapper_for_restart_t,1);
223 stub->start_routine = start_routine ;
225 stub->res = xbt_new(s_xbt_thread_t,1);
227 stub->res->handle = CreateThread(NULL,0,
228 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
229 stub,0,&(stub->res->id));
231 if(!stub->res->handle) {
233 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
240 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
242 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
243 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
247 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
248 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
251 CloseHandle(thread->handle);
254 void xbt_thread_exit(int *retval) {
256 xbt_thread_t self = xbt_thread_self();
264 xbt_thread_t xbt_thread_self(void) {
265 return TlsGetValue(xbt_self_thread_key);
268 void xbt_thread_yield(void) {
272 /****** mutex related functions ******/
273 typedef struct xbt_mutex_ {
274 CRITICAL_SECTION lock;
277 xbt_mutex_t xbt_mutex_init(void) {
278 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
280 /* initialize the critical section object */
281 InitializeCriticalSection(&(res->lock));
286 void xbt_mutex_lock(xbt_mutex_t mutex) {
288 EnterCriticalSection(& mutex->lock);
291 void xbt_mutex_unlock(xbt_mutex_t mutex) {
293 LeaveCriticalSection (& mutex->lock);
297 void xbt_mutex_destroy(xbt_mutex_t mutex) {
301 DeleteCriticalSection(& mutex->lock);
305 /***** condition related functions *****/
312 typedef struct xbt_thcond_ {
313 HANDLE events[MAX_EVENTS];
315 unsigned int waiters_count; /* the number of waiters */
316 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
319 xbt_thcond_t xbt_thcond_init(void) {
321 xbt_thcond_t res = xbt_new0(s_xbt_thcond_t,1);
323 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
325 /* initialize the critical section object */
326 InitializeCriticalSection(& res->waiters_count_lock);
328 res->waiters_count = 0;
330 /* Create an auto-reset event */
331 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
333 if(!res->events[SIGNAL]){
334 DeleteCriticalSection(& res->waiters_count_lock);
336 THROW0(system_error,0,"CreateEvent failed for the signals");
339 /* Create a manual-reset event. */
340 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
342 if(!res->events[BROADCAST]){
344 DeleteCriticalSection(& res->waiters_count_lock);
345 CloseHandle(res->events[SIGNAL]);
347 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
353 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
355 unsigned long wait_result;
358 /* lock the threads counter and increment it */
359 EnterCriticalSection (& cond->waiters_count_lock);
360 cond->waiters_count++;
361 LeaveCriticalSection (& cond->waiters_count_lock);
363 /* unlock the mutex associate with the condition */
364 LeaveCriticalSection (& mutex->lock);
366 /* wait for a signal (broadcast or no) */
367 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
369 if(wait_result == WAIT_FAILED)
370 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
372 /* we have a signal lock the condition */
373 EnterCriticalSection (& cond->waiters_count_lock);
374 cond->waiters_count--;
376 /* it's the last waiter or it's a broadcast ? */
377 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
379 LeaveCriticalSection (& cond->waiters_count_lock);
381 /* yes it's the last waiter or it's a broadcast
382 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
386 if(!ResetEvent (cond->events[BROADCAST]))
387 THROW0(system_error,0,"ResetEvent failed");
389 /* relock the mutex associated with the condition in accordance with the posix thread specification */
390 EnterCriticalSection (& mutex->lock);
393 void xbt_thcond_signal(xbt_thcond_t cond) {
396 EnterCriticalSection (& cond->waiters_count_lock);
397 have_waiters = cond->waiters_count > 0;
398 LeaveCriticalSection (& cond->waiters_count_lock);
401 if(!SetEvent(cond->events[SIGNAL]))
402 THROW0(system_error,0,"SetEvent failed");
405 void xbt_thcond_broadcast(xbt_thcond_t cond){
408 EnterCriticalSection (& cond->waiters_count_lock);
409 have_waiters = cond->waiters_count > 0;
410 LeaveCriticalSection (& cond->waiters_count_lock);
413 SetEvent(cond->events[BROADCAST]);
416 void xbt_thcond_destroy(xbt_thcond_t cond){
421 if(!CloseHandle(cond->events[SIGNAL]))
424 if(!CloseHandle(cond->events[BROADCAST]))
427 DeleteCriticalSection(& cond->waiters_count_lock);
432 THROW0(system_error,0,"Error while destroying the condition");