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");
106 free(main_thread->exception);
108 #ifndef HAVE_SEM_WAIT
109 xbt_os_mutex_destroy(next_sem_ID_lock);
113 static void * wrapper_start_routine(void *s) {
114 xbt_os_thread_t t = s;
117 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
118 THROW0(system_error,errcode,
119 "pthread_setspecific failed for xbt_self_thread_key");
121 return (*(t->start_routine))(t->param);
124 xbt_os_thread_t xbt_os_thread_create(const char*name,
125 pvoid_f_pvoid_t start_routine,
129 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
130 res_thread->name = xbt_strdup(name);
131 res_thread->start_routine = start_routine;
132 res_thread->param = param;
133 res_thread->exception = xbt_new(ex_ctx_t, 1);
134 XBT_CTX_INITIALIZE(res_thread->exception);
136 if ((errcode = pthread_create(&(res_thread->t), NULL,
137 wrapper_start_routine, res_thread)))
138 THROW1(system_error,errcode,
139 "pthread_create failed: %s",strerror(errcode));
144 const char* xbt_os_thread_name(xbt_os_thread_t t) {
148 const char* xbt_os_thread_self_name(void) {
149 xbt_os_thread_t self = xbt_os_thread_self();
150 return self?self->name:"main";
153 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
157 if ((errcode = pthread_join(thread->t,thread_return)))
158 THROW1(system_error,errcode, "pthread_join failed: %s",
160 if (thread->exception)
161 free(thread->exception);
166 if (thread == main_thread) /* just killed main thread */
172 void xbt_os_thread_exit(int *retval) {
173 pthread_exit(retval);
176 xbt_os_thread_t xbt_os_thread_self(void) {
179 if (!thread_mod_inited)
182 res = pthread_getspecific(xbt_self_thread_key);
190 void xbt_os_thread_yield(void) {
193 void xbt_os_thread_cancel(xbt_os_thread_t t) {
194 pthread_cancel(t->t);
196 /****** mutex related functions ******/
197 typedef struct xbt_os_mutex_ {
198 /* KEEP IT IN SYNC WITH xbt_thread.c */
205 xbt_os_mutex_t xbt_os_mutex_init(void) {
206 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
209 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
210 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
216 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
219 if ((errcode=pthread_mutex_lock(&(mutex->m))))
220 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
221 mutex, strerror(errcode));
225 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
229 xbt_os_mutex_acquire(mutex);
231 } else if (delay == 0) {
232 errcode=pthread_mutex_trylock(&(mutex->m));
238 THROW1(timeout_error,0,"mutex %p not ready",mutex);
240 THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
246 #ifdef HAVE_MUTEX_TIMEDLOCK
247 struct timespec ts_end;
248 double end = delay + xbt_os_time();
250 ts_end.tv_sec = (time_t) floor(end);
251 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
252 DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
254 errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
256 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
257 double start = xbt_os_time();
259 errcode = pthread_mutex_trylock(&(mutex->m));
260 if (errcode == EBUSY)
261 xbt_os_thread_yield();
262 } while (errcode == EBUSY && xbt_os_time()-start <delay);
264 if (errcode == EBUSY)
267 #endif /* HAVE_MUTEX_TIMEDLOCK */
274 THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
277 THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
282 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
285 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
286 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
287 mutex, strerror(errcode));
290 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
295 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
296 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
297 mutex, strerror(errcode));
301 /***** condition related functions *****/
302 typedef struct xbt_os_cond_ {
303 /* KEEP IT IN SYNC WITH xbt_thread.c */
307 xbt_os_cond_t xbt_os_cond_init(void) {
308 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
310 if ((errcode=pthread_cond_init(&(res->c),NULL)))
311 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
317 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
319 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
320 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
321 cond,mutex, strerror(errcode));
325 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
327 struct timespec ts_end;
328 double end = delay + xbt_os_time();
331 xbt_os_cond_wait(cond,mutex);
333 ts_end.tv_sec = (time_t) floor(end);
334 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
335 DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
336 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
340 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
343 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
344 cond,mutex, delay, strerror(errcode));
349 void xbt_os_cond_signal(xbt_os_cond_t cond) {
351 if ((errcode=pthread_cond_signal(&(cond->c))))
352 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
353 cond, strerror(errcode));
356 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
358 if ((errcode=pthread_cond_broadcast(&(cond->c))))
359 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
360 cond, strerror(errcode));
362 void xbt_os_cond_destroy(xbt_os_cond_t cond){
367 if ((errcode=pthread_cond_destroy(&(cond->c))))
368 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
369 cond, strerror(errcode));
373 void *xbt_os_thread_getparam(void) {
374 xbt_os_thread_t t = xbt_os_thread_self();
375 return t?t->param:NULL;
378 typedef struct xbt_os_sem_ {
379 #ifndef HAVE_SEM_INIT
387 #define SEM_FAILED (-1)
391 xbt_os_sem_init(unsigned int value) {
392 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
394 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
395 * Any attempt to use it leads to ENOSYS (function not implemented).
396 * If such a prehistoric system is detected, do the job with sem_open instead
399 if(sem_init(&(res->s),0,value) != 0)
400 THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
403 #else /* damn, no sem_init(). Reimplement it */
405 xbt_os_mutex_acquire(next_sem_ID_lock);
406 res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
407 xbt_os_mutex_release(next_sem_ID_lock);
409 res->ps = sem_open(res->name, O_CREAT, 0644, value);
410 if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
411 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
412 res->name[13] = '\0';
413 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
415 if ((res->ps == (sem_t *)SEM_FAILED))
416 THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
418 /* Remove the name from the semaphore namespace: we never join on it */
419 if(sem_unlink(res->name) < 0)
420 THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
428 xbt_os_sem_acquire(xbt_os_sem_t sem) {
430 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
431 if(sem_wait(sem->ps) < 0)
432 THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
435 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
439 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
442 xbt_os_sem_acquire(sem);
443 } else if (delay==0) {
444 errcode = sem_trywait(sem->ps);
450 THROW1(timeout_error,0,"semaphore %p not ready",sem);
452 THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
457 struct timespec ts_end;
458 double end = delay + xbt_os_time();
460 ts_end.tv_sec = (time_t) floor(end);
461 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
462 DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
463 errcode = sem_timedwait(sem->s,&ts_end);
465 #else /* Okay, reimplement this function then */
466 double start = xbt_os_time();
468 errcode = sem_trywait(sem->ps);
469 if (errcode == EBUSY)
470 xbt_os_thread_yield();
471 } while (errcode == EBUSY && xbt_os_time()-start <delay);
473 if (errcode == EBUSY)
482 THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
485 THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
491 xbt_os_sem_release(xbt_os_sem_t sem)
494 THROW0(arg_error,EINVAL,"Cannot release of the NULL semaphore");
496 if(sem_post(sem->ps) < 0)
497 THROW1(system_error,errno,"sem_post() failed: %s",
502 xbt_os_sem_destroy(xbt_os_sem_t sem)
505 THROW0(arg_error,EINVAL,"Cannot destroy the NULL sempahore");
508 if(sem_destroy(sem->ps)) < 0)
509 THROW1(system_error,errno,"sem_destroy() failed: %s",
512 if(sem_close(sem->ps) < 0)
513 THROW1(system_error,errno,"sem_close() failed: %s",
522 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
525 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
527 if(sem_getvalue(&(sem->s),svalue) < 0)
528 THROW1(system_error,errno,"sem_getvalue() failed: %s",
532 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
538 typedef struct xbt_os_thread_ {
540 HANDLE handle; /* the win thread handle */
541 unsigned long id; /* the win thread id */
542 pvoid_f_pvoid_t start_routine;
544 } s_xbt_os_thread_t ;
546 /* so we can specify the size of the stack of the threads */
547 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
548 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
551 /* the default size of the stack of the threads (in bytes)*/
552 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
554 /* key to the TLS containing the xbt_os_thread_t structure */
555 static unsigned long xbt_self_thread_key;
557 void xbt_os_thread_mod_init(void) {
558 xbt_self_thread_key = TlsAlloc();
560 void xbt_os_thread_mod_exit(void) {
562 if (!TlsFree(xbt_self_thread_key))
563 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
566 static DWORD WINAPI wrapper_start_routine(void *s) {
567 xbt_os_thread_t t = (xbt_os_thread_t)s;
570 if(!TlsSetValue(xbt_self_thread_key,t))
571 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
573 rv = (*(t->start_routine))(t->param);
575 return *((DWORD*)rv);
579 xbt_os_thread_t xbt_os_thread_create(const char *name,pvoid_f_pvoid_t start_routine,
582 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
584 t->name = xbt_strdup(name);
585 t->start_routine = start_routine ;
588 t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
589 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
590 t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
594 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
600 const char* xbt_os_thread_name(xbt_os_thread_t t) {
604 const char* xbt_os_thread_self_name(void) {
605 xbt_os_thread_t t = xbt_os_thread_self();
606 return t?t->name:"main";
610 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
612 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
613 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
617 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
618 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
621 CloseHandle(thread->handle);
626 void xbt_os_thread_exit(int *retval) {
633 xbt_os_thread_t xbt_os_thread_self(void) {
634 return TlsGetValue(xbt_self_thread_key);
637 void *xbt_os_thread_getparam(void) {
638 xbt_os_thread_t t = xbt_os_thread_self();
643 void xbt_os_thread_yield(void) {
646 void xbt_os_thread_cancel(xbt_os_thread_t t) {
650 /****** mutex related functions ******/
651 typedef struct xbt_os_mutex_ {
652 /* KEEP IT IN SYNC WITH xbt_thread.c */
653 CRITICAL_SECTION lock;
656 xbt_os_mutex_t xbt_os_mutex_init(void) {
657 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
659 /* initialize the critical section object */
660 InitializeCriticalSection(&(res->lock));
665 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
666 EnterCriticalSection(& mutex->lock);
669 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
673 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
675 LeaveCriticalSection (&mutex->lock);
679 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
683 DeleteCriticalSection(& mutex->lock);
687 /***** condition related functions *****/
688 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
694 typedef struct xbt_os_cond_ {
695 /* KEEP IT IN SYNC WITH xbt_thread.c */
696 HANDLE events[MAX_EVENTS];
698 unsigned int waiters_count; /* the number of waiters */
699 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
702 xbt_os_cond_t xbt_os_cond_init(void) {
704 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
706 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
708 /* initialize the critical section object */
709 InitializeCriticalSection(& res->waiters_count_lock);
711 res->waiters_count = 0;
713 /* Create an auto-reset event */
714 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
716 if(!res->events[SIGNAL]){
717 DeleteCriticalSection(& res->waiters_count_lock);
719 THROW0(system_error,0,"CreateEvent failed for the signals");
722 /* Create a manual-reset event. */
723 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
725 if(!res->events[BROADCAST]){
727 DeleteCriticalSection(& res->waiters_count_lock);
728 CloseHandle(res->events[SIGNAL]);
730 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
736 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
738 unsigned long wait_result;
741 /* lock the threads counter and increment it */
742 EnterCriticalSection (& cond->waiters_count_lock);
743 cond->waiters_count++;
744 LeaveCriticalSection (& cond->waiters_count_lock);
746 /* unlock the mutex associate with the condition */
747 LeaveCriticalSection (& mutex->lock);
749 /* wait for a signal (broadcast or no) */
750 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
752 if(wait_result == WAIT_FAILED)
753 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
755 /* we have a signal lock the condition */
756 EnterCriticalSection (& cond->waiters_count_lock);
757 cond->waiters_count--;
759 /* it's the last waiter or it's a broadcast ? */
760 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
762 LeaveCriticalSection (& cond->waiters_count_lock);
764 /* yes it's the last waiter or it's a broadcast
765 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
769 if(!ResetEvent (cond->events[BROADCAST]))
770 THROW0(system_error,0,"ResetEvent failed");
772 /* relock the mutex associated with the condition in accordance with the posix thread specification */
773 EnterCriticalSection (& mutex->lock);
775 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
777 unsigned long wait_result = WAIT_TIMEOUT;
779 unsigned long end = (unsigned long)(delay * 1000);
783 xbt_os_cond_wait(cond,mutex);
785 DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
787 /* lock the threads counter and increment it */
788 EnterCriticalSection (& cond->waiters_count_lock);
789 cond->waiters_count++;
790 LeaveCriticalSection (& cond->waiters_count_lock);
792 /* unlock the mutex associate with the condition */
793 LeaveCriticalSection (& mutex->lock);
794 /* wait for a signal (broadcast or no) */
796 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
798 switch(wait_result) {
800 THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
802 THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
805 /* we have a signal lock the condition */
806 EnterCriticalSection (& cond->waiters_count_lock);
807 cond->waiters_count--;
809 /* it's the last waiter or it's a broadcast ? */
810 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
812 LeaveCriticalSection (& cond->waiters_count_lock);
814 /* yes it's the last waiter or it's a broadcast
815 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
819 if(!ResetEvent (cond->events[BROADCAST]))
820 THROW0(system_error,0,"ResetEvent failed");
822 /* relock the mutex associated with the condition in accordance with the posix thread specification */
823 EnterCriticalSection (& mutex->lock);
825 /*THROW_UNIMPLEMENTED;*/
828 void xbt_os_cond_signal(xbt_os_cond_t cond) {
831 EnterCriticalSection (& cond->waiters_count_lock);
832 have_waiters = cond->waiters_count > 0;
833 LeaveCriticalSection (& cond->waiters_count_lock);
836 if(!SetEvent(cond->events[SIGNAL]))
837 THROW0(system_error,0,"SetEvent failed");
839 xbt_os_thread_yield();
842 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
845 EnterCriticalSection (& cond->waiters_count_lock);
846 have_waiters = cond->waiters_count > 0;
847 LeaveCriticalSection (& cond->waiters_count_lock);
850 SetEvent(cond->events[BROADCAST]);
853 void xbt_os_cond_destroy(xbt_os_cond_t cond){
858 if(!CloseHandle(cond->events[SIGNAL]))
861 if(!CloseHandle(cond->events[BROADCAST]))
864 DeleteCriticalSection(& cond->waiters_count_lock);
869 THROW0(system_error,0,"Error while destroying the condition");
872 typedef struct xbt_os_sem_ {
875 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
879 # define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
883 xbt_os_sem_init(unsigned int value)
888 THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
890 res = (xbt_os_sem_t)xbt_new0(s_xbt_os_sem_t,1);
892 if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
893 THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
894 strerror(GetLastError()));
900 InitializeCriticalSection(&(res->value_lock));
906 xbt_os_sem_acquire(xbt_os_sem_t sem)
909 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
912 if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
913 THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
914 strerror(GetLastError()));
915 EnterCriticalSection(&(sem->value_lock));
917 LeaveCriticalSection(&(sem->value_lock));
920 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
924 double end = timeout + xbt_os_time();
927 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
931 xbt_os_sem_acquire(sem);
933 else /* timeout can be zero <-> try acquire ) */
936 seconds = (long) floor(end);
937 milliseconds = (long)( ( end - seconds) * 1000);
938 milliseconds += (seconds * 1000);
940 switch(WaitForSingleObject(sem->h,milliseconds))
943 EnterCriticalSection(&(sem->value_lock));
945 LeaveCriticalSection(&(sem->value_lock));
949 THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
953 THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
959 xbt_os_sem_release(xbt_os_sem_t sem)
962 THROW0(arg_error,EINVAL,"Cannot release the NULL semaphore");
964 if(!ReleaseSemaphore(sem->h,1, NULL))
965 THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
966 strerror(GetLastError()));
967 EnterCriticalSection (&(sem->value_lock));
969 LeaveCriticalSection(&(sem->value_lock));
973 xbt_os_sem_destroy(xbt_os_sem_t sem)
976 THROW0(arg_error,EINVAL,"Cannot destroy the NULL semaphore");
978 if(!CloseHandle(sem->h))
979 THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
980 strerror(GetLastError()));
982 DeleteCriticalSection(&(sem->value_lock));
989 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
992 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
994 EnterCriticalSection(&(sem->value_lock));
995 *svalue = sem->value;
996 LeaveCriticalSection(&(sem->value_lock));