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);
110 #ifndef HAVE_SEM_WAIT
111 xbt_os_mutex_destroy(next_sem_ID_lock);
115 static void * wrapper_start_routine(void *s) {
116 xbt_os_thread_t t = s;
119 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
120 THROW0(system_error,errcode,
121 "pthread_setspecific failed for xbt_self_thread_key");
123 return (*(t->start_routine))(t->param);
126 xbt_os_thread_t xbt_os_thread_create(const char*name,
127 pvoid_f_pvoid_t start_routine,
131 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
132 res_thread->name = xbt_strdup(name);
133 res_thread->start_routine = start_routine;
134 res_thread->param = param;
135 res_thread->exception = xbt_new(ex_ctx_t, 1);
136 XBT_CTX_INITIALIZE(res_thread->exception);
138 if ((errcode = pthread_create(&(res_thread->t), NULL,
139 wrapper_start_routine, res_thread)))
140 THROW1(system_error,errcode,
141 "pthread_create failed: %s",strerror(errcode));
146 const char* xbt_os_thread_name(xbt_os_thread_t t) {
150 const char* xbt_os_thread_self_name(void) {
151 xbt_os_thread_t self = xbt_os_thread_self();
152 return self?self->name:"main";
155 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
159 if ((errcode = pthread_join(thread->t,thread_return)))
160 THROW1(system_error,errcode, "pthread_join failed: %s",
162 if (thread->exception)
163 free(thread->exception);
168 if (thread == main_thread) /* just killed main thread */
174 void xbt_os_thread_exit(int *retval) {
175 pthread_exit(retval);
178 xbt_os_thread_t xbt_os_thread_self(void) {
181 if (!thread_mod_inited)
184 res = pthread_getspecific(xbt_self_thread_key);
192 void xbt_os_thread_yield(void) {
195 void xbt_os_thread_cancel(xbt_os_thread_t t) {
196 pthread_cancel(t->t);
198 /****** mutex related functions ******/
199 typedef struct xbt_os_mutex_ {
200 /* KEEP IT IN SYNC WITH xbt_thread.c */
207 xbt_os_mutex_t xbt_os_mutex_init(void) {
208 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
211 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
212 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
218 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
221 if ((errcode=pthread_mutex_lock(&(mutex->m))))
222 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
223 mutex, strerror(errcode));
227 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
231 xbt_os_mutex_acquire(mutex);
233 } else if (delay == 0) {
234 errcode=pthread_mutex_trylock(&(mutex->m));
240 THROW1(timeout_error,0,"mutex %p not ready",mutex);
242 THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
248 #ifdef HAVE_MUTEX_TIMEDLOCK
249 struct timespec ts_end;
250 double end = delay + xbt_os_time();
252 ts_end.tv_sec = (time_t) floor(end);
253 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
254 DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
256 errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
258 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
259 double start = xbt_os_time();
261 errcode = pthread_mutex_trylock(&(mutex->m));
262 if (errcode == EBUSY)
263 xbt_os_thread_yield();
264 } while (errcode == EBUSY && xbt_os_time()-start <delay);
266 if (errcode == EBUSY)
269 #endif /* HAVE_MUTEX_TIMEDLOCK */
276 THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
279 THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
284 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
287 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
288 THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
289 mutex, strerror(errcode));
292 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
297 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
298 THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
299 mutex, strerror(errcode));
303 /***** condition related functions *****/
304 typedef struct xbt_os_cond_ {
305 /* KEEP IT IN SYNC WITH xbt_thread.c */
309 xbt_os_cond_t xbt_os_cond_init(void) {
310 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
312 if ((errcode=pthread_cond_init(&(res->c),NULL)))
313 THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
319 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
321 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
322 THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
323 cond,mutex, strerror(errcode));
327 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
329 struct timespec ts_end;
330 double end = delay + xbt_os_time();
333 xbt_os_cond_wait(cond,mutex);
335 ts_end.tv_sec = (time_t) floor(end);
336 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
337 DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
338 switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
342 THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
345 THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
346 cond,mutex, delay, strerror(errcode));
351 void xbt_os_cond_signal(xbt_os_cond_t cond) {
353 if ((errcode=pthread_cond_signal(&(cond->c))))
354 THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
355 cond, strerror(errcode));
358 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
360 if ((errcode=pthread_cond_broadcast(&(cond->c))))
361 THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
362 cond, strerror(errcode));
364 void xbt_os_cond_destroy(xbt_os_cond_t cond){
369 if ((errcode=pthread_cond_destroy(&(cond->c))))
370 THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
371 cond, strerror(errcode));
375 void *xbt_os_thread_getparam(void) {
376 xbt_os_thread_t t = xbt_os_thread_self();
377 return t?t->param:NULL;
380 typedef struct xbt_os_sem_ {
381 #ifndef HAVE_SEM_INIT
389 #define SEM_FAILED (-1)
393 xbt_os_sem_init(unsigned int value) {
394 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
396 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
397 * Any attempt to use it leads to ENOSYS (function not implemented).
398 * If such a prehistoric system is detected, do the job with sem_open instead
401 if(sem_init(&(res->s),0,value) != 0)
402 THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
405 #else /* damn, no sem_init(). Reimplement it */
407 xbt_os_mutex_acquire(next_sem_ID_lock);
408 res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
409 xbt_os_mutex_release(next_sem_ID_lock);
411 res->ps = sem_open(res->name, O_CREAT, 0644, value);
412 if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
413 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
414 res->name[13] = '\0';
415 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
417 if ((res->ps == (sem_t *)SEM_FAILED))
418 THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
420 /* Remove the name from the semaphore namespace: we never join on it */
421 if(sem_unlink(res->name) < 0)
422 THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
430 xbt_os_sem_acquire(xbt_os_sem_t sem) {
432 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
433 if(sem_wait(sem->ps) < 0)
434 THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
437 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
441 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
444 xbt_os_sem_acquire(sem);
445 } else if (delay==0) {
446 errcode = sem_trywait(sem->ps);
452 THROW1(timeout_error,0,"semaphore %p not ready",sem);
454 THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
459 struct timespec ts_end;
460 double end = delay + xbt_os_time();
462 ts_end.tv_sec = (time_t) floor(end);
463 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
464 DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
465 errcode = sem_timedwait(sem->s,&ts_end);
467 #else /* Okay, reimplement this function then */
468 double start = xbt_os_time();
470 errcode = sem_trywait(sem->ps);
471 if (errcode == EBUSY)
472 xbt_os_thread_yield();
473 } while (errcode == EBUSY && xbt_os_time()-start <delay);
475 if (errcode == EBUSY)
484 THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
487 THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
493 xbt_os_sem_release(xbt_os_sem_t sem)
496 THROW0(arg_error,EINVAL,"Cannot release of the NULL semaphore");
498 if(sem_post(sem->ps) < 0)
499 THROW1(system_error,errno,"sem_post() failed: %s",
504 xbt_os_sem_destroy(xbt_os_sem_t sem)
507 THROW0(arg_error,EINVAL,"Cannot destroy the NULL sempahore");
510 if(sem_destroy(sem->ps)) < 0)
511 THROW1(system_error,errno,"sem_destroy() failed: %s",
514 if(sem_close(sem->ps) < 0)
515 THROW1(system_error,errno,"sem_close() failed: %s",
524 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
527 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
529 if(sem_getvalue(&(sem->s),svalue) < 0)
530 THROW1(system_error,errno,"sem_getvalue() failed: %s",
534 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
540 typedef struct xbt_os_thread_ {
542 HANDLE handle; /* the win thread handle */
543 unsigned long id; /* the win thread id */
544 pvoid_f_pvoid_t start_routine;
546 } s_xbt_os_thread_t ;
548 /* so we can specify the size of the stack of the threads */
549 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
550 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
553 /* the default size of the stack of the threads (in bytes)*/
554 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
556 /* key to the TLS containing the xbt_os_thread_t structure */
557 static unsigned long xbt_self_thread_key;
559 void xbt_os_thread_mod_init(void) {
560 xbt_self_thread_key = TlsAlloc();
562 void xbt_os_thread_mod_exit(void) {
564 if (!TlsFree(xbt_self_thread_key))
565 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
568 static DWORD WINAPI wrapper_start_routine(void *s) {
569 xbt_os_thread_t t = (xbt_os_thread_t)s;
572 if(!TlsSetValue(xbt_self_thread_key,t))
573 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
575 rv = (*(t->start_routine))(t->param);
577 return *((DWORD*)rv);
581 xbt_os_thread_t xbt_os_thread_create(const char *name,pvoid_f_pvoid_t start_routine,
584 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
586 t->name = xbt_strdup(name);
587 t->start_routine = start_routine ;
590 t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
591 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
592 t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
596 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
602 const char* xbt_os_thread_name(xbt_os_thread_t t) {
606 const char* xbt_os_thread_self_name(void) {
607 xbt_os_thread_t t = xbt_os_thread_self();
608 return t?t->name:"main";
612 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
614 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
615 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
619 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
620 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
623 CloseHandle(thread->handle);
628 void xbt_os_thread_exit(int *retval) {
635 xbt_os_thread_t xbt_os_thread_self(void) {
636 return TlsGetValue(xbt_self_thread_key);
639 void *xbt_os_thread_getparam(void) {
640 xbt_os_thread_t t = xbt_os_thread_self();
645 void xbt_os_thread_yield(void) {
648 void xbt_os_thread_cancel(xbt_os_thread_t t) {
652 /****** mutex related functions ******/
653 typedef struct xbt_os_mutex_ {
654 /* KEEP IT IN SYNC WITH xbt_thread.c */
655 CRITICAL_SECTION lock;
658 xbt_os_mutex_t xbt_os_mutex_init(void) {
659 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
661 /* initialize the critical section object */
662 InitializeCriticalSection(&(res->lock));
667 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
668 EnterCriticalSection(& mutex->lock);
671 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
675 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
677 LeaveCriticalSection (&mutex->lock);
681 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
685 DeleteCriticalSection(& mutex->lock);
689 /***** condition related functions *****/
690 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
696 typedef struct xbt_os_cond_ {
697 /* KEEP IT IN SYNC WITH xbt_thread.c */
698 HANDLE events[MAX_EVENTS];
700 unsigned int waiters_count; /* the number of waiters */
701 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
704 xbt_os_cond_t xbt_os_cond_init(void) {
706 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
708 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
710 /* initialize the critical section object */
711 InitializeCriticalSection(& res->waiters_count_lock);
713 res->waiters_count = 0;
715 /* Create an auto-reset event */
716 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
718 if(!res->events[SIGNAL]){
719 DeleteCriticalSection(& res->waiters_count_lock);
721 THROW0(system_error,0,"CreateEvent failed for the signals");
724 /* Create a manual-reset event. */
725 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
727 if(!res->events[BROADCAST]){
729 DeleteCriticalSection(& res->waiters_count_lock);
730 CloseHandle(res->events[SIGNAL]);
732 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
738 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
740 unsigned long wait_result;
743 /* lock the threads counter and increment it */
744 EnterCriticalSection (& cond->waiters_count_lock);
745 cond->waiters_count++;
746 LeaveCriticalSection (& cond->waiters_count_lock);
748 /* unlock the mutex associate with the condition */
749 LeaveCriticalSection (& mutex->lock);
751 /* wait for a signal (broadcast or no) */
752 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
754 if(wait_result == WAIT_FAILED)
755 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
757 /* we have a signal lock the condition */
758 EnterCriticalSection (& cond->waiters_count_lock);
759 cond->waiters_count--;
761 /* it's the last waiter or it's a broadcast ? */
762 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
764 LeaveCriticalSection (& cond->waiters_count_lock);
766 /* yes it's the last waiter or it's a broadcast
767 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
771 if(!ResetEvent (cond->events[BROADCAST]))
772 THROW0(system_error,0,"ResetEvent failed");
774 /* relock the mutex associated with the condition in accordance with the posix thread specification */
775 EnterCriticalSection (& mutex->lock);
777 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
779 unsigned long wait_result = WAIT_TIMEOUT;
781 unsigned long end = (unsigned long)(delay * 1000);
785 xbt_os_cond_wait(cond,mutex);
787 DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
789 /* lock the threads counter and increment it */
790 EnterCriticalSection (& cond->waiters_count_lock);
791 cond->waiters_count++;
792 LeaveCriticalSection (& cond->waiters_count_lock);
794 /* unlock the mutex associate with the condition */
795 LeaveCriticalSection (& mutex->lock);
796 /* wait for a signal (broadcast or no) */
798 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
800 switch(wait_result) {
802 THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
804 THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
807 /* we have a signal lock the condition */
808 EnterCriticalSection (& cond->waiters_count_lock);
809 cond->waiters_count--;
811 /* it's the last waiter or it's a broadcast ? */
812 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
814 LeaveCriticalSection (& cond->waiters_count_lock);
816 /* yes it's the last waiter or it's a broadcast
817 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
821 if(!ResetEvent (cond->events[BROADCAST]))
822 THROW0(system_error,0,"ResetEvent failed");
824 /* relock the mutex associated with the condition in accordance with the posix thread specification */
825 EnterCriticalSection (& mutex->lock);
827 /*THROW_UNIMPLEMENTED;*/
830 void xbt_os_cond_signal(xbt_os_cond_t cond) {
833 EnterCriticalSection (& cond->waiters_count_lock);
834 have_waiters = cond->waiters_count > 0;
835 LeaveCriticalSection (& cond->waiters_count_lock);
838 if(!SetEvent(cond->events[SIGNAL]))
839 THROW0(system_error,0,"SetEvent failed");
841 xbt_os_thread_yield();
844 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
847 EnterCriticalSection (& cond->waiters_count_lock);
848 have_waiters = cond->waiters_count > 0;
849 LeaveCriticalSection (& cond->waiters_count_lock);
852 SetEvent(cond->events[BROADCAST]);
855 void xbt_os_cond_destroy(xbt_os_cond_t cond){
860 if(!CloseHandle(cond->events[SIGNAL]))
863 if(!CloseHandle(cond->events[BROADCAST]))
866 DeleteCriticalSection(& cond->waiters_count_lock);
871 THROW0(system_error,0,"Error while destroying the condition");
874 typedef struct xbt_os_sem_ {
877 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
881 # define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
885 xbt_os_sem_init(unsigned int value)
890 THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
892 res = (xbt_os_sem_t)xbt_new0(s_xbt_os_sem_t,1);
894 if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
895 THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
896 strerror(GetLastError()));
902 InitializeCriticalSection(&(res->value_lock));
908 xbt_os_sem_acquire(xbt_os_sem_t sem)
911 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
914 if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
915 THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
916 strerror(GetLastError()));
917 EnterCriticalSection(&(sem->value_lock));
919 LeaveCriticalSection(&(sem->value_lock));
922 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
926 double end = timeout + xbt_os_time();
929 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
933 xbt_os_sem_acquire(sem);
935 else /* timeout can be zero <-> try acquire ) */
938 seconds = (long) floor(end);
939 milliseconds = (long)( ( end - seconds) * 1000);
940 milliseconds += (seconds * 1000);
942 switch(WaitForSingleObject(sem->h,milliseconds))
945 EnterCriticalSection(&(sem->value_lock));
947 LeaveCriticalSection(&(sem->value_lock));
951 THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
955 THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
961 xbt_os_sem_release(xbt_os_sem_t sem)
964 THROW0(arg_error,EINVAL,"Cannot release the NULL semaphore");
966 if(!ReleaseSemaphore(sem->h,1, NULL))
967 THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
968 strerror(GetLastError()));
969 EnterCriticalSection (&(sem->value_lock));
971 LeaveCriticalSection(&(sem->value_lock));
975 xbt_os_sem_destroy(xbt_os_sem_t sem)
978 THROW0(arg_error,EINVAL,"Cannot destroy the NULL semaphore");
980 if(!CloseHandle(sem->h))
981 THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
982 strerror(GetLastError()));
984 DeleteCriticalSection(&(sem->value_lock));
991 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
994 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
996 EnterCriticalSection(&(sem->value_lock));
997 *svalue = sem->value;
998 LeaveCriticalSection(&(sem->value_lock));