threadStackUnderflow: put the new TSO on the mut list if necessary
[ghc-hetmet.git] / rts / Schedule.c
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2006
4  *
5  * The scheduler and thread-related functionality
6  *
7  * --------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #define KEEP_LOCKCLOSURE
11 #include "Rts.h"
12
13 #include "sm/Storage.h"
14 #include "RtsUtils.h"
15 #include "StgRun.h"
16 #include "Schedule.h"
17 #include "Interpreter.h"
18 #include "Printer.h"
19 #include "RtsSignals.h"
20 #include "Sanity.h"
21 #include "Stats.h"
22 #include "STM.h"
23 #include "Prelude.h"
24 #include "ThreadLabels.h"
25 #include "Updates.h"
26 #include "Proftimer.h"
27 #include "ProfHeap.h"
28 #include "Weak.h"
29 #include "sm/GC.h" // waitForGcThreads, releaseGCThreads, N
30 #include "Sparks.h"
31 #include "Capability.h"
32 #include "Task.h"
33 #include "AwaitEvent.h"
34 #if defined(mingw32_HOST_OS)
35 #include "win32/IOManager.h"
36 #endif
37 #include "Trace.h"
38 #include "RaiseAsync.h"
39 #include "Threads.h"
40 #include "Timer.h"
41 #include "ThreadPaused.h"
42
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdarg.h>
53
54 #ifdef HAVE_ERRNO_H
55 #include <errno.h>
56 #endif
57
58 /* -----------------------------------------------------------------------------
59  * Global variables
60  * -------------------------------------------------------------------------- */
61
62 #if !defined(THREADED_RTS)
63 // Blocked/sleeping thrads
64 StgTSO *blocked_queue_hd = NULL;
65 StgTSO *blocked_queue_tl = NULL;
66 StgTSO *sleeping_queue = NULL;    // perhaps replace with a hash table?
67 #endif
68
69 /* Threads blocked on blackholes.
70  * LOCK: sched_mutex+capability, or all capabilities
71  */
72 StgTSO *blackhole_queue = NULL;
73
74 /* The blackhole_queue should be checked for threads to wake up.  See
75  * Schedule.h for more thorough comment.
76  * LOCK: none (doesn't matter if we miss an update)
77  */
78 rtsBool blackholes_need_checking = rtsFalse;
79
80 /* Set to true when the latest garbage collection failed to reclaim
81  * enough space, and the runtime should proceed to shut itself down in
82  * an orderly fashion (emitting profiling info etc.)
83  */
84 rtsBool heap_overflow = rtsFalse;
85
86 /* flag that tracks whether we have done any execution in this time slice.
87  * LOCK: currently none, perhaps we should lock (but needs to be
88  * updated in the fast path of the scheduler).
89  *
90  * NB. must be StgWord, we do xchg() on it.
91  */
92 volatile StgWord recent_activity = ACTIVITY_YES;
93
94 /* if this flag is set as well, give up execution
95  * LOCK: none (changes monotonically)
96  */
97 volatile StgWord sched_state = SCHED_RUNNING;
98
99 /*  This is used in `TSO.h' and gcc 2.96 insists that this variable actually 
100  *  exists - earlier gccs apparently didn't.
101  *  -= chak
102  */
103 StgTSO dummy_tso;
104
105 /*
106  * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
107  * in an MT setting, needed to signal that a worker thread shouldn't hang around
108  * in the scheduler when it is out of work.
109  */
110 rtsBool shutting_down_scheduler = rtsFalse;
111
112 /*
113  * This mutex protects most of the global scheduler data in
114  * the THREADED_RTS runtime.
115  */
116 #if defined(THREADED_RTS)
117 Mutex sched_mutex;
118 #endif
119
120 #if !defined(mingw32_HOST_OS)
121 #define FORKPROCESS_PRIMOP_SUPPORTED
122 #endif
123
124 /* -----------------------------------------------------------------------------
125  * static function prototypes
126  * -------------------------------------------------------------------------- */
127
128 static Capability *schedule (Capability *initialCapability, Task *task);
129
130 //
131 // These function all encapsulate parts of the scheduler loop, and are
132 // abstracted only to make the structure and control flow of the
133 // scheduler clearer.
134 //
135 static void schedulePreLoop (void);
136 static void scheduleFindWork (Capability *cap);
137 #if defined(THREADED_RTS)
138 static void scheduleYield (Capability **pcap, Task *task, rtsBool);
139 #endif
140 static void scheduleStartSignalHandlers (Capability *cap);
141 static void scheduleCheckBlockedThreads (Capability *cap);
142 static void scheduleCheckWakeupThreads(Capability *cap USED_IF_NOT_THREADS);
143 static void scheduleCheckBlackHoles (Capability *cap);
144 static void scheduleDetectDeadlock (Capability *cap, Task *task);
145 static void schedulePushWork(Capability *cap, Task *task);
146 #if defined(THREADED_RTS)
147 static void scheduleActivateSpark(Capability *cap);
148 #endif
149 static void schedulePostRunThread(Capability *cap, StgTSO *t);
150 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
151 static void scheduleHandleStackOverflow( Capability *cap, Task *task, 
152                                          StgTSO *t);
153 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t, 
154                                     nat prev_what_next );
155 static void scheduleHandleThreadBlocked( StgTSO *t );
156 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
157                                              StgTSO *t );
158 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
159 static Capability *scheduleDoGC(Capability *cap, Task *task,
160                                 rtsBool force_major);
161
162 static rtsBool checkBlackHoles(Capability *cap);
163
164 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
165 static StgTSO *threadStackUnderflow(Capability *cap, Task *task, StgTSO *tso);
166
167 static void deleteThread (Capability *cap, StgTSO *tso);
168 static void deleteAllThreads (Capability *cap);
169
170 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
171 static void deleteThread_(Capability *cap, StgTSO *tso);
172 #endif
173
174 /* -----------------------------------------------------------------------------
175  * Putting a thread on the run queue: different scheduling policies
176  * -------------------------------------------------------------------------- */
177
178 STATIC_INLINE void
179 addToRunQueue( Capability *cap, StgTSO *t )
180 {
181     // this does round-robin scheduling; good for concurrency
182     appendToRunQueue(cap,t);
183 }
184
185 /* ---------------------------------------------------------------------------
186    Main scheduling loop.
187
188    We use round-robin scheduling, each thread returning to the
189    scheduler loop when one of these conditions is detected:
190
191       * out of heap space
192       * timer expires (thread yields)
193       * thread blocks
194       * thread ends
195       * stack overflow
196
197    GRAN version:
198      In a GranSim setup this loop iterates over the global event queue.
199      This revolves around the global event queue, which determines what 
200      to do next. Therefore, it's more complicated than either the 
201      concurrent or the parallel (GUM) setup.
202   This version has been entirely removed (JB 2008/08).
203
204    GUM version:
205      GUM iterates over incoming messages.
206      It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
207      and sends out a fish whenever it has nothing to do; in-between
208      doing the actual reductions (shared code below) it processes the
209      incoming messages and deals with delayed operations 
210      (see PendingFetches).
211      This is not the ugliest code you could imagine, but it's bloody close.
212
213   (JB 2008/08) This version was formerly indicated by a PP-Flag PAR,
214   now by PP-flag PARALLEL_HASKELL. The Eden RTS (in GHC-6.x) uses it,
215   as well as future GUM versions. This file has been refurbished to
216   only contain valid code, which is however incomplete, refers to
217   invalid includes etc.
218
219    ------------------------------------------------------------------------ */
220
221 static Capability *
222 schedule (Capability *initialCapability, Task *task)
223 {
224   StgTSO *t;
225   Capability *cap;
226   StgThreadReturnCode ret;
227   nat prev_what_next;
228   rtsBool ready_to_gc;
229 #if defined(THREADED_RTS)
230   rtsBool first = rtsTrue;
231   rtsBool force_yield = rtsFalse;
232 #endif
233   
234   cap = initialCapability;
235
236   // Pre-condition: this task owns initialCapability.
237   // The sched_mutex is *NOT* held
238   // NB. on return, we still hold a capability.
239
240   debugTrace (DEBUG_sched, "cap %d: schedule()", initialCapability->no);
241
242   schedulePreLoop();
243
244   // -----------------------------------------------------------
245   // Scheduler loop starts here:
246
247   while (1) {
248
249     // Check whether we have re-entered the RTS from Haskell without
250     // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
251     // call).
252     if (cap->in_haskell) {
253           errorBelch("schedule: re-entered unsafely.\n"
254                      "   Perhaps a 'foreign import unsafe' should be 'safe'?");
255           stg_exit(EXIT_FAILURE);
256     }
257
258     // The interruption / shutdown sequence.
259     // 
260     // In order to cleanly shut down the runtime, we want to:
261     //   * make sure that all main threads return to their callers
262     //     with the state 'Interrupted'.
263     //   * clean up all OS threads assocated with the runtime
264     //   * free all memory etc.
265     //
266     // So the sequence for ^C goes like this:
267     //
268     //   * ^C handler sets sched_state := SCHED_INTERRUPTING and
269     //     arranges for some Capability to wake up
270     //
271     //   * all threads in the system are halted, and the zombies are
272     //     placed on the run queue for cleaning up.  We acquire all
273     //     the capabilities in order to delete the threads, this is
274     //     done by scheduleDoGC() for convenience (because GC already
275     //     needs to acquire all the capabilities).  We can't kill
276     //     threads involved in foreign calls.
277     // 
278     //   * somebody calls shutdownHaskell(), which calls exitScheduler()
279     //
280     //   * sched_state := SCHED_SHUTTING_DOWN
281     //
282     //   * all workers exit when the run queue on their capability
283     //     drains.  All main threads will also exit when their TSO
284     //     reaches the head of the run queue and they can return.
285     //
286     //   * eventually all Capabilities will shut down, and the RTS can
287     //     exit.
288     //
289     //   * We might be left with threads blocked in foreign calls, 
290     //     we should really attempt to kill these somehow (TODO);
291     
292     switch (sched_state) {
293     case SCHED_RUNNING:
294         break;
295     case SCHED_INTERRUPTING:
296         debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
297 #if defined(THREADED_RTS)
298         discardSparksCap(cap);
299 #endif
300         /* scheduleDoGC() deletes all the threads */
301         cap = scheduleDoGC(cap,task,rtsFalse);
302
303         // after scheduleDoGC(), we must be shutting down.  Either some
304         // other Capability did the final GC, or we did it above,
305         // either way we can fall through to the SCHED_SHUTTING_DOWN
306         // case now.
307         ASSERT(sched_state == SCHED_SHUTTING_DOWN);
308         // fall through
309
310     case SCHED_SHUTTING_DOWN:
311         debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
312         // If we are a worker, just exit.  If we're a bound thread
313         // then we will exit below when we've removed our TSO from
314         // the run queue.
315         if (task->tso == NULL && emptyRunQueue(cap)) {
316             return cap;
317         }
318         break;
319     default:
320         barf("sched_state: %d", sched_state);
321     }
322
323     scheduleFindWork(cap);
324
325     /* work pushing, currently relevant only for THREADED_RTS:
326        (pushes threads, wakes up idle capabilities for stealing) */
327     schedulePushWork(cap,task);
328
329     scheduleDetectDeadlock(cap,task);
330
331 #if defined(THREADED_RTS)
332     cap = task->cap;    // reload cap, it might have changed
333 #endif
334
335     // Normally, the only way we can get here with no threads to
336     // run is if a keyboard interrupt received during 
337     // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
338     // Additionally, it is not fatal for the
339     // threaded RTS to reach here with no threads to run.
340     //
341     // win32: might be here due to awaitEvent() being abandoned
342     // as a result of a console event having been delivered.
343     
344 #if defined(THREADED_RTS)
345     if (first) 
346     {
347     // XXX: ToDo
348     //     // don't yield the first time, we want a chance to run this
349     //     // thread for a bit, even if there are others banging at the
350     //     // door.
351     //     first = rtsFalse;
352     //     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
353     }
354
355   yield:
356     scheduleYield(&cap,task,force_yield);
357     force_yield = rtsFalse;
358
359     if (emptyRunQueue(cap)) continue; // look for work again
360 #endif
361
362 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
363     if ( emptyRunQueue(cap) ) {
364         ASSERT(sched_state >= SCHED_INTERRUPTING);
365     }
366 #endif
367
368     // 
369     // Get a thread to run
370     //
371     t = popRunQueue(cap);
372
373     // Sanity check the thread we're about to run.  This can be
374     // expensive if there is lots of thread switching going on...
375     IF_DEBUG(sanity,checkTSO(t));
376
377 #if defined(THREADED_RTS)
378     // Check whether we can run this thread in the current task.
379     // If not, we have to pass our capability to the right task.
380     {
381         Task *bound = t->bound;
382       
383         if (bound) {
384             if (bound == task) {
385                 // yes, the Haskell thread is bound to the current native thread
386             } else {
387                 debugTrace(DEBUG_sched,
388                            "thread %lu bound to another OS thread",
389                            (unsigned long)t->id);
390                 // no, bound to a different Haskell thread: pass to that thread
391                 pushOnRunQueue(cap,t);
392                 continue;
393             }
394         } else {
395             // The thread we want to run is unbound.
396             if (task->tso) { 
397                 debugTrace(DEBUG_sched,
398                            "this OS thread cannot run thread %lu",
399                            (unsigned long)t->id);
400                 // no, the current native thread is bound to a different
401                 // Haskell thread, so pass it to any worker thread
402                 pushOnRunQueue(cap,t);
403                 continue; 
404             }
405         }
406     }
407 #endif
408
409     // If we're shutting down, and this thread has not yet been
410     // killed, kill it now.  This sometimes happens when a finalizer
411     // thread is created by the final GC, or a thread previously
412     // in a foreign call returns.
413     if (sched_state >= SCHED_INTERRUPTING &&
414         !(t->what_next == ThreadComplete || t->what_next == ThreadKilled)) {
415         deleteThread(cap,t);
416     }
417
418     /* context switches are initiated by the timer signal, unless
419      * the user specified "context switch as often as possible", with
420      * +RTS -C0
421      */
422     if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
423         && !emptyThreadQueues(cap)) {
424         cap->context_switch = 1;
425     }
426          
427 run_thread:
428
429     // CurrentTSO is the thread to run.  t might be different if we
430     // loop back to run_thread, so make sure to set CurrentTSO after
431     // that.
432     cap->r.rCurrentTSO = t;
433
434     startHeapProfTimer();
435
436     // Check for exceptions blocked on this thread
437     maybePerformBlockedException (cap, t);
438
439     // ----------------------------------------------------------------------
440     // Run the current thread 
441
442     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
443     ASSERT(t->cap == cap);
444     ASSERT(t->bound ? t->bound->cap == cap : 1);
445
446     prev_what_next = t->what_next;
447
448     errno = t->saved_errno;
449 #if mingw32_HOST_OS
450     SetLastError(t->saved_winerror);
451 #endif
452
453     cap->in_haskell = rtsTrue;
454
455     dirty_TSO(cap,t);
456
457 #if defined(THREADED_RTS)
458     if (recent_activity == ACTIVITY_DONE_GC) {
459         // ACTIVITY_DONE_GC means we turned off the timer signal to
460         // conserve power (see #1623).  Re-enable it here.
461         nat prev;
462         prev = xchg((P_)&recent_activity, ACTIVITY_YES);
463         if (prev == ACTIVITY_DONE_GC) {
464             startTimer();
465         }
466     } else {
467         recent_activity = ACTIVITY_YES;
468     }
469 #endif
470
471     traceSchedEvent(cap, EVENT_RUN_THREAD, t, 0);
472
473     switch (prev_what_next) {
474         
475     case ThreadKilled:
476     case ThreadComplete:
477         /* Thread already finished, return to scheduler. */
478         ret = ThreadFinished;
479         break;
480         
481     case ThreadRunGHC:
482     {
483         StgRegTable *r;
484         r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
485         cap = regTableToCapability(r);
486         ret = r->rRet;
487         break;
488     }
489     
490     case ThreadInterpret:
491         cap = interpretBCO(cap);
492         ret = cap->r.rRet;
493         break;
494         
495     default:
496         barf("schedule: invalid what_next field");
497     }
498
499     cap->in_haskell = rtsFalse;
500
501     // The TSO might have moved, eg. if it re-entered the RTS and a GC
502     // happened.  So find the new location:
503     t = cap->r.rCurrentTSO;
504
505     // We have run some Haskell code: there might be blackhole-blocked
506     // threads to wake up now.
507     // Lock-free test here should be ok, we're just setting a flag.
508     if ( blackhole_queue != END_TSO_QUEUE ) {
509         blackholes_need_checking = rtsTrue;
510     }
511     
512     // And save the current errno in this thread.
513     // XXX: possibly bogus for SMP because this thread might already
514     // be running again, see code below.
515     t->saved_errno = errno;
516 #if mingw32_HOST_OS
517     // Similarly for Windows error code
518     t->saved_winerror = GetLastError();
519 #endif
520
521     traceSchedEvent (cap, EVENT_STOP_THREAD, t, ret);
522
523 #if defined(THREADED_RTS)
524     // If ret is ThreadBlocked, and this Task is bound to the TSO that
525     // blocked, we are in limbo - the TSO is now owned by whatever it
526     // is blocked on, and may in fact already have been woken up,
527     // perhaps even on a different Capability.  It may be the case
528     // that task->cap != cap.  We better yield this Capability
529     // immediately and return to normaility.
530     if (ret == ThreadBlocked) {
531         force_yield = rtsTrue;
532         goto yield;
533     }
534 #endif
535
536     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
537     ASSERT(t->cap == cap);
538
539     // ----------------------------------------------------------------------
540     
541     // Costs for the scheduler are assigned to CCS_SYSTEM
542     stopHeapProfTimer();
543 #if defined(PROFILING)
544     CCCS = CCS_SYSTEM;
545 #endif
546     
547     schedulePostRunThread(cap,t);
548
549     if (ret != StackOverflow) {
550         t = threadStackUnderflow(cap,task,t);
551     }
552
553     ready_to_gc = rtsFalse;
554
555     switch (ret) {
556     case HeapOverflow:
557         ready_to_gc = scheduleHandleHeapOverflow(cap,t);
558         break;
559
560     case StackOverflow:
561         scheduleHandleStackOverflow(cap,task,t);
562         break;
563
564     case ThreadYielding:
565         if (scheduleHandleYield(cap, t, prev_what_next)) {
566             // shortcut for switching between compiler/interpreter:
567             goto run_thread; 
568         }
569         break;
570
571     case ThreadBlocked:
572         scheduleHandleThreadBlocked(t);
573         break;
574
575     case ThreadFinished:
576         if (scheduleHandleThreadFinished(cap, task, t)) return cap;
577         ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
578         break;
579
580     default:
581       barf("schedule: invalid thread return code %d", (int)ret);
582     }
583
584     if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
585       cap = scheduleDoGC(cap,task,rtsFalse);
586     }
587   } /* end of while() */
588 }
589
590 /* ----------------------------------------------------------------------------
591  * Setting up the scheduler loop
592  * ------------------------------------------------------------------------- */
593
594 static void
595 schedulePreLoop(void)
596 {
597   // initialisation for scheduler - what cannot go into initScheduler()  
598 }
599
600 /* -----------------------------------------------------------------------------
601  * scheduleFindWork()
602  *
603  * Search for work to do, and handle messages from elsewhere.
604  * -------------------------------------------------------------------------- */
605
606 static void
607 scheduleFindWork (Capability *cap)
608 {
609     scheduleStartSignalHandlers(cap);
610
611     // Only check the black holes here if we've nothing else to do.
612     // During normal execution, the black hole list only gets checked
613     // at GC time, to avoid repeatedly traversing this possibly long
614     // list each time around the scheduler.
615     if (emptyRunQueue(cap)) { scheduleCheckBlackHoles(cap); }
616
617     scheduleCheckWakeupThreads(cap);
618
619     scheduleCheckBlockedThreads(cap);
620
621 #if defined(THREADED_RTS)
622     if (emptyRunQueue(cap)) { scheduleActivateSpark(cap); }
623 #endif
624 }
625
626 #if defined(THREADED_RTS)
627 STATIC_INLINE rtsBool
628 shouldYieldCapability (Capability *cap, Task *task)
629 {
630     // we need to yield this capability to someone else if..
631     //   - another thread is initiating a GC
632     //   - another Task is returning from a foreign call
633     //   - the thread at the head of the run queue cannot be run
634     //     by this Task (it is bound to another Task, or it is unbound
635     //     and this task it bound).
636     return (waiting_for_gc || 
637             cap->returning_tasks_hd != NULL ||
638             (!emptyRunQueue(cap) && (task->tso == NULL
639                                      ? cap->run_queue_hd->bound != NULL
640                                      : cap->run_queue_hd->bound != task)));
641 }
642
643 // This is the single place where a Task goes to sleep.  There are
644 // two reasons it might need to sleep:
645 //    - there are no threads to run
646 //    - we need to yield this Capability to someone else 
647 //      (see shouldYieldCapability())
648 //
649 // Careful: the scheduler loop is quite delicate.  Make sure you run
650 // the tests in testsuite/concurrent (all ways) after modifying this,
651 // and also check the benchmarks in nofib/parallel for regressions.
652
653 static void
654 scheduleYield (Capability **pcap, Task *task, rtsBool force_yield)
655 {
656     Capability *cap = *pcap;
657
658     // if we have work, and we don't need to give up the Capability, continue.
659     //
660     // The force_yield flag is used when a bound thread blocks.  This
661     // is a particularly tricky situation: the current Task does not
662     // own the TSO any more, since it is on some queue somewhere, and
663     // might be woken up or manipulated by another thread at any time.
664     // The TSO and Task might be migrated to another Capability.
665     // Certain invariants might be in doubt, such as task->bound->cap
666     // == cap.  We have to yield the current Capability immediately,
667     // no messing around.
668     //
669     if (!force_yield &&
670         !shouldYieldCapability(cap,task) && 
671         (!emptyRunQueue(cap) ||
672          !emptyWakeupQueue(cap) ||
673          blackholes_need_checking ||
674          sched_state >= SCHED_INTERRUPTING))
675         return;
676
677     // otherwise yield (sleep), and keep yielding if necessary.
678     do {
679         yieldCapability(&cap,task);
680     } 
681     while (shouldYieldCapability(cap,task));
682
683     // note there may still be no threads on the run queue at this
684     // point, the caller has to check.
685
686     *pcap = cap;
687     return;
688 }
689 #endif
690     
691 /* -----------------------------------------------------------------------------
692  * schedulePushWork()
693  *
694  * Push work to other Capabilities if we have some.
695  * -------------------------------------------------------------------------- */
696
697 static void
698 schedulePushWork(Capability *cap USED_IF_THREADS, 
699                  Task *task      USED_IF_THREADS)
700 {
701   /* following code not for PARALLEL_HASKELL. I kept the call general,
702      future GUM versions might use pushing in a distributed setup */
703 #if defined(THREADED_RTS)
704
705     Capability *free_caps[n_capabilities], *cap0;
706     nat i, n_free_caps;
707
708     // migration can be turned off with +RTS -qg
709     if (!RtsFlags.ParFlags.migrate) return;
710
711     // Check whether we have more threads on our run queue, or sparks
712     // in our pool, that we could hand to another Capability.
713     if (cap->run_queue_hd == END_TSO_QUEUE) {
714         if (sparkPoolSizeCap(cap) < 2) return;
715     } else {
716         if (cap->run_queue_hd->_link == END_TSO_QUEUE &&
717             sparkPoolSizeCap(cap) < 1) return;
718     }
719
720     // First grab as many free Capabilities as we can.
721     for (i=0, n_free_caps=0; i < n_capabilities; i++) {
722         cap0 = &capabilities[i];
723         if (cap != cap0 && tryGrabCapability(cap0,task)) {
724             if (!emptyRunQueue(cap0) || cap->returning_tasks_hd != NULL) {
725                 // it already has some work, we just grabbed it at 
726                 // the wrong moment.  Or maybe it's deadlocked!
727                 releaseCapability(cap0);
728             } else {
729                 free_caps[n_free_caps++] = cap0;
730             }
731         }
732     }
733
734     // we now have n_free_caps free capabilities stashed in
735     // free_caps[].  Share our run queue equally with them.  This is
736     // probably the simplest thing we could do; improvements we might
737     // want to do include:
738     //
739     //   - giving high priority to moving relatively new threads, on 
740     //     the gournds that they haven't had time to build up a
741     //     working set in the cache on this CPU/Capability.
742     //
743     //   - giving low priority to moving long-lived threads
744
745     if (n_free_caps > 0) {
746         StgTSO *prev, *t, *next;
747         rtsBool pushed_to_all;
748
749         debugTrace(DEBUG_sched, 
750                    "cap %d: %s and %d free capabilities, sharing...", 
751                    cap->no, 
752                    (!emptyRunQueue(cap) && cap->run_queue_hd->_link != END_TSO_QUEUE)?
753                    "excess threads on run queue":"sparks to share (>=2)",
754                    n_free_caps);
755
756         i = 0;
757         pushed_to_all = rtsFalse;
758
759         if (cap->run_queue_hd != END_TSO_QUEUE) {
760             prev = cap->run_queue_hd;
761             t = prev->_link;
762             prev->_link = END_TSO_QUEUE;
763             for (; t != END_TSO_QUEUE; t = next) {
764                 next = t->_link;
765                 t->_link = END_TSO_QUEUE;
766                 if (t->what_next == ThreadRelocated
767                     || t->bound == task // don't move my bound thread
768                     || tsoLocked(t)) {  // don't move a locked thread
769                     setTSOLink(cap, prev, t);
770                     prev = t;
771                 } else if (i == n_free_caps) {
772                     pushed_to_all = rtsTrue;
773                     i = 0;
774                     // keep one for us
775                     setTSOLink(cap, prev, t);
776                     prev = t;
777                 } else {
778                     debugTrace(DEBUG_sched, "pushing thread %lu to capability %d", (unsigned long)t->id, free_caps[i]->no);
779                     appendToRunQueue(free_caps[i],t);
780
781                     traceSchedEvent (cap, EVENT_MIGRATE_THREAD, t, free_caps[i]->no);
782
783                     if (t->bound) { t->bound->cap = free_caps[i]; }
784                     t->cap = free_caps[i];
785                     i++;
786                 }
787             }
788             cap->run_queue_tl = prev;
789         }
790
791 #ifdef SPARK_PUSHING
792         /* JB I left this code in place, it would work but is not necessary */
793
794         // If there are some free capabilities that we didn't push any
795         // threads to, then try to push a spark to each one.
796         if (!pushed_to_all) {
797             StgClosure *spark;
798             // i is the next free capability to push to
799             for (; i < n_free_caps; i++) {
800                 if (emptySparkPoolCap(free_caps[i])) {
801                     spark = tryStealSpark(cap->sparks);
802                     if (spark != NULL) {
803                         debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
804
805       traceSchedEvent(free_caps[i], EVENT_STEAL_SPARK, t, cap->no);
806
807                         newSpark(&(free_caps[i]->r), spark);
808                     }
809                 }
810             }
811         }
812 #endif /* SPARK_PUSHING */
813
814         // release the capabilities
815         for (i = 0; i < n_free_caps; i++) {
816             task->cap = free_caps[i];
817             releaseAndWakeupCapability(free_caps[i]);
818         }
819     }
820     task->cap = cap; // reset to point to our Capability.
821
822 #endif /* THREADED_RTS */
823
824 }
825
826 /* ----------------------------------------------------------------------------
827  * Start any pending signal handlers
828  * ------------------------------------------------------------------------- */
829
830 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
831 static void
832 scheduleStartSignalHandlers(Capability *cap)
833 {
834     if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
835         // safe outside the lock
836         startSignalHandlers(cap);
837     }
838 }
839 #else
840 static void
841 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
842 {
843 }
844 #endif
845
846 /* ----------------------------------------------------------------------------
847  * Check for blocked threads that can be woken up.
848  * ------------------------------------------------------------------------- */
849
850 static void
851 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
852 {
853 #if !defined(THREADED_RTS)
854     //
855     // Check whether any waiting threads need to be woken up.  If the
856     // run queue is empty, and there are no other tasks running, we
857     // can wait indefinitely for something to happen.
858     //
859     if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
860     {
861         awaitEvent( emptyRunQueue(cap) && !blackholes_need_checking );
862     }
863 #endif
864 }
865
866
867 /* ----------------------------------------------------------------------------
868  * Check for threads woken up by other Capabilities
869  * ------------------------------------------------------------------------- */
870
871 static void
872 scheduleCheckWakeupThreads(Capability *cap USED_IF_THREADS)
873 {
874 #if defined(THREADED_RTS)
875     // Any threads that were woken up by other Capabilities get
876     // appended to our run queue.
877     if (!emptyWakeupQueue(cap)) {
878         ACQUIRE_LOCK(&cap->lock);
879         if (emptyRunQueue(cap)) {
880             cap->run_queue_hd = cap->wakeup_queue_hd;
881             cap->run_queue_tl = cap->wakeup_queue_tl;
882         } else {
883             setTSOLink(cap, cap->run_queue_tl, cap->wakeup_queue_hd);
884             cap->run_queue_tl = cap->wakeup_queue_tl;
885         }
886         cap->wakeup_queue_hd = cap->wakeup_queue_tl = END_TSO_QUEUE;
887         RELEASE_LOCK(&cap->lock);
888     }
889 #endif
890 }
891
892 /* ----------------------------------------------------------------------------
893  * Check for threads blocked on BLACKHOLEs that can be woken up
894  * ------------------------------------------------------------------------- */
895 static void
896 scheduleCheckBlackHoles (Capability *cap)
897 {
898     if ( blackholes_need_checking ) // check without the lock first
899     {
900         ACQUIRE_LOCK(&sched_mutex);
901         if ( blackholes_need_checking ) {
902             blackholes_need_checking = rtsFalse;
903             // important that we reset the flag *before* checking the
904             // blackhole queue, otherwise we could get deadlock.  This
905             // happens as follows: we wake up a thread that
906             // immediately runs on another Capability, blocks on a
907             // blackhole, and then we reset the blackholes_need_checking flag.
908             checkBlackHoles(cap);
909         }
910         RELEASE_LOCK(&sched_mutex);
911     }
912 }
913
914 /* ----------------------------------------------------------------------------
915  * Detect deadlock conditions and attempt to resolve them.
916  * ------------------------------------------------------------------------- */
917
918 static void
919 scheduleDetectDeadlock (Capability *cap, Task *task)
920 {
921     /* 
922      * Detect deadlock: when we have no threads to run, there are no
923      * threads blocked, waiting for I/O, or sleeping, and all the
924      * other tasks are waiting for work, we must have a deadlock of
925      * some description.
926      */
927     if ( emptyThreadQueues(cap) )
928     {
929 #if defined(THREADED_RTS)
930         /* 
931          * In the threaded RTS, we only check for deadlock if there
932          * has been no activity in a complete timeslice.  This means
933          * we won't eagerly start a full GC just because we don't have
934          * any threads to run currently.
935          */
936         if (recent_activity != ACTIVITY_INACTIVE) return;
937 #endif
938
939         debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
940
941         // Garbage collection can release some new threads due to
942         // either (a) finalizers or (b) threads resurrected because
943         // they are unreachable and will therefore be sent an
944         // exception.  Any threads thus released will be immediately
945         // runnable.
946         cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/);
947         // when force_major == rtsTrue. scheduleDoGC sets
948         // recent_activity to ACTIVITY_DONE_GC and turns off the timer
949         // signal.
950
951         if ( !emptyRunQueue(cap) ) return;
952
953 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
954         /* If we have user-installed signal handlers, then wait
955          * for signals to arrive rather then bombing out with a
956          * deadlock.
957          */
958         if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
959             debugTrace(DEBUG_sched,
960                        "still deadlocked, waiting for signals...");
961
962             awaitUserSignals();
963
964             if (signals_pending()) {
965                 startSignalHandlers(cap);
966             }
967
968             // either we have threads to run, or we were interrupted:
969             ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
970
971             return;
972         }
973 #endif
974
975 #if !defined(THREADED_RTS)
976         /* Probably a real deadlock.  Send the current main thread the
977          * Deadlock exception.
978          */
979         if (task->tso) {
980             switch (task->tso->why_blocked) {
981             case BlockedOnSTM:
982             case BlockedOnBlackHole:
983             case BlockedOnException:
984             case BlockedOnMVar:
985                 throwToSingleThreaded(cap, task->tso, 
986                                       (StgClosure *)nonTermination_closure);
987                 return;
988             default:
989                 barf("deadlock: main thread blocked in a strange way");
990             }
991         }
992         return;
993 #endif
994     }
995 }
996
997
998 /* ----------------------------------------------------------------------------
999  * Send pending messages (PARALLEL_HASKELL only)
1000  * ------------------------------------------------------------------------- */
1001
1002 #if defined(PARALLEL_HASKELL)
1003 static void
1004 scheduleSendPendingMessages(void)
1005 {
1006
1007 # if defined(PAR) // global Mem.Mgmt., omit for now
1008     if (PendingFetches != END_BF_QUEUE) {
1009         processFetches();
1010     }
1011 # endif
1012     
1013     if (RtsFlags.ParFlags.BufferTime) {
1014         // if we use message buffering, we must send away all message
1015         // packets which have become too old...
1016         sendOldBuffers(); 
1017     }
1018 }
1019 #endif
1020
1021 /* ----------------------------------------------------------------------------
1022  * Activate spark threads (PARALLEL_HASKELL and THREADED_RTS)
1023  * ------------------------------------------------------------------------- */
1024
1025 #if defined(THREADED_RTS)
1026 static void
1027 scheduleActivateSpark(Capability *cap)
1028 {
1029     if (anySparks())
1030     {
1031         createSparkThread(cap);
1032         debugTrace(DEBUG_sched, "creating a spark thread");
1033     }
1034 }
1035 #endif // PARALLEL_HASKELL || THREADED_RTS
1036
1037 /* ----------------------------------------------------------------------------
1038  * After running a thread...
1039  * ------------------------------------------------------------------------- */
1040
1041 static void
1042 schedulePostRunThread (Capability *cap, StgTSO *t)
1043 {
1044     // We have to be able to catch transactions that are in an
1045     // infinite loop as a result of seeing an inconsistent view of
1046     // memory, e.g. 
1047     //
1048     //   atomically $ do
1049     //       [a,b] <- mapM readTVar [ta,tb]
1050     //       when (a == b) loop
1051     //
1052     // and a is never equal to b given a consistent view of memory.
1053     //
1054     if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
1055         if (!stmValidateNestOfTransactions (t -> trec)) {
1056             debugTrace(DEBUG_sched | DEBUG_stm,
1057                        "trec %p found wasting its time", t);
1058             
1059             // strip the stack back to the
1060             // ATOMICALLY_FRAME, aborting the (nested)
1061             // transaction, and saving the stack of any
1062             // partially-evaluated thunks on the heap.
1063             throwToSingleThreaded_(cap, t, NULL, rtsTrue);
1064             
1065 //            ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1066         }
1067     }
1068
1069   /* some statistics gathering in the parallel case */
1070 }
1071
1072 /* -----------------------------------------------------------------------------
1073  * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1074  * -------------------------------------------------------------------------- */
1075
1076 static rtsBool
1077 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1078 {
1079     // did the task ask for a large block?
1080     if (cap->r.rHpAlloc > BLOCK_SIZE) {
1081         // if so, get one and push it on the front of the nursery.
1082         bdescr *bd;
1083         lnat blocks;
1084         
1085         blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1086         
1087         debugTrace(DEBUG_sched,
1088                    "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n", 
1089                    (long)t->id, what_next_strs[t->what_next], blocks);
1090     
1091         // don't do this if the nursery is (nearly) full, we'll GC first.
1092         if (cap->r.rCurrentNursery->link != NULL ||
1093             cap->r.rNursery->n_blocks == 1) {  // paranoia to prevent infinite loop
1094                                                // if the nursery has only one block.
1095             
1096             ACQUIRE_SM_LOCK
1097             bd = allocGroup( blocks );
1098             RELEASE_SM_LOCK
1099             cap->r.rNursery->n_blocks += blocks;
1100             
1101             // link the new group into the list
1102             bd->link = cap->r.rCurrentNursery;
1103             bd->u.back = cap->r.rCurrentNursery->u.back;
1104             if (cap->r.rCurrentNursery->u.back != NULL) {
1105                 cap->r.rCurrentNursery->u.back->link = bd;
1106             } else {
1107                 cap->r.rNursery->blocks = bd;
1108             }             
1109             cap->r.rCurrentNursery->u.back = bd;
1110             
1111             // initialise it as a nursery block.  We initialise the
1112             // step, gen_no, and flags field of *every* sub-block in
1113             // this large block, because this is easier than making
1114             // sure that we always find the block head of a large
1115             // block whenever we call Bdescr() (eg. evacuate() and
1116             // isAlive() in the GC would both have to do this, at
1117             // least).
1118             { 
1119                 bdescr *x;
1120                 for (x = bd; x < bd + blocks; x++) {
1121                     initBdescr(x,cap->r.rNursery);
1122                     x->free = x->start;
1123                     x->flags = 0;
1124                 }
1125             }
1126             
1127             // This assert can be a killer if the app is doing lots
1128             // of large block allocations.
1129             IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1130             
1131             // now update the nursery to point to the new block
1132             cap->r.rCurrentNursery = bd;
1133             
1134             // we might be unlucky and have another thread get on the
1135             // run queue before us and steal the large block, but in that
1136             // case the thread will just end up requesting another large
1137             // block.
1138             pushOnRunQueue(cap,t);
1139             return rtsFalse;  /* not actually GC'ing */
1140         }
1141     }
1142     
1143     if (cap->r.rHpLim == NULL || cap->context_switch) {
1144         // Sometimes we miss a context switch, e.g. when calling
1145         // primitives in a tight loop, MAYBE_GC() doesn't check the
1146         // context switch flag, and we end up waiting for a GC.
1147         // See #1984, and concurrent/should_run/1984
1148         cap->context_switch = 0;
1149         addToRunQueue(cap,t);
1150     } else {
1151         pushOnRunQueue(cap,t);
1152     }
1153     return rtsTrue;
1154     /* actual GC is done at the end of the while loop in schedule() */
1155 }
1156
1157 /* -----------------------------------------------------------------------------
1158  * Handle a thread that returned to the scheduler with ThreadStackOverflow
1159  * -------------------------------------------------------------------------- */
1160
1161 static void
1162 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1163 {
1164     /* just adjust the stack for this thread, then pop it back
1165      * on the run queue.
1166      */
1167     { 
1168         /* enlarge the stack */
1169         StgTSO *new_t = threadStackOverflow(cap, t);
1170         
1171         /* The TSO attached to this Task may have moved, so update the
1172          * pointer to it.
1173          */
1174         if (task->tso == t) {
1175             task->tso = new_t;
1176         }
1177         pushOnRunQueue(cap,new_t);
1178     }
1179 }
1180
1181 /* -----------------------------------------------------------------------------
1182  * Handle a thread that returned to the scheduler with ThreadYielding
1183  * -------------------------------------------------------------------------- */
1184
1185 static rtsBool
1186 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1187 {
1188     // Reset the context switch flag.  We don't do this just before
1189     // running the thread, because that would mean we would lose ticks
1190     // during GC, which can lead to unfair scheduling (a thread hogs
1191     // the CPU because the tick always arrives during GC).  This way
1192     // penalises threads that do a lot of allocation, but that seems
1193     // better than the alternative.
1194     cap->context_switch = 0;
1195     
1196     /* put the thread back on the run queue.  Then, if we're ready to
1197      * GC, check whether this is the last task to stop.  If so, wake
1198      * up the GC thread.  getThread will block during a GC until the
1199      * GC is finished.
1200      */
1201 #ifdef DEBUG
1202     if (t->what_next != prev_what_next) {
1203         debugTrace(DEBUG_sched,
1204                    "--<< thread %ld (%s) stopped to switch evaluators", 
1205                    (long)t->id, what_next_strs[t->what_next]);
1206     }
1207 #endif
1208     
1209     ASSERT(t->_link == END_TSO_QUEUE);
1210     
1211     // Shortcut if we're just switching evaluators: don't bother
1212     // doing stack squeezing (which can be expensive), just run the
1213     // thread.
1214     if (t->what_next != prev_what_next) {
1215         return rtsTrue;
1216     }
1217
1218     IF_DEBUG(sanity,
1219              //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1220              checkTSO(t));
1221
1222     addToRunQueue(cap,t);
1223
1224     return rtsFalse;
1225 }
1226
1227 /* -----------------------------------------------------------------------------
1228  * Handle a thread that returned to the scheduler with ThreadBlocked
1229  * -------------------------------------------------------------------------- */
1230
1231 static void
1232 scheduleHandleThreadBlocked( StgTSO *t
1233 #if !defined(DEBUG)
1234     STG_UNUSED
1235 #endif
1236     )
1237 {
1238
1239       // We don't need to do anything.  The thread is blocked, and it
1240       // has tidied up its stack and placed itself on whatever queue
1241       // it needs to be on.
1242
1243     // ASSERT(t->why_blocked != NotBlocked);
1244     // Not true: for example,
1245     //    - in THREADED_RTS, the thread may already have been woken
1246     //      up by another Capability.  This actually happens: try
1247     //      conc023 +RTS -N2.
1248     //    - the thread may have woken itself up already, because
1249     //      threadPaused() might have raised a blocked throwTo
1250     //      exception, see maybePerformBlockedException().
1251
1252 #ifdef DEBUG
1253     traceThreadStatus(DEBUG_sched, t);
1254 #endif
1255 }
1256
1257 /* -----------------------------------------------------------------------------
1258  * Handle a thread that returned to the scheduler with ThreadFinished
1259  * -------------------------------------------------------------------------- */
1260
1261 static rtsBool
1262 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1263 {
1264     /* Need to check whether this was a main thread, and if so,
1265      * return with the return value.
1266      *
1267      * We also end up here if the thread kills itself with an
1268      * uncaught exception, see Exception.cmm.
1269      */
1270
1271     // blocked exceptions can now complete, even if the thread was in
1272     // blocked mode (see #2910).  This unconditionally calls
1273     // lockTSO(), which ensures that we don't miss any threads that
1274     // are engaged in throwTo() with this thread as a target.
1275     awakenBlockedExceptionQueue (cap, t);
1276
1277       //
1278       // Check whether the thread that just completed was a bound
1279       // thread, and if so return with the result.  
1280       //
1281       // There is an assumption here that all thread completion goes
1282       // through this point; we need to make sure that if a thread
1283       // ends up in the ThreadKilled state, that it stays on the run
1284       // queue so it can be dealt with here.
1285       //
1286
1287       if (t->bound) {
1288
1289           if (t->bound != task) {
1290 #if !defined(THREADED_RTS)
1291               // Must be a bound thread that is not the topmost one.  Leave
1292               // it on the run queue until the stack has unwound to the
1293               // point where we can deal with this.  Leaving it on the run
1294               // queue also ensures that the garbage collector knows about
1295               // this thread and its return value (it gets dropped from the
1296               // step->threads list so there's no other way to find it).
1297               appendToRunQueue(cap,t);
1298               return rtsFalse;
1299 #else
1300               // this cannot happen in the threaded RTS, because a
1301               // bound thread can only be run by the appropriate Task.
1302               barf("finished bound thread that isn't mine");
1303 #endif
1304           }
1305
1306           ASSERT(task->tso == t);
1307
1308           if (t->what_next == ThreadComplete) {
1309               if (task->ret) {
1310                   // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1311                   *(task->ret) = (StgClosure *)task->tso->sp[1]; 
1312               }
1313               task->stat = Success;
1314           } else {
1315               if (task->ret) {
1316                   *(task->ret) = NULL;
1317               }
1318               if (sched_state >= SCHED_INTERRUPTING) {
1319                   if (heap_overflow) {
1320                       task->stat = HeapExhausted;
1321                   } else {
1322                       task->stat = Interrupted;
1323                   }
1324               } else {
1325                   task->stat = Killed;
1326               }
1327           }
1328 #ifdef DEBUG
1329           removeThreadLabel((StgWord)task->tso->id);
1330 #endif
1331           return rtsTrue; // tells schedule() to return
1332       }
1333
1334       return rtsFalse;
1335 }
1336
1337 /* -----------------------------------------------------------------------------
1338  * Perform a heap census
1339  * -------------------------------------------------------------------------- */
1340
1341 static rtsBool
1342 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1343 {
1344     // When we have +RTS -i0 and we're heap profiling, do a census at
1345     // every GC.  This lets us get repeatable runs for debugging.
1346     if (performHeapProfile ||
1347         (RtsFlags.ProfFlags.profileInterval==0 &&
1348          RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1349         return rtsTrue;
1350     } else {
1351         return rtsFalse;
1352     }
1353 }
1354
1355 /* -----------------------------------------------------------------------------
1356  * Perform a garbage collection if necessary
1357  * -------------------------------------------------------------------------- */
1358
1359 static Capability *
1360 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS, rtsBool force_major)
1361 {
1362     rtsBool heap_census;
1363 #ifdef THREADED_RTS
1364     /* extern static volatile StgWord waiting_for_gc; 
1365        lives inside capability.c */
1366     rtsBool gc_type, prev_pending_gc;
1367     nat i;
1368 #endif
1369
1370     if (sched_state == SCHED_SHUTTING_DOWN) {
1371         // The final GC has already been done, and the system is
1372         // shutting down.  We'll probably deadlock if we try to GC
1373         // now.
1374         return cap;
1375     }
1376
1377 #ifdef THREADED_RTS
1378     if (sched_state < SCHED_INTERRUPTING
1379         && RtsFlags.ParFlags.parGcEnabled
1380         && N >= RtsFlags.ParFlags.parGcGen
1381         && ! oldest_gen->steps[0].mark)
1382     {
1383         gc_type = PENDING_GC_PAR;
1384     } else {
1385         gc_type = PENDING_GC_SEQ;
1386     }
1387
1388     // In order to GC, there must be no threads running Haskell code.
1389     // Therefore, the GC thread needs to hold *all* the capabilities,
1390     // and release them after the GC has completed.  
1391     //
1392     // This seems to be the simplest way: previous attempts involved
1393     // making all the threads with capabilities give up their
1394     // capabilities and sleep except for the *last* one, which
1395     // actually did the GC.  But it's quite hard to arrange for all
1396     // the other tasks to sleep and stay asleep.
1397     //
1398
1399     /*  Other capabilities are prevented from running yet more Haskell
1400         threads if waiting_for_gc is set. Tested inside
1401         yieldCapability() and releaseCapability() in Capability.c */
1402
1403     prev_pending_gc = cas(&waiting_for_gc, 0, gc_type);
1404     if (prev_pending_gc) {
1405         do {
1406             debugTrace(DEBUG_sched, "someone else is trying to GC (%d)...", 
1407                        prev_pending_gc);
1408             ASSERT(cap);
1409             yieldCapability(&cap,task);
1410         } while (waiting_for_gc);
1411         return cap;  // NOTE: task->cap might have changed here
1412     }
1413
1414     setContextSwitches();
1415
1416     // The final shutdown GC is always single-threaded, because it's
1417     // possible that some of the Capabilities have no worker threads.
1418     
1419     if (gc_type == PENDING_GC_SEQ)
1420     {
1421         traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
1422     }
1423     else
1424     {
1425         traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
1426         debugTrace(DEBUG_sched, "ready_to_gc, grabbing GC threads");
1427     }
1428
1429     // do this while the other Capabilities stop:
1430     if (cap) scheduleCheckBlackHoles(cap);
1431
1432     if (gc_type == PENDING_GC_SEQ)
1433     {
1434         // single-threaded GC: grab all the capabilities
1435         for (i=0; i < n_capabilities; i++) {
1436             debugTrace(DEBUG_sched, "ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities);
1437             if (cap != &capabilities[i]) {
1438                 Capability *pcap = &capabilities[i];
1439                 // we better hope this task doesn't get migrated to
1440                 // another Capability while we're waiting for this one.
1441                 // It won't, because load balancing happens while we have
1442                 // all the Capabilities, but even so it's a slightly
1443                 // unsavoury invariant.
1444                 task->cap = pcap;
1445                 waitForReturnCapability(&pcap, task);
1446                 if (pcap != &capabilities[i]) {
1447                     barf("scheduleDoGC: got the wrong capability");
1448                 }
1449             }
1450         }
1451     }
1452     else
1453     {
1454         // multi-threaded GC: make sure all the Capabilities donate one
1455         // GC thread each.
1456         waitForGcThreads(cap);
1457     }
1458
1459 #else /* !THREADED_RTS */
1460
1461     // do this while the other Capabilities stop:
1462     if (cap) scheduleCheckBlackHoles(cap);
1463
1464 #endif
1465
1466     IF_DEBUG(scheduler, printAllThreads());
1467
1468 delete_threads_and_gc:
1469     /*
1470      * We now have all the capabilities; if we're in an interrupting
1471      * state, then we should take the opportunity to delete all the
1472      * threads in the system.
1473      */
1474     if (sched_state == SCHED_INTERRUPTING) {
1475         deleteAllThreads(cap);
1476         sched_state = SCHED_SHUTTING_DOWN;
1477     }
1478     
1479     heap_census = scheduleNeedHeapProfile(rtsTrue);
1480
1481 #if defined(THREADED_RTS)
1482     traceSchedEvent(cap, EVENT_GC_START, 0, 0);
1483     // reset waiting_for_gc *before* GC, so that when the GC threads
1484     // emerge they don't immediately re-enter the GC.
1485     waiting_for_gc = 0;
1486     GarbageCollect(force_major || heap_census, gc_type, cap);
1487 #else
1488     GarbageCollect(force_major || heap_census, 0, cap);
1489 #endif
1490     traceSchedEvent(cap, EVENT_GC_END, 0, 0);
1491
1492     if (recent_activity == ACTIVITY_INACTIVE && force_major)
1493     {
1494         // We are doing a GC because the system has been idle for a
1495         // timeslice and we need to check for deadlock.  Record the
1496         // fact that we've done a GC and turn off the timer signal;
1497         // it will get re-enabled if we run any threads after the GC.
1498         recent_activity = ACTIVITY_DONE_GC;
1499         stopTimer();
1500     }
1501     else
1502     {
1503         // the GC might have taken long enough for the timer to set
1504         // recent_activity = ACTIVITY_INACTIVE, but we aren't
1505         // necessarily deadlocked:
1506         recent_activity = ACTIVITY_YES;
1507     }
1508
1509 #if defined(THREADED_RTS)
1510     if (gc_type == PENDING_GC_PAR)
1511     {
1512         releaseGCThreads(cap);
1513     }
1514 #endif
1515
1516     if (heap_census) {
1517         debugTrace(DEBUG_sched, "performing heap census");
1518         heapCensus();
1519         performHeapProfile = rtsFalse;
1520     }
1521
1522     if (heap_overflow && sched_state < SCHED_INTERRUPTING) {
1523         // GC set the heap_overflow flag, so we should proceed with
1524         // an orderly shutdown now.  Ultimately we want the main
1525         // thread to return to its caller with HeapExhausted, at which
1526         // point the caller should call hs_exit().  The first step is
1527         // to delete all the threads.
1528         //
1529         // Another way to do this would be to raise an exception in
1530         // the main thread, which we really should do because it gives
1531         // the program a chance to clean up.  But how do we find the
1532         // main thread?  It should presumably be the same one that
1533         // gets ^C exceptions, but that's all done on the Haskell side
1534         // (GHC.TopHandler).
1535         sched_state = SCHED_INTERRUPTING;
1536         goto delete_threads_and_gc;
1537     }
1538
1539 #ifdef SPARKBALANCE
1540     /* JB 
1541        Once we are all together... this would be the place to balance all
1542        spark pools. No concurrent stealing or adding of new sparks can
1543        occur. Should be defined in Sparks.c. */
1544     balanceSparkPoolsCaps(n_capabilities, capabilities);
1545 #endif
1546
1547 #if defined(THREADED_RTS)
1548     if (gc_type == PENDING_GC_SEQ) {
1549         // release our stash of capabilities.
1550         for (i = 0; i < n_capabilities; i++) {
1551             if (cap != &capabilities[i]) {
1552                 task->cap = &capabilities[i];
1553                 releaseCapability(&capabilities[i]);
1554             }
1555         }
1556     }
1557     if (cap) {
1558         task->cap = cap;
1559     } else {
1560         task->cap = NULL;
1561     }
1562 #endif
1563
1564     return cap;
1565 }
1566
1567 /* ---------------------------------------------------------------------------
1568  * Singleton fork(). Do not copy any running threads.
1569  * ------------------------------------------------------------------------- */
1570
1571 pid_t
1572 forkProcess(HsStablePtr *entry
1573 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
1574             STG_UNUSED
1575 #endif
1576            )
1577 {
1578 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
1579     Task *task;
1580     pid_t pid;
1581     StgTSO* t,*next;
1582     Capability *cap;
1583     nat s;
1584     
1585 #if defined(THREADED_RTS)
1586     if (RtsFlags.ParFlags.nNodes > 1) {
1587         errorBelch("forking not supported with +RTS -N<n> greater than 1");
1588         stg_exit(EXIT_FAILURE);
1589     }
1590 #endif
1591
1592     debugTrace(DEBUG_sched, "forking!");
1593     
1594     // ToDo: for SMP, we should probably acquire *all* the capabilities
1595     cap = rts_lock();
1596     
1597     // no funny business: hold locks while we fork, otherwise if some
1598     // other thread is holding a lock when the fork happens, the data
1599     // structure protected by the lock will forever be in an
1600     // inconsistent state in the child.  See also #1391.
1601     ACQUIRE_LOCK(&sched_mutex);
1602     ACQUIRE_LOCK(&cap->lock);
1603     ACQUIRE_LOCK(&cap->running_task->lock);
1604
1605     pid = fork();
1606     
1607     if (pid) { // parent
1608         
1609         RELEASE_LOCK(&sched_mutex);
1610         RELEASE_LOCK(&cap->lock);
1611         RELEASE_LOCK(&cap->running_task->lock);
1612
1613         // just return the pid
1614         rts_unlock(cap);
1615         return pid;
1616         
1617     } else { // child
1618         
1619 #if defined(THREADED_RTS)
1620         initMutex(&sched_mutex);
1621         initMutex(&cap->lock);
1622         initMutex(&cap->running_task->lock);
1623 #endif
1624
1625         // Now, all OS threads except the thread that forked are
1626         // stopped.  We need to stop all Haskell threads, including
1627         // those involved in foreign calls.  Also we need to delete
1628         // all Tasks, because they correspond to OS threads that are
1629         // now gone.
1630
1631         for (s = 0; s < total_steps; s++) {
1632           for (t = all_steps[s].threads; t != END_TSO_QUEUE; t = next) {
1633             if (t->what_next == ThreadRelocated) {
1634                 next = t->_link;
1635             } else {
1636                 next = t->global_link;
1637                 // don't allow threads to catch the ThreadKilled
1638                 // exception, but we do want to raiseAsync() because these
1639                 // threads may be evaluating thunks that we need later.
1640                 deleteThread_(cap,t);
1641             }
1642           }
1643         }
1644         
1645         // Empty the run queue.  It seems tempting to let all the
1646         // killed threads stay on the run queue as zombies to be
1647         // cleaned up later, but some of them correspond to bound
1648         // threads for which the corresponding Task does not exist.
1649         cap->run_queue_hd = END_TSO_QUEUE;
1650         cap->run_queue_tl = END_TSO_QUEUE;
1651
1652         // Any suspended C-calling Tasks are no more, their OS threads
1653         // don't exist now:
1654         cap->suspended_ccalling_tasks = NULL;
1655
1656         // Empty the threads lists.  Otherwise, the garbage
1657         // collector may attempt to resurrect some of these threads.
1658         for (s = 0; s < total_steps; s++) {
1659             all_steps[s].threads = END_TSO_QUEUE;
1660         }
1661
1662         // Wipe the task list, except the current Task.
1663         ACQUIRE_LOCK(&sched_mutex);
1664         for (task = all_tasks; task != NULL; task=task->all_link) {
1665             if (task != cap->running_task) {
1666 #if defined(THREADED_RTS)
1667                 initMutex(&task->lock); // see #1391
1668 #endif
1669                 discardTask(task);
1670             }
1671         }
1672         RELEASE_LOCK(&sched_mutex);
1673
1674 #if defined(THREADED_RTS)
1675         // Wipe our spare workers list, they no longer exist.  New
1676         // workers will be created if necessary.
1677         cap->spare_workers = NULL;
1678         cap->returning_tasks_hd = NULL;
1679         cap->returning_tasks_tl = NULL;
1680 #endif
1681
1682         // On Unix, all timers are reset in the child, so we need to start
1683         // the timer again.
1684         initTimer();
1685         startTimer();
1686
1687 #if defined(THREADED_RTS)
1688         cap = ioManagerStartCap(cap);
1689 #endif
1690
1691         cap = rts_evalStableIO(cap, entry, NULL);  // run the action
1692         rts_checkSchedStatus("forkProcess",cap);
1693         
1694         rts_unlock(cap);
1695         hs_exit();                      // clean up and exit
1696         stg_exit(EXIT_SUCCESS);
1697     }
1698 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
1699     barf("forkProcess#: primop not supported on this platform, sorry!\n");
1700 #endif
1701 }
1702
1703 /* ---------------------------------------------------------------------------
1704  * Delete all the threads in the system
1705  * ------------------------------------------------------------------------- */
1706    
1707 static void
1708 deleteAllThreads ( Capability *cap )
1709 {
1710     // NOTE: only safe to call if we own all capabilities.
1711
1712     StgTSO* t, *next;
1713     nat s;
1714
1715     debugTrace(DEBUG_sched,"deleting all threads");
1716     for (s = 0; s < total_steps; s++) {
1717       for (t = all_steps[s].threads; t != END_TSO_QUEUE; t = next) {
1718         if (t->what_next == ThreadRelocated) {
1719             next = t->_link;
1720         } else {
1721             next = t->global_link;
1722             deleteThread(cap,t);
1723         }
1724       }
1725     }      
1726
1727     // The run queue now contains a bunch of ThreadKilled threads.  We
1728     // must not throw these away: the main thread(s) will be in there
1729     // somewhere, and the main scheduler loop has to deal with it.
1730     // Also, the run queue is the only thing keeping these threads from
1731     // being GC'd, and we don't want the "main thread has been GC'd" panic.
1732
1733 #if !defined(THREADED_RTS)
1734     ASSERT(blocked_queue_hd == END_TSO_QUEUE);
1735     ASSERT(sleeping_queue == END_TSO_QUEUE);
1736 #endif
1737 }
1738
1739 /* -----------------------------------------------------------------------------
1740    Managing the suspended_ccalling_tasks list.
1741    Locks required: sched_mutex
1742    -------------------------------------------------------------------------- */
1743
1744 STATIC_INLINE void
1745 suspendTask (Capability *cap, Task *task)
1746 {
1747     ASSERT(task->next == NULL && task->prev == NULL);
1748     task->next = cap->suspended_ccalling_tasks;
1749     task->prev = NULL;
1750     if (cap->suspended_ccalling_tasks) {
1751         cap->suspended_ccalling_tasks->prev = task;
1752     }
1753     cap->suspended_ccalling_tasks = task;
1754 }
1755
1756 STATIC_INLINE void
1757 recoverSuspendedTask (Capability *cap, Task *task)
1758 {
1759     if (task->prev) {
1760         task->prev->next = task->next;
1761     } else {
1762         ASSERT(cap->suspended_ccalling_tasks == task);
1763         cap->suspended_ccalling_tasks = task->next;
1764     }
1765     if (task->next) {
1766         task->next->prev = task->prev;
1767     }
1768     task->next = task->prev = NULL;
1769 }
1770
1771 /* ---------------------------------------------------------------------------
1772  * Suspending & resuming Haskell threads.
1773  * 
1774  * When making a "safe" call to C (aka _ccall_GC), the task gives back
1775  * its capability before calling the C function.  This allows another
1776  * task to pick up the capability and carry on running Haskell
1777  * threads.  It also means that if the C call blocks, it won't lock
1778  * the whole system.
1779  *
1780  * The Haskell thread making the C call is put to sleep for the
1781  * duration of the call, on the susepended_ccalling_threads queue.  We
1782  * give out a token to the task, which it can use to resume the thread
1783  * on return from the C function.
1784  * ------------------------------------------------------------------------- */
1785    
1786 void *
1787 suspendThread (StgRegTable *reg)
1788 {
1789   Capability *cap;
1790   int saved_errno;
1791   StgTSO *tso;
1792   Task *task;
1793 #if mingw32_HOST_OS
1794   StgWord32 saved_winerror;
1795 #endif
1796
1797   saved_errno = errno;
1798 #if mingw32_HOST_OS
1799   saved_winerror = GetLastError();
1800 #endif
1801
1802   /* assume that *reg is a pointer to the StgRegTable part of a Capability.
1803    */
1804   cap = regTableToCapability(reg);
1805
1806   task = cap->running_task;
1807   tso = cap->r.rCurrentTSO;
1808
1809   traceSchedEvent(cap, EVENT_STOP_THREAD, tso, THREAD_SUSPENDED_FOREIGN_CALL);
1810
1811   // XXX this might not be necessary --SDM
1812   tso->what_next = ThreadRunGHC;
1813
1814   threadPaused(cap,tso);
1815
1816   if ((tso->flags & TSO_BLOCKEX) == 0)  {
1817       tso->why_blocked = BlockedOnCCall;
1818       tso->flags |= TSO_BLOCKEX;
1819       tso->flags &= ~TSO_INTERRUPTIBLE;
1820   } else {
1821       tso->why_blocked = BlockedOnCCall_NoUnblockExc;
1822   }
1823
1824   // Hand back capability
1825   task->suspended_tso = tso;
1826
1827   ACQUIRE_LOCK(&cap->lock);
1828
1829   suspendTask(cap,task);
1830   cap->in_haskell = rtsFalse;
1831   releaseCapability_(cap,rtsFalse);
1832   
1833   RELEASE_LOCK(&cap->lock);
1834
1835   errno = saved_errno;
1836 #if mingw32_HOST_OS
1837   SetLastError(saved_winerror);
1838 #endif
1839   return task;
1840 }
1841
1842 StgRegTable *
1843 resumeThread (void *task_)
1844 {
1845     StgTSO *tso;
1846     Capability *cap;
1847     Task *task = task_;
1848     int saved_errno;
1849 #if mingw32_HOST_OS
1850     StgWord32 saved_winerror;
1851 #endif
1852
1853     saved_errno = errno;
1854 #if mingw32_HOST_OS
1855     saved_winerror = GetLastError();
1856 #endif
1857
1858     cap = task->cap;
1859     // Wait for permission to re-enter the RTS with the result.
1860     waitForReturnCapability(&cap,task);
1861     // we might be on a different capability now... but if so, our
1862     // entry on the suspended_ccalling_tasks list will also have been
1863     // migrated.
1864
1865     // Remove the thread from the suspended list
1866     recoverSuspendedTask(cap,task);
1867
1868     tso = task->suspended_tso;
1869     task->suspended_tso = NULL;
1870     tso->_link = END_TSO_QUEUE; // no write barrier reqd
1871
1872     traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
1873     
1874     if (tso->why_blocked == BlockedOnCCall) {
1875         // avoid locking the TSO if we don't have to
1876         if (tso->blocked_exceptions != END_TSO_QUEUE) {
1877             awakenBlockedExceptionQueue(cap,tso);
1878         }
1879         tso->flags &= ~(TSO_BLOCKEX | TSO_INTERRUPTIBLE);
1880     }
1881     
1882     /* Reset blocking status */
1883     tso->why_blocked  = NotBlocked;
1884     
1885     cap->r.rCurrentTSO = tso;
1886     cap->in_haskell = rtsTrue;
1887     errno = saved_errno;
1888 #if mingw32_HOST_OS
1889     SetLastError(saved_winerror);
1890 #endif
1891
1892     /* We might have GC'd, mark the TSO dirty again */
1893     dirty_TSO(cap,tso);
1894
1895     IF_DEBUG(sanity, checkTSO(tso));
1896
1897     return &cap->r;
1898 }
1899
1900 /* ---------------------------------------------------------------------------
1901  * scheduleThread()
1902  *
1903  * scheduleThread puts a thread on the end  of the runnable queue.
1904  * This will usually be done immediately after a thread is created.
1905  * The caller of scheduleThread must create the thread using e.g.
1906  * createThread and push an appropriate closure
1907  * on this thread's stack before the scheduler is invoked.
1908  * ------------------------------------------------------------------------ */
1909
1910 void
1911 scheduleThread(Capability *cap, StgTSO *tso)
1912 {
1913     // The thread goes at the *end* of the run-queue, to avoid possible
1914     // starvation of any threads already on the queue.
1915     appendToRunQueue(cap,tso);
1916 }
1917
1918 void
1919 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
1920 {
1921 #if defined(THREADED_RTS)
1922     tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
1923                               // move this thread from now on.
1924     cpu %= RtsFlags.ParFlags.nNodes;
1925     if (cpu == cap->no) {
1926         appendToRunQueue(cap,tso);
1927     } else {
1928         traceSchedEvent (cap, EVENT_MIGRATE_THREAD, tso, capabilities[cpu].no);
1929         wakeupThreadOnCapability(cap, &capabilities[cpu], tso);
1930     }
1931 #else
1932     appendToRunQueue(cap,tso);
1933 #endif
1934 }
1935
1936 Capability *
1937 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
1938 {
1939     Task *task;
1940
1941     // We already created/initialised the Task
1942     task = cap->running_task;
1943
1944     // This TSO is now a bound thread; make the Task and TSO
1945     // point to each other.
1946     tso->bound = task;
1947     tso->cap = cap;
1948
1949     task->tso = tso;
1950     task->ret = ret;
1951     task->stat = NoStatus;
1952
1953     appendToRunQueue(cap,tso);
1954
1955     debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)tso->id);
1956
1957     cap = schedule(cap,task);
1958
1959     ASSERT(task->stat != NoStatus);
1960     ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
1961
1962     debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)task->tso->id);
1963     return cap;
1964 }
1965
1966 /* ----------------------------------------------------------------------------
1967  * Starting Tasks
1968  * ------------------------------------------------------------------------- */
1969
1970 #if defined(THREADED_RTS)
1971 void OSThreadProcAttr
1972 workerStart(Task *task)
1973 {
1974     Capability *cap;
1975
1976     // See startWorkerTask().
1977     ACQUIRE_LOCK(&task->lock);
1978     cap = task->cap;
1979     RELEASE_LOCK(&task->lock);
1980
1981     if (RtsFlags.ParFlags.setAffinity) {
1982         setThreadAffinity(cap->no, n_capabilities);
1983     }
1984
1985     // set the thread-local pointer to the Task:
1986     taskEnter(task);
1987
1988     // schedule() runs without a lock.
1989     cap = schedule(cap,task);
1990
1991     // On exit from schedule(), we have a Capability, but possibly not
1992     // the same one we started with.
1993
1994     // During shutdown, the requirement is that after all the
1995     // Capabilities are shut down, all workers that are shutting down
1996     // have finished workerTaskStop().  This is why we hold on to
1997     // cap->lock until we've finished workerTaskStop() below.
1998     //
1999     // There may be workers still involved in foreign calls; those
2000     // will just block in waitForReturnCapability() because the
2001     // Capability has been shut down.
2002     //
2003     ACQUIRE_LOCK(&cap->lock);
2004     releaseCapability_(cap,rtsFalse);
2005     workerTaskStop(task);
2006     RELEASE_LOCK(&cap->lock);
2007 }
2008 #endif
2009
2010 /* ---------------------------------------------------------------------------
2011  * initScheduler()
2012  *
2013  * Initialise the scheduler.  This resets all the queues - if the
2014  * queues contained any threads, they'll be garbage collected at the
2015  * next pass.
2016  *
2017  * ------------------------------------------------------------------------ */
2018
2019 void 
2020 initScheduler(void)
2021 {
2022 #if !defined(THREADED_RTS)
2023   blocked_queue_hd  = END_TSO_QUEUE;
2024   blocked_queue_tl  = END_TSO_QUEUE;
2025   sleeping_queue    = END_TSO_QUEUE;
2026 #endif
2027
2028   blackhole_queue   = END_TSO_QUEUE;
2029
2030   sched_state    = SCHED_RUNNING;
2031   recent_activity = ACTIVITY_YES;
2032
2033 #if defined(THREADED_RTS)
2034   /* Initialise the mutex and condition variables used by
2035    * the scheduler. */
2036   initMutex(&sched_mutex);
2037 #endif
2038   
2039   ACQUIRE_LOCK(&sched_mutex);
2040
2041   /* A capability holds the state a native thread needs in
2042    * order to execute STG code. At least one capability is
2043    * floating around (only THREADED_RTS builds have more than one).
2044    */
2045   initCapabilities();
2046
2047   initTaskManager();
2048
2049 #if defined(THREADED_RTS)
2050   initSparkPools();
2051 #endif
2052
2053 #if defined(THREADED_RTS)
2054   /*
2055    * Eagerly start one worker to run each Capability, except for
2056    * Capability 0.  The idea is that we're probably going to start a
2057    * bound thread on Capability 0 pretty soon, so we don't want a
2058    * worker task hogging it.
2059    */
2060   { 
2061       nat i;
2062       Capability *cap;
2063       for (i = 1; i < n_capabilities; i++) {
2064           cap = &capabilities[i];
2065           ACQUIRE_LOCK(&cap->lock);
2066           startWorkerTask(cap, workerStart);
2067           RELEASE_LOCK(&cap->lock);
2068       }
2069   }
2070 #endif
2071
2072   RELEASE_LOCK(&sched_mutex);
2073 }
2074
2075 void
2076 exitScheduler(
2077     rtsBool wait_foreign
2078 #if !defined(THREADED_RTS)
2079                          __attribute__((unused))
2080 #endif
2081 )
2082                /* see Capability.c, shutdownCapability() */
2083 {
2084     Task *task = NULL;
2085
2086     task = newBoundTask();
2087
2088     // If we haven't killed all the threads yet, do it now.
2089     if (sched_state < SCHED_SHUTTING_DOWN) {
2090         sched_state = SCHED_INTERRUPTING;
2091         waitForReturnCapability(&task->cap,task);
2092         scheduleDoGC(task->cap,task,rtsFalse);    
2093         releaseCapability(task->cap);
2094     }
2095     sched_state = SCHED_SHUTTING_DOWN;
2096
2097 #if defined(THREADED_RTS)
2098     { 
2099         nat i;
2100         
2101         for (i = 0; i < n_capabilities; i++) {
2102             shutdownCapability(&capabilities[i], task, wait_foreign);
2103         }
2104     }
2105 #endif
2106
2107     boundTaskExiting(task);
2108 }
2109
2110 void
2111 freeScheduler( void )
2112 {
2113     nat still_running;
2114
2115     ACQUIRE_LOCK(&sched_mutex);
2116     still_running = freeTaskManager();
2117     // We can only free the Capabilities if there are no Tasks still
2118     // running.  We might have a Task about to return from a foreign
2119     // call into waitForReturnCapability(), for example (actually,
2120     // this should be the *only* thing that a still-running Task can
2121     // do at this point, and it will block waiting for the
2122     // Capability).
2123     if (still_running == 0) {
2124         freeCapabilities();
2125         if (n_capabilities != 1) {
2126             stgFree(capabilities);
2127         }
2128     }
2129     RELEASE_LOCK(&sched_mutex);
2130 #if defined(THREADED_RTS)
2131     closeMutex(&sched_mutex);
2132 #endif
2133 }
2134
2135 /* -----------------------------------------------------------------------------
2136    performGC
2137
2138    This is the interface to the garbage collector from Haskell land.
2139    We provide this so that external C code can allocate and garbage
2140    collect when called from Haskell via _ccall_GC.
2141    -------------------------------------------------------------------------- */
2142
2143 static void
2144 performGC_(rtsBool force_major)
2145 {
2146     Task *task;
2147
2148     // We must grab a new Task here, because the existing Task may be
2149     // associated with a particular Capability, and chained onto the 
2150     // suspended_ccalling_tasks queue.
2151     task = newBoundTask();
2152
2153     waitForReturnCapability(&task->cap,task);
2154     scheduleDoGC(task->cap,task,force_major);
2155     releaseCapability(task->cap);
2156     boundTaskExiting(task);
2157 }
2158
2159 void
2160 performGC(void)
2161 {
2162     performGC_(rtsFalse);
2163 }
2164
2165 void
2166 performMajorGC(void)
2167 {
2168     performGC_(rtsTrue);
2169 }
2170
2171 /* -----------------------------------------------------------------------------
2172    Stack overflow
2173
2174    If the thread has reached its maximum stack size, then raise the
2175    StackOverflow exception in the offending thread.  Otherwise
2176    relocate the TSO into a larger chunk of memory and adjust its stack
2177    size appropriately.
2178    -------------------------------------------------------------------------- */
2179
2180 static StgTSO *
2181 threadStackOverflow(Capability *cap, StgTSO *tso)
2182 {
2183   nat new_stack_size, stack_words;
2184   lnat new_tso_size;
2185   StgPtr new_sp;
2186   StgTSO *dest;
2187
2188   IF_DEBUG(sanity,checkTSO(tso));
2189
2190   // don't allow throwTo() to modify the blocked_exceptions queue
2191   // while we are moving the TSO:
2192   lockClosure((StgClosure *)tso);
2193
2194   if (tso->stack_size >= tso->max_stack_size
2195       && !(tso->flags & TSO_BLOCKEX)) {
2196       // NB. never raise a StackOverflow exception if the thread is
2197       // inside Control.Exceptino.block.  It is impractical to protect
2198       // against stack overflow exceptions, since virtually anything
2199       // can raise one (even 'catch'), so this is the only sensible
2200       // thing to do here.  See bug #767.
2201       //
2202
2203       if (tso->flags & TSO_SQUEEZED) {
2204           return tso;
2205       }
2206       // #3677: In a stack overflow situation, stack squeezing may
2207       // reduce the stack size, but we don't know whether it has been
2208       // reduced enough for the stack check to succeed if we try
2209       // again.  Fortunately stack squeezing is idempotent, so all we
2210       // need to do is record whether *any* squeezing happened.  If we
2211       // are at the stack's absolute -K limit, and stack squeezing
2212       // happened, then we try running the thread again.  The
2213       // TSO_SQUEEZED flag is set by threadPaused() to tell us whether
2214       // squeezing happened or not.
2215
2216       debugTrace(DEBUG_gc,
2217                  "threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)",
2218                  (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
2219       IF_DEBUG(gc,
2220                /* If we're debugging, just print out the top of the stack */
2221                printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size, 
2222                                                 tso->sp+64)));
2223
2224       // Send this thread the StackOverflow exception
2225       unlockTSO(tso);
2226       throwToSingleThreaded(cap, tso, (StgClosure *)stackOverflow_closure);
2227       return tso;
2228   }
2229
2230
2231   // We also want to avoid enlarging the stack if squeezing has
2232   // already released some of it.  However, we don't want to get into
2233   // a pathalogical situation where a thread has a nearly full stack
2234   // (near its current limit, but not near the absolute -K limit),
2235   // keeps allocating a little bit, squeezing removes a little bit,
2236   // and then it runs again.  So to avoid this, if we squeezed *and*
2237   // there is still less than BLOCK_SIZE_W words free, then we enlarge
2238   // the stack anyway.
2239   if ((tso->flags & TSO_SQUEEZED) && 
2240       ((W_)(tso->sp - tso->stack) >= BLOCK_SIZE_W)) {
2241       unlockTSO(tso);
2242       return tso;
2243   }
2244
2245   /* Try to double the current stack size.  If that takes us over the
2246    * maximum stack size for this thread, then use the maximum instead
2247    * (that is, unless we're already at or over the max size and we
2248    * can't raise the StackOverflow exception (see above), in which
2249    * case just double the size). Finally round up so the TSO ends up as
2250    * a whole number of blocks.
2251    */
2252   if (tso->stack_size >= tso->max_stack_size) {
2253       new_stack_size = tso->stack_size * 2;
2254   } else { 
2255       new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2256   }
2257   new_tso_size   = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) + 
2258                                        TSO_STRUCT_SIZE)/sizeof(W_);
2259   new_tso_size = round_to_mblocks(new_tso_size);  /* Be MBLOCK-friendly */
2260   new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2261
2262   debugTrace(DEBUG_sched, 
2263              "increasing stack size from %ld words to %d.",
2264              (long)tso->stack_size, new_stack_size);
2265
2266   dest = (StgTSO *)allocate(cap,new_tso_size);
2267   TICK_ALLOC_TSO(new_stack_size,0);
2268
2269   /* copy the TSO block and the old stack into the new area */
2270   memcpy(dest,tso,TSO_STRUCT_SIZE);
2271   stack_words = tso->stack + tso->stack_size - tso->sp;
2272   new_sp = (P_)dest + new_tso_size - stack_words;
2273   memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2274
2275   /* relocate the stack pointers... */
2276   dest->sp         = new_sp;
2277   dest->stack_size = new_stack_size;
2278         
2279   /* Mark the old TSO as relocated.  We have to check for relocated
2280    * TSOs in the garbage collector and any primops that deal with TSOs.
2281    *
2282    * It's important to set the sp value to just beyond the end
2283    * of the stack, so we don't attempt to scavenge any part of the
2284    * dead TSO's stack.
2285    */
2286   tso->what_next = ThreadRelocated;
2287   setTSOLink(cap,tso,dest);
2288   tso->sp = (P_)&(tso->stack[tso->stack_size]);
2289   tso->why_blocked = NotBlocked;
2290
2291   unlockTSO(dest);
2292   unlockTSO(tso);
2293
2294   IF_DEBUG(sanity,checkTSO(dest));
2295 #if 0
2296   IF_DEBUG(scheduler,printTSO(dest));
2297 #endif
2298
2299   return dest;
2300 }
2301
2302 static StgTSO *
2303 threadStackUnderflow (Capability *cap, Task *task, StgTSO *tso)
2304 {
2305     bdescr *bd, *new_bd;
2306     lnat free_w, tso_size_w;
2307     StgTSO *new_tso;
2308
2309     tso_size_w = tso_sizeW(tso);
2310
2311     if (tso_size_w < MBLOCK_SIZE_W ||
2312           // TSO is less than 2 mblocks (since the first mblock is
2313           // shorter than MBLOCK_SIZE_W)
2314         (tso_size_w - BLOCKS_PER_MBLOCK*BLOCK_SIZE_W) % MBLOCK_SIZE_W != 0 ||
2315           // or TSO is not a whole number of megablocks (ensuring
2316           // precondition of splitLargeBlock() below)
2317         (tso_size_w <= round_up_to_mblocks(RtsFlags.GcFlags.initialStkSize)) ||
2318           // or TSO is smaller than the minimum stack size (rounded up)
2319         (nat)(tso->stack + tso->stack_size - tso->sp) > tso->stack_size / 4) 
2320           // or stack is using more than 1/4 of the available space
2321     {
2322         // then do nothing
2323         return tso;
2324     }
2325
2326     // don't allow throwTo() to modify the blocked_exceptions queue
2327     // while we are moving the TSO:
2328     lockClosure((StgClosure *)tso);
2329
2330     // this is the number of words we'll free
2331     free_w = round_to_mblocks(tso_size_w/2);
2332
2333     bd = Bdescr((StgPtr)tso);
2334     new_bd = splitLargeBlock(bd, free_w / BLOCK_SIZE_W);
2335     bd->free = bd->start + TSO_STRUCT_SIZEW;
2336
2337     new_tso = (StgTSO *)new_bd->start;
2338     memcpy(new_tso,tso,TSO_STRUCT_SIZE);
2339     new_tso->stack_size = new_bd->free - new_tso->stack;
2340
2341     // The original TSO was dirty and probably on the mutable
2342     // list. The new TSO is not yet on the mutable list, so we better
2343     // put it there.
2344     new_tso->dirty = 0;
2345     new_tso->flags &= !TSO_LINK_DIRTY;
2346     dirty_TSO(cap, new_tso);
2347
2348     debugTrace(DEBUG_sched, "thread %ld: reducing TSO size from %lu words to %lu",
2349                (long)tso->id, tso_size_w, tso_sizeW(new_tso));
2350
2351     tso->what_next = ThreadRelocated;
2352     tso->_link = new_tso; // no write barrier reqd: same generation
2353
2354     // The TSO attached to this Task may have moved, so update the
2355     // pointer to it.
2356     if (task->tso == tso) {
2357         task->tso = new_tso;
2358     }
2359
2360     unlockTSO(new_tso);
2361     unlockTSO(tso);
2362
2363     IF_DEBUG(sanity,checkTSO(new_tso));
2364
2365     return new_tso;
2366 }
2367
2368 /* ---------------------------------------------------------------------------
2369    Interrupt execution
2370    - usually called inside a signal handler so it mustn't do anything fancy.   
2371    ------------------------------------------------------------------------ */
2372
2373 void
2374 interruptStgRts(void)
2375 {
2376     sched_state = SCHED_INTERRUPTING;
2377     setContextSwitches();
2378 #if defined(THREADED_RTS)
2379     wakeUpRts();
2380 #endif
2381 }
2382
2383 /* -----------------------------------------------------------------------------
2384    Wake up the RTS
2385    
2386    This function causes at least one OS thread to wake up and run the
2387    scheduler loop.  It is invoked when the RTS might be deadlocked, or
2388    an external event has arrived that may need servicing (eg. a
2389    keyboard interrupt).
2390
2391    In the single-threaded RTS we don't do anything here; we only have
2392    one thread anyway, and the event that caused us to want to wake up
2393    will have interrupted any blocking system call in progress anyway.
2394    -------------------------------------------------------------------------- */
2395
2396 #if defined(THREADED_RTS)
2397 void wakeUpRts(void)
2398 {
2399     // This forces the IO Manager thread to wakeup, which will
2400     // in turn ensure that some OS thread wakes up and runs the
2401     // scheduler loop, which will cause a GC and deadlock check.
2402     ioManagerWakeup();
2403 }
2404 #endif
2405
2406 /* -----------------------------------------------------------------------------
2407  * checkBlackHoles()
2408  *
2409  * Check the blackhole_queue for threads that can be woken up.  We do
2410  * this periodically: before every GC, and whenever the run queue is
2411  * empty.
2412  *
2413  * An elegant solution might be to just wake up all the blocked
2414  * threads with awakenBlockedQueue occasionally: they'll go back to
2415  * sleep again if the object is still a BLACKHOLE.  Unfortunately this
2416  * doesn't give us a way to tell whether we've actually managed to
2417  * wake up any threads, so we would be busy-waiting.
2418  *
2419  * -------------------------------------------------------------------------- */
2420
2421 static rtsBool
2422 checkBlackHoles (Capability *cap)
2423 {
2424     StgTSO **prev, *t;
2425     rtsBool any_woke_up = rtsFalse;
2426     StgHalfWord type;
2427
2428     // blackhole_queue is global:
2429     ASSERT_LOCK_HELD(&sched_mutex);
2430
2431     debugTrace(DEBUG_sched, "checking threads blocked on black holes");
2432
2433     // ASSUMES: sched_mutex
2434     prev = &blackhole_queue;
2435     t = blackhole_queue;
2436     while (t != END_TSO_QUEUE) {
2437         if (t->what_next == ThreadRelocated) {
2438             t = t->_link;
2439             continue;
2440         }
2441         ASSERT(t->why_blocked == BlockedOnBlackHole);
2442         type = get_itbl(UNTAG_CLOSURE(t->block_info.closure))->type;
2443         if (type != BLACKHOLE && type != CAF_BLACKHOLE) {
2444             IF_DEBUG(sanity,checkTSO(t));
2445             t = unblockOne(cap, t);
2446             *prev = t;
2447             any_woke_up = rtsTrue;
2448         } else {
2449             prev = &t->_link;
2450             t = t->_link;
2451         }
2452     }
2453
2454     return any_woke_up;
2455 }
2456
2457 /* -----------------------------------------------------------------------------
2458    Deleting threads
2459
2460    This is used for interruption (^C) and forking, and corresponds to
2461    raising an exception but without letting the thread catch the
2462    exception.
2463    -------------------------------------------------------------------------- */
2464
2465 static void 
2466 deleteThread (Capability *cap, StgTSO *tso)
2467 {
2468     // NOTE: must only be called on a TSO that we have exclusive
2469     // access to, because we will call throwToSingleThreaded() below.
2470     // The TSO must be on the run queue of the Capability we own, or 
2471     // we must own all Capabilities.
2472
2473     if (tso->why_blocked != BlockedOnCCall &&
2474         tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
2475         throwToSingleThreaded(cap,tso,NULL);
2476     }
2477 }
2478
2479 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2480 static void 
2481 deleteThread_(Capability *cap, StgTSO *tso)
2482 { // for forkProcess only:
2483   // like deleteThread(), but we delete threads in foreign calls, too.
2484
2485     if (tso->why_blocked == BlockedOnCCall ||
2486         tso->why_blocked == BlockedOnCCall_NoUnblockExc) {
2487         unblockOne(cap,tso);
2488         tso->what_next = ThreadKilled;
2489     } else {
2490         deleteThread(cap,tso);
2491     }
2492 }
2493 #endif
2494
2495 /* -----------------------------------------------------------------------------
2496    raiseExceptionHelper
2497    
2498    This function is called by the raise# primitve, just so that we can
2499    move some of the tricky bits of raising an exception from C-- into
2500    C.  Who knows, it might be a useful re-useable thing here too.
2501    -------------------------------------------------------------------------- */
2502
2503 StgWord
2504 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2505 {
2506     Capability *cap = regTableToCapability(reg);
2507     StgThunk *raise_closure = NULL;
2508     StgPtr p, next;
2509     StgRetInfoTable *info;
2510     //
2511     // This closure represents the expression 'raise# E' where E
2512     // is the exception raise.  It is used to overwrite all the
2513     // thunks which are currently under evaluataion.
2514     //
2515
2516     // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2517     // LDV profiling: stg_raise_info has THUNK as its closure
2518     // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2519     // payload, MIN_UPD_SIZE is more approprate than 1.  It seems that
2520     // 1 does not cause any problem unless profiling is performed.
2521     // However, when LDV profiling goes on, we need to linearly scan
2522     // small object pool, where raise_closure is stored, so we should
2523     // use MIN_UPD_SIZE.
2524     //
2525     // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2526     //                                 sizeofW(StgClosure)+1);
2527     //
2528
2529     //
2530     // Walk up the stack, looking for the catch frame.  On the way,
2531     // we update any closures pointed to from update frames with the
2532     // raise closure that we just built.
2533     //
2534     p = tso->sp;
2535     while(1) {
2536         info = get_ret_itbl((StgClosure *)p);
2537         next = p + stack_frame_sizeW((StgClosure *)p);
2538         switch (info->i.type) {
2539             
2540         case UPDATE_FRAME:
2541             // Only create raise_closure if we need to.
2542             if (raise_closure == NULL) {
2543                 raise_closure = 
2544                     (StgThunk *)allocate(cap,sizeofW(StgThunk)+1);
2545                 SET_HDR(raise_closure, &stg_raise_info, CCCS);
2546                 raise_closure->payload[0] = exception;
2547             }
2548             UPD_IND(((StgUpdateFrame *)p)->updatee,(StgClosure *)raise_closure);
2549             p = next;
2550             continue;
2551
2552         case ATOMICALLY_FRAME:
2553             debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
2554             tso->sp = p;
2555             return ATOMICALLY_FRAME;
2556             
2557         case CATCH_FRAME:
2558             tso->sp = p;
2559             return CATCH_FRAME;
2560
2561         case CATCH_STM_FRAME:
2562             debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
2563             tso->sp = p;
2564             return CATCH_STM_FRAME;
2565             
2566         case STOP_FRAME:
2567             tso->sp = p;
2568             return STOP_FRAME;
2569
2570         case CATCH_RETRY_FRAME:
2571         default:
2572             p = next; 
2573             continue;
2574         }
2575     }
2576 }
2577
2578
2579 /* -----------------------------------------------------------------------------
2580    findRetryFrameHelper
2581
2582    This function is called by the retry# primitive.  It traverses the stack
2583    leaving tso->sp referring to the frame which should handle the retry.  
2584
2585    This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#) 
2586    or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).  
2587
2588    We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
2589    create) because retries are not considered to be exceptions, despite the
2590    similar implementation.
2591
2592    We should not expect to see CATCH_FRAME or STOP_FRAME because those should
2593    not be created within memory transactions.
2594    -------------------------------------------------------------------------- */
2595
2596 StgWord
2597 findRetryFrameHelper (StgTSO *tso)
2598 {
2599   StgPtr           p, next;
2600   StgRetInfoTable *info;
2601
2602   p = tso -> sp;
2603   while (1) {
2604     info = get_ret_itbl((StgClosure *)p);
2605     next = p + stack_frame_sizeW((StgClosure *)p);
2606     switch (info->i.type) {
2607       
2608     case ATOMICALLY_FRAME:
2609         debugTrace(DEBUG_stm,
2610                    "found ATOMICALLY_FRAME at %p during retry", p);
2611         tso->sp = p;
2612         return ATOMICALLY_FRAME;
2613       
2614     case CATCH_RETRY_FRAME:
2615         debugTrace(DEBUG_stm,
2616                    "found CATCH_RETRY_FRAME at %p during retrry", p);
2617         tso->sp = p;
2618         return CATCH_RETRY_FRAME;
2619       
2620     case CATCH_STM_FRAME: {
2621         StgTRecHeader *trec = tso -> trec;
2622         StgTRecHeader *outer = trec -> enclosing_trec;
2623         debugTrace(DEBUG_stm,
2624                    "found CATCH_STM_FRAME at %p during retry", p);
2625         debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
2626         stmAbortTransaction(tso -> cap, trec);
2627         stmFreeAbortedTRec(tso -> cap, trec);
2628         tso -> trec = outer;
2629         p = next; 
2630         continue;
2631     }
2632       
2633
2634     default:
2635       ASSERT(info->i.type != CATCH_FRAME);
2636       ASSERT(info->i.type != STOP_FRAME);
2637       p = next; 
2638       continue;
2639     }
2640   }
2641 }
2642
2643 /* -----------------------------------------------------------------------------
2644    resurrectThreads is called after garbage collection on the list of
2645    threads found to be garbage.  Each of these threads will be woken
2646    up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2647    on an MVar, or NonTermination if the thread was blocked on a Black
2648    Hole.
2649
2650    Locks: assumes we hold *all* the capabilities.
2651    -------------------------------------------------------------------------- */
2652
2653 void
2654 resurrectThreads (StgTSO *threads)
2655 {
2656     StgTSO *tso, *next;
2657     Capability *cap;
2658     step *step;
2659
2660     for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2661         next = tso->global_link;
2662
2663         step = Bdescr((P_)tso)->step;
2664         tso->global_link = step->threads;
2665         step->threads = tso;
2666
2667         debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
2668         
2669         // Wake up the thread on the Capability it was last on
2670         cap = tso->cap;
2671         
2672         switch (tso->why_blocked) {
2673         case BlockedOnMVar:
2674             /* Called by GC - sched_mutex lock is currently held. */
2675             throwToSingleThreaded(cap, tso,
2676                                   (StgClosure *)blockedIndefinitelyOnMVar_closure);
2677             break;
2678         case BlockedOnBlackHole:
2679             throwToSingleThreaded(cap, tso,
2680                                   (StgClosure *)nonTermination_closure);
2681             break;
2682         case BlockedOnSTM:
2683             throwToSingleThreaded(cap, tso,
2684                                   (StgClosure *)blockedIndefinitelyOnSTM_closure);
2685             break;
2686         case NotBlocked:
2687             /* This might happen if the thread was blocked on a black hole
2688              * belonging to a thread that we've just woken up (raiseAsync
2689              * can wake up threads, remember...).
2690              */
2691             continue;
2692         case BlockedOnException:
2693             // throwTo should never block indefinitely: if the target
2694             // thread dies or completes, throwTo returns.
2695             barf("resurrectThreads: thread BlockedOnException");
2696             break;
2697         default:
2698             barf("resurrectThreads: thread blocked in a strange way");
2699         }
2700     }
2701 }
2702
2703 /* -----------------------------------------------------------------------------
2704    performPendingThrowTos is called after garbage collection, and
2705    passed a list of threads that were found to have pending throwTos
2706    (tso->blocked_exceptions was not empty), and were blocked.
2707    Normally this doesn't happen, because we would deliver the
2708    exception directly if the target thread is blocked, but there are
2709    small windows where it might occur on a multiprocessor (see
2710    throwTo()).
2711
2712    NB. we must be holding all the capabilities at this point, just
2713    like resurrectThreads().
2714    -------------------------------------------------------------------------- */
2715
2716 void
2717 performPendingThrowTos (StgTSO *threads)
2718 {
2719     StgTSO *tso, *next;
2720     Capability *cap;
2721     Task *task, *saved_task;;
2722     step *step;
2723
2724     task = myTask();
2725     cap = task->cap;
2726
2727     for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2728         next = tso->global_link;
2729
2730         step = Bdescr((P_)tso)->step;
2731         tso->global_link = step->threads;
2732         step->threads = tso;
2733
2734         debugTrace(DEBUG_sched, "performing blocked throwTo to thread %lu", (unsigned long)tso->id);
2735         
2736         // We must pretend this Capability belongs to the current Task
2737         // for the time being, as invariants will be broken otherwise.
2738         // In fact the current Task has exclusive access to the systme
2739         // at this point, so this is just bookkeeping:
2740         task->cap = tso->cap;
2741         saved_task = tso->cap->running_task;
2742         tso->cap->running_task = task;
2743         maybePerformBlockedException(tso->cap, tso);
2744         tso->cap->running_task = saved_task;
2745     }
2746
2747     // Restore our original Capability:
2748     task->cap = cap;
2749 }