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_time.h" /* Portable time facilities */
17 #include "xbt/xbt_os_thread.h" /* This module */
18 #include "xbt_modinter.h" /* Initialization/finalization of this module */
22 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
26 typedef struct xbt_os_thread_ {
29 pvoid_f_pvoid_t *start_routine;
32 /* thread-specific data containing the xbt_os_thread_t structure */
33 static pthread_key_t xbt_self_thread_key;
35 /* frees the xbt_os_thread_t corresponding to the current thread */
36 static void xbt_os_thread_free_thread_data(void*d){
40 void xbt_os_thread_mod_init(void) {
43 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
44 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
46 void xbt_os_thread_mod_exit(void) {
47 /* FIXME: don't try to free our key on shutdown. Valgrind detects no leak if we don't, and whine if we try to */
50 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
51 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
54 static void * wrapper_start_routine(void *s) {
55 xbt_os_thread_t t = s;
58 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
59 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
60 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 void xbt_os_thread_cancel(xbt_os_thread_t t) {
101 pthread_cancel(t->t);
103 /****** mutex related functions ******/
104 typedef struct xbt_os_mutex_ {
105 /* KEEP IT IN SYNC WITH xbt_thread.c */
109 xbt_os_mutex_t xbt_os_mutex_init(void) {
110 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
113 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
114 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
120 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
123 if ((errcode=pthread_mutex_lock(&(mutex->m))))
124 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
125 mutex, strerror(errcode));
128 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
131 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
132 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
133 mutex, strerror(errcode));
136 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
141 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
142 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
143 mutex, strerror(errcode));
147 /***** condition related functions *****/
148 typedef struct xbt_os_cond_ {
149 /* KEEP IT IN SYNC WITH xbt_thread.c */
153 xbt_os_cond_t xbt_os_cond_init(void) {
154 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
156 if ((errcode=pthread_cond_init(&(res->c),NULL)))
157 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
163 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
165 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
166 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
167 cond,mutex, strerror(errcode));
172 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
174 struct timespec ts_end;
175 double end = delay + xbt_os_time();
176 ts_end.tv_sec = (time_t) floor(end);
177 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
178 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
180 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
183 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
184 cond,mutex, delay, strerror(errcode));
188 void xbt_os_cond_signal(xbt_os_cond_t cond) {
190 if ((errcode=pthread_cond_signal(&(cond->c))))
191 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
192 cond, strerror(errcode));
195 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
197 if ((errcode=pthread_cond_broadcast(&(cond->c))))
198 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
199 cond, strerror(errcode));
201 void xbt_os_cond_destroy(xbt_os_cond_t cond){
206 if ((errcode=pthread_cond_destroy(&(cond->c))))
207 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
208 cond, strerror(errcode));
212 void *xbt_os_thread_getparam(void) {
213 xbt_os_thread_t t = xbt_os_thread_self();
217 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
221 typedef struct xbt_os_thread_ {
222 HANDLE handle; /* the win thread handle */
223 unsigned long id; /* the win thread id */
224 pvoid_f_pvoid_t *start_routine;
226 } s_xbt_os_thread_t ;
228 /* key to the TLS containing the xbt_os_thread_t structure */
229 static unsigned long xbt_self_thread_key;
231 void xbt_os_thread_mod_init(void) {
232 xbt_self_thread_key = TlsAlloc();
234 void xbt_os_thread_mod_exit(void) {
236 if (!TlsFree(xbt_self_thread_key))
237 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
240 static DWORD WINAPI wrapper_start_routine(void *s) {
241 xbt_os_thread_t t = (xbt_os_thread_t)s;
243 if(!TlsSetValue(xbt_self_thread_key,t))
244 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
246 return (DWORD)t->start_routine(t->param);
250 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
253 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
255 t->start_routine = start_routine ;
258 t->handle = CreateThread(NULL,0,
259 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
264 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
271 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
273 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
274 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
278 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
279 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
282 CloseHandle(thread->handle);
286 void xbt_os_thread_exit(int *retval) {
293 xbt_os_thread_t xbt_os_thread_self(void) {
294 return TlsGetValue(xbt_self_thread_key);
297 void *xbt_os_thread_getparam(void) {
298 xbt_os_thread_t t = xbt_os_thread_self();
303 void xbt_os_thread_yield(void) {
306 void xbt_os_thread_cancel(xbt_os_thread_t t) {
310 /****** mutex related functions ******/
311 typedef struct xbt_os_mutex_ {
312 /* KEEP IT IN SYNC WITH xbt_thread.c */
313 CRITICAL_SECTION lock;
316 xbt_os_mutex_t xbt_os_mutex_init(void) {
317 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
319 /* initialize the critical section object */
320 InitializeCriticalSection(&(res->lock));
325 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
327 EnterCriticalSection(& mutex->lock);
330 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
332 LeaveCriticalSection (& mutex->lock);
336 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
340 DeleteCriticalSection(& mutex->lock);
344 /***** condition related functions *****/
345 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
351 typedef struct xbt_os_cond_ {
352 /* KEEP IT IN SYNC WITH xbt_thread.c */
353 HANDLE events[MAX_EVENTS];
355 unsigned int waiters_count; /* the number of waiters */
356 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
359 xbt_os_cond_t xbt_os_cond_init(void) {
361 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
363 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
365 /* initialize the critical section object */
366 InitializeCriticalSection(& res->waiters_count_lock);
368 res->waiters_count = 0;
370 /* Create an auto-reset event */
371 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
373 if(!res->events[SIGNAL]){
374 DeleteCriticalSection(& res->waiters_count_lock);
376 THROW0(system_error,0,"CreateEvent failed for the signals");
379 /* Create a manual-reset event. */
380 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
382 if(!res->events[BROADCAST]){
384 DeleteCriticalSection(& res->waiters_count_lock);
385 CloseHandle(res->events[SIGNAL]);
387 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
393 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
395 unsigned long wait_result;
398 /* lock the threads counter and increment it */
399 EnterCriticalSection (& cond->waiters_count_lock);
400 cond->waiters_count++;
401 LeaveCriticalSection (& cond->waiters_count_lock);
403 /* unlock the mutex associate with the condition */
404 LeaveCriticalSection (& mutex->lock);
406 /* wait for a signal (broadcast or no) */
407 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
409 if(wait_result == WAIT_FAILED)
410 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
412 /* we have a signal lock the condition */
413 EnterCriticalSection (& cond->waiters_count_lock);
414 cond->waiters_count--;
416 /* it's the last waiter or it's a broadcast ? */
417 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
419 LeaveCriticalSection (& cond->waiters_count_lock);
421 /* yes it's the last waiter or it's a broadcast
422 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
426 if(!ResetEvent (cond->events[BROADCAST]))
427 THROW0(system_error,0,"ResetEvent failed");
429 /* relock the mutex associated with the condition in accordance with the posix thread specification */
430 EnterCriticalSection (& mutex->lock);
432 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
436 void xbt_os_cond_signal(xbt_os_cond_t cond) {
439 EnterCriticalSection (& cond->waiters_count_lock);
440 have_waiters = cond->waiters_count > 0;
441 LeaveCriticalSection (& cond->waiters_count_lock);
444 if(!SetEvent(cond->events[SIGNAL]))
445 THROW0(system_error,0,"SetEvent failed");
447 xbt_os_thread_yield();
450 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
453 EnterCriticalSection (& cond->waiters_count_lock);
454 have_waiters = cond->waiters_count > 0;
455 LeaveCriticalSection (& cond->waiters_count_lock);
458 SetEvent(cond->events[BROADCAST]);
461 void xbt_os_cond_destroy(xbt_os_cond_t cond){
466 if(!CloseHandle(cond->events[SIGNAL]))
469 if(!CloseHandle(cond->events[BROADCAST]))
472 DeleteCriticalSection(& cond->waiters_count_lock);
477 THROW0(system_error,0,"Error while destroying the condition");