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_ {
26 /* KEEP IT IN SYNC WITH xbt_thread.c */
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);
63 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
67 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
68 res_thread->start_routine = start_routine;
69 res_thread->param = param;
72 if ((errcode = pthread_create(&(res_thread->t), NULL, wrapper_start_routine, res_thread)))
73 THROW1(system_error,errcode, "pthread_create failed: %s",strerror(errcode));
79 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
83 if ((errcode = pthread_join(thread->t,thread_return)))
84 THROW1(system_error,errcode, "pthread_join failed: %s",
89 void xbt_os_thread_exit(int *retval) {
93 xbt_os_thread_t xbt_os_thread_self(void) {
94 return pthread_getspecific(xbt_self_thread_key);
98 void xbt_os_thread_yield(void) {
101 /****** mutex related functions ******/
102 typedef struct xbt_os_mutex_ {
103 /* KEEP IT IN SYNC WITH xbt_thread.c */
107 xbt_os_mutex_t xbt_os_mutex_init(void) {
108 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
111 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
112 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
118 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
121 if ((errcode=pthread_mutex_lock(&(mutex->m))))
122 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
123 mutex, strerror(errcode));
126 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
129 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
130 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
131 mutex, strerror(errcode));
134 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
139 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
140 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
141 mutex, strerror(errcode));
145 /***** condition related functions *****/
146 typedef struct xbt_os_cond_ {
147 /* KEEP IT IN SYNC WITH xbt_thread.c */
151 xbt_os_cond_t xbt_os_cond_init(void) {
152 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
154 if ((errcode=pthread_cond_init(&(res->c),NULL)))
155 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
161 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
163 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
164 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
165 cond,mutex, strerror(errcode));
168 void xbt_os_cond_signal(xbt_os_cond_t cond) {
170 if ((errcode=pthread_cond_signal(&(cond->c))))
171 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
172 cond, strerror(errcode));
175 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
177 if ((errcode=pthread_cond_broadcast(&(cond->c))))
178 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
179 cond, strerror(errcode));
181 void xbt_os_cond_destroy(xbt_os_cond_t cond){
186 if ((errcode=pthread_cond_destroy(&(cond->c))))
187 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
188 cond, strerror(errcode));
192 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
196 typedef struct xbt_os_thread_ {
197 /* KEEP IT IN SYNC WITH xbt_thread */
198 HANDLE handle; /* the win thread handle */
199 unsigned long id; /* the win thread id */
200 pvoid_f_pvoid_t *start_routine;
202 } s_xbt_os_thread_t ;
204 /* key to the TLS containing the xbt_os_thread_t structure */
205 static unsigned long xbt_self_thread_key;
207 void xbt_os_thread_mod_init(void) {
208 xbt_self_thread_key = TlsAlloc();
210 void xbt_os_thread_mod_exit(void) {
212 if (!TlsFree(xbt_self_thread_key))
213 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
216 static DWORD WINAPI wrapper_start_routine(void *s) {
217 xbt_os_thread_t t = (xbt_os_thread_t)s;
219 if(!TlsSetValue(xbt_self_thread_key,t))
220 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
222 return (DWORD)t->start_routine(t->param);
226 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
229 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
231 t->start_routine = start_routine ;
234 t->handle = CreateThread(NULL,0,
235 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
240 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
247 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
249 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
250 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
254 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
255 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
258 CloseHandle(thread->handle);
262 void xbt_os_thread_exit(int *retval) {
269 xbt_os_thread_t xbt_os_thread_self(void) {
270 return TlsGetValue(xbt_self_thread_key);
273 void xbt_os_thread_yield(void) {
277 /****** mutex related functions ******/
278 typedef struct xbt_os_mutex_ {
279 /* KEEP IT IN SYNC WITH xbt_thread.c */
280 CRITICAL_SECTION lock;
283 xbt_os_mutex_t xbt_os_mutex_init(void) {
284 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
286 /* initialize the critical section object */
287 InitializeCriticalSection(&(res->lock));
292 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
294 EnterCriticalSection(& mutex->lock);
297 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
299 LeaveCriticalSection (& mutex->lock);
303 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
307 DeleteCriticalSection(& mutex->lock);
311 /***** condition related functions *****/
312 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
318 typedef struct xbt_os_cond_ {
319 /* KEEP IT IN SYNC WITH xbt_thread.c */
320 HANDLE events[MAX_EVENTS];
322 unsigned int waiters_count; /* the number of waiters */
323 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
326 xbt_os_cond_t xbt_os_cond_init(void) {
328 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
330 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
332 /* initialize the critical section object */
333 InitializeCriticalSection(& res->waiters_count_lock);
335 res->waiters_count = 0;
337 /* Create an auto-reset event */
338 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
340 if(!res->events[SIGNAL]){
341 DeleteCriticalSection(& res->waiters_count_lock);
343 THROW0(system_error,0,"CreateEvent failed for the signals");
346 /* Create a manual-reset event. */
347 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
349 if(!res->events[BROADCAST]){
351 DeleteCriticalSection(& res->waiters_count_lock);
352 CloseHandle(res->events[SIGNAL]);
354 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
360 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
362 unsigned long wait_result;
365 /* lock the threads counter and increment it */
366 EnterCriticalSection (& cond->waiters_count_lock);
367 cond->waiters_count++;
368 LeaveCriticalSection (& cond->waiters_count_lock);
370 /* unlock the mutex associate with the condition */
371 LeaveCriticalSection (& mutex->lock);
373 /* wait for a signal (broadcast or no) */
374 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
376 if(wait_result == WAIT_FAILED)
377 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
379 /* we have a signal lock the condition */
380 EnterCriticalSection (& cond->waiters_count_lock);
381 cond->waiters_count--;
383 /* it's the last waiter or it's a broadcast ? */
384 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
386 LeaveCriticalSection (& cond->waiters_count_lock);
388 /* yes it's the last waiter or it's a broadcast
389 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
393 if(!ResetEvent (cond->events[BROADCAST]))
394 THROW0(system_error,0,"ResetEvent failed");
396 /* relock the mutex associated with the condition in accordance with the posix thread specification */
397 EnterCriticalSection (& mutex->lock);
400 void xbt_os_cond_signal(xbt_os_cond_t cond) {
403 EnterCriticalSection (& cond->waiters_count_lock);
404 have_waiters = cond->waiters_count > 0;
405 LeaveCriticalSection (& cond->waiters_count_lock);
408 if(!SetEvent(cond->events[SIGNAL]))
409 THROW0(system_error,0,"SetEvent failed");
411 xbt_os_thread_yield();
414 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
417 EnterCriticalSection (& cond->waiters_count_lock);
418 have_waiters = cond->waiters_count > 0;
419 LeaveCriticalSection (& cond->waiters_count_lock);
422 SetEvent(cond->events[BROADCAST]);
425 void xbt_os_cond_destroy(xbt_os_cond_t cond){
430 if(!CloseHandle(cond->events[SIGNAL]))
433 if(!CloseHandle(cond->events[BROADCAST]))
436 DeleteCriticalSection(& cond->waiters_count_lock);
441 THROW0(system_error,0,"Error while destroying the condition");