+
+ rts_n_waiting_workers--;
+ signalCondition(&returning_worker_cond);
+ IF_DEBUG(scheduler, sched_belch("worker: released capability to returning worker"));
+ } else if (passingCapability) {
+ if (passTarget == NULL) {
+ signalCondition(&thread_ready_cond);
+ startSchedulerTaskIfNecessary();
+ } else {
+ signalCondition(passTarget);
+ }
+ rts_n_free_capabilities = 1;
+ IF_DEBUG(scheduler, sched_belch("worker: released capability, passing it"));
+
+ } else {
+#if defined(SMP)
+ cap->link = free_capabilities;
+ free_capabilities = cap;
+ rts_n_free_capabilities++;
+#else
+ rts_n_free_capabilities = 1;
+#endif
+ // Signal that a capability is available
+ signalCondition(&thread_ready_cond);
+ startSchedulerTaskIfNecessary();
+ 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 waitForReturnCapability().
+ * - If no capabilities are readily available, waitForReturnCapability()
+ * 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.
+ */
+
+/* ----------------------------------------------------------------------------
+ * waitForReturnCapability( Mutext *pMutex, 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.
+ *
+ * ------------------------------------------------------------------------- */
+
+void
+waitForReturnCapability( Mutex* pMutex, Capability** pCap )
+{
+ // Pre-condition: pMutex is held.
+
+ 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);
+ }
+
+ // Post-condition: pMutex is held, pCap points to a capability
+ // which is now held by the current thread.
+ return;
+}
+
+
+/* ----------------------------------------------------------------------------
+ * yieldCapability( Mutex* pMutex, Capability** pCap )
+ * ------------------------------------------------------------------------- */
+
+void
+yieldCapability( Capability** pCap )
+{
+ // Pre-condition: pMutex is assumed held, the current thread
+ // holds the capability pointed to by pCap.
+
+ if ( rts_n_waiting_workers > 0 || passingCapability ) {
+ IF_DEBUG(scheduler, sched_belch("worker: giving up capability"));
+ releaseCapability(*pCap);
+ *pCap = NULL;
+ }
+
+ // Post-condition: pMutex is assumed held, and either:
+ //
+ // 1. *pCap is NULL, in which case the current thread does not
+ // hold a capability now, or
+ // 2. *pCap is not NULL, in which case the current thread still
+ // holds the capability.
+ //
+ return;
+}
+
+
+/* ----------------------------------------------------------------------------
+ * waitForCapability( Mutex*, Capability**, Condition* )
+ *
+ * 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.
+ * If pThreadCond is not NULL, a capability can be specifically
+ * passed to this thread using passCapability.
+ * ------------------------------------------------------------------------- */
+
+void
+waitForCapability( Mutex* pMutex, Capability** pCap, Condition* pThreadCond )
+{
+ // Pre-condition: pMutex is held.
+
+ while ( noCapabilities() ||
+ (passingCapability && passTarget != pThreadCond)) {
+ IF_DEBUG(scheduler,
+ sched_belch("worker: wait for capability (cond: %p)",
+ pThreadCond));
+
+ if (pThreadCond != NULL) {
+ waitCondition(pThreadCond, pMutex);
+ IF_DEBUG(scheduler, sched_belch("worker: get passed capability"));
+ } else {
+ rts_n_waiting_tasks++;
+ waitCondition(&thread_ready_cond, pMutex);
+ rts_n_waiting_tasks--;
+ IF_DEBUG(scheduler, sched_belch("worker: get normal capability"));
+ }
+ }
+ passingCapability = rtsFalse;
+ grabCapability(pCap);
+
+ // Post-condition: pMutex is held and *pCap is held by the current thread
+ return;
+}
+
+/* ----------------------------------------------------------------------------
+ passCapability, passCapabilityToWorker
+ ------------------------------------------------------------------------- */
+
+void
+passCapability( Condition *pTargetThreadCond )
+{
+ // Pre-condition: pMutex is held and cap is held by the current thread
+
+ passTarget = pTargetThreadCond;
+ passingCapability = rtsTrue;
+ IF_DEBUG(scheduler, sched_belch("worker: passCapability"));
+
+ // Post-condition: pMutex is held; cap is still held, but will be
+ // passed to the target thread when next released.