[project @ 2005-10-21 14:02:17 by simonmar]
[ghc-hetmet.git] / ghc / rts / win32 / AwaitEvent.c
index 92a84bc..6986bc9 100644 (file)
 #include "Schedule.h"
 #include <windows.h>
 #include "win32/AsyncIO.h"
-#if defined(RTS_SUPPORTS_THREADS)
+#if defined(THREADED_RTS)
 #include "Capability.h"
 #endif
 
+// Used to avoid calling abandonRequestWait() if we don't need to.
+// Protected by sched_mutex.
+static nat workerWaitingForRequests = 0;
+
 void
 awaitEvent(rtsBool wait)
 {
   int ret;
 
-#ifdef RTS_SUPPORTS_THREADS
+#ifdef THREADED_RTS
   // Small optimisation: we don't want the waiting thread to wake
   // up straight away just because a previous returning worker has
   // called abandonRequestWait().  If the event is no longer needed,
@@ -38,9 +42,11 @@ awaitEvent(rtsBool wait)
   do {
     /* Try to de-queue completed IO requests
      */
+    workerWaitingForRequests = 1;
     RELEASE_LOCK(&sched_mutex);
     ret = awaitRequests(wait);
     ACQUIRE_LOCK(&sched_mutex);
+    workerWaitingForRequests = 0;
     if (!ret) { 
       return; /* still hold the lock */
     }
@@ -49,22 +55,23 @@ awaitEvent(rtsBool wait)
     //
     //  - we were interrupted
     //  - new threads have arrived
-    //  - another worker wants to take over (RTS_SUPPORTS_THREADS)
+    //  - another worker wants to take over (THREADED_RTS)
 
   } while (wait
           && !interrupted
           && run_queue_hd == END_TSO_QUEUE
-#ifdef RTS_SUPPORTS_THREADS
+#ifdef THREADED_RTS
           && !needToYieldToReturningWorker()
 #endif
       );
 }
 
-#ifdef RTS_SUPPORTS_THREADS
+#ifdef THREADED_RTS
 void
 wakeBlockedWorkerThread()
 {
-  abandonRequestWait();
+    if (workerWaitingForRequests) {
+       abandonRequestWait();
+    }
 }
 #endif
-