1 /* xbt_os_thread -- portability layer over the pthread API */
2 /* Used in RL to get win/lin portability, and in SG when CONTEXT_THREAD */
3 /* in SG, when using CONTEXT_UCONTEXT, xbt_os_thread_stub is used instead */
5 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
13 #include "xbt/ex_interface.h" /* We play crude games with exceptions */
15 #include "xbt/xbt_os_time.h" /* Portable time facilities */
16 #include "xbt/xbt_os_thread.h" /* This module */
17 #include "xbt_modinter.h" /* Initialization/finalization of this module */
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt,
20 "Synchronization mechanism (OS-level)");
22 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
26 #include <semaphore.h>
28 #ifdef HAVE_MUTEX_TIMEDLOCK
29 /* redefine the function header since we fail to get this from system headers on amd (at least) */
30 int pthread_mutex_timedlock(pthread_mutex_t * mutex,
31 const struct timespec *abs_timeout);
35 /* use named sempahore when sem_init() does not work */
37 static int next_sem_ID = 0;
38 static xbt_os_mutex_t next_sem_ID_lock;
41 typedef struct xbt_os_thread_ {
45 pvoid_f_pvoid_t start_routine;
48 static xbt_os_thread_t main_thread = NULL;
50 /* thread-specific data containing the xbt_os_thread_t structure */
51 static pthread_key_t xbt_self_thread_key;
52 static int thread_mod_inited = 0;
54 /* frees the xbt_os_thread_t corresponding to the current thread */
55 static void xbt_os_thread_free_thread_data(void *d)
60 /* callback: context fetching */
61 static ex_ctx_t *_os_thread_ex_ctx(void)
63 return xbt_os_thread_self()->exception;
66 /* callback: termination */
67 static void _os_thread_ex_terminate(xbt_ex_t * e)
72 /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
75 void xbt_os_thread_mod_preinit(void)
79 if (thread_mod_inited)
82 if ((errcode = pthread_key_create(&xbt_self_thread_key, NULL)))
83 THROW0(system_error, errcode,
84 "pthread_key_create failed for xbt_self_thread_key");
86 main_thread = xbt_new(s_xbt_os_thread_t, 1);
87 main_thread->name = (char *) "main";
88 main_thread->start_routine = NULL;
89 main_thread->param = NULL;
90 main_thread->exception = xbt_new(ex_ctx_t, 1);
91 XBT_CTX_INITIALIZE(main_thread->exception);
93 __xbt_ex_ctx = _os_thread_ex_ctx;
94 __xbt_ex_terminate = _os_thread_ex_terminate;
96 thread_mod_inited = 1;
99 next_sem_ID_lock = xbt_os_mutex_init();
104 void xbt_os_thread_mod_postexit(void)
106 /* FIXME: don't try to free our key on shutdown.
107 Valgrind detects no leak if we don't, and whine if we try to */
110 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
111 // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
112 free(main_thread->exception);
115 thread_mod_inited = 0;
116 #ifndef HAVE_SEM_INIT
117 xbt_os_mutex_destroy(next_sem_ID_lock);
120 /* Restore the default exception setup */
121 __xbt_ex_ctx = &__xbt_ex_ctx_default;
122 __xbt_ex_terminate = &__xbt_ex_terminate_default;
125 int xbt_os_thread_atfork(void (*prepare)(void),
126 void (*parent)(void), void (*child)(void))
128 return pthread_atfork(prepare, parent, child);
131 static void *wrapper_start_routine(void *s)
133 xbt_os_thread_t t = s;
136 if ((errcode = pthread_setspecific(xbt_self_thread_key, t)))
137 THROW0(system_error, errcode,
138 "pthread_setspecific failed for xbt_self_thread_key");
140 return (*(t->start_routine)) (t->param);
143 xbt_os_thread_t xbt_os_thread_create(const char *name,
144 pvoid_f_pvoid_t start_routine,
149 xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
150 res_thread->name = xbt_strdup(name);
151 res_thread->start_routine = start_routine;
152 res_thread->param = param;
153 res_thread->exception = xbt_new(ex_ctx_t, 1);
154 XBT_CTX_INITIALIZE(res_thread->exception);
156 if ((errcode = pthread_create(&(res_thread->t), NULL,
157 wrapper_start_routine, res_thread)))
158 THROW1(system_error, errcode,
159 "pthread_create failed: %s", strerror(errcode));
164 const char *xbt_os_thread_name(xbt_os_thread_t t)
169 const char *xbt_os_thread_self_name(void)
171 xbt_os_thread_t me = xbt_os_thread_self();
172 return me ? me->name : "main";
175 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
180 if ((errcode = pthread_join(thread->t, thread_return)))
181 THROW1(system_error, errcode, "pthread_join failed: %s",
183 if (thread->exception)
184 free(thread->exception);
189 if (thread == main_thread) /* just killed main thread */
195 void xbt_os_thread_exit(int *retval)
197 pthread_exit(retval);
200 xbt_os_thread_t xbt_os_thread_self(void)
204 if (!thread_mod_inited)
207 res = pthread_getspecific(xbt_self_thread_key);
215 void xbt_os_thread_yield(void)
220 void xbt_os_thread_cancel(xbt_os_thread_t t)
222 pthread_cancel(t->t);
225 /****** mutex related functions ******/
226 typedef struct xbt_os_mutex_ {
227 /* KEEP IT IN SYNC WITH xbt_thread.c */
234 xbt_os_mutex_t xbt_os_mutex_init(void)
236 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
239 if ((errcode = pthread_mutex_init(&(res->m), NULL)))
240 THROW1(system_error, errcode, "pthread_mutex_init() failed: %s",
246 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
250 if ((errcode = pthread_mutex_lock(&(mutex->m))))
251 THROW2(system_error, errcode, "pthread_mutex_lock(%p) failed: %s",
252 mutex, strerror(errcode));
256 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
261 xbt_os_mutex_acquire(mutex);
263 } else if (delay == 0) {
264 errcode = pthread_mutex_trylock(&(mutex->m));
270 THROW1(timeout_error, 0, "mutex %p not ready", mutex);
272 THROW2(system_error, errcode,
273 "xbt_mutex_timedacquire(%p) failed: %s", mutex,
280 #ifdef HAVE_MUTEX_TIMEDLOCK
281 struct timespec ts_end;
282 double end = delay + xbt_os_time();
284 ts_end.tv_sec = (time_t) floor(end);
285 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
286 DEBUG2("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
288 errcode = pthread_mutex_timedlock(&(mutex->m), &ts_end);
290 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
291 double start = xbt_os_time();
293 errcode = pthread_mutex_trylock(&(mutex->m));
294 if (errcode == EBUSY)
295 xbt_os_thread_yield();
296 } while (errcode == EBUSY && xbt_os_time() - start < delay);
298 if (errcode == EBUSY)
301 #endif /* HAVE_MUTEX_TIMEDLOCK */
308 THROW2(timeout_error, delay,
309 "mutex %p wasn't signaled before timeout (%f)", mutex, delay);
312 THROW3(system_error, errcode,
313 "pthread_mutex_timedlock(%p,%f) failed: %s", mutex, delay,
319 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
323 if ((errcode = pthread_mutex_unlock(&(mutex->m))))
324 THROW2(system_error, errcode, "pthread_mutex_unlock(%p) failed: %s",
325 mutex, strerror(errcode));
328 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
335 if ((errcode = pthread_mutex_destroy(&(mutex->m))))
336 THROW2(system_error, errcode, "pthread_mutex_destroy(%p) failed: %s",
337 mutex, strerror(errcode));
341 /***** condition related functions *****/
342 typedef struct xbt_os_cond_ {
343 /* KEEP IT IN SYNC WITH xbt_thread.c */
347 xbt_os_cond_t xbt_os_cond_init(void)
349 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t, 1);
351 if ((errcode = pthread_cond_init(&(res->c), NULL)))
352 THROW1(system_error, errcode, "pthread_cond_init() failed: %s",
358 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
361 if ((errcode = pthread_cond_wait(&(cond->c), &(mutex->m))))
362 THROW3(system_error, errcode, "pthread_cond_wait(%p,%p) failed: %s",
363 cond, mutex, strerror(errcode));
367 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
371 struct timespec ts_end;
372 double end = delay + xbt_os_time();
375 xbt_os_cond_wait(cond, mutex);
377 ts_end.tv_sec = (time_t) floor(end);
378 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
379 DEBUG3("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m),
382 pthread_cond_timedwait(&(cond->c), &(mutex->m), &ts_end))) {
386 THROW3(timeout_error, errcode,
387 "condition %p (mutex %p) wasn't signaled before timeout (%f)",
390 THROW4(system_error, errcode,
391 "pthread_cond_timedwait(%p,%p,%f) failed: %s", cond, mutex,
392 delay, strerror(errcode));
397 void xbt_os_cond_signal(xbt_os_cond_t cond)
400 if ((errcode = pthread_cond_signal(&(cond->c))))
401 THROW2(system_error, errcode, "pthread_cond_signal(%p) failed: %s",
402 cond, strerror(errcode));
405 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
408 if ((errcode = pthread_cond_broadcast(&(cond->c))))
409 THROW2(system_error, errcode, "pthread_cond_broadcast(%p) failed: %s",
410 cond, strerror(errcode));
413 void xbt_os_cond_destroy(xbt_os_cond_t cond)
420 if ((errcode = pthread_cond_destroy(&(cond->c))))
421 THROW2(system_error, errcode, "pthread_cond_destroy(%p) failed: %s",
422 cond, strerror(errcode));
426 void *xbt_os_thread_getparam(void)
428 xbt_os_thread_t t = xbt_os_thread_self();
429 return t ? t->param : NULL;
432 typedef struct xbt_os_sem_ {
433 #ifndef HAVE_SEM_INIT
441 #define SEM_FAILED (-1)
444 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
446 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t, 1);
448 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
449 * Any attempt to use it leads to ENOSYS (function not implemented).
450 * If such a prehistoric system is detected, do the job with sem_open instead
453 if (sem_init(&(res->s), 0, value) != 0)
454 THROW1(system_error, errno, "sem_init() failed: %s", strerror(errno));
457 #else /* damn, no sem_init(). Reimplement it */
459 xbt_os_mutex_acquire(next_sem_ID_lock);
460 res->name = bprintf("/%d.%d", (*xbt_getpid) (), ++next_sem_ID);
461 xbt_os_mutex_release(next_sem_ID_lock);
463 res->ps = sem_open(res->name, O_CREAT, 0644, value);
464 if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
465 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
466 res->name[13] = '\0';
467 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
469 if ((res->ps == (sem_t *) SEM_FAILED))
470 THROW1(system_error, errno, "sem_open() failed: %s", strerror(errno));
472 /* Remove the name from the semaphore namespace: we never join on it */
473 if (sem_unlink(res->name) < 0)
474 THROW1(system_error, errno, "sem_unlink() failed: %s",
482 void xbt_os_sem_acquire(xbt_os_sem_t sem)
485 THROW0(arg_error, EINVAL, "Cannot acquire of the NULL semaphore");
486 if (sem_wait(sem->ps) < 0)
487 THROW1(system_error, errno, "sem_wait() failed: %s", strerror(errno));
490 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
495 THROW0(arg_error, EINVAL, "Cannot acquire of the NULL semaphore");
498 xbt_os_sem_acquire(sem);
499 } else if (delay == 0) {
500 errcode = sem_trywait(sem->ps);
506 THROW1(timeout_error, 0, "semaphore %p not ready", sem);
508 THROW2(system_error, errcode,
509 "xbt_os_sem_timedacquire(%p) failed: %s", sem,
515 struct timespec ts_end;
516 double end = delay + xbt_os_time();
518 ts_end.tv_sec = (time_t) floor(end);
519 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
520 DEBUG2("sem_timedwait(%p,%p)", sem->ps, &ts_end);
521 errcode = sem_timedwait(sem->s, &ts_end);
523 #else /* Okay, reimplement this function then */
524 double start = xbt_os_time();
526 errcode = sem_trywait(sem->ps);
527 if (errcode == EBUSY)
528 xbt_os_thread_yield();
529 } while (errcode == EBUSY && xbt_os_time() - start < delay);
531 if (errcode == EBUSY)
540 THROW2(timeout_error, delay,
541 "semaphore %p wasn't signaled before timeout (%f)", sem,
545 THROW3(system_error, errcode, "sem_timedwait(%p,%f) failed: %s", sem,
546 delay, strerror(errcode));
551 void xbt_os_sem_release(xbt_os_sem_t sem)
554 THROW0(arg_error, EINVAL, "Cannot release of the NULL semaphore");
556 if (sem_post(sem->ps) < 0)
557 THROW1(system_error, errno, "sem_post() failed: %s", strerror(errno));
560 void xbt_os_sem_destroy(xbt_os_sem_t sem)
563 THROW0(arg_error, EINVAL, "Cannot destroy the NULL sempahore");
566 if (sem_destroy(sem->ps) < 0)
567 THROW1(system_error, errno, "sem_destroy() failed: %s",
570 if (sem_close(sem->ps) < 0)
571 THROW1(system_error, errno, "sem_close() failed: %s", strerror(errno));
578 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
581 THROW0(arg_error, EINVAL,
582 "Cannot get the value of the NULL semaphore");
584 if (sem_getvalue(&(sem->s), svalue) < 0)
585 THROW1(system_error, errno, "sem_getvalue() failed: %s",
589 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
591 #elif defined(_XBT_WIN32)
595 typedef struct xbt_os_thread_ {
597 HANDLE handle; /* the win thread handle */
598 unsigned long id; /* the win thread id */
599 pvoid_f_pvoid_t start_routine;
603 /* so we can specify the size of the stack of the threads */
604 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
605 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
608 /* the default size of the stack of the threads (in bytes)*/
609 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
611 /* key to the TLS containing the xbt_os_thread_t structure */
612 static unsigned long xbt_self_thread_key;
614 void xbt_os_thread_mod_preinit(void)
616 xbt_self_thread_key = TlsAlloc();
619 void xbt_os_thread_mod_postexit(void)
622 if (!TlsFree(xbt_self_thread_key))
623 THROW0(system_error, (int) GetLastError(),
624 "TlsFree() failed to cleanup the thread submodule");
627 int xbt_os_thread_atfork(void (*prepare)(void),
628 void (*parent)(void), void (*child)(void))
633 static DWORD WINAPI wrapper_start_routine(void *s)
635 xbt_os_thread_t t = (xbt_os_thread_t) s;
638 if (!TlsSetValue(xbt_self_thread_key, t))
639 THROW0(system_error, (int) GetLastError(),
640 "TlsSetValue of data describing the created thread failed");
642 rv = (DWORD *) ((t->start_routine) (t->param));
649 xbt_os_thread_t xbt_os_thread_create(const char *name,
650 pvoid_f_pvoid_t start_routine,
654 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t, 1);
656 t->name = xbt_strdup(name);
657 t->start_routine = start_routine;
660 t->handle = CreateThread(NULL, XBT_DEFAULT_THREAD_STACK_SIZE,
661 (LPTHREAD_START_ROUTINE) wrapper_start_routine,
662 t, STACK_SIZE_PARAM_IS_A_RESERVATION, &(t->id));
666 THROW0(system_error, (int) GetLastError(), "CreateThread failed");
672 const char *xbt_os_thread_name(xbt_os_thread_t t)
677 const char *xbt_os_thread_self_name(void)
679 xbt_os_thread_t t = xbt_os_thread_self();
680 return t ? t->name : "main";
683 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
686 if (WAIT_OBJECT_0 != WaitForSingleObject(thread->handle, INFINITE))
687 THROW0(system_error, (int) GetLastError(),
688 "WaitForSingleObject failed");
692 if (!GetExitCodeThread(thread->handle, (DWORD *) (*thread_return)))
693 THROW0(system_error, (int) GetLastError(),
694 "GetExitCodeThread failed");
697 CloseHandle(thread->handle);
705 void xbt_os_thread_exit(int *retval)
713 xbt_os_thread_t xbt_os_thread_self(void)
715 return TlsGetValue(xbt_self_thread_key);
718 void *xbt_os_thread_getparam(void)
720 xbt_os_thread_t t = xbt_os_thread_self();
725 void xbt_os_thread_yield(void)
730 void xbt_os_thread_cancel(xbt_os_thread_t t)
732 if (!TerminateThread(t->handle, 0))
733 THROW0(system_error, (int) GetLastError(), "TerminateThread failed");
736 /****** mutex related functions ******/
737 typedef struct xbt_os_mutex_ {
738 /* KEEP IT IN SYNC WITH xbt_thread.c */
739 CRITICAL_SECTION lock;
742 xbt_os_mutex_t xbt_os_mutex_init(void)
744 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
746 /* initialize the critical section object */
747 InitializeCriticalSection(&(res->lock));
752 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
754 EnterCriticalSection(&mutex->lock);
757 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
762 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
765 LeaveCriticalSection(&mutex->lock);
769 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
775 DeleteCriticalSection(&mutex->lock);
779 /***** condition related functions *****/
780 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
786 typedef struct xbt_os_cond_ {
787 /* KEEP IT IN SYNC WITH xbt_thread.c */
788 HANDLE events[MAX_EVENTS];
790 unsigned int waiters_count; /* the number of waiters */
791 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
794 xbt_os_cond_t xbt_os_cond_init(void)
797 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t, 1);
799 memset(&res->waiters_count_lock, 0, sizeof(CRITICAL_SECTION));
801 /* initialize the critical section object */
802 InitializeCriticalSection(&res->waiters_count_lock);
804 res->waiters_count = 0;
806 /* Create an auto-reset event */
807 res->events[SIGNAL] = CreateEvent(NULL, FALSE, FALSE, NULL);
809 if (!res->events[SIGNAL]) {
810 DeleteCriticalSection(&res->waiters_count_lock);
812 THROW0(system_error, 0, "CreateEvent failed for the signals");
815 /* Create a manual-reset event. */
816 res->events[BROADCAST] = CreateEvent(NULL, TRUE, FALSE, NULL);
818 if (!res->events[BROADCAST]) {
820 DeleteCriticalSection(&res->waiters_count_lock);
821 CloseHandle(res->events[SIGNAL]);
823 THROW0(system_error, 0, "CreateEvent failed for the broadcasts");
829 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
832 unsigned long wait_result;
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);
843 /* wait for a signal (broadcast or no) */
844 wait_result = WaitForMultipleObjects(2, cond->events, FALSE, INFINITE);
846 if (wait_result == WAIT_FAILED)
847 THROW0(system_error, 0,
848 "WaitForMultipleObjects failed, so we cannot wait on the condition");
850 /* we have a signal lock the condition */
851 EnterCriticalSection(&cond->waiters_count_lock);
852 cond->waiters_count--;
854 /* it's the last waiter or it's a broadcast ? */
855 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1)
856 && (cond->waiters_count == 0));
858 LeaveCriticalSection(&cond->waiters_count_lock);
860 /* yes it's the last waiter or it's a broadcast
861 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
865 if (!ResetEvent(cond->events[BROADCAST]))
866 THROW0(system_error, 0, "ResetEvent failed");
868 /* relock the mutex associated with the condition in accordance with the posix thread specification */
869 EnterCriticalSection(&mutex->lock);
872 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
876 unsigned long wait_result = WAIT_TIMEOUT;
878 unsigned long end = (unsigned long) (delay * 1000);
882 xbt_os_cond_wait(cond, mutex);
884 DEBUG3("xbt_cond_timedwait(%p,%p,%lu)", &(cond->events),
885 &(mutex->lock), end);
887 /* lock the threads counter and increment it */
888 EnterCriticalSection(&cond->waiters_count_lock);
889 cond->waiters_count++;
890 LeaveCriticalSection(&cond->waiters_count_lock);
892 /* unlock the mutex associate with the condition */
893 LeaveCriticalSection(&mutex->lock);
894 /* wait for a signal (broadcast or no) */
896 wait_result = WaitForMultipleObjects(2, cond->events, FALSE, end);
898 switch (wait_result) {
900 THROW3(timeout_error, GetLastError(),
901 "condition %p (mutex %p) wasn't signaled before timeout (%f)",
904 THROW0(system_error, GetLastError(),
905 "WaitForMultipleObjects failed, so we cannot wait on the condition");
908 /* we have a signal lock the condition */
909 EnterCriticalSection(&cond->waiters_count_lock);
910 cond->waiters_count--;
912 /* it's the last waiter or it's a broadcast ? */
913 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1)
914 && (cond->waiters_count == 0));
916 LeaveCriticalSection(&cond->waiters_count_lock);
918 /* yes it's the last waiter or it's a broadcast
919 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
923 if (!ResetEvent(cond->events[BROADCAST]))
924 THROW0(system_error, 0, "ResetEvent failed");
926 /* relock the mutex associated with the condition in accordance with the posix thread specification */
927 EnterCriticalSection(&mutex->lock);
929 /*THROW_UNIMPLEMENTED; */
932 void xbt_os_cond_signal(xbt_os_cond_t cond)
936 EnterCriticalSection(&cond->waiters_count_lock);
937 have_waiters = cond->waiters_count > 0;
938 LeaveCriticalSection(&cond->waiters_count_lock);
941 if (!SetEvent(cond->events[SIGNAL]))
942 THROW0(system_error, 0, "SetEvent failed");
944 xbt_os_thread_yield();
947 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
951 EnterCriticalSection(&cond->waiters_count_lock);
952 have_waiters = cond->waiters_count > 0;
953 LeaveCriticalSection(&cond->waiters_count_lock);
956 SetEvent(cond->events[BROADCAST]);
959 void xbt_os_cond_destroy(xbt_os_cond_t cond)
966 if (!CloseHandle(cond->events[SIGNAL]))
969 if (!CloseHandle(cond->events[BROADCAST]))
972 DeleteCriticalSection(&cond->waiters_count_lock);
977 THROW0(system_error, 0, "Error while destroying the condition");
980 typedef struct xbt_os_sem_ {
983 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
987 # define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
990 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
995 THROW1(arg_error, value,
996 "Semaphore initial value too big: %ud cannot be stored as a signed int",
999 res = (xbt_os_sem_t) xbt_new0(s_xbt_os_sem_t, 1);
1001 if (!(res->h = CreateSemaphore(NULL, value, (long) INT_MAX, NULL))) {
1002 THROW1(system_error, GetLastError(), "CreateSemaphore() failed: %s",
1003 strerror(GetLastError()));
1009 InitializeCriticalSection(&(res->value_lock));
1014 void xbt_os_sem_acquire(xbt_os_sem_t sem)
1017 THROW0(arg_error, EINVAL, "Cannot acquire the NULL semaphore");
1020 if (WAIT_OBJECT_0 != WaitForSingleObject(sem->h, INFINITE))
1021 THROW1(system_error, GetLastError(),
1022 "WaitForSingleObject() failed: %s", strerror(GetLastError()));
1023 EnterCriticalSection(&(sem->value_lock));
1025 LeaveCriticalSection(&(sem->value_lock));
1028 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
1032 double end = timeout + xbt_os_time();
1035 THROW0(arg_error, EINVAL, "Cannot acquire the NULL semaphore");
1038 xbt_os_sem_acquire(sem);
1039 } else { /* timeout can be zero <-> try acquire ) */
1042 seconds = (long) floor(end);
1043 milliseconds = (long) ((end - seconds) * 1000);
1044 milliseconds += (seconds * 1000);
1046 switch (WaitForSingleObject(sem->h, milliseconds)) {
1048 EnterCriticalSection(&(sem->value_lock));
1050 LeaveCriticalSection(&(sem->value_lock));
1054 THROW2(timeout_error, GetLastError(),
1055 "semaphore %p wasn't signaled before timeout (%f)", sem,
1060 THROW3(system_error, GetLastError(),
1061 "WaitForSingleObject(%p,%f) failed: %s", sem, timeout,
1062 strerror(GetLastError()));
1067 void xbt_os_sem_release(xbt_os_sem_t sem)
1070 THROW0(arg_error, EINVAL, "Cannot release the NULL semaphore");
1072 if (!ReleaseSemaphore(sem->h, 1, NULL))
1073 THROW1(system_error, GetLastError(), "ReleaseSemaphore() failed: %s",
1074 strerror(GetLastError()));
1075 EnterCriticalSection(&(sem->value_lock));
1077 LeaveCriticalSection(&(sem->value_lock));
1080 void xbt_os_sem_destroy(xbt_os_sem_t sem)
1083 THROW0(arg_error, EINVAL, "Cannot destroy the NULL semaphore");
1085 if (!CloseHandle(sem->h))
1086 THROW1(system_error, GetLastError(), "CloseHandle() failed: %s",
1087 strerror(GetLastError()));
1089 DeleteCriticalSection(&(sem->value_lock));
1095 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
1098 THROW0(arg_error, EINVAL,
1099 "Cannot get the value of the NULL semaphore");
1101 EnterCriticalSection(&(sem->value_lock));
1102 *svalue = sem->value;
1103 LeaveCriticalSection(&(sem->value_lock));