apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JF B <jfbri...@yahoo.com>
Subject [PATCH] WIN32 implementations of apr_thread_cond_timedwait + apr_thread_cond_wait
Date Wed, 15 May 2002 21:40:33 GMT
--- apr/locks/win32/thread_cond.c Wed Mar 13 15:39:22 2002
+++ apr_new/locks/win32/thread_cond.c Wed May  8 18:25:52 2002
@@ -85,28 +85,33 @@
 {
     DWORD rv;
 
-    while (1) {
-        WaitForSingleObject(cond->mutex, INFINITE);
-        cond->num_waiting++;
-        ReleaseMutex(cond->mutex);
+    WaitForSingleObject(cond->mutex, INFINITE);
+    cond->num_waiting++;
+    ReleaseMutex(cond->mutex);
 
-        apr_thread_mutex_unlock(mutex);
+    apr_thread_mutex_unlock(mutex);
+    while (1) {
         rv = WaitForSingleObject(cond->event, INFINITE);
-        cond->num_waiting--;
         if (rv == WAIT_FAILED) {
+            apr_thread_mutex_lock(mutex);
             return apr_get_os_error();
         }
+        WaitForSingleObject(cond->mutex, INFINITE);
+        cond->num_waiting--;
         if (cond->signal_all) {
             if (cond->num_waiting == 0) {
                 ResetEvent(cond->event);
             }
+            ReleaseMutex(cond->mutex);
             break;
         }
         if (cond->signalled) {
             cond->signalled = 0;
             ResetEvent(cond->event);
+            ReleaseMutex(cond->mutex);
             break;
         }
+        cond->num_waiting++;
         ReleaseMutex(cond->mutex);
     }
     apr_thread_mutex_lock(mutex);
@@ -115,8 +120,52 @@
 
 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
                                                     apr_thread_mutex_t *mutex,
-                                                    apr_interval_time_t timeout){
-    return APR_ENOTIMPL;
+                                                    apr_interval_time_t timeout)
+{
+    DWORD rv;
+    DWORD time_left = (DWORD)(timeout / 1000);
+    apr_time_t time_begin;
+
+    WaitForSingleObject(cond->mutex, INFINITE);
+    cond->num_waiting++;
+    ReleaseMutex(cond->mutex);
+
+    apr_thread_mutex_unlock(mutex);
+    time_begin = apr_time_now();
+    while (1) {
+        rv = WaitForSingleObject(cond->event, time_left);
+        if (rv == WAIT_FAILED) {
+            apr_thread_mutex_lock(mutex);
+            return apr_get_os_error();
+        }
+        if (rv == WAIT_TIMEOUT) {
+            apr_thread_mutex_lock(mutex);
+            return APR_TIMEUP;
+        }
+        time_left -= (DWORD)((apr_time_now() - time_begin) / 1000);
+        if (time_left < 0) {
+            time_left = 0;
+        }
+        WaitForSingleObject(cond->mutex, INFINITE);
+        cond->num_waiting--;
+        if (cond->signal_all) {
+            if (cond->num_waiting == 0) {
+                ResetEvent(cond->event);
+            }
+            ReleaseMutex(cond->mutex);
+            break;
+        }
+        if (cond->signalled) {
+            cond->signalled = 0;
+            ResetEvent(cond->event);
+            ReleaseMutex(cond->mutex);
+            break;
+        }
+        cond->num_waiting++;
+        ReleaseMutex(cond->mutex);
+    }
+    apr_thread_mutex_lock(mutex);
+    return APR_SUCCESS;
 }
 
 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)



---------------------------------
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
Mime
View raw message