+#endif
+
+#if defined(RTS_SUPPORTS_THREADS)
+ /* Check to see whether a worker thread can be given
+ the go-ahead to return the result of an external call..*/
+ if (rts_n_waiting_workers > 0) {
+ /* Decrement the counter here to avoid livelock where the
+ * thread that is yielding its capability will repeatedly
+ * signal returning_worker_cond.
+ */
+ rts_n_waiting_workers--;
+ signalCondition(&returning_worker_cond);
+ } else if ( !EMPTY_RUN_QUEUE() ) {
+ /* Signal that work is available */
+ signalCondition(&thread_ready_cond);
+ }
+#endif
+ return;
+}
+
+#if defined(RTS_SUPPORTS_THREADS)
+/*
+ * When a native thread has completed the execution of an external
+ * call, it needs to communicate the result back. This is done
+ * as follows:
+ *
+ * - in resumeThread(), the thread calls grabReturnCapability().
+ * - If no capabilities are readily available, grabReturnCapability()
+ * increments a counter rts_n_waiting_workers, and blocks
+ * waiting for the condition returning_worker_cond to become
+ * signalled.
+ * - upon entry to the Scheduler, a worker thread checks the
+ * value of rts_n_waiting_workers. If > 0, the worker thread
+ * will yield its capability to let a returning worker thread
+ * proceed with returning its result -- this is done via
+ * yieldToReturningWorker().
+ * - the worker thread that yielded its capability then tries
+ * to re-grab a capability and re-enter the Scheduler.
+ */
+
+/*
+ * Function: grabReturnCapability(Capability**)
+ *
+ * Purpose: when an OS thread returns from an external call,
+ * it calls grabReturnCapability() (via Schedule.resumeThread())
+ * to wait for permissions to enter the RTS & communicate the
+ * result of the external call back to the Haskell thread that
+ * made it.
+ *
+ * Pre-condition: pMutex is held.
+ * Post-condition: pMutex is still held and a capability has
+ * been assigned to the worker thread.
+ */
+void
+grabReturnCapability(Mutex* pMutex, Capability** pCap)
+{
+ IF_DEBUG(scheduler,
+ fprintf(stderr,"worker (%ld): returning, waiting for lock.\n", osThreadId()));
+ rts_n_waiting_workers++;
+ IF_DEBUG(scheduler,
+ fprintf(stderr,"worker (%ld): returning; workers waiting: %d\n",
+ osThreadId(), rts_n_waiting_workers));
+ while ( noCapabilities() ) {
+ waitCondition(&returning_worker_cond, pMutex);
+ }
+
+ grabCapability(pCap);
+ return;
+}
+
+
+/* -----------------------------------------------------------------------------
+ Yielding/waiting for capabilities
+ -------------------------------------------------------------------------- */
+
+/*
+ * Function: yieldToReturningWorker(Mutex*,Capability*)
+ *
+ * Purpose: when, upon entry to the Scheduler, an OS worker thread
+ * spots that one or more threads are blocked waiting for
+ * permission to return back their result, it gives up
+ * its Capability.
+ *
+ * Pre-condition: pMutex is assumed held and the thread possesses
+ * a Capability.
+ * Post-condition: pMutex isn't held and the Capability has
+ * been given back.
+ */
+void
+yieldToReturningWorker(Mutex* pMutex, Capability** pCap)
+{
+ if ( rts_n_waiting_workers > 0 && noCapabilities() ) {
+ IF_DEBUG(scheduler,
+ fprintf(stderr,"worker thread (%ld): giving up RTS token\n", osThreadId()));
+ releaseCapability(*pCap);
+ /* And wait for work */
+ waitForWorkCapability(pMutex, pCap, rtsFalse);
+ }
+ return;
+}
+
+
+/*
+ * Function: waitForWorkCapability(Mutex*, Capability**, rtsBool)
+ *
+ * Purpose: wait for a Capability to become available. In
+ * the process of doing so, updates the number
+ * of tasks currently blocked waiting for a capability/more
+ * work. That counter is used when deciding whether or
+ * not to create a new worker thread when an external
+ * call is made.
+ *
+ * Pre-condition: pMutex is held.
+ */
+void
+waitForWorkCapability(Mutex* pMutex, Capability** pCap, rtsBool runnable)
+{
+ while ( noCapabilities() || (runnable && EMPTY_RUN_QUEUE()) ) {
+ rts_n_waiting_tasks++;
+ waitCondition(&thread_ready_cond, pMutex);
+ rts_n_waiting_tasks--;
+ }
+ grabCapability(pCap);