+ startSchedulerTaskIfNecessary(); // if there is more work to be done,
+ // we'll need a new thread
+ IF_DEBUG(scheduler, sched_belch("worker: released capability"));
+ }
+#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,
+ sched_belch("worker: returning; workers waiting: %d",
+ rts_n_waiting_workers));
+ if ( noCapabilities() || passingCapability ) {
+ rts_n_waiting_workers++;
+ wakeBlockedWorkerThread();
+ context_switch = 1; // make sure it's our turn soon
+ waitCondition(&returning_worker_cond, pMutex);
+#if defined(SMP)
+ *pCap = returning_capabilities;
+ returning_capabilities = (*pCap)->link;
+#else
+ *pCap = &MainCapability;
+ ASSERT(rts_n_free_capabilities == 0);
+ handleSignalsInThisThread();
+#endif
+ } else {
+ grabCapability(pCap);
+ }
+ return;
+}
+
+
+/* -----------------------------------------------------------------------------
+ Yielding/waiting for capabilities
+ -------------------------------------------------------------------------- */
+
+/*
+ * Function: yieldToReturningWorker(Mutex*,Capability*,Condition*)
+ *
+ * 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.
+ * Immediately afterwards, it tries to reaquire the Capabilty
+ * using waitForWorkCapability.
+ *
+ *
+ * Pre-condition: pMutex is assumed held and the thread possesses
+ * a Capability.
+ * Post-condition: pMutex is held and the thread possesses
+ * a Capability.
+ */
+void
+yieldToReturningWorker(Mutex* pMutex, Capability** pCap, Condition* pThreadCond)
+{
+ if ( rts_n_waiting_workers > 0 ) {
+ IF_DEBUG(scheduler, sched_belch("worker: giving up capability"));
+ releaseCapability(*pCap);
+ /* And wait for work */
+ waitForWorkCapability(pMutex, pCap, pThreadCond);
+ IF_DEBUG(scheduler,
+ sched_belch("worker: got back capability (after yieldToReturningWorker)"));