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);
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));
170 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
172 struct timespec ts_end;
173 double end = delay + xbt_os_time();
174 ts_end.tv_sec = (time_t) floor(end);
175 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
176 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
178 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
181 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
182 cond,mutex, delay, strerror(errcode));
186 void xbt_os_cond_signal(xbt_os_cond_t cond) {
188 if ((errcode=pthread_cond_signal(&(cond->c))))
189 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
190 cond, strerror(errcode));
193 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
195 if ((errcode=pthread_cond_broadcast(&(cond->c))))
196 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
197 cond, strerror(errcode));
199 void xbt_os_cond_destroy(xbt_os_cond_t cond){
204 if ((errcode=pthread_cond_destroy(&(cond->c))))
205 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
206 cond, strerror(errcode));
210 void *xbt_os_thread_getparam(void) {
211 xbt_os_thread_t t = xbt_os_thread_self();
215 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
219 typedef struct xbt_os_thread_ {
220 HANDLE handle; /* the win thread handle */
221 unsigned long id; /* the win thread id */
222 pvoid_f_pvoid_t *start_routine;
224 } s_xbt_os_thread_t ;
226 /* key to the TLS containing the xbt_os_thread_t structure */
227 static unsigned long xbt_self_thread_key;
229 void xbt_os_thread_mod_init(void) {
230 xbt_self_thread_key = TlsAlloc();
232 void xbt_os_thread_mod_exit(void) {
234 if (!TlsFree(xbt_self_thread_key))
235 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
238 static DWORD WINAPI wrapper_start_routine(void *s) {
239 xbt_os_thread_t t = (xbt_os_thread_t)s;
241 if(!TlsSetValue(xbt_self_thread_key,t))
242 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
244 return (DWORD)t->start_routine(t->param);
248 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine,
251 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
253 t->start_routine = start_routine ;
256 t->handle = CreateThread(NULL,0,
257 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
262 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
269 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
271 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
272 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
276 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
277 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
280 CloseHandle(thread->handle);
284 void xbt_os_thread_exit(int *retval) {
291 xbt_os_thread_t xbt_os_thread_self(void) {
292 return TlsGetValue(xbt_self_thread_key);
295 void *xbt_os_thread_getparam(void) {
296 xbt_os_thread_t t = xbt_os_thread_self();
301 void xbt_os_thread_yield(void) {
305 /****** mutex related functions ******/
306 typedef struct xbt_os_mutex_ {
307 /* KEEP IT IN SYNC WITH xbt_thread.c */
308 CRITICAL_SECTION lock;
311 xbt_os_mutex_t xbt_os_mutex_init(void) {
312 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
314 /* initialize the critical section object */
315 InitializeCriticalSection(&(res->lock));
320 void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
322 EnterCriticalSection(& mutex->lock);
325 void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
327 LeaveCriticalSection (& mutex->lock);
331 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
335 DeleteCriticalSection(& mutex->lock);
339 /***** condition related functions *****/
340 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
346 typedef struct xbt_os_cond_ {
347 /* KEEP IT IN SYNC WITH xbt_thread.c */
348 HANDLE events[MAX_EVENTS];
350 unsigned int waiters_count; /* the number of waiters */
351 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
354 xbt_os_cond_t xbt_os_cond_init(void) {
356 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
358 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
360 /* initialize the critical section object */
361 InitializeCriticalSection(& res->waiters_count_lock);
363 res->waiters_count = 0;
365 /* Create an auto-reset event */
366 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
368 if(!res->events[SIGNAL]){
369 DeleteCriticalSection(& res->waiters_count_lock);
371 THROW0(system_error,0,"CreateEvent failed for the signals");
374 /* Create a manual-reset event. */
375 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
377 if(!res->events[BROADCAST]){
379 DeleteCriticalSection(& res->waiters_count_lock);
380 CloseHandle(res->events[SIGNAL]);
382 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
388 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
390 unsigned long wait_result;
393 /* lock the threads counter and increment it */
394 EnterCriticalSection (& cond->waiters_count_lock);
395 cond->waiters_count++;
396 LeaveCriticalSection (& cond->waiters_count_lock);
398 /* unlock the mutex associate with the condition */
399 LeaveCriticalSection (& mutex->lock);
401 /* wait for a signal (broadcast or no) */
402 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
404 if(wait_result == WAIT_FAILED)
405 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
407 /* we have a signal lock the condition */
408 EnterCriticalSection (& cond->waiters_count_lock);
409 cond->waiters_count--;
411 /* it's the last waiter or it's a broadcast ? */
412 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
414 LeaveCriticalSection (& cond->waiters_count_lock);
416 /* yes it's the last waiter or it's a broadcast
417 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
421 if(!ResetEvent (cond->events[BROADCAST]))
422 THROW0(system_error,0,"ResetEvent failed");
424 /* relock the mutex associated with the condition in accordance with the posix thread specification */
425 EnterCriticalSection (& mutex->lock);
427 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
431 void xbt_os_cond_signal(xbt_os_cond_t cond) {
434 EnterCriticalSection (& cond->waiters_count_lock);
435 have_waiters = cond->waiters_count > 0;
436 LeaveCriticalSection (& cond->waiters_count_lock);
439 if(!SetEvent(cond->events[SIGNAL]))
440 THROW0(system_error,0,"SetEvent failed");
442 xbt_os_thread_yield();
445 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
448 EnterCriticalSection (& cond->waiters_count_lock);
449 have_waiters = cond->waiters_count > 0;
450 LeaveCriticalSection (& cond->waiters_count_lock);
453 SetEvent(cond->events[BROADCAST]);
456 void xbt_os_cond_destroy(xbt_os_cond_t cond){
461 if(!CloseHandle(cond->events[SIGNAL]))
464 if(!CloseHandle(cond->events[BROADCAST]))
467 DeleteCriticalSection(& cond->waiters_count_lock);
472 THROW0(system_error,0,"Error while destroying the condition");