3 /* xbt_os_thread -- portability layer over the pthread API */
4 /* Used in RL to get win/lin portability, and in SG when CONTEXT_THREAD */
5 /* in SG, when using CONTEXT_UCONTEXT, xbt_os_thread_stub is used instead */
7 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
8 * All right 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"
16 #include "xbt/xbt_os_thread.h" /* This module */
17 #include "xbt_modinter.h" /* Initialization/finalization of this module */
21 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
25 typedef struct xbt_os_thread_ {
28 pvoid_f_pvoid_t *start_routine;
31 /* thread-specific data containing the xbt_os_thread_t structure */
32 static pthread_key_t xbt_self_thread_key;
34 /* frees the xbt_os_thread_t corresponding to the current thread */
35 static void xbt_os_thread_free_thread_data(void*d){
39 void xbt_os_thread_mod_init(void) {
42 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
43 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
45 void xbt_os_thread_mod_exit(void) {
46 /* FIXME: don't try to free our key on shutdown. Valgrind detects no leak if we don't, and whine if we try to */
49 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
50 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
53 static void * wrapper_start_routine(void *s) {
54 xbt_os_thread_t t = s;
57 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
58 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
59 return t->start_routine(t->param);
62 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
66 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
67 res_thread->start_routine = start_routine;
68 res_thread->param = param;
71 if ((errcode = pthread_create(&(res_thread->t), NULL, wrapper_start_routine, res_thread)))
72 THROW1(system_error,errcode, "pthread_create failed: %s",strerror(errcode));
78 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
82 if ((errcode = pthread_join(thread->t,thread_return)))
83 THROW1(system_error,errcode, "pthread_join failed: %s",
88 void xbt_os_thread_exit(int *retval) {
92 xbt_os_thread_t xbt_os_thread_self(void) {
93 return pthread_getspecific(xbt_self_thread_key);
97 void xbt_os_thread_yield(void) {
100 /****** mutex related functions ******/
101 typedef struct xbt_os_mutex_ {
105 xbt_os_mutex_t xbt_os_mutex_init(void) {
106 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
109 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
110 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
116 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
119 if ((errcode=pthread_mutex_lock(&(mutex->m))))
120 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
121 mutex, strerror(errcode));
124 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
127 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
128 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
129 mutex, strerror(errcode));
132 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
137 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
138 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
139 mutex, strerror(errcode));
143 /***** condition related functions *****/
144 typedef struct xbt_os_cond_ {
148 xbt_os_cond_t xbt_os_cond_init(void) {
149 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
151 if ((errcode=pthread_cond_init(&(res->c),NULL)))
152 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
158 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
160 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
161 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
162 cond,mutex, strerror(errcode));
165 void xbt_os_cond_signal(xbt_os_cond_t cond) {
167 if ((errcode=pthread_cond_signal(&(cond->c))))
168 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
169 cond, strerror(errcode));
172 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
174 if ((errcode=pthread_cond_broadcast(&(cond->c))))
175 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
176 cond, strerror(errcode));
178 void xbt_os_cond_destroy(xbt_os_cond_t cond){
183 if ((errcode=pthread_cond_destroy(&(cond->c))))
184 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
185 cond, strerror(errcode));
189 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
193 typedef struct xbt_os_thread_ {
194 HANDLE handle; /* the win thread handle */
195 unsigned long id; /* the win thread id */
196 pvoid_f_pvoid_t *start_routine;
198 } s_xbt_os_thread_t ;
200 /* key to the TLS containing the xbt_os_thread_t structure */
201 static unsigned long xbt_self_thread_key;
203 void xbt_os_thread_mod_init(void) {
204 xbt_self_thread_key = TlsAlloc();
206 void xbt_os_thread_mod_exit(void) {
208 if (!TlsFree(xbt_self_thread_key))
209 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
212 static DWORD WINAPI wrapper_start_routine(void *s) {
213 xbt_os_thread_t t = (xbt_os_thread_t)s;
215 if(!TlsSetValue(xbt_self_thread_key,t))
216 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
218 return (DWORD)t->start_routine(t->param);
222 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
225 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
227 t->start_routine = start_routine ;
230 t->handle = CreateThread(NULL,0,
231 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
236 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
243 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
245 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
246 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
250 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
251 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
254 CloseHandle(thread->handle);
258 void xbt_os_thread_exit(int *retval) {
265 xbt_os_thread_t xbt_os_thread_self(void) {
266 return TlsGetValue(xbt_self_thread_key);
269 void xbt_os_thread_yield(void) {
273 /****** mutex related functions ******/
274 typedef struct xbt_os_mutex_ {
275 CRITICAL_SECTION lock;
278 xbt_os_mutex_t xbt_os_mutex_init(void) {
279 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
281 /* initialize the critical section object */
282 InitializeCriticalSection(&(res->lock));
287 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
289 EnterCriticalSection(& mutex->lock);
292 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
294 LeaveCriticalSection (& mutex->lock);
298 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
302 DeleteCriticalSection(& mutex->lock);
306 /***** condition related functions *****/
313 typedef struct xbt_os_cond_ {
314 HANDLE events[MAX_EVENTS];
316 unsigned int waiters_count; /* the number of waiters */
317 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
320 xbt_os_cond_t xbt_os_cond_init(void) {
322 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
324 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
326 /* initialize the critical section object */
327 InitializeCriticalSection(& res->waiters_count_lock);
329 res->waiters_count = 0;
331 /* Create an auto-reset event */
332 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
334 if(!res->events[SIGNAL]){
335 DeleteCriticalSection(& res->waiters_count_lock);
337 THROW0(system_error,0,"CreateEvent failed for the signals");
340 /* Create a manual-reset event. */
341 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
343 if(!res->events[BROADCAST]){
345 DeleteCriticalSection(& res->waiters_count_lock);
346 CloseHandle(res->events[SIGNAL]);
348 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
354 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
356 unsigned long wait_result;
359 /* lock the threads counter and increment it */
360 EnterCriticalSection (& cond->waiters_count_lock);
361 cond->waiters_count++;
362 LeaveCriticalSection (& cond->waiters_count_lock);
364 /* unlock the mutex associate with the condition */
365 LeaveCriticalSection (& mutex->lock);
367 /* wait for a signal (broadcast or no) */
368 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
370 if(wait_result == WAIT_FAILED)
371 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
373 /* we have a signal lock the condition */
374 EnterCriticalSection (& cond->waiters_count_lock);
375 cond->waiters_count--;
377 /* it's the last waiter or it's a broadcast ? */
378 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
380 LeaveCriticalSection (& cond->waiters_count_lock);
382 /* yes it's the last waiter or it's a broadcast
383 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
387 if(!ResetEvent (cond->events[BROADCAST]))
388 THROW0(system_error,0,"ResetEvent failed");
390 /* relock the mutex associated with the condition in accordance with the posix thread specification */
391 EnterCriticalSection (& mutex->lock);
394 void xbt_os_cond_signal(xbt_os_cond_t cond) {
397 EnterCriticalSection (& cond->waiters_count_lock);
398 have_waiters = cond->waiters_count > 0;
399 LeaveCriticalSection (& cond->waiters_count_lock);
402 if(!SetEvent(cond->events[SIGNAL]))
403 THROW0(system_error,0,"SetEvent failed");
405 xbt_os_thread_yield();
408 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
411 EnterCriticalSection (& cond->waiters_count_lock);
412 have_waiters = cond->waiters_count > 0;
413 LeaveCriticalSection (& cond->waiters_count_lock);
416 SetEvent(cond->events[BROADCAST]);
419 void xbt_os_cond_destroy(xbt_os_cond_t cond){
424 if(!CloseHandle(cond->events[SIGNAL]))
427 if(!CloseHandle(cond->events[BROADCAST]))
430 DeleteCriticalSection(& cond->waiters_count_lock);
435 THROW0(system_error,0,"Error while destroying the condition");