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. */
14 /* XOPEN_SOURCE is needed to get sem_timedwait (on amd64 at least). Declare it before everything else to play safe. */
15 #define _XOPEN_SOURCE 600
18 #include "xbt/sysdep.h"
20 #include "xbt/ex_interface.h" /* We play crude games with exceptions */
22 #include "xbt/xbt_os_time.h" /* Portable time facilities */
23 #include "xbt/xbt_os_thread.h" /* This module */
24 #include "xbt_modinter.h" /* Initialization/finalization of this module */
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os,xbt,"Synchronization mechanism (OS-level)");
28 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
31 /* XOPEN_SOURCE is needed to get sem_timedwait (on amd64 at least) according to the man page,
32 but the headers seem to follow __USE_XOPEN2K.
33 So let's get safe and declare both before loading headers. */
34 #define _XOPEN_SOURCE 600
38 #include <semaphore.h>
41 /* use named sempahore when sem_init() does not work */
43 static int next_sem_ID = 0;
44 static xbt_os_mutex_t next_sem_ID_lock;
47 typedef struct xbt_os_thread_ {
51 pvoid_f_pvoid_t start_routine;
54 static xbt_os_thread_t main_thread = NULL;
56 /* thread-specific data containing the xbt_os_thread_t structure */
57 static pthread_key_t xbt_self_thread_key;
58 static int thread_mod_inited = 0;
60 /* frees the xbt_os_thread_t corresponding to the current thread */
61 static void xbt_os_thread_free_thread_data(void*d){
65 /* callback: context fetching */
66 static ex_ctx_t *_os_thread_ex_ctx(void) {
67 return xbt_os_thread_self()->exception;
70 /* callback: termination */
71 static void _os_thread_ex_terminate(xbt_ex_t * e) {
75 /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
78 void xbt_os_thread_mod_init(void) {
81 if (thread_mod_inited)
84 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
85 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
87 main_thread=xbt_new(s_xbt_os_thread_t,1);
88 main_thread->name = (char*)"main";
89 main_thread->start_routine = NULL;
90 main_thread->param = NULL;
91 main_thread->exception = xbt_new(ex_ctx_t, 1);
92 XBT_CTX_INITIALIZE(main_thread->exception);
94 __xbt_ex_ctx = _os_thread_ex_ctx;
95 __xbt_ex_terminate = _os_thread_ex_terminate;
97 thread_mod_inited = 1;
100 next_sem_ID_lock = xbt_os_mutex_init();
104 void xbt_os_thread_mod_exit(void) {
105 /* FIXME: don't try to free our key on shutdown.
106 Valgrind detects no leak if we don't, and whine if we try to */
109 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
110 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
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);
163 if (thread == main_thread) /* just killed main thread */
169 void xbt_os_thread_exit(int *retval) {
170 pthread_exit(retval);
173 xbt_os_thread_t xbt_os_thread_self(void) {
176 if (!thread_mod_inited)
179 res = pthread_getspecific(xbt_self_thread_key);
187 void xbt_os_thread_yield(void) {
190 void xbt_os_thread_cancel(xbt_os_thread_t t) {
191 pthread_cancel(t->t);
193 /****** mutex related functions ******/
194 typedef struct xbt_os_mutex_ {
195 /* KEEP IT IN SYNC WITH xbt_thread.c */
202 xbt_os_mutex_t xbt_os_mutex_init(void) {
203 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
206 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
207 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
213 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
216 if ((errcode=pthread_mutex_lock(&(mutex->m))))
217 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
218 mutex, strerror(errcode));
222 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
226 xbt_os_mutex_acquire(mutex);
228 } else if (delay == 0) {
229 errcode=pthread_mutex_trylock(&(mutex->m));
235 THROW1(timeout_error,0,"mutex %p not ready",mutex);
237 THROW2(system_error,errcode,"xbt_mutex_tryacquire(%p) failed: %s",mutex, strerror(errcode));
243 #ifdef HAVE_MUTEX_TIMEDLOCK
244 struct timespec ts_end;
245 double end = delay + xbt_os_time();
247 ts_end.tv_sec = (time_t) floor(end);
248 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
249 DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
251 errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
253 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
254 double start = xbt_os_time();
256 errcode = pthread_mutex_trylock(&(mutex->m));
257 if (errcode == EBUSY)
258 xbt_os_thread_yield();
259 } while (errcode == EBUSY && xbt_os_time()-start <delay);
261 if (errcode == EBUSY)
264 #endif /* HAVE_MUTEX_TIMEDLOCK */
271 THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
274 THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
279 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
282 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
283 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
284 mutex, strerror(errcode));
287 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
292 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
293 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
294 mutex, strerror(errcode));
298 /***** condition related functions *****/
299 typedef struct xbt_os_cond_ {
300 /* KEEP IT IN SYNC WITH xbt_thread.c */
304 xbt_os_cond_t xbt_os_cond_init(void) {
305 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
307 if ((errcode=pthread_cond_init(&(res->c),NULL)))
308 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
314 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
316 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
317 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
318 cond,mutex, strerror(errcode));
322 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
324 struct timespec ts_end;
325 double end = delay + xbt_os_time();
328 xbt_os_cond_wait(cond,mutex);
330 ts_end.tv_sec = (time_t) floor(end);
331 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
332 DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
333 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
337 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
340 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
341 cond,mutex, delay, strerror(errcode));
346 void xbt_os_cond_signal(xbt_os_cond_t cond) {
348 if ((errcode=pthread_cond_signal(&(cond->c))))
349 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
350 cond, strerror(errcode));
353 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
355 if ((errcode=pthread_cond_broadcast(&(cond->c))))
356 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
357 cond, strerror(errcode));
359 void xbt_os_cond_destroy(xbt_os_cond_t cond){
364 if ((errcode=pthread_cond_destroy(&(cond->c))))
365 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
366 cond, strerror(errcode));
370 void *xbt_os_thread_getparam(void) {
371 xbt_os_thread_t t = xbt_os_thread_self();
372 return t?t->param:NULL;
375 typedef struct xbt_os_sem_ {
376 #ifndef HAVE_SEM_INIT
384 #define SEM_FAILED (-1)
388 xbt_os_sem_init(unsigned int value) {
389 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
391 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
392 * Any attempt to use it leads to ENOSYS (function not implemented).
393 * If such a prehistoric system is detected, do the job with sem_open instead
396 if(sem_init(&(res->s),0,value) != 0)
397 THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
400 #else /* damn, no sem_init(). Reimplement it */
402 xbt_os_mutex_acquire(next_sem_ID_lock);
403 res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
404 xbt_os_mutex_release(next_sem_ID_lock);
406 res->ps = sem_open(res->name, O_CREAT, 0644, value);
407 if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
408 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
409 res->name[13] = '\0';
410 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
412 if ((res->ps == (sem_t *)SEM_FAILED))
413 THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
415 /* Remove the name from the semaphore namespace: we never join on it */
416 if(sem_unlink(res->name) < 0)
417 THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
425 xbt_os_sem_acquire(xbt_os_sem_t sem) {
427 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
428 if(sem_wait(sem->ps) < 0)
429 THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
432 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
436 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
439 xbt_os_sem_acquire(sem);
440 } else if (delay==0) {
441 errcode = sem_trywait(sem->ps);
447 THROW1(timeout_error,0,"semaphore %p not ready",sem);
449 THROW2(system_error,errcode,"xbt_sem_tryacquire(%p) failed: %s",sem, strerror(errcode));
454 struct timespec ts_end;
455 double end = delay + xbt_os_time();
457 ts_end.tv_sec = (time_t) floor(end);
458 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
459 DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
460 errcode = sem_timedwait(sem->s,&ts_end);
462 #else /* Okay, reimplement this function then */
463 double start = xbt_os_time();
465 errcode = sem_trywait(sem->ps);
466 if (errcode == EBUSY)
467 xbt_os_thread_yield();
468 } while (errcode == EBUSY && xbt_os_time()-start <delay);
470 if (errcode == EBUSY)
479 THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
482 THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
488 xbt_os_sem_release(xbt_os_sem_t sem)
491 THROW0(arg_error,EINVAL,"Cannot release of the NULL semaphore");
493 if(sem_post(sem->ps) < 0)
494 THROW1(system_error,errno,"sem_post() failed: %s",
499 xbt_os_sem_destroy(xbt_os_sem_t sem)
502 THROW0(arg_error,EINVAL,"Cannot destroy the NULL sempahore");
505 if(sem_destroy(sem->ps)) < 0)
506 THROW1(system_error,errno,"sem_destroy() failed: %s",
509 if(sem_close(sem->ps) < 0)
510 THROW1(system_error,errno,"sem_close() failed: %s",
519 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
522 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
524 if(sem_getvalue(&(sem->s),svalue) < 0)
525 THROW1(system_error,errno,"sem_getvalue() failed: %s",
529 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
535 typedef struct xbt_os_thread_ {
537 HANDLE handle; /* the win thread handle */
538 unsigned long id; /* the win thread id */
539 pvoid_f_pvoid_t start_routine;
541 } s_xbt_os_thread_t ;
543 /* so we can specify the size of the stack of the threads */
544 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
545 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
548 /* the default size of the stack of the threads (in bytes)*/
549 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
551 /* key to the TLS containing the xbt_os_thread_t structure */
552 static unsigned long xbt_self_thread_key;
554 void xbt_os_thread_mod_init(void) {
555 xbt_self_thread_key = TlsAlloc();
557 void xbt_os_thread_mod_exit(void) {
559 if (!TlsFree(xbt_self_thread_key))
560 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
563 static DWORD WINAPI wrapper_start_routine(void *s) {
564 xbt_os_thread_t t = (xbt_os_thread_t)s;
567 if(!TlsSetValue(xbt_self_thread_key,t))
568 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
570 rv = (*(t->start_routine))(t->param);
572 return *((DWORD*)rv);
576 xbt_os_thread_t xbt_os_thread_create(const char *name,pvoid_f_pvoid_t start_routine,
579 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
581 t->name = xbt_strdup(name);
582 t->start_routine = start_routine ;
585 t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
586 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
587 t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
591 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
597 const char* xbt_os_thread_name(xbt_os_thread_t t) {
601 const char* xbt_os_thread_self_name(void) {
602 xbt_os_thread_t t = xbt_os_thread_self();
603 return t?t->name:"main";
607 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
609 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
610 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
614 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
615 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
618 CloseHandle(thread->handle);
623 void xbt_os_thread_exit(int *retval) {
630 xbt_os_thread_t xbt_os_thread_self(void) {
631 return TlsGetValue(xbt_self_thread_key);
634 void *xbt_os_thread_getparam(void) {
635 xbt_os_thread_t t = xbt_os_thread_self();
640 void xbt_os_thread_yield(void) {
643 void xbt_os_thread_cancel(xbt_os_thread_t t) {
647 /****** mutex related functions ******/
648 typedef struct xbt_os_mutex_ {
649 /* KEEP IT IN SYNC WITH xbt_thread.c */
650 CRITICAL_SECTION lock;
653 xbt_os_mutex_t xbt_os_mutex_init(void) {
654 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
656 /* initialize the critical section object */
657 InitializeCriticalSection(&(res->lock));
662 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
664 EnterCriticalSection(& mutex->lock);
667 void xbt_os_mutex_tryacquire(xbt_os_mutex_t mutex)
669 TryEnterCriticalSection(&mutex->lock);
672 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
676 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
678 LeaveCriticalSection (&mutex->lock);
682 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
686 DeleteCriticalSection(& mutex->lock);
690 /***** condition related functions *****/
691 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
697 typedef struct xbt_os_cond_ {
698 /* KEEP IT IN SYNC WITH xbt_thread.c */
699 HANDLE events[MAX_EVENTS];
701 unsigned int waiters_count; /* the number of waiters */
702 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
705 xbt_os_cond_t xbt_os_cond_init(void) {
707 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
709 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
711 /* initialize the critical section object */
712 InitializeCriticalSection(& res->waiters_count_lock);
714 res->waiters_count = 0;
716 /* Create an auto-reset event */
717 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
719 if(!res->events[SIGNAL]){
720 DeleteCriticalSection(& res->waiters_count_lock);
722 THROW0(system_error,0,"CreateEvent failed for the signals");
725 /* Create a manual-reset event. */
726 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
728 if(!res->events[BROADCAST]){
730 DeleteCriticalSection(& res->waiters_count_lock);
731 CloseHandle(res->events[SIGNAL]);
733 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
739 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
741 unsigned long wait_result;
744 /* lock the threads counter and increment it */
745 EnterCriticalSection (& cond->waiters_count_lock);
746 cond->waiters_count++;
747 LeaveCriticalSection (& cond->waiters_count_lock);
749 /* unlock the mutex associate with the condition */
750 LeaveCriticalSection (& mutex->lock);
752 /* wait for a signal (broadcast or no) */
753 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
755 if(wait_result == WAIT_FAILED)
756 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
758 /* we have a signal lock the condition */
759 EnterCriticalSection (& cond->waiters_count_lock);
760 cond->waiters_count--;
762 /* it's the last waiter or it's a broadcast ? */
763 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
765 LeaveCriticalSection (& cond->waiters_count_lock);
767 /* yes it's the last waiter or it's a broadcast
768 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
772 if(!ResetEvent (cond->events[BROADCAST]))
773 THROW0(system_error,0,"ResetEvent failed");
775 /* relock the mutex associated with the condition in accordance with the posix thread specification */
776 EnterCriticalSection (& mutex->lock);
778 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
780 unsigned long wait_result = WAIT_TIMEOUT;
782 unsigned long end = (unsigned long)(delay * 1000);
786 xbt_os_cond_wait(cond,mutex);
788 DEBUG3("xbt_cond_timedwait(%p,%p,%ul)",&(cond->events),&(mutex->lock),end);
790 /* lock the threads counter and increment it */
791 EnterCriticalSection (& cond->waiters_count_lock);
792 cond->waiters_count++;
793 LeaveCriticalSection (& cond->waiters_count_lock);
795 /* unlock the mutex associate with the condition */
796 LeaveCriticalSection (& mutex->lock);
797 /* wait for a signal (broadcast or no) */
799 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
801 switch(wait_result) {
803 THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
805 THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
808 /* we have a signal lock the condition */
809 EnterCriticalSection (& cond->waiters_count_lock);
810 cond->waiters_count--;
812 /* it's the last waiter or it's a broadcast ? */
813 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
815 LeaveCriticalSection (& cond->waiters_count_lock);
817 /* yes it's the last waiter or it's a broadcast
818 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
822 if(!ResetEvent (cond->events[BROADCAST]))
823 THROW0(system_error,0,"ResetEvent failed");
825 /* relock the mutex associated with the condition in accordance with the posix thread specification */
826 EnterCriticalSection (& mutex->lock);
828 /*THROW_UNIMPLEMENTED;*/
831 void xbt_os_cond_signal(xbt_os_cond_t cond) {
834 EnterCriticalSection (& cond->waiters_count_lock);
835 have_waiters = cond->waiters_count > 0;
836 LeaveCriticalSection (& cond->waiters_count_lock);
839 if(!SetEvent(cond->events[SIGNAL]))
840 THROW0(system_error,0,"SetEvent failed");
842 xbt_os_thread_yield();
845 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
848 EnterCriticalSection (& cond->waiters_count_lock);
849 have_waiters = cond->waiters_count > 0;
850 LeaveCriticalSection (& cond->waiters_count_lock);
853 SetEvent(cond->events[BROADCAST]);
856 void xbt_os_cond_destroy(xbt_os_cond_t cond){
861 if(!CloseHandle(cond->events[SIGNAL]))
864 if(!CloseHandle(cond->events[BROADCAST]))
867 DeleteCriticalSection(& cond->waiters_count_lock);
872 THROW0(system_error,0,"Error while destroying the condition");
875 typedef struct xbt_os_sem_ {
878 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
882 xbt_os_sem_init(unsigned int value)
887 THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
889 res = (xbt_os_sem_t)xbt_new0(s_xbt_os_sem_t,1);
891 if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
892 THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
893 strerror(GetLastError()));
899 InitializeCriticalSection(&(res->value_lock));
905 xbt_os_sem_acquire(xbt_os_sem_t sem)
908 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
911 if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
912 THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
913 strerror(GetLastError()));
914 EnterCriticalSection(&(sem->value_lock));
916 LeaveCriticalSection(&(sem->value_lock));
919 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
923 double end = timeout + xbt_os_time();
926 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
930 xbt_os_sem_acquire(sem);
932 else /* timeout can be zero <-> try acquire ) */
935 seconds = (long) floor(end);
936 milliseconds = (long)( ( end - seconds) * 1000);
937 milliseconds += (seconds * 1000);
939 switch(WaitForSingleObject(sem->h,milliseconds))
942 EnterCriticalSection(&(sem->value_lock));
944 LeaveCriticalSection(&(sem->value_lock));
948 THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
952 THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
958 xbt_os_sem_release(xbt_os_sem_t sem)
961 THROW0(arg_error,EINVAL,"Cannot release the NULL semaphore");
963 if(!ReleaseSemaphore(sem->h,1, NULL))
964 THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
965 strerror(GetLastError()));
966 EnterCriticalSection (&(sem->value_lock));
968 LeaveCriticalSection(&(sem->value_lock));
972 xbt_os_sem_destroy(xbt_os_sem_t sem)
975 THROW0(arg_error,EINVAL,"Cannot destroy the NULL semaphore");
977 if(!CloseHandle(sem->h))
978 THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
979 strerror(GetLastError()));
981 DeleteCriticalSection(&(sem->value_lock));
988 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
991 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
993 EnterCriticalSection(&(sem->value_lock));
994 *svalue = sem->value;
995 LeaveCriticalSection(&(sem->value_lock));