apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yla...@apache.org
Subject svn commit: r1794266 - in /apr/apr/trunk/locks: netware/thread_mutex.c unix/thread_mutex.c
Date Sun, 07 May 2017 21:35:30 GMT
Author: ylavic
Date: Sun May  7 21:35:30 2017
New Revision: 1794266

URL: http://svn.apache.org/viewvc?rev=1794266&view=rev
Log:
locks: thread: timedlock: better handling of spurious wakeups that may be
inherent to some native/OS condvar implementation.


Modified:
    apr/apr/trunk/locks/netware/thread_mutex.c
    apr/apr/trunk/locks/unix/thread_mutex.c

Modified: apr/apr/trunk/locks/netware/thread_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/locks/netware/thread_mutex.c?rev=1794266&r1=1794265&r2=1794266&view=diff
==============================================================================
--- apr/apr/trunk/locks/netware/thread_mutex.c (original)
+++ apr/apr/trunk/locks/netware/thread_mutex.c Sun May  7 21:35:30 2017
@@ -115,7 +115,8 @@ APR_DECLARE(apr_status_t) apr_thread_mut
                                                  apr_interval_time_t timeout)
 {
     if (mutex->cond) {
-        apr_status_t rv;
+        apr_status_t rv = APR_SUCCESS;
+
         NXLock(mutex->mutex);
         if (mutex->locked) {
             if (timeout <= 0) {
@@ -123,13 +124,15 @@ APR_DECLARE(apr_status_t) apr_thread_mut
             }
             else {
                 mutex->num_waiters++;
-                rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout);
+                do {
+                    rv = apr_thread_cond_timedwait(mutex->cond, mutex,
+                                                   timeout);
+                } while (rv == APR_SUCCESS && mutex->locked);
                 mutex->num_waiters--;
             }
         }
-        else {
+        if (rv == APR_SUCCESS) {
             mutex->locked = 1;
-            rv = APR_SUCCESS;
         }
         NXUnlock(mutex->mutex);
         return rv;
@@ -140,25 +143,24 @@ APR_DECLARE(apr_status_t) apr_thread_mut
 
 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
 {
+    apr_status_t rv = APR_SUCCESS;
+
     if (mutex->cond) {
-        apr_status_t rv;
         NXLock(mutex->mutex);
+
         if (!mutex->locked) {
             rv = APR_EINVAL;
         }
         else if (mutex->num_waiters) {
             rv = apr_thread_cond_signal(mutex->cond);
         }
-        else {
+        if (rv == APR_SUCCESS) {
             mutex->locked = 0;
-            rv = APR_SUCCESS;
         }
-        NXUnlock(mutex->mutex);
-        return rv;
     }
 
     NXUnlock(mutex->mutex);
-    return APR_SUCCESS;
+    return rv;
 }
 
 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)

Modified: apr/apr/trunk/locks/unix/thread_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/locks/unix/thread_mutex.c?rev=1794266&r1=1794265&r2=1794266&view=diff
==============================================================================
--- apr/apr/trunk/locks/unix/thread_mutex.c (original)
+++ apr/apr/trunk/locks/unix/thread_mutex.c Sun May  7 21:35:30 2017
@@ -241,23 +241,26 @@ APR_DECLARE(apr_status_t) apr_thread_mut
             }
             else {
                 mutex->num_waiters++;
-                rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout);
+                do {
+                    rv = apr_thread_cond_timedwait(mutex->cond, mutex,
+                                                   timeout);
+                    if (rv) {
 #ifdef HAVE_ZOS_PTHREADS
-                if (rv) {
-                    rv = errno;
-                }
+                        rv = errno;
 #endif
+                        break;
+                    }
+                } while (mutex->locked);
                 mutex->num_waiters--;
             }
-        }
-        else {
-            mutex->locked = 1;
-        }
-        if (rv) {
-            pthread_mutex_unlock(&mutex->mutex);
-            return rv;
+            if (rv) {
+                pthread_mutex_unlock(&mutex->mutex);
+                return rv;
+            }
         }
 
+        mutex->locked = 1;
+
         rv = pthread_mutex_unlock(&mutex->mutex);
         if (rv) {
 #ifdef HAVE_ZOS_PTHREADS
@@ -277,8 +280,6 @@ APR_DECLARE(apr_status_t) apr_thread_mut
     apr_status_t status;
 
     if (mutex->cond) {
-        apr_status_t stat2;
-
         status = pthread_mutex_lock(&mutex->mutex);
         if (status) {
 #ifdef HAVE_ZOS_PTHREADS
@@ -293,21 +294,12 @@ APR_DECLARE(apr_status_t) apr_thread_mut
         else if (mutex->num_waiters) {
             status = apr_thread_cond_signal(mutex->cond);
         }
-        else {
-            mutex->locked = 0;
-            status = APR_SUCCESS;
-        }
-
-        stat2 = pthread_mutex_unlock(&mutex->mutex);
-        if (stat2) {
-#ifdef HAVE_ZOS_PTHREADS
-            status = errno;
-#else
-            status = stat2;
-#endif
+        if (status) {
+            pthread_mutex_unlock(&mutex->mutex);
+            return status;
         }
 
-        return status;
+        mutex->locked = 0;
     }
 
     status = pthread_mutex_unlock(&mutex->mutex);



Mime
View raw message