Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Strict ANSI compilers (such as microsoft ones) do not allow non-constant initializers...
[simgrid.git] / src / xbt / xbt_os_thread.c
index 63f7bb0..00e5c55 100644 (file)
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#ifdef HAVE_PTHREAD_H
-/* XOPEN_SOURCE is needed to get sem_timedwait (on amd64 at least). Declare it before everything else to play safe.  */
-#define _XOPEN_SOURCE 600
-#endif
-
 #include "xbt/sysdep.h"
 #include "xbt/ex.h"
 #include "xbt/ex_interface.h" /* We play crude games with exceptions */
@@ -28,15 +23,15 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os,xbt,"Synchronization mechanism (OS-l
 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
 #ifdef HAVE_PTHREAD_H
 
-/* XOPEN_SOURCE is needed to get sem_timedwait (on amd64 at least) according to the man page, 
-   but the headers seem to follow __USE_XOPEN2K. 
-   So let's get safe and declare both before loading headers. */
-#define _XOPEN_SOURCE 600
-#include <features.h>
-
 #include <pthread.h>
 #include <semaphore.h>
 
+#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,
+                           const struct timespec *abs_timeout);
+#endif
+  
 
 /* use named sempahore when sem_init() does not work */
 #ifndef HAVE_SEM_INIT
@@ -234,7 +229,7 @@ void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
        case ETIMEDOUT:
         THROW1(timeout_error,0,"mutex %p not ready",mutex);    
        default:
-        THROW2(system_error,errcode,"xbt_mutex_tryacquire(%p) failed: %s",mutex, strerror(errcode));
+        THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
       }
 
                
@@ -446,7 +441,7 @@ void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
        case ETIMEDOUT:
         THROW1(timeout_error,0,"semaphore %p not ready",sem);
        default:
-        THROW2(system_error,errcode,"xbt_sem_tryacquire(%p) failed: %s",sem, strerror(errcode));
+        THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
       }
       
    } else {
@@ -660,15 +655,9 @@ xbt_os_mutex_t xbt_os_mutex_init(void) {
 }
 
 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
-
    EnterCriticalSection(& mutex->lock);
 }
 
-void xbt_os_mutex_tryacquire(xbt_os_mutex_t mutex)
-{
-       TryEnterCriticalSection(&mutex->lock);
-}
-
 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
        THROW_UNIMPLEMENTED;
 }
@@ -785,7 +774,7 @@ void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double dela
    if (delay < 0) {
       xbt_os_cond_wait(cond,mutex);
    } else {
-         DEBUG3("xbt_cond_timedwait(%p,%p,%ul)",&(cond->events),&(mutex->lock),end);
+         DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
 
    /* lock the threads counter and increment it */
    EnterCriticalSection (& cond->waiters_count_lock);
@@ -878,6 +867,10 @@ typedef struct xbt_os_sem_ {
    CRITICAL_SECTION value_lock;  /* protect access to value of the semaphore  */
 }s_xbt_os_sem_t ;
 
+#ifndef INT_MAX
+# define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
+#endif
+
 xbt_os_sem_t
 xbt_os_sem_init(unsigned int value)
 {