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 ************************************ */
26 #include <semaphore.h>
28 /* use named sempahore instead */
30 # define MAX_SEM_NAME ((size_t)29)
31 # define MIN_SEM_NAME ((size_t)11)
32 static int __next_sem_ID = 0;
33 static pthread_mutex_t __next_sem_ID_lock;
36 typedef struct xbt_os_thread_ {
40 pvoid_f_pvoid_t start_routine;
43 static xbt_os_thread_t main_thread = NULL;
45 /* thread-specific data containing the xbt_os_thread_t structure */
46 static pthread_key_t xbt_self_thread_key;
47 static int thread_mod_inited = 0;
49 /* frees the xbt_os_thread_t corresponding to the current thread */
50 static void xbt_os_thread_free_thread_data(void*d){
54 /* callback: context fetching */
55 static ex_ctx_t *_os_thread_ex_ctx(void) {
56 return xbt_os_thread_self()->exception;
59 /* callback: termination */
60 static void _os_thread_ex_terminate(xbt_ex_t * e) {
64 /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
67 void xbt_os_thread_mod_init(void) {
70 if (thread_mod_inited)
73 if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
74 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
76 main_thread=xbt_new(s_xbt_os_thread_t,1);
77 main_thread->name = (char*)"main";
78 main_thread->start_routine = NULL;
79 main_thread->param = NULL;
80 main_thread->exception = xbt_new(ex_ctx_t, 1);
81 XBT_CTX_INITIALIZE(main_thread->exception);
83 __xbt_ex_ctx = _os_thread_ex_ctx;
84 __xbt_ex_terminate = _os_thread_ex_terminate;
88 /* initialize the mutex use to protect the incrementation of the variable __next_sem_ID
89 * used to build the name of the named sempahore
91 if ((errcode = pthread_mutex_init(&__next_sem_ID_lock,NULL)))
92 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",strerror(errcode));
96 thread_mod_inited = 1;
98 void xbt_os_thread_mod_exit(void) {
99 /* FIXME: don't try to free our key on shutdown.
100 Valgrind detects no leak if we don't, and whine if we try to */
103 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
104 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
107 static void * wrapper_start_routine(void *s) {
108 xbt_os_thread_t t = s;
111 if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
112 THROW0(system_error,errcode,
113 "pthread_setspecific failed for xbt_self_thread_key");
115 return (*(t->start_routine))(t->param);
117 xbt_os_thread_t xbt_os_thread_create(const char*name,
118 pvoid_f_pvoid_t start_routine,
122 xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
123 res_thread->name = xbt_strdup(name);
124 res_thread->start_routine = start_routine;
125 res_thread->param = param;
126 res_thread->exception = xbt_new(ex_ctx_t, 1);
127 XBT_CTX_INITIALIZE(res_thread->exception);
129 if ((errcode = pthread_create(&(res_thread->t), NULL,
130 wrapper_start_routine, res_thread)))
131 THROW1(system_error,errcode,
132 "pthread_create failed: %s",strerror(errcode));
137 const char* xbt_os_thread_name(xbt_os_thread_t t) {
141 const char* xbt_os_thread_self_name(void) {
142 xbt_os_thread_t self = xbt_os_thread_self();
143 return self?self->name:"main";
146 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
150 if ((errcode = pthread_join(thread->t,thread_return)))
151 THROW1(system_error,errcode, "pthread_join failed: %s",
153 if (thread->exception)
154 free(thread->exception);
156 if (thread == main_thread) /* just killed main thread */
162 void xbt_os_thread_exit(int *retval) {
163 pthread_exit(retval);
166 xbt_os_thread_t xbt_os_thread_self(void) {
169 if (!thread_mod_inited)
172 res = pthread_getspecific(xbt_self_thread_key);
180 void xbt_os_thread_yield(void) {
183 void xbt_os_thread_cancel(xbt_os_thread_t t) {
184 pthread_cancel(t->t);
186 /****** mutex related functions ******/
187 typedef struct xbt_os_mutex_ {
188 /* KEEP IT IN SYNC WITH xbt_thread.c */
195 xbt_os_mutex_t xbt_os_mutex_init(void) {
196 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
199 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
200 THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
206 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
209 if ((errcode=pthread_mutex_lock(&(mutex->m))))
210 THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
211 mutex, strerror(errcode));
214 void xbt_os_mutex_tryacquire(xbt_os_mutex_t mutex) {
217 if ((errcode=pthread_mutex_trylock(&(mutex->m))))
218 THROW2(system_error,errcode,"pthread_mutex_trylock(%p) failed: %s",
219 mutex, strerror(errcode));
224 #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
225 /* if the function is not availabled or if is MAC OS X use pthread_mutex_trylock() to define
228 #include <time.h> /* declaration of the timespec structure and of the nanosleep() function */
229 int pthread_mutex_timedlock(pthread_mutex_t * mutex, const struct timespec * abs_timeout)
232 long ellapsed_time = 0;
237 /* the mutex could not be acquired because it was already locked by an other thread */
238 rv = pthread_mutex_trylock(mutex);
241 ts.tv_nsec = 2.5e7 /* (25 ms (2 context switch + 5 ms)) */;
246 }while(EINTR == errno);
248 ellapsed_time += ts.tv_nsec;
251 while (/* locked */ (EBUSY == rv) && /* !timeout */ (ellapsed_time < ((long)(abs_timeout->tv_sec * 1e9) + abs_timeout->tv_nsec)));
253 return (EBUSY == rv) ? ETIMEDOUT : rv;
258 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
260 struct timespec ts_end;
261 double end = delay + xbt_os_time();
264 xbt_os_mutex_acquire(mutex);
266 ts_end.tv_sec = (time_t) floor(end);
267 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
268 DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
270 switch ((errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end)))
276 THROW2(timeout_error,errcode,"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_WAIT
390 xbt_os_sem_init(unsigned int value)
392 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
395 /* On MAC OS X, it seems that the sem_init is failing with ENOSYS,
396 * which means the sem_init function is not implemented use sem_open()
399 #ifndef HAVE_SEM_INIT
400 res->name = (char*) calloc(MAX_SEM_NAME + 1,sizeof(char));
402 if((errcode = pthread_mutex_lock(&__next_sem_ID_lock)))
403 THROW1(system_error,errcode,"pthread_mutex_lock() failed: %s", strerror(errcode));
407 if((errcode = pthread_mutex_unlock(&__next_sem_ID_lock)))
408 THROW1(system_error,errcode,"pthread_mutex_unlock() failed: %s", strerror(errcode));
410 sprintf(res->name,"/%d",__next_sem_ID);
412 if((res->s = sem_open(res->name, O_CREAT | O_EXCL, 0644, value)) == (sem_t*)SEM_FAILED)
413 THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
416 /* sem_init() is implemented, use it */
417 if(sem_init(&(res->s),0,value) < 0)
418 THROW1(system_error,errno,"sem_init() failed: %s",
426 xbt_os_sem_acquire(xbt_os_sem_t sem)
429 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
430 #ifndef HAVE_SEM_WAIT
431 if(sem_wait((sem->s)) < 0)
432 THROW1(system_error,errno,"sem_wait() failed: %s",
435 if(sem_wait(&(sem->s)) < 0)
436 THROW1(system_error,errno,"sem_wait() failed: %s",
441 #ifndef HAVE_SEM_TIMEDWAIT
442 /* if the function is not availabled or if is MAC OS X use sem_trywait() to define
445 #include <time.h> /* declaration of the timespec structure and of the nanosleep() function */
446 int sem_timedwait(sem_t* sem, const struct timespec * abs_timeout)
449 long ellapsed_time = 0;
454 rv = sem_trywait(sem);
456 ts.tv_nsec = 2.5e7 /* (25 ms (2 * context switch + 5 ms)) */;
461 }while(EINTR == errno);
463 ellapsed_time += ts.tv_nsec;
466 while(/* locked */ (EAGAIN == rv) && /* !timeout */ ellapsed_time < ((long)(abs_timeout->tv_sec * 1e9) + abs_timeout->tv_nsec));
468 return (EAGAIN == rv) ? ETIMEDOUT : rv;
473 void xbt_os_sem_timedacquire(xbt_os_sem_t sem,double timeout)
476 struct timespec ts_end;
477 double end = timeout + xbt_os_time();
480 THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
484 xbt_os_sem_acquire(sem);
488 ts_end.tv_sec = (time_t) floor(end);
489 ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
490 DEBUG2("sem_timedwait(%p,%p)",&(sem->s),&ts_end);
492 #ifndef HAVE_SEM_WAIT
493 switch ((errcode = sem_timedwait(sem->s,&ts_end)))
495 switch ((errcode = sem_timedwait(&(sem->s),&ts_end)))
502 THROW2(timeout_error,errcode,"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
505 THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,timeout, strerror(errcode));
512 xbt_os_sem_release(xbt_os_sem_t sem)
515 THROW0(arg_error,EINVAL,"Cannot release of the NULL semaphore");
517 #ifndef HAVE_SEM_WAIT
518 if(sem_post((sem->s)) < 0)
519 THROW1(system_error,errno,"sem_post() failed: %s",
522 if(sem_post(&(sem->s)) < 0)
523 THROW1(system_error,errno,"sem_post() failed: %s",
529 xbt_os_sem_destroy(xbt_os_sem_t sem)
532 THROW0(arg_error,EINVAL,"Cannot destroy the NULL sempahore");
534 #ifndef HAVE_SEM_WAIT
535 /* MAC OS X does not implement the sem_init() function so,
536 * we use the named semaphore (sem_open)
538 if(sem_close((sem->s)) < 0)
539 THROW1(system_error,errno,"sem_close() failed: %s",
542 if(sem_unlink(sem->name) < 0)
543 THROW1(system_error,errno,"sem_unlink() failed: %s",
549 if(sem_destroy(&(sem->s)) < 0)
550 THROW1(system_error,errno,"sem_destroy() failed: %s",
558 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
561 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
563 #ifndef HAVE_SEM_WAIT
564 if(sem_getvalue((sem->s),svalue) < 0)
565 THROW1(system_error,errno,"sem_getvalue() failed: %s",
568 if(sem_getvalue(&(sem->s),svalue) < 0)
569 THROW1(system_error,errno,"sem_getvalue() failed: %s",
574 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
580 typedef struct xbt_os_thread_ {
582 HANDLE handle; /* the win thread handle */
583 unsigned long id; /* the win thread id */
584 pvoid_f_pvoid_t start_routine;
586 } s_xbt_os_thread_t ;
588 /* so we can specify the size of the stack of the threads */
589 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
590 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
593 /* the default size of the stack of the threads (in bytes)*/
594 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
596 /* key to the TLS containing the xbt_os_thread_t structure */
597 static unsigned long xbt_self_thread_key;
599 void xbt_os_thread_mod_init(void) {
600 xbt_self_thread_key = TlsAlloc();
602 void xbt_os_thread_mod_exit(void) {
604 if (!TlsFree(xbt_self_thread_key))
605 THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
608 static DWORD WINAPI wrapper_start_routine(void *s) {
609 xbt_os_thread_t t = (xbt_os_thread_t)s;
612 if(!TlsSetValue(xbt_self_thread_key,t))
613 THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
615 rv = (*(t->start_routine))(t->param);
617 return *((DWORD*)rv);
621 xbt_os_thread_t xbt_os_thread_create(const char *name,pvoid_f_pvoid_t start_routine,
624 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t,1);
626 t->name = xbt_strdup(name);
627 t->start_routine = start_routine ;
630 t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
631 (LPTHREAD_START_ROUTINE)wrapper_start_routine,
632 t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
636 THROW0(system_error,(int)GetLastError(),"CreateThread failed");
642 const char* xbt_os_thread_name(xbt_os_thread_t t) {
646 const char* xbt_os_thread_self_name(void) {
647 xbt_os_thread_t t = xbt_os_thread_self();
648 return t?t->name:"main";
652 xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
654 if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
655 THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
659 if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
660 THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
663 CloseHandle(thread->handle);
668 void xbt_os_thread_exit(int *retval) {
675 xbt_os_thread_t xbt_os_thread_self(void) {
676 return TlsGetValue(xbt_self_thread_key);
679 void *xbt_os_thread_getparam(void) {
680 xbt_os_thread_t t = xbt_os_thread_self();
685 void xbt_os_thread_yield(void) {
688 void xbt_os_thread_cancel(xbt_os_thread_t t) {
692 /****** mutex related functions ******/
693 typedef struct xbt_os_mutex_ {
694 /* KEEP IT IN SYNC WITH xbt_thread.c */
695 CRITICAL_SECTION lock;
698 xbt_os_mutex_t xbt_os_mutex_init(void) {
699 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t,1);
701 /* initialize the critical section object */
702 InitializeCriticalSection(&(res->lock));
707 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
709 EnterCriticalSection(& mutex->lock);
712 void xbt_os_mutex_tryacquire(xbt_os_mutex_t mutex)
714 TryEnterCriticalSection(&mutex->lock);
717 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
721 void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
723 LeaveCriticalSection (& mutex->lock);
727 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex) {
731 DeleteCriticalSection(& mutex->lock);
735 /***** condition related functions *****/
736 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
742 typedef struct xbt_os_cond_ {
743 /* KEEP IT IN SYNC WITH xbt_thread.c */
744 HANDLE events[MAX_EVENTS];
746 unsigned int waiters_count; /* the number of waiters */
747 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
750 xbt_os_cond_t xbt_os_cond_init(void) {
752 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t,1);
754 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
756 /* initialize the critical section object */
757 InitializeCriticalSection(& res->waiters_count_lock);
759 res->waiters_count = 0;
761 /* Create an auto-reset event */
762 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
764 if(!res->events[SIGNAL]){
765 DeleteCriticalSection(& res->waiters_count_lock);
767 THROW0(system_error,0,"CreateEvent failed for the signals");
770 /* Create a manual-reset event. */
771 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
773 if(!res->events[BROADCAST]){
775 DeleteCriticalSection(& res->waiters_count_lock);
776 CloseHandle(res->events[SIGNAL]);
778 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
784 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) {
786 unsigned long wait_result;
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);
797 /* wait for a signal (broadcast or no) */
798 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
800 if(wait_result == WAIT_FAILED)
801 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
803 /* we have a signal lock the condition */
804 EnterCriticalSection (& cond->waiters_count_lock);
805 cond->waiters_count--;
807 /* it's the last waiter or it's a broadcast ? */
808 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
810 LeaveCriticalSection (& cond->waiters_count_lock);
812 /* yes it's the last waiter or it's a broadcast
813 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
817 if(!ResetEvent (cond->events[BROADCAST]))
818 THROW0(system_error,0,"ResetEvent failed");
820 /* relock the mutex associated with the condition in accordance with the posix thread specification */
821 EnterCriticalSection (& mutex->lock);
823 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) {
825 unsigned long wait_result = WAIT_TIMEOUT;
827 unsigned long end = (unsigned long)(delay * 1000);
831 xbt_os_cond_wait(cond,mutex);
833 DEBUG3("xbt_cond_timedwait(%p,%p,%ul)",&(cond->events),&(mutex->lock),end);
835 /* lock the threads counter and increment it */
836 EnterCriticalSection (& cond->waiters_count_lock);
837 cond->waiters_count++;
838 LeaveCriticalSection (& cond->waiters_count_lock);
840 /* unlock the mutex associate with the condition */
841 LeaveCriticalSection (& mutex->lock);
842 /* wait for a signal (broadcast or no) */
844 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
846 switch(wait_result) {
848 THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
850 THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
853 /* we have a signal lock the condition */
854 EnterCriticalSection (& cond->waiters_count_lock);
855 cond->waiters_count--;
857 /* it's the last waiter or it's a broadcast ? */
858 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
860 LeaveCriticalSection (& cond->waiters_count_lock);
862 /* yes it's the last waiter or it's a broadcast
863 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
867 if(!ResetEvent (cond->events[BROADCAST]))
868 THROW0(system_error,0,"ResetEvent failed");
870 /* relock the mutex associated with the condition in accordance with the posix thread specification */
871 EnterCriticalSection (& mutex->lock);
873 /*THROW_UNIMPLEMENTED;*/
876 void xbt_os_cond_signal(xbt_os_cond_t cond) {
879 EnterCriticalSection (& cond->waiters_count_lock);
880 have_waiters = cond->waiters_count > 0;
881 LeaveCriticalSection (& cond->waiters_count_lock);
884 if(!SetEvent(cond->events[SIGNAL]))
885 THROW0(system_error,0,"SetEvent failed");
887 xbt_os_thread_yield();
890 void xbt_os_cond_broadcast(xbt_os_cond_t cond){
893 EnterCriticalSection (& cond->waiters_count_lock);
894 have_waiters = cond->waiters_count > 0;
895 LeaveCriticalSection (& cond->waiters_count_lock);
898 SetEvent(cond->events[BROADCAST]);
901 void xbt_os_cond_destroy(xbt_os_cond_t cond){
906 if(!CloseHandle(cond->events[SIGNAL]))
909 if(!CloseHandle(cond->events[BROADCAST]))
912 DeleteCriticalSection(& cond->waiters_count_lock);
917 THROW0(system_error,0,"Error while destroying the condition");
920 typedef struct xbt_os_sem_ {
923 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
927 xbt_os_sem_init(unsigned int value)
932 THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
934 res = (xbt_os_sem_t)xbt_new0(s_xbt_os_sem_t,1);
936 if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
937 THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
938 strerror(GetLastError()));
944 InitializeCriticalSection(&(res->value_lock));
950 xbt_os_sem_acquire(xbt_os_sem_t sem)
953 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
956 if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
957 THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
958 strerror(GetLastError()));
959 EnterCriticalSection(&(sem->value_lock));
961 LeaveCriticalSection(&(sem->value_lock));
964 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
968 double end = timeout + xbt_os_time();
971 THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
975 xbt_os_sem_acquire(sem);
980 seconds = (long) floor(end);
981 milliseconds = (long)( ( end - seconds) * 1000);
982 milliseconds += (seconds * 1000);
984 switch(WaitForSingleObject(sem->h,milliseconds))
987 EnterCriticalSection(&(sem->value_lock));
989 LeaveCriticalSection(&(sem->value_lock));
993 THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
998 THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
1004 xbt_os_sem_release(xbt_os_sem_t sem)
1007 THROW0(arg_error,EINVAL,"Cannot release the NULL semaphore");
1009 if(!ReleaseSemaphore(sem->h,1, NULL))
1010 THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
1011 strerror(GetLastError()));
1012 EnterCriticalSection (&(sem->value_lock));
1014 LeaveCriticalSection(&(sem->value_lock));
1018 xbt_os_sem_destroy(xbt_os_sem_t sem)
1021 THROW0(arg_error,EINVAL,"Cannot destroy the NULL semaphore");
1023 if(!CloseHandle(sem->h))
1024 THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
1025 strerror(GetLastError()));
1027 DeleteCriticalSection(&(sem->value_lock));
1034 xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
1037 THROW0(arg_error,EINVAL,"Cannot get the value of the NULL semaphore");
1039 EnterCriticalSection(&(sem->value_lock));
1040 *svalue = sem->value;
1041 LeaveCriticalSection(&(sem->value_lock));