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");
85 void xbt_thread_exit(int *retval) {
89 xbt_thread_t xbt_thread_self(void) {
90 return pthread_getspecific(xbt_self_thread_key);
94 void xbt_thread_yield(void) {
97 /****** mutex related functions ******/
98 typedef struct xbt_mutex_ {
102 xbt_mutex_t xbt_mutex_init(void) {
103 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
106 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
107 THROW0(system_error,errcode,"pthread_mutex_init() failed");
112 void xbt_mutex_lock(xbt_mutex_t mutex) {
115 if ((errcode=pthread_mutex_lock(&(mutex->m))))
116 THROW1(system_error,errcode,"pthread_mutex_lock(%p) failed",mutex);
119 void xbt_mutex_unlock(xbt_mutex_t mutex) {
122 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
123 THROW1(system_error,errcode,"pthread_mutex_unlock(%p) failed",mutex);
126 void xbt_mutex_destroy(xbt_mutex_t mutex) {
131 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
132 THROW1(system_error,errcode,"pthread_mutex_destroy(%p) failed",mutex);
136 /***** condition related functions *****/
137 typedef struct xbt_thcond_ {
141 xbt_thcond_t xbt_thcond_init(void) {
142 xbt_thcond_t res = xbt_new(s_xbt_thcond_t,1);
144 if ((errcode=pthread_cond_init(&(res->c),NULL)))
145 THROW0(system_error,errcode,"pthread_cond_init() failed");
150 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
152 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
153 THROW2(system_error,errcode,"pthread_cond_wait(%p,%p) failed",cond,mutex);
156 void xbt_thcond_signal(xbt_thcond_t cond) {
158 if ((errcode=pthread_cond_signal(&(cond->c))))
159 THROW1(system_error,errcode,"pthread_cond_signal(%p) failed",cond);
162 void xbt_thcond_broadcast(xbt_thcond_t cond){
164 if ((errcode=pthread_cond_broadcast(&(cond->c))))
165 THROW1(system_error,errcode,"pthread_cond_broadcast(%p) failed",cond);
167 void xbt_thcond_destroy(xbt_thcond_t cond){
172 if ((errcode=pthread_cond_destroy(&(cond->c))))
173 THROW1(system_error,errcode,"pthread_cond_destroy(%p) failed",cond);
177 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
181 typedef struct xbt_thread_ {
182 HANDLE handle; /* the win thread handle */
183 unsigned long id; /* the win thread id */
184 pvoid_f_pvoid_t *start_routine;
188 /* key to the TLS containing the xbt_thread_t structure */
189 static unsigned long xbt_self_thread_key;
191 void xbt_thread_mod_init(void) {
192 xbt_self_thread_key = TlsAlloc();
194 void xbt_thread_mod_exit(void) {
196 if (!TlsFree(xbt_self_thread_key))
197 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
200 static DWORD WINAPI wrapper_start_routine(void *s) {
201 xbt_thread_t t = (xbt_thread_t)s;
203 if(!TlsSetValue(xbt_self_thread_key,t))
204 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
206 return (DWORD)t->start_routine(t->param);
210 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
213 xbt_thread_t t = xbt_new(s_xbt_thread_t,1);
215 t->start_routine = start_routine ;
218 t->handle = CreateThread(NULL,0,
219 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
224 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
231 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
233 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
234 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
238 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
239 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
242 CloseHandle(thread->handle);
245 void xbt_thread_exit(int *retval) {
252 xbt_thread_t xbt_thread_self(void) {
253 return TlsGetValue(xbt_self_thread_key);
256 void xbt_thread_yield(void) {
260 /****** mutex related functions ******/
261 typedef struct xbt_mutex_ {
262 CRITICAL_SECTION lock;
265 xbt_mutex_t xbt_mutex_init(void) {
266 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
268 /* initialize the critical section object */
269 InitializeCriticalSection(&(res->lock));
274 void xbt_mutex_lock(xbt_mutex_t mutex) {
276 EnterCriticalSection(& mutex->lock);
279 void xbt_mutex_unlock(xbt_mutex_t mutex) {
281 LeaveCriticalSection (& mutex->lock);
285 void xbt_mutex_destroy(xbt_mutex_t mutex) {
289 DeleteCriticalSection(& mutex->lock);
293 /***** condition related functions *****/
300 typedef struct xbt_thcond_ {
301 HANDLE events[MAX_EVENTS];
303 unsigned int waiters_count; /* the number of waiters */
304 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
307 xbt_thcond_t xbt_thcond_init(void) {
309 xbt_thcond_t res = xbt_new0(s_xbt_thcond_t,1);
311 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
313 /* initialize the critical section object */
314 InitializeCriticalSection(& res->waiters_count_lock);
316 res->waiters_count = 0;
318 /* Create an auto-reset event */
319 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
321 if(!res->events[SIGNAL]){
322 DeleteCriticalSection(& res->waiters_count_lock);
324 THROW0(system_error,0,"CreateEvent failed for the signals");
327 /* Create a manual-reset event. */
328 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
330 if(!res->events[BROADCAST]){
332 DeleteCriticalSection(& res->waiters_count_lock);
333 CloseHandle(res->events[SIGNAL]);
335 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
341 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
343 unsigned long wait_result;
346 /* lock the threads counter and increment it */
347 EnterCriticalSection (& cond->waiters_count_lock);
348 cond->waiters_count++;
349 LeaveCriticalSection (& cond->waiters_count_lock);
351 /* unlock the mutex associate with the condition */
352 LeaveCriticalSection (& mutex->lock);
354 /* wait for a signal (broadcast or no) */
355 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
357 if(wait_result == WAIT_FAILED)
358 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
360 /* we have a signal lock the condition */
361 EnterCriticalSection (& cond->waiters_count_lock);
362 cond->waiters_count--;
364 /* it's the last waiter or it's a broadcast ? */
365 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
367 LeaveCriticalSection (& cond->waiters_count_lock);
369 /* yes it's the last waiter or it's a broadcast
370 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
374 if(!ResetEvent (cond->events[BROADCAST]))
375 THROW0(system_error,0,"ResetEvent failed");
377 /* relock the mutex associated with the condition in accordance with the posix thread specification */
378 EnterCriticalSection (& mutex->lock);
381 void xbt_thcond_signal(xbt_thcond_t cond) {
384 EnterCriticalSection (& cond->waiters_count_lock);
385 have_waiters = cond->waiters_count > 0;
386 LeaveCriticalSection (& cond->waiters_count_lock);
389 if(!SetEvent(cond->events[SIGNAL]))
390 THROW0(system_error,0,"SetEvent failed");
395 void xbt_thcond_broadcast(xbt_thcond_t cond){
398 EnterCriticalSection (& cond->waiters_count_lock);
399 have_waiters = cond->waiters_count > 0;
400 LeaveCriticalSection (& cond->waiters_count_lock);
403 SetEvent(cond->events[BROADCAST]);
406 void xbt_thcond_destroy(xbt_thcond_t cond){
411 if(!CloseHandle(cond->events[SIGNAL]))
414 if(!CloseHandle(cond->events[BROADCAST]))
417 DeleteCriticalSection(& cond->waiters_count_lock);
422 THROW0(system_error,0,"Error while destroying the condition");