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) {
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");
111 free(main_thread->exception);
114 thread_mod_inited = 0;
115 #ifndef HAVE_SEM_WAIT
116 xbt_os_mutex_destroy(next_sem_ID_lock);
119 /* Restore the default exception setup */
120 __xbt_ex_ctx = &__xbt_ex_ctx_default;
121 __xbt_ex_terminate = &__xbt_ex_terminate_default;
124 static void *wrapper_start_routine(void *s)
126 xbt_os_thread_t t = s;
129 if ((errcode = pthread_setspecific(xbt_self_thread_key, t)))
130 THROW0(system_error, errcode,
131 "pthread_setspecific failed for xbt_self_thread_key");
133 return (*(t->start_routine)) (t->param);
136 xbt_os_thread_t xbt_os_thread_create(const char *name,
137 pvoid_f_pvoid_t start_routine,
142 xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
143 res_thread->name = xbt_strdup(name);
144 res_thread->start_routine = start_routine;
145 res_thread->param = param;
146 res_thread->exception = xbt_new(ex_ctx_t, 1);
147 XBT_CTX_INITIALIZE(res_thread->exception);
149 if ((errcode = pthread_create(&(res_thread->t), NULL,
150 wrapper_start_routine, res_thread)))
151 THROW1(system_error, errcode,
152 "pthread_create failed: %s", strerror(errcode));
157 const char *xbt_os_thread_name(xbt_os_thread_t t)
162 const char *xbt_os_thread_self_name(void)
164 xbt_os_thread_t self = xbt_os_thread_self();
165 return self ? self->name : "main";
168 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
173 if ((errcode = pthread_join(thread->t, thread_return)))
174 THROW1(system_error, errcode, "pthread_join failed: %s",
176 if (thread->exception)
177 free(thread->exception);
182 if (thread == main_thread) /* just killed main thread */
188 void xbt_os_thread_exit(int *retval)
190 pthread_exit(retval);
193 xbt_os_thread_t xbt_os_thread_self(void)
197 if (!thread_mod_inited)
200 res = pthread_getspecific(xbt_self_thread_key);
208 void xbt_os_thread_yield(void)
213 void xbt_os_thread_cancel(xbt_os_thread_t t)
215 pthread_cancel(t->t);
218 /****** mutex related functions ******/
219 typedef struct xbt_os_mutex_ {
220 /* KEEP IT IN SYNC WITH xbt_thread.c */
227 xbt_os_mutex_t xbt_os_mutex_init(void)
229 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
232 if ((errcode = pthread_mutex_init(&(res->m), NULL)))
233 THROW1(system_error, errcode, "pthread_mutex_init() failed: %s",
239 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
243 if ((errcode = pthread_mutex_lock(&(mutex->m))))
244 THROW2(system_error, errcode, "pthread_mutex_lock(%p) failed: %s",
245 mutex, strerror(errcode));
249 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
254 xbt_os_mutex_acquire(mutex);
256 } else if (delay == 0) {
257 errcode = pthread_mutex_trylock(&(mutex->m));
263 THROW1(timeout_error, 0, "mutex %p not ready", mutex);
265 THROW2(system_error, errcode, "xbt_mutex_timedacquire(%p) failed: %s",
266 mutex, strerror(errcode));
272 #ifdef HAVE_MUTEX_TIMEDLOCK
273 struct timespec ts_end;
274 double end = delay + xbt_os_time();
276 ts_end.tv_sec = (time_t) floor(end);
277 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
278 DEBUG2("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
280 errcode = pthread_mutex_timedlock(&(mutex->m), &ts_end);
282 #else /* Well, let's reimplement it since those lazy libc dudes didn't */
283 double start = xbt_os_time();
285 errcode = pthread_mutex_trylock(&(mutex->m));
286 if (errcode == EBUSY)
287 xbt_os_thread_yield();
288 } while (errcode == EBUSY && xbt_os_time() - start < delay);
290 if (errcode == EBUSY)
293 #endif /* HAVE_MUTEX_TIMEDLOCK */
300 THROW2(timeout_error, delay,
301 "mutex %p wasn't signaled before timeout (%f)", mutex, delay);
304 THROW3(system_error, errcode,
305 "pthread_mutex_timedlock(%p,%f) failed: %s", mutex, delay,
311 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
315 if ((errcode = pthread_mutex_unlock(&(mutex->m))))
316 THROW2(system_error, errcode, "pthread_mutex_unlock(%p) failed: %s",
317 mutex, strerror(errcode));
320 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
327 if ((errcode = pthread_mutex_destroy(&(mutex->m))))
328 THROW2(system_error, errcode, "pthread_mutex_destroy(%p) failed: %s",
329 mutex, strerror(errcode));
333 /***** condition related functions *****/
334 typedef struct xbt_os_cond_ {
335 /* KEEP IT IN SYNC WITH xbt_thread.c */
339 xbt_os_cond_t xbt_os_cond_init(void)
341 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t, 1);
343 if ((errcode = pthread_cond_init(&(res->c), NULL)))
344 THROW1(system_error, errcode, "pthread_cond_init() failed: %s",
350 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
353 if ((errcode = pthread_cond_wait(&(cond->c), &(mutex->m))))
354 THROW3(system_error, errcode, "pthread_cond_wait(%p,%p) failed: %s",
355 cond, mutex, strerror(errcode));
359 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
363 struct timespec ts_end;
364 double end = delay + xbt_os_time();
367 xbt_os_cond_wait(cond, mutex);
369 ts_end.tv_sec = (time_t) floor(end);
370 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
371 DEBUG3("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m),
374 pthread_cond_timedwait(&(cond->c), &(mutex->m), &ts_end))) {
378 THROW3(timeout_error, errcode,
379 "condition %p (mutex %p) wasn't signaled before timeout (%f)",
382 THROW4(system_error, errcode,
383 "pthread_cond_timedwait(%p,%p,%f) failed: %s", cond, mutex,
384 delay, strerror(errcode));
389 void xbt_os_cond_signal(xbt_os_cond_t cond)
392 if ((errcode = pthread_cond_signal(&(cond->c))))
393 THROW2(system_error, errcode, "pthread_cond_signal(%p) failed: %s",
394 cond, strerror(errcode));
397 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
400 if ((errcode = pthread_cond_broadcast(&(cond->c))))
401 THROW2(system_error, errcode, "pthread_cond_broadcast(%p) failed: %s",
402 cond, strerror(errcode));
405 void xbt_os_cond_destroy(xbt_os_cond_t cond)
412 if ((errcode = pthread_cond_destroy(&(cond->c))))
413 THROW2(system_error, errcode, "pthread_cond_destroy(%p) failed: %s",
414 cond, strerror(errcode));
418 void *xbt_os_thread_getparam(void)
420 xbt_os_thread_t t = xbt_os_thread_self();
421 return t ? t->param : NULL;
424 typedef struct xbt_os_sem_ {
425 #ifndef HAVE_SEM_INIT
433 #define SEM_FAILED (-1)
436 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
438 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t, 1);
440 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
441 * Any attempt to use it leads to ENOSYS (function not implemented).
442 * If such a prehistoric system is detected, do the job with sem_open instead
445 if (sem_init(&(res->s), 0, value) != 0)
446 THROW1(system_error, errno, "sem_init() failed: %s", strerror(errno));
449 #else /* damn, no sem_init(). Reimplement it */
451 xbt_os_mutex_acquire(next_sem_ID_lock);
452 res->name = bprintf("/%d.%d", (*xbt_getpid) (), ++next_sem_ID);
453 xbt_os_mutex_release(next_sem_ID_lock);
455 res->ps = sem_open(res->name, O_CREAT, 0644, value);
456 if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
457 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
458 res->name[13] = '\0';
459 res->ps = sem_open(res->name, O_CREAT, 0644, 1);
461 if ((res->ps == (sem_t *) SEM_FAILED))
462 THROW1(system_error, errno, "sem_open() failed: %s", strerror(errno));
464 /* Remove the name from the semaphore namespace: we never join on it */
465 if (sem_unlink(res->name) < 0)
466 THROW1(system_error, errno, "sem_unlink() failed: %s", strerror(errno));
473 void xbt_os_sem_acquire(xbt_os_sem_t sem)
476 THROW0(arg_error, EINVAL, "Cannot acquire of the NULL semaphore");
477 if (sem_wait(sem->ps) < 0)
478 THROW1(system_error, errno, "sem_wait() failed: %s", strerror(errno));
481 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
486 THROW0(arg_error, EINVAL, "Cannot acquire of the NULL semaphore");
489 xbt_os_sem_acquire(sem);
490 } else if (delay == 0) {
491 errcode = sem_trywait(sem->ps);
497 THROW1(timeout_error, 0, "semaphore %p not ready", sem);
499 THROW2(system_error, errcode, "xbt_os_sem_timedacquire(%p) failed: %s",
500 sem, strerror(errcode));
505 struct timespec ts_end;
506 double end = delay + xbt_os_time();
508 ts_end.tv_sec = (time_t) floor(end);
509 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
510 DEBUG2("sem_timedwait(%p,%p)", sem->ps, &ts_end);
511 errcode = sem_timedwait(sem->s, &ts_end);
513 #else /* Okay, reimplement this function then */
514 double start = xbt_os_time();
516 errcode = sem_trywait(sem->ps);
517 if (errcode == EBUSY)
518 xbt_os_thread_yield();
519 } while (errcode == EBUSY && xbt_os_time() - start < delay);
521 if (errcode == EBUSY)
530 THROW2(timeout_error, delay,
531 "semaphore %p wasn't signaled before timeout (%f)", sem, delay);
534 THROW3(system_error, errcode, "sem_timedwait(%p,%f) failed: %s", sem,
535 delay, strerror(errcode));
540 void xbt_os_sem_release(xbt_os_sem_t sem)
543 THROW0(arg_error, EINVAL, "Cannot release of the NULL semaphore");
545 if (sem_post(sem->ps) < 0)
546 THROW1(system_error, errno, "sem_post() failed: %s", strerror(errno));
549 void xbt_os_sem_destroy(xbt_os_sem_t sem)
552 THROW0(arg_error, EINVAL, "Cannot destroy the NULL sempahore");
555 if (sem_destroy(sem->ps)<0)
556 THROW1(system_error, errno, "sem_destroy() failed: %s",
559 if (sem_close(sem->ps) < 0)
560 THROW1(system_error, errno, "sem_close() failed: %s", strerror(errno));
567 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
570 THROW0(arg_error, EINVAL, "Cannot get the value of the NULL semaphore");
572 if (sem_getvalue(&(sem->s), svalue) < 0)
573 THROW1(system_error, errno, "sem_getvalue() failed: %s", strerror(errno));
576 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
578 #elif defined(_XBT_WIN32)
582 typedef struct xbt_os_thread_ {
584 HANDLE handle; /* the win thread handle */
585 unsigned long id; /* the win thread id */
586 pvoid_f_pvoid_t start_routine;
590 /* so we can specify the size of the stack of the threads */
591 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
592 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
595 /* the default size of the stack of the threads (in bytes)*/
596 #define XBT_DEFAULT_THREAD_STACK_SIZE 4096
598 /* key to the TLS containing the xbt_os_thread_t structure */
599 static unsigned long xbt_self_thread_key;
601 void xbt_os_thread_mod_preinit(void)
603 xbt_self_thread_key = TlsAlloc();
606 void xbt_os_thread_mod_postexit(void)
609 if (!TlsFree(xbt_self_thread_key))
610 THROW0(system_error, (int) GetLastError(),
611 "TlsFree() failed to cleanup the thread submodule");
614 static DWORD WINAPI wrapper_start_routine(void *s)
616 xbt_os_thread_t t = (xbt_os_thread_t) s;
619 if (!TlsSetValue(xbt_self_thread_key, t))
620 THROW0(system_error, (int) GetLastError(),
621 "TlsSetValue of data describing the created thread failed");
623 rv = (DWORD *) ((t->start_routine) (t->param));
630 xbt_os_thread_t xbt_os_thread_create(const char *name,
631 pvoid_f_pvoid_t start_routine,
635 xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t, 1);
637 t->name = xbt_strdup(name);
638 t->start_routine = start_routine;
641 t->handle = CreateThread(NULL, XBT_DEFAULT_THREAD_STACK_SIZE,
642 (LPTHREAD_START_ROUTINE) wrapper_start_routine,
643 t, STACK_SIZE_PARAM_IS_A_RESERVATION, &(t->id));
647 THROW0(system_error, (int) GetLastError(), "CreateThread failed");
653 const char *xbt_os_thread_name(xbt_os_thread_t t)
658 const char *xbt_os_thread_self_name(void)
660 xbt_os_thread_t t = xbt_os_thread_self();
661 return t ? t->name : "main";
664 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
667 if (WAIT_OBJECT_0 != WaitForSingleObject(thread->handle, INFINITE))
668 THROW0(system_error, (int) GetLastError(), "WaitForSingleObject failed");
672 if (!GetExitCodeThread(thread->handle, (DWORD *) (*thread_return)))
673 THROW0(system_error, (int) GetLastError(), "GetExitCodeThread failed");
676 CloseHandle(thread->handle);
684 void xbt_os_thread_exit(int *retval)
692 xbt_os_thread_t xbt_os_thread_self(void)
694 return TlsGetValue(xbt_self_thread_key);
697 void *xbt_os_thread_getparam(void)
699 xbt_os_thread_t t = xbt_os_thread_self();
704 void xbt_os_thread_yield(void)
709 void xbt_os_thread_cancel(xbt_os_thread_t t)
711 if (!TerminateThread(t->handle, 0))
712 THROW0(system_error, (int) GetLastError(), "TerminateThread failed");
715 /****** mutex related functions ******/
716 typedef struct xbt_os_mutex_ {
717 /* KEEP IT IN SYNC WITH xbt_thread.c */
718 CRITICAL_SECTION lock;
721 xbt_os_mutex_t xbt_os_mutex_init(void)
723 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
725 /* initialize the critical section object */
726 InitializeCriticalSection(&(res->lock));
731 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
733 EnterCriticalSection(&mutex->lock);
736 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
741 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
744 LeaveCriticalSection(&mutex->lock);
748 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
754 DeleteCriticalSection(&mutex->lock);
758 /***** condition related functions *****/
759 enum { /* KEEP IT IN SYNC WITH xbt_thread.c */
765 typedef struct xbt_os_cond_ {
766 /* KEEP IT IN SYNC WITH xbt_thread.c */
767 HANDLE events[MAX_EVENTS];
769 unsigned int waiters_count; /* the number of waiters */
770 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
773 xbt_os_cond_t xbt_os_cond_init(void)
776 xbt_os_cond_t res = xbt_new0(s_xbt_os_cond_t, 1);
778 memset(&res->waiters_count_lock, 0, sizeof(CRITICAL_SECTION));
780 /* initialize the critical section object */
781 InitializeCriticalSection(&res->waiters_count_lock);
783 res->waiters_count = 0;
785 /* Create an auto-reset event */
786 res->events[SIGNAL] = CreateEvent(NULL, FALSE, FALSE, NULL);
788 if (!res->events[SIGNAL]) {
789 DeleteCriticalSection(&res->waiters_count_lock);
791 THROW0(system_error, 0, "CreateEvent failed for the signals");
794 /* Create a manual-reset event. */
795 res->events[BROADCAST] = CreateEvent(NULL, TRUE, FALSE, NULL);
797 if (!res->events[BROADCAST]) {
799 DeleteCriticalSection(&res->waiters_count_lock);
800 CloseHandle(res->events[SIGNAL]);
802 THROW0(system_error, 0, "CreateEvent failed for the broadcasts");
808 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
811 unsigned long wait_result;
814 /* lock the threads counter and increment it */
815 EnterCriticalSection(&cond->waiters_count_lock);
816 cond->waiters_count++;
817 LeaveCriticalSection(&cond->waiters_count_lock);
819 /* unlock the mutex associate with the condition */
820 LeaveCriticalSection(&mutex->lock);
822 /* wait for a signal (broadcast or no) */
823 wait_result = WaitForMultipleObjects(2, cond->events, FALSE, INFINITE);
825 if (wait_result == WAIT_FAILED)
826 THROW0(system_error, 0,
827 "WaitForMultipleObjects failed, so we cannot wait on the condition");
829 /* we have a signal lock the condition */
830 EnterCriticalSection(&cond->waiters_count_lock);
831 cond->waiters_count--;
833 /* it's the last waiter or it's a broadcast ? */
834 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1)
835 && (cond->waiters_count == 0));
837 LeaveCriticalSection(&cond->waiters_count_lock);
839 /* yes it's the last waiter or it's a broadcast
840 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
844 if (!ResetEvent(cond->events[BROADCAST]))
845 THROW0(system_error, 0, "ResetEvent failed");
847 /* relock the mutex associated with the condition in accordance with the posix thread specification */
848 EnterCriticalSection(&mutex->lock);
851 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
855 unsigned long wait_result = WAIT_TIMEOUT;
857 unsigned long end = (unsigned long) (delay * 1000);
861 xbt_os_cond_wait(cond, mutex);
863 DEBUG3("xbt_cond_timedwait(%p,%p,%lu)", &(cond->events), &(mutex->lock),
866 /* lock the threads counter and increment it */
867 EnterCriticalSection(&cond->waiters_count_lock);
868 cond->waiters_count++;
869 LeaveCriticalSection(&cond->waiters_count_lock);
871 /* unlock the mutex associate with the condition */
872 LeaveCriticalSection(&mutex->lock);
873 /* wait for a signal (broadcast or no) */
875 wait_result = WaitForMultipleObjects(2, cond->events, FALSE, end);
877 switch (wait_result) {
879 THROW3(timeout_error, GetLastError(),
880 "condition %p (mutex %p) wasn't signaled before timeout (%f)",
883 THROW0(system_error, GetLastError(),
884 "WaitForMultipleObjects failed, so we cannot wait on the condition");
887 /* we have a signal lock the condition */
888 EnterCriticalSection(&cond->waiters_count_lock);
889 cond->waiters_count--;
891 /* it's the last waiter or it's a broadcast ? */
892 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1)
893 && (cond->waiters_count == 0));
895 LeaveCriticalSection(&cond->waiters_count_lock);
897 /* yes it's the last waiter or it's a broadcast
898 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
902 if (!ResetEvent(cond->events[BROADCAST]))
903 THROW0(system_error, 0, "ResetEvent failed");
905 /* relock the mutex associated with the condition in accordance with the posix thread specification */
906 EnterCriticalSection(&mutex->lock);
908 /*THROW_UNIMPLEMENTED; */
911 void xbt_os_cond_signal(xbt_os_cond_t cond)
915 EnterCriticalSection(&cond->waiters_count_lock);
916 have_waiters = cond->waiters_count > 0;
917 LeaveCriticalSection(&cond->waiters_count_lock);
920 if (!SetEvent(cond->events[SIGNAL]))
921 THROW0(system_error, 0, "SetEvent failed");
923 xbt_os_thread_yield();
926 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
930 EnterCriticalSection(&cond->waiters_count_lock);
931 have_waiters = cond->waiters_count > 0;
932 LeaveCriticalSection(&cond->waiters_count_lock);
935 SetEvent(cond->events[BROADCAST]);
938 void xbt_os_cond_destroy(xbt_os_cond_t cond)
945 if (!CloseHandle(cond->events[SIGNAL]))
948 if (!CloseHandle(cond->events[BROADCAST]))
951 DeleteCriticalSection(&cond->waiters_count_lock);
956 THROW0(system_error, 0, "Error while destroying the condition");
959 typedef struct xbt_os_sem_ {
962 CRITICAL_SECTION value_lock; /* protect access to value of the semaphore */
966 # define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
969 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
974 THROW1(arg_error, value,
975 "Semaphore initial value too big: %ud cannot be stored as a signed int",
978 res = (xbt_os_sem_t) xbt_new0(s_xbt_os_sem_t, 1);
980 if (!(res->h = CreateSemaphore(NULL, value, (long) INT_MAX, NULL))) {
981 THROW1(system_error, GetLastError(), "CreateSemaphore() failed: %s",
982 strerror(GetLastError()));
988 InitializeCriticalSection(&(res->value_lock));
993 void xbt_os_sem_acquire(xbt_os_sem_t sem)
996 THROW0(arg_error, EINVAL, "Cannot acquire the NULL semaphore");
999 if (WAIT_OBJECT_0 != WaitForSingleObject(sem->h, INFINITE))
1000 THROW1(system_error, GetLastError(), "WaitForSingleObject() failed: %s",
1001 strerror(GetLastError()));
1002 EnterCriticalSection(&(sem->value_lock));
1004 LeaveCriticalSection(&(sem->value_lock));
1007 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
1011 double end = timeout + xbt_os_time();
1014 THROW0(arg_error, EINVAL, "Cannot acquire the NULL semaphore");
1017 xbt_os_sem_acquire(sem);
1018 } else { /* timeout can be zero <-> try acquire ) */
1021 seconds = (long) floor(end);
1022 milliseconds = (long) ((end - seconds) * 1000);
1023 milliseconds += (seconds * 1000);
1025 switch (WaitForSingleObject(sem->h, milliseconds)) {
1027 EnterCriticalSection(&(sem->value_lock));
1029 LeaveCriticalSection(&(sem->value_lock));
1033 THROW2(timeout_error, GetLastError(),
1034 "semaphore %p wasn't signaled before timeout (%f)", sem,
1039 THROW3(system_error, GetLastError(),
1040 "WaitForSingleObject(%p,%f) failed: %s", sem, timeout,
1041 strerror(GetLastError()));
1046 void xbt_os_sem_release(xbt_os_sem_t sem)
1049 THROW0(arg_error, EINVAL, "Cannot release the NULL semaphore");
1051 if (!ReleaseSemaphore(sem->h, 1, NULL))
1052 THROW1(system_error, GetLastError(), "ReleaseSemaphore() failed: %s",
1053 strerror(GetLastError()));
1054 EnterCriticalSection(&(sem->value_lock));
1056 LeaveCriticalSection(&(sem->value_lock));
1059 void xbt_os_sem_destroy(xbt_os_sem_t sem)
1062 THROW0(arg_error, EINVAL, "Cannot destroy the NULL semaphore");
1064 if (!CloseHandle(sem->h))
1065 THROW1(system_error, GetLastError(), "CloseHandle() failed: %s",
1066 strerror(GetLastError()));
1068 DeleteCriticalSection(&(sem->value_lock));
1074 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
1077 THROW0(arg_error, EINVAL, "Cannot get the value of the NULL semaphore");
1079 EnterCriticalSection(&(sem->value_lock));
1080 *svalue = sem->value;
1081 LeaveCriticalSection(&(sem->value_lock));