#ifdef HAVE_MUTEX_TIMEDLOCK
/* redefine the function header since we fail to get this from system headers on amd (at least) */
int pthread_mutex_timedlock(pthread_mutex_t *mutex,
#ifdef HAVE_MUTEX_TIMEDLOCK
/* redefine the function header since we fail to get this from system headers on amd (at least) */
int pthread_mutex_timedlock(pthread_mutex_t *mutex,
/* frees the xbt_os_thread_t corresponding to the current thread */
static void xbt_os_thread_free_thread_data(void*d){
/* frees the xbt_os_thread_t corresponding to the current thread */
static void xbt_os_thread_free_thread_data(void*d){
- if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
- THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
+ if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL)))
+ THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
- main_thread=xbt_new(s_xbt_os_thread_t,1);
- main_thread->name = (char*)"main";
- main_thread->start_routine = NULL;
- main_thread->param = NULL;
- main_thread->exception = xbt_new(ex_ctx_t, 1);
- XBT_CTX_INITIALIZE(main_thread->exception);
+ main_thread=xbt_new(s_xbt_os_thread_t,1);
+ main_thread->name = (char*)"main";
+ main_thread->start_routine = NULL;
+ main_thread->param = NULL;
+ main_thread->exception = xbt_new(ex_ctx_t, 1);
+ XBT_CTX_INITIALIZE(main_thread->exception);
-// int errcode;
-
-// if ((errcode=pthread_key_delete(xbt_self_thread_key)))
-// THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
- free(main_thread->exception);
- free(main_thread);
- main_thread = NULL;
- thread_mod_inited=0;
+ // int errcode;
+
+ // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
+ // THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
+ free(main_thread->exception);
+ free(main_thread);
+ main_thread = NULL;
+ thread_mod_inited=0;
if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
THROW0(system_error,errcode,
if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
THROW0(system_error,errcode,
return (*(t->start_routine))(t->param);
}
xbt_os_thread_t xbt_os_thread_create(const char*name,
return (*(t->start_routine))(t->param);
}
xbt_os_thread_t xbt_os_thread_create(const char*name,
- xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
- res_thread->name = xbt_strdup(name);
- res_thread->start_routine = start_routine;
- res_thread->param = param;
- res_thread->exception = xbt_new(ex_ctx_t, 1);
- XBT_CTX_INITIALIZE(res_thread->exception);
+ xbt_os_thread_t res_thread=xbt_new(s_xbt_os_thread_t,1);
+ res_thread->name = xbt_strdup(name);
+ res_thread->start_routine = start_routine;
+ res_thread->param = param;
+ res_thread->exception = xbt_new(ex_ctx_t, 1);
+ XBT_CTX_INITIALIZE(res_thread->exception);
- if ((errcode = pthread_create(&(res_thread->t), NULL,
- wrapper_start_routine, res_thread)))
- THROW1(system_error,errcode,
- "pthread_create failed: %s",strerror(errcode));
+ if ((errcode = pthread_create(&(res_thread->t), NULL,
+ wrapper_start_routine, res_thread)))
+ THROW1(system_error,errcode,
+ "pthread_create failed: %s",strerror(errcode));
if ((errcode = pthread_join(thread->t,thread_return)))
THROW1(system_error,errcode, "pthread_join failed: %s",
if ((errcode = pthread_join(thread->t,thread_return)))
THROW1(system_error,errcode, "pthread_join failed: %s",
}
/****** mutex related functions ******/
typedef struct xbt_os_mutex_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
}
/****** mutex related functions ******/
typedef struct xbt_os_mutex_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
- if ((errcode = pthread_mutex_init(&(res->m),NULL)))
- THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
- strerror(errcode));
+ if ((errcode = pthread_mutex_init(&(res->m),NULL)))
+ THROW1(system_error,errcode,"pthread_mutex_init() failed: %s",
+ strerror(errcode));
- if ((errcode=pthread_mutex_lock(&(mutex->m))))
- THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
- mutex, strerror(errcode));
+ if ((errcode=pthread_mutex_lock(&(mutex->m))))
+ THROW2(system_error,errcode,"pthread_mutex_lock(%p) failed: %s",
+ mutex, strerror(errcode));
- int errcode;
-
- if (delay < 0) {
- xbt_os_mutex_acquire(mutex);
-
- } else if (delay == 0) {
- errcode=pthread_mutex_trylock(&(mutex->m));
-
- switch (errcode) {
- case 0:
- return;
- case ETIMEDOUT:
- THROW1(timeout_error,0,"mutex %p not ready",mutex);
- default:
- THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
- }
-
-
- } else {
-
+ int errcode;
+
+ if (delay < 0) {
+ xbt_os_mutex_acquire(mutex);
+
+ } else if (delay == 0) {
+ errcode=pthread_mutex_trylock(&(mutex->m));
+
+ switch (errcode) {
+ case 0:
+ return;
+ case ETIMEDOUT:
+ THROW1(timeout_error,0,"mutex %p not ready",mutex);
+ default:
+ THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
+ }
+
+
+ } else {
+
- struct timespec ts_end;
- double end = delay + xbt_os_time();
-
- ts_end.tv_sec = (time_t) floor(end);
- ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
- DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
-
- errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
-
+ struct timespec ts_end;
+ double end = delay + xbt_os_time();
+
+ ts_end.tv_sec = (time_t) floor(end);
+ ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
+ DEBUG2("pthread_mutex_timedlock(%p,%p)",&(mutex->m), &ts_end);
+
+ errcode=pthread_mutex_timedlock(&(mutex->m),&ts_end);
+
- double start = xbt_os_time();
- do {
- errcode = pthread_mutex_trylock(&(mutex->m));
- if (errcode == EBUSY)
- xbt_os_thread_yield();
- } while (errcode == EBUSY && xbt_os_time()-start <delay);
-
+ double start = xbt_os_time();
+ do {
+ errcode = pthread_mutex_trylock(&(mutex->m));
-
- switch (errcode) {
- case 0:
- return;
-
- case ETIMEDOUT:
- THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
-
- default:
- THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
- }
- }
+
+ switch (errcode) {
+ case 0:
+ return;
+
+ case ETIMEDOUT:
+ THROW2(timeout_error,delay,"mutex %p wasn't signaled before timeout (%f)",mutex,delay);
+
+ default:
+ THROW3(system_error,errcode,"pthread_mutex_timedlock(%p,%f) failed: %s",mutex,delay, strerror(errcode));
+ }
+ }
- if ((errcode=pthread_mutex_unlock(&(mutex->m))))
- THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
- mutex, strerror(errcode));
+ if ((errcode=pthread_mutex_unlock(&(mutex->m))))
+ THROW2(system_error,errcode,"pthread_mutex_unlock(%p) failed: %s",
+ mutex, strerror(errcode));
- if ((errcode=pthread_mutex_destroy(&(mutex->m))))
- THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
- mutex, strerror(errcode));
- free(mutex);
+ if ((errcode=pthread_mutex_destroy(&(mutex->m))))
+ THROW2(system_error,errcode,"pthread_mutex_destroy(%p) failed: %s",
+ mutex, strerror(errcode));
+ free(mutex);
}
/***** condition related functions *****/
typedef struct xbt_os_cond_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
}
/***** condition related functions *****/
typedef struct xbt_os_cond_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
- xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
- int errcode;
- if ((errcode=pthread_cond_init(&(res->c),NULL)))
- THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
- strerror(errcode));
+ xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t,1);
+ int errcode;
+ if ((errcode=pthread_cond_init(&(res->c),NULL)))
+ THROW1(system_error,errcode,"pthread_cond_init() failed: %s",
+ strerror(errcode));
- int errcode;
- if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
- THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
- cond,mutex, strerror(errcode));
+ int errcode;
+ if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
+ THROW3(system_error,errcode,"pthread_cond_wait(%p,%p) failed: %s",
+ cond,mutex, strerror(errcode));
- int errcode;
- struct timespec ts_end;
- double end = delay + xbt_os_time();
-
- if (delay < 0) {
- xbt_os_cond_wait(cond,mutex);
- } else {
- ts_end.tv_sec = (time_t) floor(end);
- ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
- DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
- switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
- case 0:
- return;
- case ETIMEDOUT:
- THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
- cond,mutex, delay);
- default:
- THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
- cond,mutex, delay, strerror(errcode));
- }
- }
+ int errcode;
+ struct timespec ts_end;
+ double end = delay + xbt_os_time();
+
+ if (delay < 0) {
+ xbt_os_cond_wait(cond,mutex);
+ } else {
+ ts_end.tv_sec = (time_t) floor(end);
+ ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
+ DEBUG3("pthread_cond_timedwait(%p,%p,%p)",&(cond->c),&(mutex->m), &ts_end);
+ switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) {
+ case 0:
+ return;
+ case ETIMEDOUT:
+ THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)",
+ cond,mutex, delay);
+ default:
+ THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s",
+ cond,mutex, delay, strerror(errcode));
+ }
+ }
- int errcode;
- if ((errcode=pthread_cond_signal(&(cond->c))))
- THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
- cond, strerror(errcode));
+ int errcode;
+ if ((errcode=pthread_cond_signal(&(cond->c))))
+ THROW2(system_error,errcode,"pthread_cond_signal(%p) failed: %s",
+ cond, strerror(errcode));
- int errcode;
- if ((errcode=pthread_cond_broadcast(&(cond->c))))
- THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
- cond, strerror(errcode));
+ int errcode;
+ if ((errcode=pthread_cond_broadcast(&(cond->c))))
+ THROW2(system_error,errcode,"pthread_cond_broadcast(%p) failed: %s",
+ cond, strerror(errcode));
- if ((errcode=pthread_cond_destroy(&(cond->c))))
- THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
- cond, strerror(errcode));
- free(cond);
+ if ((errcode=pthread_cond_destroy(&(cond->c))))
+ THROW2(system_error,errcode,"pthread_cond_destroy(%p) failed: %s",
+ cond, strerror(errcode));
+ free(cond);
- /* On some systems (MAC OS X), only the stub of sem_init is to be found.
- * Any attempt to use it leads to ENOSYS (function not implemented).
- * If such a prehistoric system is detected, do the job with sem_open instead
- */
+ /* On some systems (MAC OS X), only the stub of sem_init is to be found.
+ * Any attempt to use it leads to ENOSYS (function not implemented).
+ * If such a prehistoric system is detected, do the job with sem_open instead
+ */
- if(sem_init(&(res->s),0,value) != 0)
- THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
- res->ps = &(res->s);
-
+ if(sem_init(&(res->s),0,value) != 0)
+ THROW1(system_error,errno,"sem_init() failed: %s", strerror(errno));
+ res->ps = &(res->s);
+
- xbt_os_mutex_acquire(next_sem_ID_lock);
- res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
- xbt_os_mutex_release(next_sem_ID_lock);
-
- res->ps = sem_open(res->name, O_CREAT, 0644, value);
- if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
- /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
- res->name[13] = '\0';
- res->ps = sem_open(res->name, O_CREAT, 0644, 1);
- }
- if ((res->ps == (sem_t *)SEM_FAILED))
- THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
-
- /* Remove the name from the semaphore namespace: we never join on it */
- if(sem_unlink(res->name) < 0)
- THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
+ xbt_os_mutex_acquire(next_sem_ID_lock);
+ res->name = bprintf("/%d.%d",(*xbt_getpid)(),++next_sem_ID);
+ xbt_os_mutex_release(next_sem_ID_lock);
+
+ res->ps = sem_open(res->name, O_CREAT, 0644, value);
+ if ((res->ps == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
+ /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
+ res->name[13] = '\0';
+ res->ps = sem_open(res->name, O_CREAT, 0644, 1);
+ }
+ if ((res->ps == (sem_t *)SEM_FAILED))
+ THROW1(system_error,errno,"sem_open() failed: %s",strerror(errno));
+
+ /* Remove the name from the semaphore namespace: we never join on it */
+ if(sem_unlink(res->name) < 0)
+ THROW1(system_error,errno,"sem_unlink() failed: %s", strerror(errno));
- if(!sem)
- THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
- if(sem_wait(sem->ps) < 0)
- THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
+ if(!sem)
+ THROW0(arg_error,EINVAL,"Cannot acquire of the NULL semaphore");
+ if(sem_wait(sem->ps) < 0)
+ THROW1(system_error,errno,"sem_wait() failed: %s", strerror(errno));
- switch (errcode) {
- case 0:
- return;
- case ETIMEDOUT:
- THROW1(timeout_error,0,"semaphore %p not ready",sem);
- default:
- THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
- }
-
- } else {
+ switch (errcode) {
+ case 0:
+ return;
+ case ETIMEDOUT:
+ THROW1(timeout_error,0,"semaphore %p not ready",sem);
+ default:
+ THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
+ }
+
+ } else {
- struct timespec ts_end;
- double end = delay + xbt_os_time();
-
- ts_end.tv_sec = (time_t) floor(end);
- ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
- DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
- errcode = sem_timedwait(sem->s,&ts_end);
-
+ struct timespec ts_end;
+ double end = delay + xbt_os_time();
+
+ ts_end.tv_sec = (time_t) floor(end);
+ ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000);
+ DEBUG2("sem_timedwait(%p,%p)",sem->ps,&ts_end);
+ errcode = sem_timedwait(sem->s,&ts_end);
+
- double start = xbt_os_time();
- do {
- errcode = sem_trywait(sem->ps);
- if (errcode == EBUSY)
- xbt_os_thread_yield();
- } while (errcode == EBUSY && xbt_os_time()-start <delay);
-
+ double start = xbt_os_time();
+ do {
+ errcode = sem_trywait(sem->ps);
- case ETIMEDOUT:
- THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
+ switch (errcode) {
+ case 0:
+ return;
+
+ case ETIMEDOUT:
+ THROW2(timeout_error,delay,"semaphore %p wasn't signaled before timeout (%f)",sem,delay);
- default:
- THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
- }
- }
+ default:
+ THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,delay, strerror(errcode));
+ }
+ }
- if(sem_post(sem->ps) < 0)
- THROW1(system_error,errno,"sem_post() failed: %s",
- strerror(errno));
+ if(sem_post(sem->ps) < 0)
+ THROW1(system_error,errno,"sem_post() failed: %s",
+ strerror(errno));
- if(sem_destroy(sem->ps)) < 0)
- THROW1(system_error,errno,"sem_destroy() failed: %s",
- strerror(errno));
+ if(sem_destroy(sem->ps)) < 0)
+ THROW1(system_error,errno,"sem_destroy() failed: %s",
+ strerror(errno));
- if(sem_close(sem->ps) < 0)
- THROW1(system_error,errno,"sem_close() failed: %s",
- strerror(errno));
- xbt_free(sem->name);
+ if(sem_close(sem->ps) < 0)
+ THROW1(system_error,errno,"sem_close() failed: %s",
+ strerror(errno));
+ xbt_free(sem->name);
- if(sem_getvalue(&(sem->s),svalue) < 0)
- THROW1(system_error,errno,"sem_getvalue() failed: %s",
- strerror(errno));
+ if(sem_getvalue(&(sem->s),svalue) < 0)
+ THROW1(system_error,errno,"sem_getvalue() failed: %s",
+ strerror(errno));
- if (!TlsFree(xbt_self_thread_key))
- THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
+ if (!TlsFree(xbt_self_thread_key))
+ THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
- if(!TlsSetValue(xbt_self_thread_key,t))
- THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
+ if(!TlsSetValue(xbt_self_thread_key,t))
+ THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
+
+ rv = (DWORD*)((t->start_routine)(t->param));
- t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
- (LPTHREAD_START_ROUTINE)wrapper_start_routine,
- t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
+ t->handle = CreateThread(NULL,XBT_DEFAULT_THREAD_STACK_SIZE,
+ (LPTHREAD_START_ROUTINE)wrapper_start_routine,
+ t,STACK_SIZE_PARAM_IS_A_RESERVATION,&(t->id));
- if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
- THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
+ if(WAIT_OBJECT_0 != WaitForSingleObject(thread->handle,INFINITE))
+ THROW0(system_error,(int)GetLastError(), "WaitForSingleObject failed");
- if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
- THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
- }
+ if(!GetExitCodeThread(thread->handle,(DWORD*)(*thread_return)))
+ THROW0(system_error,(int)GetLastError(), "GetExitCodeThread failed");
+ }
}
void xbt_os_thread_cancel(xbt_os_thread_t t) {
if(!TerminateThread(t->handle,0))
}
void xbt_os_thread_cancel(xbt_os_thread_t t) {
if(!TerminateThread(t->handle,0))
}
/****** mutex related functions ******/
typedef struct xbt_os_mutex_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
}
/****** mutex related functions ******/
typedef struct xbt_os_mutex_ {
/* KEEP IT IN SYNC WITH xbt_thread.c */
- unsigned int waiters_count; /* the number of waiters */
- CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
+ unsigned int waiters_count; /* the number of waiters */
+ CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
- /* initialize the critical section object */
- InitializeCriticalSection(& res->waiters_count_lock);
+ /* initialize the critical section object */
+ InitializeCriticalSection(& res->waiters_count_lock);
- /* Create an auto-reset event */
- res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
+ /* Create an auto-reset event */
+ res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
- if(!res->events[SIGNAL]){
- DeleteCriticalSection(& res->waiters_count_lock);
- free(res);
- THROW0(system_error,0,"CreateEvent failed for the signals");
- }
+ if(!res->events[SIGNAL]){
+ DeleteCriticalSection(& res->waiters_count_lock);
+ free(res);
+ THROW0(system_error,0,"CreateEvent failed for the signals");
+ }
- /* Create a manual-reset event. */
- res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
+ /* Create a manual-reset event. */
+ res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
- DeleteCriticalSection(& res->waiters_count_lock);
- CloseHandle(res->events[SIGNAL]);
- free(res);
- THROW0(system_error,0,"CreateEvent failed for the broadcasts");
- }
+ DeleteCriticalSection(& res->waiters_count_lock);
+ CloseHandle(res->events[SIGNAL]);
+ free(res);
+ THROW0(system_error,0,"CreateEvent failed for the broadcasts");
+ }
- /* lock the threads counter and increment it */
- EnterCriticalSection (& cond->waiters_count_lock);
- cond->waiters_count++;
- LeaveCriticalSection (& cond->waiters_count_lock);
+ /* lock the threads counter and increment it */
+ EnterCriticalSection (& cond->waiters_count_lock);
+ cond->waiters_count++;
+ LeaveCriticalSection (& cond->waiters_count_lock);
- /* wait for a signal (broadcast or no) */
- wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
+ /* wait for a signal (broadcast or no) */
+ wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
- if(wait_result == WAIT_FAILED)
- THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
+ if(wait_result == WAIT_FAILED)
+ THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
- /* it's the last waiter or it's a broadcast ? */
- is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
+ /* it's the last waiter or it's a broadcast ? */
+ is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
- /* yes it's the last waiter or it's a broadcast
- * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
- * by the system.
- */
- if (is_last_waiter)
- if(!ResetEvent (cond->events[BROADCAST]))
- THROW0(system_error,0,"ResetEvent failed");
+ /* yes it's the last waiter or it's a broadcast
+ * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
+ * by the system.
+ */
+ if (is_last_waiter)
+ if(!ResetEvent (cond->events[BROADCAST]))
+ THROW0(system_error,0,"ResetEvent failed");
- /* relock the mutex associated with the condition in accordance with the posix thread specification */
- EnterCriticalSection (& mutex->lock);
+ /* relock the mutex associated with the condition in accordance with the posix thread specification */
+ EnterCriticalSection (& mutex->lock);
- if (delay < 0) {
- xbt_os_cond_wait(cond,mutex);
- } else {
- DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
+ if (delay < 0) {
+ xbt_os_cond_wait(cond,mutex);
+ } else {
+ DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
- /* lock the threads counter and increment it */
- EnterCriticalSection (& cond->waiters_count_lock);
- cond->waiters_count++;
- LeaveCriticalSection (& cond->waiters_count_lock);
+ /* lock the threads counter and increment it */
+ EnterCriticalSection (& cond->waiters_count_lock);
+ cond->waiters_count++;
+ LeaveCriticalSection (& cond->waiters_count_lock);
- /* unlock the mutex associate with the condition */
- LeaveCriticalSection (& mutex->lock);
- /* wait for a signal (broadcast or no) */
+ /* unlock the mutex associate with the condition */
+ LeaveCriticalSection (& mutex->lock);
+ /* wait for a signal (broadcast or no) */
- wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
+ wait_result = WaitForMultipleObjects (2, cond->events, FALSE, end);
- switch(wait_result) {
- case WAIT_TIMEOUT:
- THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
- case WAIT_FAILED:
- THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
- }
+ switch(wait_result) {
+ case WAIT_TIMEOUT:
+ THROW3(timeout_error,GetLastError(),"condition %p (mutex %p) wasn't signaled before timeout (%f)",cond,mutex, delay);
+ case WAIT_FAILED:
+ THROW0(system_error,GetLastError(),"WaitForMultipleObjects failed, so we cannot wait on the condition");
+ }
- /* it's the last waiter or it's a broadcast ? */
- is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
+ /* it's the last waiter or it's a broadcast ? */
+ is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
- /* yes it's the last waiter or it's a broadcast
- * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
- * by the system.
- */
- if (is_last_waiter)
+ /* yes it's the last waiter or it's a broadcast
+ * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
+ * by the system.
+ */
+ if (is_last_waiter)
- /* relock the mutex associated with the condition in accordance with the posix thread specification */
- EnterCriticalSection (& mutex->lock);
- }
- /*THROW_UNIMPLEMENTED;*/
+ /* relock the mutex associated with the condition in accordance with the posix thread specification */
+ EnterCriticalSection (& mutex->lock);
+ }
+ /*THROW_UNIMPLEMENTED;*/
- EnterCriticalSection (& cond->waiters_count_lock);
- have_waiters = cond->waiters_count > 0;
- LeaveCriticalSection (& cond->waiters_count_lock);
+ EnterCriticalSection (& cond->waiters_count_lock);
+ have_waiters = cond->waiters_count > 0;
+ LeaveCriticalSection (& cond->waiters_count_lock);
- if (have_waiters)
- if(!SetEvent(cond->events[SIGNAL]))
- THROW0(system_error,0,"SetEvent failed");
+ if (have_waiters)
+ if(!SetEvent(cond->events[SIGNAL]))
+ THROW0(system_error,0,"SetEvent failed");
- EnterCriticalSection (& cond->waiters_count_lock);
- have_waiters = cond->waiters_count > 0;
- LeaveCriticalSection (& cond->waiters_count_lock);
+ EnterCriticalSection (& cond->waiters_count_lock);
+ have_waiters = cond->waiters_count > 0;
+ LeaveCriticalSection (& cond->waiters_count_lock);
- if(value > INT_MAX)
- THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
+ if(value > INT_MAX)
+ THROW1(arg_error,value,"Semaphore initial value too big: %ud cannot be stored as a signed int",value);
- if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
- THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
- strerror(GetLastError()));
- return NULL;
- }
+ if(!(res->h = CreateSemaphore(NULL,value,(long)INT_MAX,NULL))) {
+ THROW1(system_error,GetLastError(),"CreateSemaphore() failed: %s",
+ strerror(GetLastError()));
+ return NULL;
+ }
- /* wait failure */
- if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
- THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
- strerror(GetLastError()));
- EnterCriticalSection(&(sem->value_lock));
- sem->value--;
- LeaveCriticalSection(&(sem->value_lock));
+ /* wait failure */
+ if(WAIT_OBJECT_0 != WaitForSingleObject(sem->h,INFINITE))
+ THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
+ strerror(GetLastError()));
+ EnterCriticalSection(&(sem->value_lock));
+ sem->value--;
+ LeaveCriticalSection(&(sem->value_lock));
- long seconds;
- long milliseconds;
- double end = timeout + xbt_os_time();
-
- if(!sem)
- THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
-
- if (timeout < 0)
- {
- xbt_os_sem_acquire(sem);
- }
- else /* timeout can be zero <-> try acquire ) */
- {
-
- seconds = (long) floor(end);
- milliseconds = (long)( ( end - seconds) * 1000);
- milliseconds += (seconds * 1000);
-
- switch(WaitForSingleObject(sem->h,milliseconds))
- {
- case WAIT_OBJECT_0:
- EnterCriticalSection(&(sem->value_lock));
- sem->value--;
- LeaveCriticalSection(&(sem->value_lock));
- return;
-
- case WAIT_TIMEOUT:
- THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
- return;
-
- default:
- THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
- }
- }
+ long seconds;
+ long milliseconds;
+ double end = timeout + xbt_os_time();
+
+ if(!sem)
+ THROW0(arg_error,EINVAL,"Cannot acquire the NULL semaphore");
+
+ if (timeout < 0)
+ {
+ xbt_os_sem_acquire(sem);
+ }
+ else /* timeout can be zero <-> try acquire ) */
+ {
+
+ seconds = (long) floor(end);
+ milliseconds = (long)( ( end - seconds) * 1000);
+ milliseconds += (seconds * 1000);
+
+ switch(WaitForSingleObject(sem->h,milliseconds))
+ {
+ case WAIT_OBJECT_0:
+ EnterCriticalSection(&(sem->value_lock));
+ sem->value--;
+ LeaveCriticalSection(&(sem->value_lock));
+ return;
+
+ case WAIT_TIMEOUT:
+ THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
+ return;
+
+ default:
+ THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
+ }
+ }
- if(!ReleaseSemaphore(sem->h,1, NULL))
- THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
- strerror(GetLastError()));
- EnterCriticalSection (&(sem->value_lock));
- sem->value++;
- LeaveCriticalSection(&(sem->value_lock));
+ if(!ReleaseSemaphore(sem->h,1, NULL))
+ THROW1(system_error,GetLastError(),"ReleaseSemaphore() failed: %s",
+ strerror(GetLastError()));
+ EnterCriticalSection (&(sem->value_lock));
+ sem->value++;
+ LeaveCriticalSection(&(sem->value_lock));
- if(!CloseHandle(sem->h))
- THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
- strerror(GetLastError()));
+ if(!CloseHandle(sem->h))
+ THROW1(system_error,GetLastError(),"CloseHandle() failed: %s",
+ strerror(GetLastError()));