Prechádzať zdrojové kódy

fix correctness of pthread error reporting

Samuel Thibault 15 rokov pred
rodič
commit
bc58d77524
1 zmenil súbory, kde vykonal 45 pridanie a 48 odobranie
  1. 45 48
      include/pthread_win32/pthread.h

+ 45 - 48
include/pthread_win32/pthread.h

@@ -37,19 +37,23 @@ extern "C" {
 
 #ifdef __CYGWIN32__
 #include <sys/cygwin.h>
-#define setSystemErrno() errno = cygwin_internal(CW_GET_ERRNO_FROM_WINERROR, (GetLastError())
+#define unixErrno() cygwin_internal(CW_GET_ERRNO_FROM_WINERROR, (GetLastError())
 #else
+#define unixErrno() EIO
+#endif
 #if 0
-#define setSystemErrno() do { fprintf(stderr,"%s:%d: win %d\n", __FILE__, __LINE__, GetLastError()); errno = EIO; } while (0)
+#define setSystemErrno() do { fprintf(stderr,"%s:%d: win %d\n", __FILE__, __LINE__, GetLastError()); errno = unixErrno(); } while (0)
+#define winPthreadAssertWindows(expr) do { if (!(expr)) { fprintf(stderr,"%s:%d: %d\n", __FILE__, __LINE__, unixErrno()); return unixErrno(); } } while (0)
+#define winPthreadAssertPthread(expr) do { int ret = (expr); if (ret) { fprintf(stderr,"%s:%d: %d\n", __FILE__, __LINE__, ret); return ret; } } while (0)
+#define winPthreadAssert(expr) do { if (!(expr)) { fprintf(stderr,"%s:%d: %d\n", __FILE__, __LINE__, errno); return EIO; } } while (0)
 #else
-#define setSystemErrno() errno = EIO
-#endif
+#define setSystemErrno() errno = unixErrno()
+#define winPthreadAssertWindows(expr) do { if (!(expr)) { return unixErrno(); } } while (0)
+#define winPthreadAssertPthread(expr) do { int ret = (expr); if (ret) return ret; } while (0)
+#define winPthreadAssert(expr) do { if (!(expr)) return EIO; } while (0)
 #endif
-#define winPthreadAssertWindows(expr) do { if (!(expr)) { setSystemErrno(); return -1; } } while (0)
 #if 0
-#define winPthreadAssert(expr) do { if (!(expr)) { fprintf(stderr,"%s:%d: %d\n", __FILE__, __LINE__, errno); return -1; } } while (0)
 #else
-#define winPthreadAssert(expr) do { if (!(expr)) return -1; } while (0)
 #endif
 
 /***********
@@ -106,10 +110,8 @@ static inline int pthread_create (
   pthread_t *thread, const pthread_attr_t *attr,
   void * (*fun) (void *), void *arg
 ) {
-  if (attr && *attr) {
-    errno = EINVAL;
-    return -1;
-  }
+  if (attr && *attr)
+    return EINVAL;
   winPthreadAssertWindows(*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) fun, arg, 0, NULL));
   return 0;
 }
@@ -134,8 +136,7 @@ again:
   switch (WaitForSingleObject(thread, INFINITE)) {
     default:
     case WAIT_FAILED:
-      setSystemErrno();
-      return errno;
+      return unixErrno();
     case WAIT_ABANDONED:
     case WAIT_OBJECT_0:
       break;
@@ -172,10 +173,8 @@ static inline int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
 }
 
 static inline int pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
-  if (attr && *attr!=PTHREAD_MUTEX_RECURSIVE) {
-    errno = EINVAL;
-    return -1;
-  }
+  if (attr && *attr!=PTHREAD_MUTEX_RECURSIVE)
+    return EINVAL;
   winPthreadAssertWindows(*mutex = CreateMutex(NULL, FALSE, NULL));
   return 0;
 }
@@ -190,11 +189,11 @@ static inline int __pthread_mutex_alloc_concurrently (pthread_mutex_t *mutex) {
   HANDLE mutex_init_mutex;
   /* Get access to one global named mutex to serialize mutex initialization */
   winPthreadAssertWindows((mutex_init_mutex = CreateMutex(NULL, FALSE, "StarPU mutex init")));
-  winPthreadAssert(!pthread_mutex_lock(&mutex_init_mutex));
+  winPthreadAssertPthread(pthread_mutex_lock(&mutex_init_mutex));
   /* Now we are the one that can initialize it */
   if (!*mutex)
-    winPthreadAssert(!pthread_mutex_init(mutex,NULL));
-  winPthreadAssert(!pthread_mutex_unlock(&mutex_init_mutex));
+    winPthreadAssertPthread(pthread_mutex_init(mutex,NULL));
+  winPthreadAssertPthread(pthread_mutex_unlock(&mutex_init_mutex));
   winPthreadAssertWindows(CloseHandle(mutex_init_mutex));
   return 0;
 }
@@ -206,8 +205,7 @@ again:
   switch (WaitForSingleObject(*mutex, INFINITE)) {
     default:
     case WAIT_FAILED:
-      setSystemErrno();
-      return -1;
+      return unixErrno();;
     case WAIT_ABANDONED:
     case WAIT_OBJECT_0:
       return 0;
@@ -222,8 +220,7 @@ static inline int pthread_mutex_trylock (pthread_mutex_t *mutex) {
   switch (WaitForSingleObject(*mutex, 0)) {
     default:
     case WAIT_FAILED:
-      setSystemErrno();
-      return errno;
+      return unixErrno();
     case WAIT_ABANDONED:
     case WAIT_OBJECT_0:
       return 0;
@@ -270,10 +267,8 @@ struct timespec {
 typedef unsigned pthread_condattr_t;
 
 static inline int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr) {
-  if (attr) {
-    errno = EINVAL;
-    return -1;
-  }
+  if (attr)
+    return EINVAL;
   winPthreadAssertWindows(cond->sem = CreateSemaphore(NULL, 0, MAXLONG, NULL));
   cond->nbwait = 0;
   return 0;
@@ -281,52 +276,57 @@ static inline int pthread_cond_init (pthread_cond_t *cond, const pthread_condatt
 
 static inline int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *time) {
   if (!cond->sem)
-    winPthreadAssert(!pthread_cond_init(cond,NULL));
+    winPthreadAssertPthread(pthread_cond_init(cond,NULL));
   cond->nbwait++;
-  winPthreadAssert(!pthread_mutex_unlock(mutex));
+  winPthreadAssertPthread(pthread_mutex_unlock(mutex));
 again:
   switch (WaitForSingleObject(cond->sem, time->tv_sec*1000+time->tv_nsec/1000)) {
     default:
     case WAIT_FAILED:
-      setSystemErrno();
-      winPthreadAssert(!pthread_mutex_lock(mutex));
-      return -1;
+    {
+      int error = unixErrno();
+      winPthreadAssertPthread(pthread_mutex_lock(mutex));
+      return error;
+    }
     case WAIT_TIMEOUT:
       goto again;
     case WAIT_ABANDONED:
     case WAIT_OBJECT_0:
       break;
   }
-  winPthreadAssert(!pthread_mutex_lock(mutex));
+  winPthreadAssertPthread(pthread_mutex_lock(mutex));
   cond->nbwait--;
   return 0;
 }
 
 static inline int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) {
   if (!cond->sem)
-    winPthreadAssert(!pthread_cond_init(cond,NULL));
+    winPthreadAssertPthread(pthread_cond_init(cond,NULL));
   cond->nbwait++;
-  winPthreadAssert(!pthread_mutex_unlock(mutex));
+  winPthreadAssertPthread(pthread_mutex_unlock(mutex));
 again:
   switch (WaitForSingleObject(cond->sem, INFINITE)) {
     case WAIT_FAILED:
-      setSystemErrno();
-      winPthreadAssert(!pthread_mutex_lock(mutex));
-      return -1;
+    {
+      int error;
+      error = unixErrno();
+      winPthreadAssertPthread(pthread_mutex_lock(mutex));
+      return error;
+    }
     case WAIT_TIMEOUT:
       goto again;
     case WAIT_ABANDONED:
     case WAIT_OBJECT_0:
       break;
   }
-  winPthreadAssert(!pthread_mutex_lock(mutex));
+  winPthreadAssertPthread(pthread_mutex_lock(mutex));
   cond->nbwait--;
   return 0;
 }
 
 static inline int pthread_cond_signal (pthread_cond_t *cond) {
   if (!cond->sem)
-    winPthreadAssert(!pthread_cond_init(cond,NULL));
+    winPthreadAssertPthread(pthread_cond_init(cond,NULL));
   if (cond->nbwait)
     ReleaseSemaphore(cond->sem, 1, NULL);
   return 0;
@@ -334,7 +334,7 @@ static inline int pthread_cond_signal (pthread_cond_t *cond) {
 
 static inline int pthread_cond_broadcast (pthread_cond_t *cond) {
   if (!cond->sem)
-    winPthreadAssert(!pthread_cond_init(cond,NULL));
+    winPthreadAssertPthread(pthread_cond_init(cond,NULL));
   ReleaseSemaphore(cond->sem, cond->nbwait, NULL);
   return 0;
 }
@@ -359,12 +359,12 @@ typedef struct {
 } pthread_once_t;
 
 static inline int pthread_once (pthread_once_t *once, void (*oncefun)(void)) {
-  winPthreadAssert(pthread_mutex_lock(&once->mutex));
+  winPthreadAssertPthread(pthread_mutex_lock(&once->mutex));
   if (!once->done) {
     oncefun();
     once->done = 1;
   }
-  winPthreadAssert(!pthread_mutex_unlock(&once->mutex));
+  winPthreadAssertPthread(pthread_mutex_unlock(&once->mutex));
   return 0;
 }
 
@@ -381,10 +381,7 @@ static inline int pthread_key_delete (pthread_key_t key) {
 }
 
 static inline void *pthread_getspecific (pthread_key_t key) {
-  void * res = TlsGetValue(key);
-  if (!res)
-    errno = EIO;
-  return res;
+  return TlsGetValue(key);
 }
 
 static inline int pthread_setspecific (pthread_key_t key, const void *data) {