fix haddock submodule pointer
[ghc-hetmet.git] / rts / Capability.c
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2003-2006
4  *
5  * Capabilities
6  *
7  * A Capability represent the token required to execute STG code,
8  * and all the state an OS thread/task needs to run Haskell code:
9  * its STG registers, a pointer to its TSO, a nursery etc. During
10  * STG execution, a pointer to the capabilitity is kept in a
11  * register (BaseReg; actually it is a pointer to cap->r).
12  *
13  * Only in an THREADED_RTS build will there be multiple capabilities,
14  * for non-threaded builds there is only one global capability, namely
15  * MainCapability.
16  *
17  * --------------------------------------------------------------------------*/
18
19 #include "PosixSource.h"
20 #include "Rts.h"
21
22 #include "Capability.h"
23 #include "Schedule.h"
24 #include "Sparks.h"
25 #include "Trace.h"
26 #include "sm/GC.h" // for gcWorkerThread()
27 #include "STM.h"
28 #include "RtsUtils.h"
29
30 // one global capability, this is the Capability for non-threaded
31 // builds, and for +RTS -N1
32 Capability MainCapability;
33
34 nat n_capabilities = 0;
35 Capability *capabilities = NULL;
36
37 // Holds the Capability which last became free.  This is used so that
38 // an in-call has a chance of quickly finding a free Capability.
39 // Maintaining a global free list of Capabilities would require global
40 // locking, so we don't do that.
41 Capability *last_free_capability = NULL;
42
43 /* GC indicator, in scope for the scheduler, init'ed to false */
44 volatile StgWord waiting_for_gc = 0;
45
46 /* Let foreign code get the current Capability -- assuming there is one!
47  * This is useful for unsafe foreign calls because they are called with
48  * the current Capability held, but they are not passed it. For example,
49  * see see the integer-gmp package which calls allocateLocal() in its
50  * stgAllocForGMP() function (which gets called by gmp functions).
51  * */
52 Capability * rts_unsafeGetMyCapability (void)
53 {
54 #if defined(THREADED_RTS)
55   return myTask()->cap;
56 #else
57   return &MainCapability;
58 #endif
59 }
60
61 #if defined(THREADED_RTS)
62 STATIC_INLINE rtsBool
63 globalWorkToDo (void)
64 {
65     return sched_state >= SCHED_INTERRUPTING
66         || recent_activity == ACTIVITY_INACTIVE; // need to check for deadlock
67 }
68 #endif
69
70 #if defined(THREADED_RTS)
71 StgClosure *
72 findSpark (Capability *cap)
73 {
74   Capability *robbed;
75   StgClosurePtr spark;
76   rtsBool retry;
77   nat i = 0;
78
79   if (!emptyRunQueue(cap) || cap->returning_tasks_hd != NULL) {
80       // If there are other threads, don't try to run any new
81       // sparks: sparks might be speculative, we don't want to take
82       // resources away from the main computation.
83       return 0;
84   }
85
86   do {
87       retry = rtsFalse;
88
89       // first try to get a spark from our own pool.
90       // We should be using reclaimSpark(), because it works without
91       // needing any atomic instructions:
92       //   spark = reclaimSpark(cap->sparks);
93       // However, measurements show that this makes at least one benchmark
94       // slower (prsa) and doesn't affect the others.
95       spark = tryStealSpark(cap);
96       if (spark != NULL) {
97           cap->sparks_converted++;
98
99           // Post event for running a spark from capability's own pool.
100           traceEventRunSpark(cap, cap->r.rCurrentTSO);
101
102           return spark;
103       }
104       if (!emptySparkPoolCap(cap)) {
105           retry = rtsTrue;
106       }
107
108       if (n_capabilities == 1) { return NULL; } // makes no sense...
109
110       debugTrace(DEBUG_sched,
111                  "cap %d: Trying to steal work from other capabilities", 
112                  cap->no);
113
114       /* visit cap.s 0..n-1 in sequence until a theft succeeds. We could
115       start at a random place instead of 0 as well.  */
116       for ( i=0 ; i < n_capabilities ; i++ ) {
117           robbed = &capabilities[i];
118           if (cap == robbed)  // ourselves...
119               continue;
120
121           if (emptySparkPoolCap(robbed)) // nothing to steal here
122               continue;
123
124           spark = tryStealSpark(robbed);
125           if (spark == NULL && !emptySparkPoolCap(robbed)) {
126               // we conflicted with another thread while trying to steal;
127               // try again later.
128               retry = rtsTrue;
129           }
130
131           if (spark != NULL) {
132               cap->sparks_converted++;
133
134               traceEventStealSpark(cap, cap->r.rCurrentTSO, robbed->no);
135               
136               return spark;
137           }
138           // otherwise: no success, try next one
139       }
140   } while (retry);
141
142   debugTrace(DEBUG_sched, "No sparks stolen");
143   return NULL;
144 }
145
146 // Returns True if any spark pool is non-empty at this moment in time
147 // The result is only valid for an instant, of course, so in a sense
148 // is immediately invalid, and should not be relied upon for
149 // correctness.
150 rtsBool
151 anySparks (void)
152 {
153     nat i;
154
155     for (i=0; i < n_capabilities; i++) {
156         if (!emptySparkPoolCap(&capabilities[i])) {
157             return rtsTrue;
158         }
159     }
160     return rtsFalse;
161 }
162 #endif
163
164 /* -----------------------------------------------------------------------------
165  * Manage the returning_tasks lists.
166  *
167  * These functions require cap->lock
168  * -------------------------------------------------------------------------- */
169
170 #if defined(THREADED_RTS)
171 STATIC_INLINE void
172 newReturningTask (Capability *cap, Task *task)
173 {
174     ASSERT_LOCK_HELD(&cap->lock);
175     ASSERT(task->next == NULL);
176     if (cap->returning_tasks_hd) {
177         ASSERT(cap->returning_tasks_tl->next == NULL);
178         cap->returning_tasks_tl->next = task;
179     } else {
180         cap->returning_tasks_hd = task;
181     }
182     cap->returning_tasks_tl = task;
183 }
184
185 STATIC_INLINE Task *
186 popReturningTask (Capability *cap)
187 {
188     ASSERT_LOCK_HELD(&cap->lock);
189     Task *task;
190     task = cap->returning_tasks_hd;
191     ASSERT(task);
192     cap->returning_tasks_hd = task->next;
193     if (!cap->returning_tasks_hd) {
194         cap->returning_tasks_tl = NULL;
195     }
196     task->next = NULL;
197     return task;
198 }
199 #endif
200
201 /* ----------------------------------------------------------------------------
202  * Initialisation
203  *
204  * The Capability is initially marked not free.
205  * ------------------------------------------------------------------------- */
206
207 static void
208 initCapability( Capability *cap, nat i )
209 {
210     nat g;
211
212     cap->no = i;
213     cap->in_haskell        = rtsFalse;
214
215     cap->run_queue_hd      = END_TSO_QUEUE;
216     cap->run_queue_tl      = END_TSO_QUEUE;
217
218 #if defined(THREADED_RTS)
219     initMutex(&cap->lock);
220     cap->running_task      = NULL; // indicates cap is free
221     cap->spare_workers     = NULL;
222     cap->n_spare_workers   = 0;
223     cap->suspended_ccalls  = NULL;
224     cap->returning_tasks_hd = NULL;
225     cap->returning_tasks_tl = NULL;
226     cap->inbox              = (Message*)END_TSO_QUEUE;
227     cap->sparks_created     = 0;
228     cap->sparks_dud         = 0;
229     cap->sparks_converted   = 0;
230     cap->sparks_gcd         = 0;
231     cap->sparks_fizzled     = 0;
232 #endif
233
234     cap->f.stgEagerBlackholeInfo = (W_)&__stg_EAGER_BLACKHOLE_info;
235     cap->f.stgGCEnter1     = (StgFunPtr)__stg_gc_enter_1;
236     cap->f.stgGCFun        = (StgFunPtr)__stg_gc_fun;
237
238     cap->mut_lists  = stgMallocBytes(sizeof(bdescr *) *
239                                      RtsFlags.GcFlags.generations,
240                                      "initCapability");
241     cap->saved_mut_lists = stgMallocBytes(sizeof(bdescr *) *
242                                           RtsFlags.GcFlags.generations,
243                                           "initCapability");
244
245     for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
246         cap->mut_lists[g] = NULL;
247     }
248
249     cap->free_tvar_watch_queues = END_STM_WATCH_QUEUE;
250     cap->free_invariant_check_queues = END_INVARIANT_CHECK_QUEUE;
251     cap->free_trec_chunks = END_STM_CHUNK_LIST;
252     cap->free_trec_headers = NO_TREC;
253     cap->transaction_tokens = 0;
254     cap->context_switch = 0;
255     cap->pinned_object_block = NULL;
256
257     traceCapsetAssignCap(CAPSET_OSPROCESS_DEFAULT, i);
258 }
259
260 /* ---------------------------------------------------------------------------
261  * Function:  initCapabilities()
262  *
263  * Purpose:   set up the Capability handling. For the THREADED_RTS build,
264  *            we keep a table of them, the size of which is
265  *            controlled by the user via the RTS flag -N.
266  *
267  * ------------------------------------------------------------------------- */
268 void
269 initCapabilities( void )
270 {
271     /* Declare a single capability set representing the process. 
272        Each capability will get added to this capset. */ 
273     traceCapsetCreate(CAPSET_OSPROCESS_DEFAULT, CapsetTypeOsProcess);
274
275 #if defined(THREADED_RTS)
276     nat i;
277
278 #ifndef REG_Base
279     // We can't support multiple CPUs if BaseReg is not a register
280     if (RtsFlags.ParFlags.nNodes > 1) {
281         errorBelch("warning: multiple CPUs not supported in this build, reverting to 1");
282         RtsFlags.ParFlags.nNodes = 1;
283     }
284 #endif
285
286     n_capabilities = RtsFlags.ParFlags.nNodes;
287
288     if (n_capabilities == 1) {
289         capabilities = &MainCapability;
290         // THREADED_RTS must work on builds that don't have a mutable
291         // BaseReg (eg. unregisterised), so in this case
292         // capabilities[0] must coincide with &MainCapability.
293     } else {
294         capabilities = stgMallocBytes(n_capabilities * sizeof(Capability),
295                                       "initCapabilities");
296     }
297
298     for (i = 0; i < n_capabilities; i++) {
299         initCapability(&capabilities[i], i);
300     }
301
302     debugTrace(DEBUG_sched, "allocated %d capabilities", n_capabilities);
303
304 #else /* !THREADED_RTS */
305
306     n_capabilities = 1;
307     capabilities = &MainCapability;
308     initCapability(&MainCapability, 0);
309
310 #endif
311
312     // There are no free capabilities to begin with.  We will start
313     // a worker Task to each Capability, which will quickly put the
314     // Capability on the free list when it finds nothing to do.
315     last_free_capability = &capabilities[0];
316 }
317
318 /* ----------------------------------------------------------------------------
319  * setContextSwitches: cause all capabilities to context switch as
320  * soon as possible.
321  * ------------------------------------------------------------------------- */
322
323 void setContextSwitches(void)
324 {
325     nat i;
326     for (i=0; i < n_capabilities; i++) {
327         contextSwitchCapability(&capabilities[i]);
328     }
329 }
330
331 /* ----------------------------------------------------------------------------
332  * Give a Capability to a Task.  The task must currently be sleeping
333  * on its condition variable.
334  *
335  * Requires cap->lock (modifies cap->running_task).
336  *
337  * When migrating a Task, the migrater must take task->lock before
338  * modifying task->cap, to synchronise with the waking up Task.
339  * Additionally, the migrater should own the Capability (when
340  * migrating the run queue), or cap->lock (when migrating
341  * returning_workers).
342  *
343  * ------------------------------------------------------------------------- */
344
345 #if defined(THREADED_RTS)
346 STATIC_INLINE void
347 giveCapabilityToTask (Capability *cap USED_IF_DEBUG, Task *task)
348 {
349     ASSERT_LOCK_HELD(&cap->lock);
350     ASSERT(task->cap == cap);
351     debugTrace(DEBUG_sched, "passing capability %d to %s %p",
352                cap->no, task->incall->tso ? "bound task" : "worker",
353                (void *)task->id);
354     ACQUIRE_LOCK(&task->lock);
355     task->wakeup = rtsTrue;
356     // the wakeup flag is needed because signalCondition() doesn't
357     // flag the condition if the thread is already runniing, but we want
358     // it to be sticky.
359     signalCondition(&task->cond);
360     RELEASE_LOCK(&task->lock);
361 }
362 #endif
363
364 /* ----------------------------------------------------------------------------
365  * Function:  releaseCapability(Capability*)
366  *
367  * Purpose:   Letting go of a capability. Causes a
368  *            'returning worker' thread or a 'waiting worker'
369  *            to wake up, in that order.
370  * ------------------------------------------------------------------------- */
371
372 #if defined(THREADED_RTS)
373 void
374 releaseCapability_ (Capability* cap, 
375                     rtsBool always_wakeup)
376 {
377     Task *task;
378
379     task = cap->running_task;
380
381     ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task);
382
383     cap->running_task = NULL;
384
385     // Check to see whether a worker thread can be given
386     // the go-ahead to return the result of an external call..
387     if (cap->returning_tasks_hd != NULL) {
388         giveCapabilityToTask(cap,cap->returning_tasks_hd);
389         // The Task pops itself from the queue (see waitForReturnCapability())
390         return;
391     }
392
393     if (waiting_for_gc == PENDING_GC_SEQ) {
394       last_free_capability = cap; // needed?
395       debugTrace(DEBUG_sched, "GC pending, set capability %d free", cap->no);
396       return;
397     } 
398
399
400     // If the next thread on the run queue is a bound thread,
401     // give this Capability to the appropriate Task.
402     if (!emptyRunQueue(cap) && cap->run_queue_hd->bound) {
403         // Make sure we're not about to try to wake ourselves up
404         // ASSERT(task != cap->run_queue_hd->bound);
405         // assertion is false: in schedule() we force a yield after
406         // ThreadBlocked, but the thread may be back on the run queue
407         // by now.
408         task = cap->run_queue_hd->bound->task;
409         giveCapabilityToTask(cap,task);
410         return;
411     }
412
413     if (!cap->spare_workers) {
414         // Create a worker thread if we don't have one.  If the system
415         // is interrupted, we only create a worker task if there
416         // are threads that need to be completed.  If the system is
417         // shutting down, we never create a new worker.
418         if (sched_state < SCHED_SHUTTING_DOWN || !emptyRunQueue(cap)) {
419             debugTrace(DEBUG_sched,
420                        "starting new worker on capability %d", cap->no);
421             startWorkerTask(cap);
422             return;
423         }
424     }
425
426     // If we have an unbound thread on the run queue, or if there's
427     // anything else to do, give the Capability to a worker thread.
428     if (always_wakeup || 
429         !emptyRunQueue(cap) || !emptyInbox(cap) ||
430         !emptySparkPoolCap(cap) || globalWorkToDo()) {
431         if (cap->spare_workers) {
432             giveCapabilityToTask(cap,cap->spare_workers);
433             // The worker Task pops itself from the queue;
434             return;
435         }
436     }
437
438     last_free_capability = cap;
439     debugTrace(DEBUG_sched, "freeing capability %d", cap->no);
440 }
441
442 void
443 releaseCapability (Capability* cap USED_IF_THREADS)
444 {
445     ACQUIRE_LOCK(&cap->lock);
446     releaseCapability_(cap, rtsFalse);
447     RELEASE_LOCK(&cap->lock);
448 }
449
450 void
451 releaseAndWakeupCapability (Capability* cap USED_IF_THREADS)
452 {
453     ACQUIRE_LOCK(&cap->lock);
454     releaseCapability_(cap, rtsTrue);
455     RELEASE_LOCK(&cap->lock);
456 }
457
458 static void
459 releaseCapabilityAndQueueWorker (Capability* cap USED_IF_THREADS)
460 {
461     Task *task;
462
463     ACQUIRE_LOCK(&cap->lock);
464
465     task = cap->running_task;
466
467     // If the Task is stopped, we shouldn't be yielding, we should
468     // be just exiting.
469     ASSERT(!task->stopped);
470
471     // If the current task is a worker, save it on the spare_workers
472     // list of this Capability.  A worker can mark itself as stopped,
473     // in which case it is not replaced on the spare_worker queue.
474     // This happens when the system is shutting down (see
475     // Schedule.c:workerStart()).
476     if (!isBoundTask(task))
477     {
478         if (cap->n_spare_workers < MAX_SPARE_WORKERS)
479         {
480             task->next = cap->spare_workers;
481             cap->spare_workers = task;
482             cap->n_spare_workers++;
483         }
484         else
485         {
486             debugTrace(DEBUG_sched, "%d spare workers already, exiting",
487                        cap->n_spare_workers);
488             releaseCapability_(cap,rtsFalse);
489             // hold the lock until after workerTaskStop; c.f. scheduleWorker()
490             workerTaskStop(task);
491             RELEASE_LOCK(&cap->lock);
492             shutdownThread();
493         }
494     }
495     // Bound tasks just float around attached to their TSOs.
496
497     releaseCapability_(cap,rtsFalse);
498
499     RELEASE_LOCK(&cap->lock);
500 }
501 #endif
502
503 /* ----------------------------------------------------------------------------
504  * waitForReturnCapability( Task *task )
505  *
506  * Purpose:  when an OS thread returns from an external call,
507  * it calls waitForReturnCapability() (via Schedule.resumeThread())
508  * to wait for permission to enter the RTS & communicate the
509  * result of the external call back to the Haskell thread that
510  * made it.
511  *
512  * ------------------------------------------------------------------------- */
513 void
514 waitForReturnCapability (Capability **pCap, Task *task)
515 {
516 #if !defined(THREADED_RTS)
517
518     MainCapability.running_task = task;
519     task->cap = &MainCapability;
520     *pCap = &MainCapability;
521
522 #else
523     Capability *cap = *pCap;
524
525     if (cap == NULL) {
526         // Try last_free_capability first
527         cap = last_free_capability;
528         if (cap->running_task) {
529             nat i;
530             // otherwise, search for a free capability
531             cap = NULL;
532             for (i = 0; i < n_capabilities; i++) {
533                 if (!capabilities[i].running_task) {
534                     cap = &capabilities[i];
535                     break;
536                 }
537             }
538             if (cap == NULL) {
539                 // Can't find a free one, use last_free_capability.
540                 cap = last_free_capability;
541             }
542         }
543
544         // record the Capability as the one this Task is now assocated with.
545         task->cap = cap;
546
547     } else {
548         ASSERT(task->cap == cap);
549     }
550
551     ACQUIRE_LOCK(&cap->lock);
552
553     debugTrace(DEBUG_sched, "returning; I want capability %d", cap->no);
554
555     if (!cap->running_task) {
556         // It's free; just grab it
557         cap->running_task = task;
558         RELEASE_LOCK(&cap->lock);
559     } else {
560         newReturningTask(cap,task);
561         RELEASE_LOCK(&cap->lock);
562
563         for (;;) {
564             ACQUIRE_LOCK(&task->lock);
565             // task->lock held, cap->lock not held
566             if (!task->wakeup) waitCondition(&task->cond, &task->lock);
567             cap = task->cap;
568             task->wakeup = rtsFalse;
569             RELEASE_LOCK(&task->lock);
570
571             // now check whether we should wake up...
572             ACQUIRE_LOCK(&cap->lock);
573             if (cap->running_task == NULL) {
574                 if (cap->returning_tasks_hd != task) {
575                     giveCapabilityToTask(cap,cap->returning_tasks_hd);
576                     RELEASE_LOCK(&cap->lock);
577                     continue;
578                 }
579                 cap->running_task = task;
580                 popReturningTask(cap);
581                 RELEASE_LOCK(&cap->lock);
582                 break;
583             }
584             RELEASE_LOCK(&cap->lock);
585         }
586
587     }
588
589     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
590
591     debugTrace(DEBUG_sched, "resuming capability %d", cap->no);
592
593     *pCap = cap;
594 #endif
595 }
596
597 #if defined(THREADED_RTS)
598 /* ----------------------------------------------------------------------------
599  * yieldCapability
600  * ------------------------------------------------------------------------- */
601
602 void
603 yieldCapability (Capability** pCap, Task *task)
604 {
605     Capability *cap = *pCap;
606
607     if (waiting_for_gc == PENDING_GC_PAR) {
608         traceEventGcStart(cap);
609         gcWorkerThread(cap);
610         traceEventGcEnd(cap);
611         return;
612     }
613
614         debugTrace(DEBUG_sched, "giving up capability %d", cap->no);
615
616         // We must now release the capability and wait to be woken up
617         // again.
618         task->wakeup = rtsFalse;
619         releaseCapabilityAndQueueWorker(cap);
620
621         for (;;) {
622             ACQUIRE_LOCK(&task->lock);
623             // task->lock held, cap->lock not held
624             if (!task->wakeup) waitCondition(&task->cond, &task->lock);
625             cap = task->cap;
626             task->wakeup = rtsFalse;
627             RELEASE_LOCK(&task->lock);
628
629             debugTrace(DEBUG_sched, "woken up on capability %d", cap->no);
630
631             ACQUIRE_LOCK(&cap->lock);
632             if (cap->running_task != NULL) {
633                 debugTrace(DEBUG_sched, 
634                            "capability %d is owned by another task", cap->no);
635                 RELEASE_LOCK(&cap->lock);
636                 continue;
637             }
638
639             if (task->incall->tso == NULL) {
640                 ASSERT(cap->spare_workers != NULL);
641                 // if we're not at the front of the queue, release it
642                 // again.  This is unlikely to happen.
643                 if (cap->spare_workers != task) {
644                     giveCapabilityToTask(cap,cap->spare_workers);
645                     RELEASE_LOCK(&cap->lock);
646                     continue;
647                 }
648                 cap->spare_workers = task->next;
649                 task->next = NULL;
650                 cap->n_spare_workers--;
651             }
652             cap->running_task = task;
653             RELEASE_LOCK(&cap->lock);
654             break;
655         }
656
657         debugTrace(DEBUG_sched, "resuming capability %d", cap->no);
658         ASSERT(cap->running_task == task);
659
660     *pCap = cap;
661
662     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
663
664     return;
665 }
666
667 /* ----------------------------------------------------------------------------
668  * prodCapability
669  *
670  * If a Capability is currently idle, wake up a Task on it.  Used to 
671  * get every Capability into the GC.
672  * ------------------------------------------------------------------------- */
673
674 void
675 prodCapability (Capability *cap, Task *task)
676 {
677     ACQUIRE_LOCK(&cap->lock);
678     if (!cap->running_task) {
679         cap->running_task = task;
680         releaseCapability_(cap,rtsTrue);
681     }
682     RELEASE_LOCK(&cap->lock);
683 }
684
685 /* ----------------------------------------------------------------------------
686  * tryGrabCapability
687  *
688  * Attempt to gain control of a Capability if it is free.
689  *
690  * ------------------------------------------------------------------------- */
691
692 rtsBool
693 tryGrabCapability (Capability *cap, Task *task)
694 {
695     if (cap->running_task != NULL) return rtsFalse;
696     ACQUIRE_LOCK(&cap->lock);
697     if (cap->running_task != NULL) {
698         RELEASE_LOCK(&cap->lock);
699         return rtsFalse;
700     }
701     task->cap = cap;
702     cap->running_task = task;
703     RELEASE_LOCK(&cap->lock);
704     return rtsTrue;
705 }
706
707
708 #endif /* THREADED_RTS */
709
710 /* ----------------------------------------------------------------------------
711  * shutdownCapability
712  *
713  * At shutdown time, we want to let everything exit as cleanly as
714  * possible.  For each capability, we let its run queue drain, and
715  * allow the workers to stop.
716  *
717  * This function should be called when interrupted and
718  * shutting_down_scheduler = rtsTrue, thus any worker that wakes up
719  * will exit the scheduler and call taskStop(), and any bound thread
720  * that wakes up will return to its caller.  Runnable threads are
721  * killed.
722  *
723  * ------------------------------------------------------------------------- */
724
725 void
726 shutdownCapability (Capability *cap,
727                     Task *task USED_IF_THREADS,
728                     rtsBool safe USED_IF_THREADS)
729 {
730 #if defined(THREADED_RTS)
731     nat i;
732
733     task->cap = cap;
734
735     // Loop indefinitely until all the workers have exited and there
736     // are no Haskell threads left.  We used to bail out after 50
737     // iterations of this loop, but that occasionally left a worker
738     // running which caused problems later (the closeMutex() below
739     // isn't safe, for one thing).
740
741     for (i = 0; /* i < 50 */; i++) {
742         ASSERT(sched_state == SCHED_SHUTTING_DOWN);
743
744         debugTrace(DEBUG_sched, 
745                    "shutting down capability %d, attempt %d", cap->no, i);
746         ACQUIRE_LOCK(&cap->lock);
747         if (cap->running_task) {
748             RELEASE_LOCK(&cap->lock);
749             debugTrace(DEBUG_sched, "not owner, yielding");
750             yieldThread();
751             continue;
752         }
753         cap->running_task = task;
754
755         if (cap->spare_workers) {
756             // Look for workers that have died without removing
757             // themselves from the list; this could happen if the OS
758             // summarily killed the thread, for example.  This
759             // actually happens on Windows when the system is
760             // terminating the program, and the RTS is running in a
761             // DLL.
762             Task *t, *prev;
763             prev = NULL;
764             for (t = cap->spare_workers; t != NULL; t = t->next) {
765                 if (!osThreadIsAlive(t->id)) {
766                     debugTrace(DEBUG_sched, 
767                                "worker thread %p has died unexpectedly", (void *)t->id);
768                     cap->n_spare_workers--;
769                     if (!prev) {
770                         cap->spare_workers = t->next;
771                     } else {
772                         prev->next = t->next;
773                     }
774                     prev = t;
775                 }
776             }
777         }
778
779         if (!emptyRunQueue(cap) || cap->spare_workers) {
780             debugTrace(DEBUG_sched, 
781                        "runnable threads or workers still alive, yielding");
782             releaseCapability_(cap,rtsFalse); // this will wake up a worker
783             RELEASE_LOCK(&cap->lock);
784             yieldThread();
785             continue;
786         }
787
788         // If "safe", then busy-wait for any threads currently doing
789         // foreign calls.  If we're about to unload this DLL, for
790         // example, we need to be sure that there are no OS threads
791         // that will try to return to code that has been unloaded.
792         // We can be a bit more relaxed when this is a standalone
793         // program that is about to terminate, and let safe=false.
794         if (cap->suspended_ccalls && safe) {
795             debugTrace(DEBUG_sched, 
796                        "thread(s) are involved in foreign calls, yielding");
797             cap->running_task = NULL;
798             RELEASE_LOCK(&cap->lock);
799             // The IO manager thread might have been slow to start up,
800             // so the first attempt to kill it might not have
801             // succeeded.  Just in case, try again - the kill message
802             // will only be sent once.
803             //
804             // To reproduce this deadlock: run ffi002(threaded1)
805             // repeatedly on a loaded machine.
806             ioManagerDie();
807             yieldThread();
808             continue;
809         }
810
811         traceEventShutdown(cap);
812         RELEASE_LOCK(&cap->lock);
813         break;
814     }
815     // we now have the Capability, its run queue and spare workers
816     // list are both empty.
817
818     // ToDo: we can't drop this mutex, because there might still be
819     // threads performing foreign calls that will eventually try to 
820     // return via resumeThread() and attempt to grab cap->lock.
821     // closeMutex(&cap->lock);
822     
823 #endif /* THREADED_RTS */
824
825     traceCapsetRemoveCap(CAPSET_OSPROCESS_DEFAULT, cap->no);
826 }
827
828 void
829 shutdownCapabilities(Task *task, rtsBool safe)
830 {
831     nat i;
832     for (i=0; i < n_capabilities; i++) {
833         ASSERT(task->incall->tso == NULL);
834         shutdownCapability(&capabilities[i], task, safe);
835     }
836     traceCapsetDelete(CAPSET_OSPROCESS_DEFAULT);
837 }
838
839 static void
840 freeCapability (Capability *cap)
841 {
842     stgFree(cap->mut_lists);
843     stgFree(cap->saved_mut_lists);
844 #if defined(THREADED_RTS)
845     freeSparkPool(cap->sparks);
846 #endif
847 }
848
849 void
850 freeCapabilities (void)
851 {
852 #if defined(THREADED_RTS)
853     nat i;
854     for (i=0; i < n_capabilities; i++) {
855         freeCapability(&capabilities[i]);
856     }
857 #else
858     freeCapability(&MainCapability);
859 #endif
860 }
861
862 /* ---------------------------------------------------------------------------
863    Mark everything directly reachable from the Capabilities.  When
864    using multiple GC threads, each GC thread marks all Capabilities
865    for which (c `mod` n == 0), for Capability c and thread n.
866    ------------------------------------------------------------------------ */
867
868 void
869 markCapability (evac_fn evac, void *user, Capability *cap,
870                 rtsBool no_mark_sparks USED_IF_THREADS)
871 {
872     InCall *incall;
873
874     // Each GC thread is responsible for following roots from the
875     // Capability of the same number.  There will usually be the same
876     // or fewer Capabilities as GC threads, but just in case there
877     // are more, we mark every Capability whose number is the GC
878     // thread's index plus a multiple of the number of GC threads.
879     evac(user, (StgClosure **)(void *)&cap->run_queue_hd);
880     evac(user, (StgClosure **)(void *)&cap->run_queue_tl);
881 #if defined(THREADED_RTS)
882     evac(user, (StgClosure **)(void *)&cap->inbox);
883 #endif
884     for (incall = cap->suspended_ccalls; incall != NULL;
885          incall=incall->next) {
886         evac(user, (StgClosure **)(void *)&incall->suspended_tso);
887     }
888
889 #if defined(THREADED_RTS)
890     if (!no_mark_sparks) {
891         traverseSparkQueue (evac, user, cap);
892     }
893 #endif
894
895     // Free STM structures for this Capability
896     stmPreGCHook(cap);
897 }
898
899 void
900 markCapabilities (evac_fn evac, void *user)
901 {
902     nat n;
903     for (n = 0; n < n_capabilities; n++) {
904         markCapability(evac, user, &capabilities[n], rtsFalse);
905     }
906 }