+yieldToReturningWorker(Mutex* pMutex, Capability** pCap)
+{
+ if ( rts_n_waiting_workers > 0 ) {
+ IF_DEBUG(scheduler,
+ fprintf(stderr,"worker thread (%p): giving up RTS token\n", osThreadId()));
+ releaseCapability(*pCap);
+ /* And wait for work */
+ waitForWorkCapability(pMutex, pCap, rtsFalse);
+ IF_DEBUG(scheduler,
+ fprintf(stderr,"worker thread (%p): got back RTS token (after yieldToReturningWorker)\n",
+ osThreadId()));
+ }
+ 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.
+ * Post-condition: pMutex is held and *pCap is held by the current thread
+ */
+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);
+ return;
+}
+
+#endif /* RTS_SUPPORTS_THREADS */
+
+#if defined(SMP)
+/*
+ * Function: initCapabilities_(nat)
+ *
+ * Purpose: upon startup, allocate and fill in table
+ * holding 'n' Capabilities. Only for SMP, since
+ * it is the only build that supports multiple
+ * capabilities within the RTS.
+ */
+static void
+initCapabilities_(nat n)