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"
15 #include "xbt/ex_interface.h" /* We play crude games with exceptions */
17 #include "xbt/xbt_os_time.h" /* Portable time facilities */
18 #include "xbt/xbt_os_thread.h" /* This module */
19 #include "xbt_modinter.h" /* Initialization/finalization of this module */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os,xbt,"Synchronization mechanism (OS-level)");
23 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
27 #include <semaphore.h>
29 #ifdef HAVE_MUTEX_TIMEDLOCK
30 /* redefine the function header since we fail to get this from system headers on amd (at least) */
31 int pthread_mutex_timedlock(pthread_mutex_t *mutex,
32 const struct timespec *abs_timeout);
36 /* use named sempahore when sem_init() does not work */
38 static int next_sem_ID = 0;
39 static xbt_os_mutex_t next_sem_ID_lock;
42 typedef struct xbt_os_thread_ {
46 pvoid_f_pvoid_t start_routine;
49 static xbt_os_thread_t main_thread = NULL;
51 /* thread-specific data containing the xbt_os_thread_t structure */
52 static pthread_key_t xbt_self_thread_key;
53 static int thread_mod_inited = 0;
55 /* frees the xbt_os_thread_t corresponding to the current thread */
56 static void xbt_os_thread_free_thread_data(void*d){
60 /* callback: context fetching */
61 static ex_ctx_t *_os_thread_ex_ctx(void) {
62 return xbt_os_thread_self()->exception;
65 /* callback: termination */
66 static void _os_thread_ex_terminate(xbt_ex_t * e) {
70 /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
73 void xbt_os_thread_mod_init(void) {
76 if (thread_mod_inited)
79 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
80 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
82 main_thread=xbt_new(s_xbt_os_thread_t,1);
83 main_thread->name = (char*)"main";
84 main_thread->start_routine = NULL;
85 main_thread->param = NULL;
86 main_thread->exception = xbt_new(ex_ctx_t, 1);
87 XBT_CTX_INITIALIZE(main_thread->exception);
89 __xbt_ex_ctx = _os_thread_ex_ctx;
90 __xbt_ex_terminate = _os_thread_ex_terminate;
92 thread_mod_inited = 1;
95 next_sem_ID_lock = xbt_os_mutex_init();
99 void xbt_os_thread_mod_exit(void) {
100 /* FIXME: don't try to free our key on shutdown.
101 Valgrind detects no leak if we don't, and whine if we try to */
104 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
105 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
108 static void * wrapper_start_routine(void *s) {
109 xbt_os_thread_t t = s;
112 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
113 THROW0(system_error,errcode,
114 "pthread_setspecific failed for xbt_self_thread_key");
116 return (*(t->start_routine))(t->param);
119 xbt_os_thread_t xbt_os_thread_create(const char*name,
120 pvoid_f_pvoid_t start_routine,
124 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
125 res_thread->name = xbt_strdup(name);
126 res_thread->start_routine = start_routine;
127 res_thread->param = param;
128 res_thread->exception = xbt_new(ex_ctx_t, 1);
129 XBT_CTX_INITIALIZE(res_thread->exception);
131 if ((errcode = pthread_create(&(res_thread->t), NULL,
132 wrapper_start_routine, res_thread)))
133 THROW1(system_error,errcode,
134 "pthread_create failed: %s",strerror(errcode));
139 const char* xbt_os_thread_name(xbt_os_thread_t t) {
143 const char* xbt_os_thread_self_name(void) {
144 xbt_os_thread_t self = xbt_os_thread_self();
145 return self?self->name:"main";
148 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
152 if ((errcode = pthread_join(thread->t,thread_return)))
153 THROW1(system_error,errcode, "pthread_join failed: %s",
155 if (thread->exception)
156 free(thread->exception);
161 if (thread == main_thread) /* just killed main thread */
167 void xbt_os_thread_exit(int *retval) {
168 pthread_exit(retval);
171 xbt_os_thread_t xbt_os_thread_self(void) {
174 if (!thread_mod_inited)
177 res = pthread_getspecific(xbt_self_thread_key);
185 void xbt_os_thread_yield(void) {
188 void xbt_os_thread_cancel(xbt_os_thread_t t) {
189 pthread_cancel(t->t);
191 /****** mutex related functions ******/
192 typedef struct xbt_os_mutex_ {
193 /* KEEP IT IN SYNC WITH xbt_thread.c */
200 xbt_os_mutex_t xbt_os_mutex_init(void) {
201 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
204 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
205 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
211 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
214 if ((errcode=pthread_mutex_lock(&(mutex->m))))
215 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
216 mutex, strerror(errcode));
220 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
224 xbt_os_mutex_acquire(mutex);
226 } else if (delay == 0) {
227 errcode=pthread_mutex_trylock(&(mutex->m));
233 THROW1(timeout_error,0,"mutex %p not ready",mutex);
235 THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
241 #ifdef HAVE_MUTEX_TIMEDLOCK
242 struct timespec ts_end;
243 double end = delay + xbt_os_time();
245 ts_end.tv_sec = (time_t) floor(end);
246 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
247 DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
249 errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
251 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
252 double start = xbt_os_time();
254 errcode = pthread_mutex_trylock(&(mutex->m));
255 if (errcode == EBUSY)
256 xbt_os_thread_yield();
257 } while (errcode == EBUSY && xbt_os_time()-start <delay);
259 if (errcode == EBUSY)
262 #endif /* HAVE_MUTEX_TIMEDLOCK */
269 THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
272 THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
277 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
280 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
281 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
282 mutex, strerror(errcode));
285 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
290 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
291 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
292 mutex, strerror(errcode));
296 /***** condition related functions *****/
297 typedef struct xbt_os_cond_ {
298 /* KEEP IT IN SYNC WITH xbt_thread.c */
302 xbt_os_cond_t xbt_os_cond_init(void) {
303 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
305 if ((errcode=pthread_cond_init(&(res->c),NULL)))
306 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
312 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
314 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
315 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
316 cond,mutex, strerror(errcode));
320 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
322 struct timespec ts_end;
323 double end = delay + xbt_os_time();
326 xbt_os_cond_wait(cond,mutex);
328 ts_end.tv_sec = (time_t) floor(end);
329 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
330 DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
331 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
335 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
338 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
339 cond,mutex, delay, strerror(errcode));
344 void xbt_os_cond_signal(xbt_os_cond_t cond) {
346 if ((errcode=pthread_cond_signal(&(cond->c))))
347 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
348 cond, strerror(errcode));
351 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
353 if ((errcode=pthread_cond_broadcast(&(cond->c))))
354 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
355 cond, strerror(errcode));
357 void xbt_os_cond_destroy(xbt_os_cond_t cond){
362 if ((errcode=pthread_cond_destroy(&(cond->c))))
363 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
364 cond, strerror(errcode));
368 void *xbt_os_thread_getparam(void) {
369 xbt_os_thread_t t = xbt_os_thread_self();
370 return t?t->param:NULL;
373 typedef struct xbt_os_sem_ {
374 #ifndef HAVE_SEM_INIT
382 #define SEM_FAILED (-1)
386 xbt_os_sem_init(unsigned int value) {
387 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
389 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
390 * Any attempt to use it leads to ENOSYS (function not implemented).
391 * If such a prehistoric system is detected, do the job with sem_open instead
394 if(sem_init(&(res->s),0,value) != 0)
395 THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
398 #else /* damn, no sem_init(). Reimplement it */
400 xbt_os_mutex_acquire(next_sem_ID_lock);
401 res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
402 xbt_os_mutex_release(next_sem_ID_lock);
404 res->ps = sem_open(res->name, O_CREAT, 0644, value);
405 if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
406 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
407 res->name[13] = '\0';
408 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
410 if ((res->ps == (sem_t *)SEM_FAILED))
411 THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
413 /* Remove the name from the semaphore namespace: we never join on it */
414 if(sem_unlink(res->name) < 0)
415 THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
423 xbt_os_sem_acquire(xbt_os_sem_t sem) {
425 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
426 if(sem_wait(sem->ps) < 0)
427 THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
430 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
434 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
437 xbt_os_sem_acquire(sem);
438 } else if (delay==0) {
439 errcode = sem_trywait(sem->ps);
445 THROW1(timeout_error,0,"semaphore %p not ready",sem);
447 THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
452 struct timespec ts_end;
453 double end = delay + xbt_os_time();
455 ts_end.tv_sec = (time_t) floor(end);
456 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
457 DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
458 errcode = sem_timedwait(sem->s,&ts_end);
460 #else /* Okay, reimplement this function then */
461 double start = xbt_os_time();
463 errcode = sem_trywait(sem->ps);
464 if (errcode == EBUSY)
465 xbt_os_thread_yield();
466 } while (errcode == EBUSY && xbt_os_time()-start <delay);
468 if (errcode == EBUSY)
477 THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
480 THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
486 xbt_os_sem_release(xbt_os_sem_t sem)
489 THROW0(arg_error,EINVAL,"Cannot release of the NULL semaphore");
491 if(sem_post(sem->ps) < 0)
492 THROW1(system_error,errno,"sem_post() failed: %s",
497 xbt_os_sem_destroy(xbt_os_sem_t sem)
500 THROW0(arg_error,EINVAL,"Cannot destroy the NULL sempahore");
503 if(sem_destroy(sem->ps)) < 0)
504 THROW1(system_error,errno,"sem_destroy() failed: %s",
507 if(sem_close(sem->ps) < 0)
508 THROW1(system_error,errno,"sem_close() failed: %s",
517 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
520 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
522 if(sem_getvalue(&(sem->s),svalue) < 0)
523 THROW1(system_error,errno,"sem_getvalue() failed: %s",
527 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
533 typedef struct xbt_os_thread_ {
535 HANDLE handle; /* the win thread handle */
536 unsigned long id; /* the win thread id */
537 pvoid_f_pvoid_t start_routine;
539 } s_xbt_os_thread_t ;
541 /* so we can specify the size of the stack of the threads */
542 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
543 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
546 /* the default size of the stack of the threads (in bytes)*/
547 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
549 /* key to the TLS containing the xbt_os_thread_t structure */
550 static unsigned long xbt_self_thread_key;
552 void xbt_os_thread_mod_init(void) {
553 xbt_self_thread_key = TlsAlloc();
555 void xbt_os_thread_mod_exit(void) {
557 if (!TlsFree(xbt_self_thread_key))
558 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
561 static DWORD WINAPI wrapper_start_routine(void *s) {
562 xbt_os_thread_t t = (xbt_os_thread_t)s;
565 if(!TlsSetValue(xbt_self_thread_key,t))
566 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
568 rv = (*(t->start_routine))(t->param);
570 return *((DWORD*)rv);
574 xbt_os_thread_t xbt_os_thread_create(const char *name,pvoid_f_pvoid_t start_routine,
577 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
579 t->name = xbt_strdup(name);
580 t->start_routine = start_routine ;
583 t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
584 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
585 t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
589 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
595 const char* xbt_os_thread_name(xbt_os_thread_t t) {
599 const char* xbt_os_thread_self_name(void) {
600 xbt_os_thread_t t = xbt_os_thread_self();
601 return t?t->name:"main";
605 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
607 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
608 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
612 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
613 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
616 CloseHandle(thread->handle);
621 void xbt_os_thread_exit(int *retval) {
628 xbt_os_thread_t xbt_os_thread_self(void) {
629 return TlsGetValue(xbt_self_thread_key);
632 void *xbt_os_thread_getparam(void) {
633 xbt_os_thread_t t = xbt_os_thread_self();
638 void xbt_os_thread_yield(void) {
641 void xbt_os_thread_cancel(xbt_os_thread_t t) {
645 /****** mutex related functions ******/
646 typedef struct xbt_os_mutex_ {
647 /* KEEP IT IN SYNC WITH xbt_thread.c */
648 CRITICAL_SECTION lock;
651 xbt_os_mutex_t xbt_os_mutex_init(void) {
652 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
654 /* initialize the critical section object */
655 InitializeCriticalSection(&(res->lock));
660 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
661 EnterCriticalSection(& mutex->lock);
664 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
668 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
670 LeaveCriticalSection (&mutex->lock);
674 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
678 DeleteCriticalSection(& mutex->lock);
682 /***** condition related functions *****/
683 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
689 typedef struct xbt_os_cond_ {
690 /* KEEP IT IN SYNC WITH xbt_thread.c */
691 HANDLE events[MAX_EVENTS];
693 unsigned int waiters_count; /* the number of waiters */
694 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
697 xbt_os_cond_t xbt_os_cond_init(void) {
699 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
701 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
703 /* initialize the critical section object */
704 InitializeCriticalSection(& res->waiters_count_lock);
706 res->waiters_count = 0;
708 /* Create an auto-reset event */
709 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
711 if(!res->events[SIGNAL]){
712 DeleteCriticalSection(& res->waiters_count_lock);
714 THROW0(system_error,0,"CreateEvent failed for the signals");
717 /* Create a manual-reset event. */
718 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
720 if(!res->events[BROADCAST]){
722 DeleteCriticalSection(& res->waiters_count_lock);
723 CloseHandle(res->events[SIGNAL]);
725 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
731 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
733 unsigned long wait_result;
736 /* lock the threads counter and increment it */
737 EnterCriticalSection (& cond->waiters_count_lock);
738 cond->waiters_count++;
739 LeaveCriticalSection (& cond->waiters_count_lock);
741 /* unlock the mutex associate with the condition */
742 LeaveCriticalSection (& mutex->lock);
744 /* wait for a signal (broadcast or no) */
745 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
747 if(wait_result == WAIT_FAILED)
748 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
750 /* we have a signal lock the condition */
751 EnterCriticalSection (& cond->waiters_count_lock);
752 cond->waiters_count--;
754 /* it's the last waiter or it's a broadcast ? */
755 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
757 LeaveCriticalSection (& cond->waiters_count_lock);
759 /* yes it's the last waiter or it's a broadcast
760 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
764 if(!ResetEvent (cond->events[BROADCAST]))
765 THROW0(system_error,0,"ResetEvent failed");
767 /* relock the mutex associated with the condition in accordance with the posix thread specification */
768 EnterCriticalSection (& mutex->lock);
770 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
772 unsigned long wait_result = WAIT_TIMEOUT;
774 unsigned long end = (unsigned long)(delay * 1000);
778 xbt_os_cond_wait(cond,mutex);
780 DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
782 /* lock the threads counter and increment it */
783 EnterCriticalSection (& cond->waiters_count_lock);
784 cond->waiters_count++;
785 LeaveCriticalSection (& cond->waiters_count_lock);
787 /* unlock the mutex associate with the condition */
788 LeaveCriticalSection (& mutex->lock);
789 /* wait for a signal (broadcast or no) */
791 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
793 switch(wait_result) {
795 THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
797 THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
800 /* we have a signal lock the condition */
801 EnterCriticalSection (& cond->waiters_count_lock);
802 cond->waiters_count--;
804 /* it's the last waiter or it's a broadcast ? */
805 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
807 LeaveCriticalSection (& cond->waiters_count_lock);
809 /* yes it's the last waiter or it's a broadcast
810 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
814 if(!ResetEvent (cond->events[BROADCAST]))
815 THROW0(system_error,0,"ResetEvent failed");
817 /* relock the mutex associated with the condition in accordance with the posix thread specification */
818 EnterCriticalSection (& mutex->lock);
820 /*THROW_UNIMPLEMENTED;*/
823 void xbt_os_cond_signal(xbt_os_cond_t cond) {
826 EnterCriticalSection (& cond->waiters_count_lock);
827 have_waiters = cond->waiters_count > 0;
828 LeaveCriticalSection (& cond->waiters_count_lock);
831 if(!SetEvent(cond->events[SIGNAL]))
832 THROW0(system_error,0,"SetEvent failed");
834 xbt_os_thread_yield();
837 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
840 EnterCriticalSection (& cond->waiters_count_lock);
841 have_waiters = cond->waiters_count > 0;
842 LeaveCriticalSection (& cond->waiters_count_lock);
845 SetEvent(cond->events[BROADCAST]);
848 void xbt_os_cond_destroy(xbt_os_cond_t cond){
853 if(!CloseHandle(cond->events[SIGNAL]))
856 if(!CloseHandle(cond->events[BROADCAST]))
859 DeleteCriticalSection(& cond->waiters_count_lock);
864 THROW0(system_error,0,"Error while destroying the condition");
867 typedef struct xbt_os_sem_ {
870 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
874 # define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
878 xbt_os_sem_init(unsigned int value)
883 THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
885 res = (xbt_os_sem_t)xbt_new0(s_xbt_os_sem_t,1);
887 if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
888 THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
889 strerror(GetLastError()));
895 InitializeCriticalSection(&(res->value_lock));
901 xbt_os_sem_acquire(xbt_os_sem_t sem)
904 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
907 if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
908 THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
909 strerror(GetLastError()));
910 EnterCriticalSection(&(sem->value_lock));
912 LeaveCriticalSection(&(sem->value_lock));
915 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
919 double end = timeout + xbt_os_time();
922 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
926 xbt_os_sem_acquire(sem);
928 else /* timeout can be zero <-> try acquire ) */
931 seconds = (long) floor(end);
932 milliseconds = (long)( ( end - seconds) * 1000);
933 milliseconds += (seconds * 1000);
935 switch(WaitForSingleObject(sem->h,milliseconds))
938 EnterCriticalSection(&(sem->value_lock));
940 LeaveCriticalSection(&(sem->value_lock));
944 THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
948 THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
954 xbt_os_sem_release(xbt_os_sem_t sem)
957 THROW0(arg_error,EINVAL,"Cannot release the NULL semaphore");
959 if(!ReleaseSemaphore(sem->h,1, NULL))
960 THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
961 strerror(GetLastError()));
962 EnterCriticalSection (&(sem->value_lock));
964 LeaveCriticalSection(&(sem->value_lock));
968 xbt_os_sem_destroy(xbt_os_sem_t sem)
971 THROW0(arg_error,EINVAL,"Cannot destroy the NULL semaphore");
973 if(!CloseHandle(sem->h))
974 THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
975 strerror(GetLastError()));
977 DeleteCriticalSection(&(sem->value_lock));
984 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
987 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
989 EnterCriticalSection(&(sem->value_lock));
990 *svalue = sem->value;
991 LeaveCriticalSection(&(sem->value_lock));