Browse Source

Replacement for pthread_cond in Windows

bel 11 years ago
parent
commit
9fde8e3320
2 changed files with 29 additions and 19 deletions
  1. 22 14
      src/civetweb.c
  2. 7 5
      test/ajax/test.html

+ 22 - 14
src/civetweb.c

@@ -1100,18 +1100,28 @@ static int pthread_cond_signal(pthread_cond_t *cv)
     HANDLE wkup = NULL;
 
     EnterCriticalSection(&cv->threadIdSec);
-    for (j=0; j<cv->waitingthreadcount; j++) {
-      wkup = cv->waitingthreadids[j];
-      susCnt = ResumeThread(wkup);
-      assert(susCnt<2);
-      if (susCnt==1) {
-          CloseHandle(wkup);
-          for (i=1;i<cv->waitingthreadcount;i++) {
-              cv->waitingthreadids[i-1] = cv->waitingthreadids[i];
-          }
-          cv->waitingthreadcount--;
-          break;
-      }
+    if (cv->waitingthreadcount) {
+        for (;;) {
+            for (j=0; j<cv->waitingthreadcount; j++) {
+                wkup = cv->waitingthreadids[j];
+                susCnt = ResumeThread(wkup);
+                assert(susCnt<2);
+                if (susCnt==1) {
+                    CloseHandle(wkup);
+                    for (i=1;i<cv->waitingthreadcount;i++) {
+                        cv->waitingthreadids[i-1] = cv->waitingthreadids[i];
+                    }
+                    cv->waitingthreadcount--;
+                    break;
+                }
+            }
+            if (wkup) {
+                break;
+            } else {
+                /* All theads between enqueue and suspend */
+                Sleep(1);
+            }
+        }
     }
     LeaveCriticalSection(&cv->threadIdSec);
 
@@ -1120,8 +1130,6 @@ static int pthread_cond_signal(pthread_cond_t *cv)
 
 static int pthread_cond_broadcast(pthread_cond_t *cv)
 {
-    /* The only usecase here is after a call to mg_exit. */
-    /* This will work here, for WinCE (realtime) one could switch the thread priority of the master thread to below normal when exit is performed. */
     EnterCriticalSection(&cv->threadIdSec);
     while (cv->waitingthreadcount) {
         pthread_cond_signal(cv);

+ 7 - 5
test/ajax/test.html

@@ -16,7 +16,7 @@
     var errorCount = 0;
     var pushCount = 0;
     var allErrorCount = 0;
-
+    var autoTest = false;
 
     function runTest(method, isAsync) {
 
@@ -38,8 +38,10 @@
     }
     
     function runAutoTest() {
-       runTest("POST", true)
-       setTimeout("runAutoTest()", 250)
+      if (autoTest) {
+        runTest("POST", true)
+        setTimeout("runAutoTest()", 250)
+      }
     }
 
 
@@ -114,13 +116,13 @@
         </tr>
         <tr>
           <td>
-            <input id="testButton5" type="button" onclick="javascript:runAutoTest()" value="automatic test"></input>
+            <input id="testButton5" type="button" onclick="autoTest=!autoTest; javascript:runAutoTest()" value="automatic test"></input>
           </td>        
         </tr>
         
         <tr>
           <td>
-            <input id="testButtonReset" type="button" onclick="javascript:location.reload(true)" value="reset"></input>
+            <input id="testButtonReset" type="button" onclick="autoTest=false; javascript:location.reload(true)" value="reset"></input>
           </td>
           <td>
           </td>