- * Pre-condition: sched_mutex isn't held.
- * Post-condition: sched_mutex is held and a capability has
- * been assigned to the worker thread.
- */
+ * ------------------------------------------------------------------------- */
+
+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++;
+ context_switch = 1; // make sure it's our turn soon
+ waitCondition(&returning_worker_cond, pMutex);
+#if defined(SMP)
+ *pCap = free_capabilities;
+ free_capabilities = (*pCap)->link;
+ ASSERT(pCap != NULL);
+#else
+ *pCap = &MainCapability;
+ ASSERT(rts_n_free_capabilities == 0);
+#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 )
+ * ------------------------------------------------------------------------- */
+