1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2006
5 * The scheduler and thread-related functionality
7 * --------------------------------------------------------------------------*/
9 #include "PosixSource.h"
10 #define KEEP_LOCKCLOSURE
13 #include "sm/Storage.h"
17 #include "Interpreter.h"
19 #include "RtsSignals.h"
20 #include "sm/Sanity.h"
24 #include "ThreadLabels.h"
26 #include "Proftimer.h"
29 #include "sm/GC.h" // waitForGcThreads, releaseGCThreads, N
31 #include "Capability.h"
33 #include "AwaitEvent.h"
34 #if defined(mingw32_HOST_OS)
35 #include "win32/IOManager.h"
38 #include "RaiseAsync.h"
41 #include "ThreadPaused.h"
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
60 #include "eventlog/EventLog.h"
62 /* -----------------------------------------------------------------------------
64 * -------------------------------------------------------------------------- */
66 #if !defined(THREADED_RTS)
67 // Blocked/sleeping thrads
68 StgTSO *blocked_queue_hd = NULL;
69 StgTSO *blocked_queue_tl = NULL;
70 StgTSO *sleeping_queue = NULL; // perhaps replace with a hash table?
73 /* Set to true when the latest garbage collection failed to reclaim
74 * enough space, and the runtime should proceed to shut itself down in
75 * an orderly fashion (emitting profiling info etc.)
77 rtsBool heap_overflow = rtsFalse;
79 /* flag that tracks whether we have done any execution in this time slice.
80 * LOCK: currently none, perhaps we should lock (but needs to be
81 * updated in the fast path of the scheduler).
83 * NB. must be StgWord, we do xchg() on it.
85 volatile StgWord recent_activity = ACTIVITY_YES;
87 /* if this flag is set as well, give up execution
88 * LOCK: none (changes monotonically)
90 volatile StgWord sched_state = SCHED_RUNNING;
92 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
93 * exists - earlier gccs apparently didn't.
99 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
100 * in an MT setting, needed to signal that a worker thread shouldn't hang around
101 * in the scheduler when it is out of work.
103 rtsBool shutting_down_scheduler = rtsFalse;
106 * This mutex protects most of the global scheduler data in
107 * the THREADED_RTS runtime.
109 #if defined(THREADED_RTS)
113 #if !defined(mingw32_HOST_OS)
114 #define FORKPROCESS_PRIMOP_SUPPORTED
117 /* -----------------------------------------------------------------------------
118 * static function prototypes
119 * -------------------------------------------------------------------------- */
121 static Capability *schedule (Capability *initialCapability, Task *task);
124 // These function all encapsulate parts of the scheduler loop, and are
125 // abstracted only to make the structure and control flow of the
126 // scheduler clearer.
128 static void schedulePreLoop (void);
129 static void scheduleFindWork (Capability *cap);
130 #if defined(THREADED_RTS)
131 static void scheduleYield (Capability **pcap, Task *task);
133 static void scheduleStartSignalHandlers (Capability *cap);
134 static void scheduleCheckBlockedThreads (Capability *cap);
135 static void scheduleProcessInbox(Capability *cap);
136 static void scheduleDetectDeadlock (Capability *cap, Task *task);
137 static void schedulePushWork(Capability *cap, Task *task);
138 #if defined(THREADED_RTS)
139 static void scheduleActivateSpark(Capability *cap);
141 static void schedulePostRunThread(Capability *cap, StgTSO *t);
142 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
143 static void scheduleHandleStackOverflow( Capability *cap, Task *task,
145 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
146 nat prev_what_next );
147 static void scheduleHandleThreadBlocked( StgTSO *t );
148 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
150 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
151 static Capability *scheduleDoGC(Capability *cap, Task *task,
152 rtsBool force_major);
154 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
155 static StgTSO *threadStackUnderflow(Capability *cap, Task *task, StgTSO *tso);
157 static void deleteThread (Capability *cap, StgTSO *tso);
158 static void deleteAllThreads (Capability *cap);
160 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
161 static void deleteThread_(Capability *cap, StgTSO *tso);
164 /* ---------------------------------------------------------------------------
165 Main scheduling loop.
167 We use round-robin scheduling, each thread returning to the
168 scheduler loop when one of these conditions is detected:
171 * timer expires (thread yields)
177 In a GranSim setup this loop iterates over the global event queue.
178 This revolves around the global event queue, which determines what
179 to do next. Therefore, it's more complicated than either the
180 concurrent or the parallel (GUM) setup.
181 This version has been entirely removed (JB 2008/08).
184 GUM iterates over incoming messages.
185 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
186 and sends out a fish whenever it has nothing to do; in-between
187 doing the actual reductions (shared code below) it processes the
188 incoming messages and deals with delayed operations
189 (see PendingFetches).
190 This is not the ugliest code you could imagine, but it's bloody close.
192 (JB 2008/08) This version was formerly indicated by a PP-Flag PAR,
193 now by PP-flag PARALLEL_HASKELL. The Eden RTS (in GHC-6.x) uses it,
194 as well as future GUM versions. This file has been refurbished to
195 only contain valid code, which is however incomplete, refers to
196 invalid includes etc.
198 ------------------------------------------------------------------------ */
201 schedule (Capability *initialCapability, Task *task)
205 StgThreadReturnCode ret;
208 #if defined(THREADED_RTS)
209 rtsBool first = rtsTrue;
212 cap = initialCapability;
214 // Pre-condition: this task owns initialCapability.
215 // The sched_mutex is *NOT* held
216 // NB. on return, we still hold a capability.
218 debugTrace (DEBUG_sched, "cap %d: schedule()", initialCapability->no);
222 // -----------------------------------------------------------
223 // Scheduler loop starts here:
227 // Check whether we have re-entered the RTS from Haskell without
228 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
230 if (cap->in_haskell) {
231 errorBelch("schedule: re-entered unsafely.\n"
232 " Perhaps a 'foreign import unsafe' should be 'safe'?");
233 stg_exit(EXIT_FAILURE);
236 // The interruption / shutdown sequence.
238 // In order to cleanly shut down the runtime, we want to:
239 // * make sure that all main threads return to their callers
240 // with the state 'Interrupted'.
241 // * clean up all OS threads assocated with the runtime
242 // * free all memory etc.
244 // So the sequence for ^C goes like this:
246 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
247 // arranges for some Capability to wake up
249 // * all threads in the system are halted, and the zombies are
250 // placed on the run queue for cleaning up. We acquire all
251 // the capabilities in order to delete the threads, this is
252 // done by scheduleDoGC() for convenience (because GC already
253 // needs to acquire all the capabilities). We can't kill
254 // threads involved in foreign calls.
256 // * somebody calls shutdownHaskell(), which calls exitScheduler()
258 // * sched_state := SCHED_SHUTTING_DOWN
260 // * all workers exit when the run queue on their capability
261 // drains. All main threads will also exit when their TSO
262 // reaches the head of the run queue and they can return.
264 // * eventually all Capabilities will shut down, and the RTS can
267 // * We might be left with threads blocked in foreign calls,
268 // we should really attempt to kill these somehow (TODO);
270 switch (sched_state) {
273 case SCHED_INTERRUPTING:
274 debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
275 #if defined(THREADED_RTS)
276 discardSparksCap(cap);
278 /* scheduleDoGC() deletes all the threads */
279 cap = scheduleDoGC(cap,task,rtsFalse);
281 // after scheduleDoGC(), we must be shutting down. Either some
282 // other Capability did the final GC, or we did it above,
283 // either way we can fall through to the SCHED_SHUTTING_DOWN
285 ASSERT(sched_state == SCHED_SHUTTING_DOWN);
288 case SCHED_SHUTTING_DOWN:
289 debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
290 // If we are a worker, just exit. If we're a bound thread
291 // then we will exit below when we've removed our TSO from
293 if (!isBoundTask(task) && emptyRunQueue(cap)) {
298 barf("sched_state: %d", sched_state);
301 scheduleFindWork(cap);
303 /* work pushing, currently relevant only for THREADED_RTS:
304 (pushes threads, wakes up idle capabilities for stealing) */
305 schedulePushWork(cap,task);
307 scheduleDetectDeadlock(cap,task);
309 #if defined(THREADED_RTS)
310 cap = task->cap; // reload cap, it might have changed
313 // Normally, the only way we can get here with no threads to
314 // run is if a keyboard interrupt received during
315 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
316 // Additionally, it is not fatal for the
317 // threaded RTS to reach here with no threads to run.
319 // win32: might be here due to awaitEvent() being abandoned
320 // as a result of a console event having been delivered.
322 #if defined(THREADED_RTS)
326 // // don't yield the first time, we want a chance to run this
327 // // thread for a bit, even if there are others banging at the
330 // ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
333 scheduleYield(&cap,task);
335 if (emptyRunQueue(cap)) continue; // look for work again
338 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
339 if ( emptyRunQueue(cap) ) {
340 ASSERT(sched_state >= SCHED_INTERRUPTING);
345 // Get a thread to run
347 t = popRunQueue(cap);
349 // Sanity check the thread we're about to run. This can be
350 // expensive if there is lots of thread switching going on...
351 IF_DEBUG(sanity,checkTSO(t));
353 #if defined(THREADED_RTS)
354 // Check whether we can run this thread in the current task.
355 // If not, we have to pass our capability to the right task.
357 InCall *bound = t->bound;
360 if (bound->task == task) {
361 // yes, the Haskell thread is bound to the current native thread
363 debugTrace(DEBUG_sched,
364 "thread %lu bound to another OS thread",
365 (unsigned long)t->id);
366 // no, bound to a different Haskell thread: pass to that thread
367 pushOnRunQueue(cap,t);
371 // The thread we want to run is unbound.
372 if (task->incall->tso) {
373 debugTrace(DEBUG_sched,
374 "this OS thread cannot run thread %lu",
375 (unsigned long)t->id);
376 // no, the current native thread is bound to a different
377 // Haskell thread, so pass it to any worker thread
378 pushOnRunQueue(cap,t);
385 // If we're shutting down, and this thread has not yet been
386 // killed, kill it now. This sometimes happens when a finalizer
387 // thread is created by the final GC, or a thread previously
388 // in a foreign call returns.
389 if (sched_state >= SCHED_INTERRUPTING &&
390 !(t->what_next == ThreadComplete || t->what_next == ThreadKilled)) {
394 /* context switches are initiated by the timer signal, unless
395 * the user specified "context switch as often as possible", with
398 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
399 && !emptyThreadQueues(cap)) {
400 cap->context_switch = 1;
405 // CurrentTSO is the thread to run. t might be different if we
406 // loop back to run_thread, so make sure to set CurrentTSO after
408 cap->r.rCurrentTSO = t;
410 startHeapProfTimer();
412 // ----------------------------------------------------------------------
413 // Run the current thread
415 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
416 ASSERT(t->cap == cap);
417 ASSERT(t->bound ? t->bound->task->cap == cap : 1);
419 prev_what_next = t->what_next;
421 errno = t->saved_errno;
423 SetLastError(t->saved_winerror);
426 cap->in_haskell = rtsTrue;
430 #if defined(THREADED_RTS)
431 if (recent_activity == ACTIVITY_DONE_GC) {
432 // ACTIVITY_DONE_GC means we turned off the timer signal to
433 // conserve power (see #1623). Re-enable it here.
435 prev = xchg((P_)&recent_activity, ACTIVITY_YES);
436 if (prev == ACTIVITY_DONE_GC) {
439 } else if (recent_activity != ACTIVITY_INACTIVE) {
440 // If we reached ACTIVITY_INACTIVE, then don't reset it until
441 // we've done the GC. The thread running here might just be
442 // the IO manager thread that handle_tick() woke up via
444 recent_activity = ACTIVITY_YES;
448 traceEventRunThread(cap, t);
450 switch (prev_what_next) {
454 /* Thread already finished, return to scheduler. */
455 ret = ThreadFinished;
461 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
462 cap = regTableToCapability(r);
467 case ThreadInterpret:
468 cap = interpretBCO(cap);
473 barf("schedule: invalid what_next field");
476 cap->in_haskell = rtsFalse;
478 // The TSO might have moved, eg. if it re-entered the RTS and a GC
479 // happened. So find the new location:
480 t = cap->r.rCurrentTSO;
482 // And save the current errno in this thread.
483 // XXX: possibly bogus for SMP because this thread might already
484 // be running again, see code below.
485 t->saved_errno = errno;
487 // Similarly for Windows error code
488 t->saved_winerror = GetLastError();
491 traceEventStopThread(cap, t, ret);
493 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
494 ASSERT(t->cap == cap);
496 // ----------------------------------------------------------------------
498 // Costs for the scheduler are assigned to CCS_SYSTEM
500 #if defined(PROFILING)
504 schedulePostRunThread(cap,t);
506 if (ret != StackOverflow) {
507 t = threadStackUnderflow(cap,task,t);
510 ready_to_gc = rtsFalse;
514 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
518 scheduleHandleStackOverflow(cap,task,t);
522 if (scheduleHandleYield(cap, t, prev_what_next)) {
523 // shortcut for switching between compiler/interpreter:
529 scheduleHandleThreadBlocked(t);
533 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
534 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
538 barf("schedule: invalid thread return code %d", (int)ret);
541 if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
542 cap = scheduleDoGC(cap,task,rtsFalse);
544 } /* end of while() */
547 /* -----------------------------------------------------------------------------
548 * Run queue operations
549 * -------------------------------------------------------------------------- */
552 removeFromRunQueue (Capability *cap, StgTSO *tso)
554 if (tso->block_info.prev == END_TSO_QUEUE) {
555 ASSERT(cap->run_queue_hd == tso);
556 cap->run_queue_hd = tso->_link;
558 setTSOLink(cap, tso->block_info.prev, tso->_link);
560 if (tso->_link == END_TSO_QUEUE) {
561 ASSERT(cap->run_queue_tl == tso);
562 cap->run_queue_tl = tso->block_info.prev;
564 setTSOPrev(cap, tso->_link, tso->block_info.prev);
566 tso->_link = tso->block_info.prev = END_TSO_QUEUE;
568 IF_DEBUG(sanity, checkRunQueue(cap));
571 /* ----------------------------------------------------------------------------
572 * Setting up the scheduler loop
573 * ------------------------------------------------------------------------- */
576 schedulePreLoop(void)
578 // initialisation for scheduler - what cannot go into initScheduler()
581 /* -----------------------------------------------------------------------------
584 * Search for work to do, and handle messages from elsewhere.
585 * -------------------------------------------------------------------------- */
588 scheduleFindWork (Capability *cap)
590 scheduleStartSignalHandlers(cap);
592 scheduleProcessInbox(cap);
594 scheduleCheckBlockedThreads(cap);
596 #if defined(THREADED_RTS)
597 if (emptyRunQueue(cap)) { scheduleActivateSpark(cap); }
601 #if defined(THREADED_RTS)
602 STATIC_INLINE rtsBool
603 shouldYieldCapability (Capability *cap, Task *task)
605 // we need to yield this capability to someone else if..
606 // - another thread is initiating a GC
607 // - another Task is returning from a foreign call
608 // - the thread at the head of the run queue cannot be run
609 // by this Task (it is bound to another Task, or it is unbound
610 // and this task it bound).
611 return (waiting_for_gc ||
612 cap->returning_tasks_hd != NULL ||
613 (!emptyRunQueue(cap) && (task->incall->tso == NULL
614 ? cap->run_queue_hd->bound != NULL
615 : cap->run_queue_hd->bound != task->incall)));
618 // This is the single place where a Task goes to sleep. There are
619 // two reasons it might need to sleep:
620 // - there are no threads to run
621 // - we need to yield this Capability to someone else
622 // (see shouldYieldCapability())
624 // Careful: the scheduler loop is quite delicate. Make sure you run
625 // the tests in testsuite/concurrent (all ways) after modifying this,
626 // and also check the benchmarks in nofib/parallel for regressions.
629 scheduleYield (Capability **pcap, Task *task)
631 Capability *cap = *pcap;
633 // if we have work, and we don't need to give up the Capability, continue.
635 if (!shouldYieldCapability(cap,task) &&
636 (!emptyRunQueue(cap) ||
638 sched_state >= SCHED_INTERRUPTING))
641 // otherwise yield (sleep), and keep yielding if necessary.
643 yieldCapability(&cap,task);
645 while (shouldYieldCapability(cap,task));
647 // note there may still be no threads on the run queue at this
648 // point, the caller has to check.
655 /* -----------------------------------------------------------------------------
658 * Push work to other Capabilities if we have some.
659 * -------------------------------------------------------------------------- */
662 schedulePushWork(Capability *cap USED_IF_THREADS,
663 Task *task USED_IF_THREADS)
665 /* following code not for PARALLEL_HASKELL. I kept the call general,
666 future GUM versions might use pushing in a distributed setup */
667 #if defined(THREADED_RTS)
669 Capability *free_caps[n_capabilities], *cap0;
672 // migration can be turned off with +RTS -qm
673 if (!RtsFlags.ParFlags.migrate) return;
675 // Check whether we have more threads on our run queue, or sparks
676 // in our pool, that we could hand to another Capability.
677 if (cap->run_queue_hd == END_TSO_QUEUE) {
678 if (sparkPoolSizeCap(cap) < 2) return;
680 if (cap->run_queue_hd->_link == END_TSO_QUEUE &&
681 sparkPoolSizeCap(cap) < 1) return;
684 // First grab as many free Capabilities as we can.
685 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
686 cap0 = &capabilities[i];
687 if (cap != cap0 && tryGrabCapability(cap0,task)) {
688 if (!emptyRunQueue(cap0)
689 || cap->returning_tasks_hd != NULL
690 || cap->inbox != (Message*)END_TSO_QUEUE) {
691 // it already has some work, we just grabbed it at
692 // the wrong moment. Or maybe it's deadlocked!
693 releaseCapability(cap0);
695 free_caps[n_free_caps++] = cap0;
700 // we now have n_free_caps free capabilities stashed in
701 // free_caps[]. Share our run queue equally with them. This is
702 // probably the simplest thing we could do; improvements we might
703 // want to do include:
705 // - giving high priority to moving relatively new threads, on
706 // the gournds that they haven't had time to build up a
707 // working set in the cache on this CPU/Capability.
709 // - giving low priority to moving long-lived threads
711 if (n_free_caps > 0) {
712 StgTSO *prev, *t, *next;
713 rtsBool pushed_to_all;
715 debugTrace(DEBUG_sched,
716 "cap %d: %s and %d free capabilities, sharing...",
718 (!emptyRunQueue(cap) && cap->run_queue_hd->_link != END_TSO_QUEUE)?
719 "excess threads on run queue":"sparks to share (>=2)",
723 pushed_to_all = rtsFalse;
725 if (cap->run_queue_hd != END_TSO_QUEUE) {
726 prev = cap->run_queue_hd;
728 prev->_link = END_TSO_QUEUE;
729 for (; t != END_TSO_QUEUE; t = next) {
731 t->_link = END_TSO_QUEUE;
732 if (t->what_next == ThreadRelocated
733 || t->bound == task->incall // don't move my bound thread
734 || tsoLocked(t)) { // don't move a locked thread
735 setTSOLink(cap, prev, t);
736 setTSOPrev(cap, t, prev);
738 } else if (i == n_free_caps) {
739 pushed_to_all = rtsTrue;
742 setTSOLink(cap, prev, t);
743 setTSOPrev(cap, t, prev);
746 appendToRunQueue(free_caps[i],t);
748 traceEventMigrateThread (cap, t, free_caps[i]->no);
750 if (t->bound) { t->bound->task->cap = free_caps[i]; }
751 t->cap = free_caps[i];
755 cap->run_queue_tl = prev;
757 IF_DEBUG(sanity, checkRunQueue(cap));
761 /* JB I left this code in place, it would work but is not necessary */
763 // If there are some free capabilities that we didn't push any
764 // threads to, then try to push a spark to each one.
765 if (!pushed_to_all) {
767 // i is the next free capability to push to
768 for (; i < n_free_caps; i++) {
769 if (emptySparkPoolCap(free_caps[i])) {
770 spark = tryStealSpark(cap->sparks);
772 debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
774 traceEventStealSpark(free_caps[i], t, cap->no);
776 newSpark(&(free_caps[i]->r), spark);
781 #endif /* SPARK_PUSHING */
783 // release the capabilities
784 for (i = 0; i < n_free_caps; i++) {
785 task->cap = free_caps[i];
786 releaseAndWakeupCapability(free_caps[i]);
789 task->cap = cap; // reset to point to our Capability.
791 #endif /* THREADED_RTS */
795 /* ----------------------------------------------------------------------------
796 * Start any pending signal handlers
797 * ------------------------------------------------------------------------- */
799 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
801 scheduleStartSignalHandlers(Capability *cap)
803 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
804 // safe outside the lock
805 startSignalHandlers(cap);
810 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
815 /* ----------------------------------------------------------------------------
816 * Check for blocked threads that can be woken up.
817 * ------------------------------------------------------------------------- */
820 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
822 #if !defined(THREADED_RTS)
824 // Check whether any waiting threads need to be woken up. If the
825 // run queue is empty, and there are no other tasks running, we
826 // can wait indefinitely for something to happen.
828 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
830 awaitEvent (emptyRunQueue(cap));
835 /* ----------------------------------------------------------------------------
836 * Detect deadlock conditions and attempt to resolve them.
837 * ------------------------------------------------------------------------- */
840 scheduleDetectDeadlock (Capability *cap, Task *task)
843 * Detect deadlock: when we have no threads to run, there are no
844 * threads blocked, waiting for I/O, or sleeping, and all the
845 * other tasks are waiting for work, we must have a deadlock of
848 if ( emptyThreadQueues(cap) )
850 #if defined(THREADED_RTS)
852 * In the threaded RTS, we only check for deadlock if there
853 * has been no activity in a complete timeslice. This means
854 * we won't eagerly start a full GC just because we don't have
855 * any threads to run currently.
857 if (recent_activity != ACTIVITY_INACTIVE) return;
860 debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
862 // Garbage collection can release some new threads due to
863 // either (a) finalizers or (b) threads resurrected because
864 // they are unreachable and will therefore be sent an
865 // exception. Any threads thus released will be immediately
867 cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/);
868 // when force_major == rtsTrue. scheduleDoGC sets
869 // recent_activity to ACTIVITY_DONE_GC and turns off the timer
872 if ( !emptyRunQueue(cap) ) return;
874 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
875 /* If we have user-installed signal handlers, then wait
876 * for signals to arrive rather then bombing out with a
879 if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
880 debugTrace(DEBUG_sched,
881 "still deadlocked, waiting for signals...");
885 if (signals_pending()) {
886 startSignalHandlers(cap);
889 // either we have threads to run, or we were interrupted:
890 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
896 #if !defined(THREADED_RTS)
897 /* Probably a real deadlock. Send the current main thread the
898 * Deadlock exception.
900 if (task->incall->tso) {
901 switch (task->incall->tso->why_blocked) {
903 case BlockedOnBlackHole:
904 case BlockedOnMsgThrowTo:
906 throwToSingleThreaded(cap, task->incall->tso,
907 (StgClosure *)nonTermination_closure);
910 barf("deadlock: main thread blocked in a strange way");
919 /* ----------------------------------------------------------------------------
920 * Send pending messages (PARALLEL_HASKELL only)
921 * ------------------------------------------------------------------------- */
923 #if defined(PARALLEL_HASKELL)
925 scheduleSendPendingMessages(void)
928 # if defined(PAR) // global Mem.Mgmt., omit for now
929 if (PendingFetches != END_BF_QUEUE) {
934 if (RtsFlags.ParFlags.BufferTime) {
935 // if we use message buffering, we must send away all message
936 // packets which have become too old...
942 /* ----------------------------------------------------------------------------
943 * Process message in the current Capability's inbox
944 * ------------------------------------------------------------------------- */
947 scheduleProcessInbox (Capability *cap USED_IF_THREADS)
949 #if defined(THREADED_RTS)
952 while (!emptyInbox(cap)) {
953 ACQUIRE_LOCK(&cap->lock);
955 cap->inbox = m->link;
956 RELEASE_LOCK(&cap->lock);
957 executeMessage(cap, (Message *)m);
962 /* ----------------------------------------------------------------------------
963 * Activate spark threads (PARALLEL_HASKELL and THREADED_RTS)
964 * ------------------------------------------------------------------------- */
966 #if defined(THREADED_RTS)
968 scheduleActivateSpark(Capability *cap)
972 createSparkThread(cap);
973 debugTrace(DEBUG_sched, "creating a spark thread");
976 #endif // PARALLEL_HASKELL || THREADED_RTS
978 /* ----------------------------------------------------------------------------
979 * After running a thread...
980 * ------------------------------------------------------------------------- */
983 schedulePostRunThread (Capability *cap, StgTSO *t)
985 // We have to be able to catch transactions that are in an
986 // infinite loop as a result of seeing an inconsistent view of
990 // [a,b] <- mapM readTVar [ta,tb]
991 // when (a == b) loop
993 // and a is never equal to b given a consistent view of memory.
995 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
996 if (!stmValidateNestOfTransactions (t -> trec)) {
997 debugTrace(DEBUG_sched | DEBUG_stm,
998 "trec %p found wasting its time", t);
1000 // strip the stack back to the
1001 // ATOMICALLY_FRAME, aborting the (nested)
1002 // transaction, and saving the stack of any
1003 // partially-evaluated thunks on the heap.
1004 throwToSingleThreaded_(cap, t, NULL, rtsTrue);
1006 // ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1010 /* some statistics gathering in the parallel case */
1013 /* -----------------------------------------------------------------------------
1014 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1015 * -------------------------------------------------------------------------- */
1018 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1020 // did the task ask for a large block?
1021 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1022 // if so, get one and push it on the front of the nursery.
1026 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1028 if (blocks > BLOCKS_PER_MBLOCK) {
1029 barf("allocation of %ld bytes too large (GHC should have complained at compile-time)", (long)cap->r.rHpAlloc);
1032 debugTrace(DEBUG_sched,
1033 "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1034 (long)t->id, what_next_strs[t->what_next], blocks);
1036 // don't do this if the nursery is (nearly) full, we'll GC first.
1037 if (cap->r.rCurrentNursery->link != NULL ||
1038 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1039 // if the nursery has only one block.
1041 bd = allocGroup_lock(blocks);
1042 cap->r.rNursery->n_blocks += blocks;
1044 // link the new group into the list
1045 bd->link = cap->r.rCurrentNursery;
1046 bd->u.back = cap->r.rCurrentNursery->u.back;
1047 if (cap->r.rCurrentNursery->u.back != NULL) {
1048 cap->r.rCurrentNursery->u.back->link = bd;
1050 cap->r.rNursery->blocks = bd;
1052 cap->r.rCurrentNursery->u.back = bd;
1054 // initialise it as a nursery block. We initialise the
1055 // step, gen_no, and flags field of *every* sub-block in
1056 // this large block, because this is easier than making
1057 // sure that we always find the block head of a large
1058 // block whenever we call Bdescr() (eg. evacuate() and
1059 // isAlive() in the GC would both have to do this, at
1063 for (x = bd; x < bd + blocks; x++) {
1064 initBdescr(x,g0,g0);
1070 // This assert can be a killer if the app is doing lots
1071 // of large block allocations.
1072 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1074 // now update the nursery to point to the new block
1075 cap->r.rCurrentNursery = bd;
1077 // we might be unlucky and have another thread get on the
1078 // run queue before us and steal the large block, but in that
1079 // case the thread will just end up requesting another large
1081 pushOnRunQueue(cap,t);
1082 return rtsFalse; /* not actually GC'ing */
1086 if (cap->r.rHpLim == NULL || cap->context_switch) {
1087 // Sometimes we miss a context switch, e.g. when calling
1088 // primitives in a tight loop, MAYBE_GC() doesn't check the
1089 // context switch flag, and we end up waiting for a GC.
1090 // See #1984, and concurrent/should_run/1984
1091 cap->context_switch = 0;
1092 appendToRunQueue(cap,t);
1094 pushOnRunQueue(cap,t);
1097 /* actual GC is done at the end of the while loop in schedule() */
1100 /* -----------------------------------------------------------------------------
1101 * Handle a thread that returned to the scheduler with ThreadStackOverflow
1102 * -------------------------------------------------------------------------- */
1105 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1107 /* just adjust the stack for this thread, then pop it back
1111 /* enlarge the stack */
1112 StgTSO *new_t = threadStackOverflow(cap, t);
1114 /* The TSO attached to this Task may have moved, so update the
1117 if (task->incall->tso == t) {
1118 task->incall->tso = new_t;
1120 pushOnRunQueue(cap,new_t);
1124 /* -----------------------------------------------------------------------------
1125 * Handle a thread that returned to the scheduler with ThreadYielding
1126 * -------------------------------------------------------------------------- */
1129 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1131 /* put the thread back on the run queue. Then, if we're ready to
1132 * GC, check whether this is the last task to stop. If so, wake
1133 * up the GC thread. getThread will block during a GC until the
1137 ASSERT(t->_link == END_TSO_QUEUE);
1139 // Shortcut if we're just switching evaluators: don't bother
1140 // doing stack squeezing (which can be expensive), just run the
1142 if (cap->context_switch == 0 && t->what_next != prev_what_next) {
1143 debugTrace(DEBUG_sched,
1144 "--<< thread %ld (%s) stopped to switch evaluators",
1145 (long)t->id, what_next_strs[t->what_next]);
1149 // Reset the context switch flag. We don't do this just before
1150 // running the thread, because that would mean we would lose ticks
1151 // during GC, which can lead to unfair scheduling (a thread hogs
1152 // the CPU because the tick always arrives during GC). This way
1153 // penalises threads that do a lot of allocation, but that seems
1154 // better than the alternative.
1155 cap->context_switch = 0;
1158 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1161 appendToRunQueue(cap,t);
1166 /* -----------------------------------------------------------------------------
1167 * Handle a thread that returned to the scheduler with ThreadBlocked
1168 * -------------------------------------------------------------------------- */
1171 scheduleHandleThreadBlocked( StgTSO *t
1178 // We don't need to do anything. The thread is blocked, and it
1179 // has tidied up its stack and placed itself on whatever queue
1180 // it needs to be on.
1182 // ASSERT(t->why_blocked != NotBlocked);
1183 // Not true: for example,
1184 // - the thread may have woken itself up already, because
1185 // threadPaused() might have raised a blocked throwTo
1186 // exception, see maybePerformBlockedException().
1189 traceThreadStatus(DEBUG_sched, t);
1193 /* -----------------------------------------------------------------------------
1194 * Handle a thread that returned to the scheduler with ThreadFinished
1195 * -------------------------------------------------------------------------- */
1198 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1200 /* Need to check whether this was a main thread, and if so,
1201 * return with the return value.
1203 * We also end up here if the thread kills itself with an
1204 * uncaught exception, see Exception.cmm.
1207 // blocked exceptions can now complete, even if the thread was in
1208 // blocked mode (see #2910).
1209 awakenBlockedExceptionQueue (cap, t);
1212 // Check whether the thread that just completed was a bound
1213 // thread, and if so return with the result.
1215 // There is an assumption here that all thread completion goes
1216 // through this point; we need to make sure that if a thread
1217 // ends up in the ThreadKilled state, that it stays on the run
1218 // queue so it can be dealt with here.
1223 if (t->bound != task->incall) {
1224 #if !defined(THREADED_RTS)
1225 // Must be a bound thread that is not the topmost one. Leave
1226 // it on the run queue until the stack has unwound to the
1227 // point where we can deal with this. Leaving it on the run
1228 // queue also ensures that the garbage collector knows about
1229 // this thread and its return value (it gets dropped from the
1230 // step->threads list so there's no other way to find it).
1231 appendToRunQueue(cap,t);
1234 // this cannot happen in the threaded RTS, because a
1235 // bound thread can only be run by the appropriate Task.
1236 barf("finished bound thread that isn't mine");
1240 ASSERT(task->incall->tso == t);
1242 if (t->what_next == ThreadComplete) {
1243 if (task->incall->ret) {
1244 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1245 *(task->incall->ret) = (StgClosure *)task->incall->tso->sp[1];
1247 task->incall->stat = Success;
1249 if (task->incall->ret) {
1250 *(task->incall->ret) = NULL;
1252 if (sched_state >= SCHED_INTERRUPTING) {
1253 if (heap_overflow) {
1254 task->incall->stat = HeapExhausted;
1256 task->incall->stat = Interrupted;
1259 task->incall->stat = Killed;
1263 removeThreadLabel((StgWord)task->incall->tso->id);
1266 // We no longer consider this thread and task to be bound to
1267 // each other. The TSO lives on until it is GC'd, but the
1268 // task is about to be released by the caller, and we don't
1269 // want anyone following the pointer from the TSO to the
1270 // defunct task (which might have already been
1271 // re-used). This was a real bug: the GC updated
1272 // tso->bound->tso which lead to a deadlock.
1274 task->incall->tso = NULL;
1276 return rtsTrue; // tells schedule() to return
1282 /* -----------------------------------------------------------------------------
1283 * Perform a heap census
1284 * -------------------------------------------------------------------------- */
1287 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1289 // When we have +RTS -i0 and we're heap profiling, do a census at
1290 // every GC. This lets us get repeatable runs for debugging.
1291 if (performHeapProfile ||
1292 (RtsFlags.ProfFlags.profileInterval==0 &&
1293 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1300 /* -----------------------------------------------------------------------------
1301 * Perform a garbage collection if necessary
1302 * -------------------------------------------------------------------------- */
1305 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS, rtsBool force_major)
1307 rtsBool heap_census;
1309 /* extern static volatile StgWord waiting_for_gc;
1310 lives inside capability.c */
1311 rtsBool gc_type, prev_pending_gc;
1315 if (sched_state == SCHED_SHUTTING_DOWN) {
1316 // The final GC has already been done, and the system is
1317 // shutting down. We'll probably deadlock if we try to GC
1323 if (sched_state < SCHED_INTERRUPTING
1324 && RtsFlags.ParFlags.parGcEnabled
1325 && N >= RtsFlags.ParFlags.parGcGen
1326 && ! oldest_gen->mark)
1328 gc_type = PENDING_GC_PAR;
1330 gc_type = PENDING_GC_SEQ;
1333 // In order to GC, there must be no threads running Haskell code.
1334 // Therefore, the GC thread needs to hold *all* the capabilities,
1335 // and release them after the GC has completed.
1337 // This seems to be the simplest way: previous attempts involved
1338 // making all the threads with capabilities give up their
1339 // capabilities and sleep except for the *last* one, which
1340 // actually did the GC. But it's quite hard to arrange for all
1341 // the other tasks to sleep and stay asleep.
1344 /* Other capabilities are prevented from running yet more Haskell
1345 threads if waiting_for_gc is set. Tested inside
1346 yieldCapability() and releaseCapability() in Capability.c */
1348 prev_pending_gc = cas(&waiting_for_gc, 0, gc_type);
1349 if (prev_pending_gc) {
1351 debugTrace(DEBUG_sched, "someone else is trying to GC (%d)...",
1354 yieldCapability(&cap,task);
1355 } while (waiting_for_gc);
1356 return cap; // NOTE: task->cap might have changed here
1359 setContextSwitches();
1361 // The final shutdown GC is always single-threaded, because it's
1362 // possible that some of the Capabilities have no worker threads.
1364 if (gc_type == PENDING_GC_SEQ)
1366 traceEventRequestSeqGc(cap);
1370 traceEventRequestParGc(cap);
1371 debugTrace(DEBUG_sched, "ready_to_gc, grabbing GC threads");
1374 if (gc_type == PENDING_GC_SEQ)
1376 // single-threaded GC: grab all the capabilities
1377 for (i=0; i < n_capabilities; i++) {
1378 debugTrace(DEBUG_sched, "ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities);
1379 if (cap != &capabilities[i]) {
1380 Capability *pcap = &capabilities[i];
1381 // we better hope this task doesn't get migrated to
1382 // another Capability while we're waiting for this one.
1383 // It won't, because load balancing happens while we have
1384 // all the Capabilities, but even so it's a slightly
1385 // unsavoury invariant.
1387 waitForReturnCapability(&pcap, task);
1388 if (pcap != &capabilities[i]) {
1389 barf("scheduleDoGC: got the wrong capability");
1396 // multi-threaded GC: make sure all the Capabilities donate one
1398 waitForGcThreads(cap);
1403 IF_DEBUG(scheduler, printAllThreads());
1405 delete_threads_and_gc:
1407 * We now have all the capabilities; if we're in an interrupting
1408 * state, then we should take the opportunity to delete all the
1409 * threads in the system.
1411 if (sched_state == SCHED_INTERRUPTING) {
1412 deleteAllThreads(cap);
1413 sched_state = SCHED_SHUTTING_DOWN;
1416 heap_census = scheduleNeedHeapProfile(rtsTrue);
1418 traceEventGcStart(cap);
1419 #if defined(THREADED_RTS)
1420 // reset waiting_for_gc *before* GC, so that when the GC threads
1421 // emerge they don't immediately re-enter the GC.
1423 GarbageCollect(force_major || heap_census, gc_type, cap);
1425 GarbageCollect(force_major || heap_census, 0, cap);
1427 traceEventGcEnd(cap);
1429 if (recent_activity == ACTIVITY_INACTIVE && force_major)
1431 // We are doing a GC because the system has been idle for a
1432 // timeslice and we need to check for deadlock. Record the
1433 // fact that we've done a GC and turn off the timer signal;
1434 // it will get re-enabled if we run any threads after the GC.
1435 recent_activity = ACTIVITY_DONE_GC;
1440 // the GC might have taken long enough for the timer to set
1441 // recent_activity = ACTIVITY_INACTIVE, but we aren't
1442 // necessarily deadlocked:
1443 recent_activity = ACTIVITY_YES;
1446 #if defined(THREADED_RTS)
1447 if (gc_type == PENDING_GC_PAR)
1449 releaseGCThreads(cap);
1454 debugTrace(DEBUG_sched, "performing heap census");
1456 performHeapProfile = rtsFalse;
1459 if (heap_overflow && sched_state < SCHED_INTERRUPTING) {
1460 // GC set the heap_overflow flag, so we should proceed with
1461 // an orderly shutdown now. Ultimately we want the main
1462 // thread to return to its caller with HeapExhausted, at which
1463 // point the caller should call hs_exit(). The first step is
1464 // to delete all the threads.
1466 // Another way to do this would be to raise an exception in
1467 // the main thread, which we really should do because it gives
1468 // the program a chance to clean up. But how do we find the
1469 // main thread? It should presumably be the same one that
1470 // gets ^C exceptions, but that's all done on the Haskell side
1471 // (GHC.TopHandler).
1472 sched_state = SCHED_INTERRUPTING;
1473 goto delete_threads_and_gc;
1478 Once we are all together... this would be the place to balance all
1479 spark pools. No concurrent stealing or adding of new sparks can
1480 occur. Should be defined in Sparks.c. */
1481 balanceSparkPoolsCaps(n_capabilities, capabilities);
1484 #if defined(THREADED_RTS)
1485 if (gc_type == PENDING_GC_SEQ) {
1486 // release our stash of capabilities.
1487 for (i = 0; i < n_capabilities; i++) {
1488 if (cap != &capabilities[i]) {
1489 task->cap = &capabilities[i];
1490 releaseCapability(&capabilities[i]);
1504 /* ---------------------------------------------------------------------------
1505 * Singleton fork(). Do not copy any running threads.
1506 * ------------------------------------------------------------------------- */
1509 forkProcess(HsStablePtr *entry
1510 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
1515 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
1521 #if defined(THREADED_RTS)
1522 if (RtsFlags.ParFlags.nNodes > 1) {
1523 errorBelch("forking not supported with +RTS -N<n> greater than 1");
1524 stg_exit(EXIT_FAILURE);
1528 debugTrace(DEBUG_sched, "forking!");
1530 // ToDo: for SMP, we should probably acquire *all* the capabilities
1533 // no funny business: hold locks while we fork, otherwise if some
1534 // other thread is holding a lock when the fork happens, the data
1535 // structure protected by the lock will forever be in an
1536 // inconsistent state in the child. See also #1391.
1537 ACQUIRE_LOCK(&sched_mutex);
1538 ACQUIRE_LOCK(&cap->lock);
1539 ACQUIRE_LOCK(&cap->running_task->lock);
1541 stopTimer(); // See #4074
1543 #if defined(TRACING)
1544 flushEventLog(); // so that child won't inherit dirty file buffers
1549 if (pid) { // parent
1551 startTimer(); // #4074
1553 RELEASE_LOCK(&sched_mutex);
1554 RELEASE_LOCK(&cap->lock);
1555 RELEASE_LOCK(&cap->running_task->lock);
1557 // just return the pid
1563 #if defined(THREADED_RTS)
1564 initMutex(&sched_mutex);
1565 initMutex(&cap->lock);
1566 initMutex(&cap->running_task->lock);
1569 #if defined(TRACING)
1570 abortEventLogging(); // abort eventlog inherited from parent
1571 initEventLogging(); // child starts its own eventlog
1573 // Now, all OS threads except the thread that forked are
1574 // stopped. We need to stop all Haskell threads, including
1575 // those involved in foreign calls. Also we need to delete
1576 // all Tasks, because they correspond to OS threads that are
1579 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1580 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
1581 if (t->what_next == ThreadRelocated) {
1584 next = t->global_link;
1585 // don't allow threads to catch the ThreadKilled
1586 // exception, but we do want to raiseAsync() because these
1587 // threads may be evaluating thunks that we need later.
1588 deleteThread_(cap,t);
1590 // stop the GC from updating the InCall to point to
1591 // the TSO. This is only necessary because the
1592 // OSThread bound to the TSO has been killed, and
1593 // won't get a chance to exit in the usual way (see
1594 // also scheduleHandleThreadFinished).
1600 // Empty the run queue. It seems tempting to let all the
1601 // killed threads stay on the run queue as zombies to be
1602 // cleaned up later, but some of them correspond to bound
1603 // threads for which the corresponding Task does not exist.
1604 cap->run_queue_hd = END_TSO_QUEUE;
1605 cap->run_queue_tl = END_TSO_QUEUE;
1607 // Any suspended C-calling Tasks are no more, their OS threads
1609 cap->suspended_ccalls = NULL;
1611 // Empty the threads lists. Otherwise, the garbage
1612 // collector may attempt to resurrect some of these threads.
1613 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1614 generations[g].threads = END_TSO_QUEUE;
1617 discardTasksExcept(cap->running_task);
1619 #if defined(THREADED_RTS)
1620 // Wipe our spare workers list, they no longer exist. New
1621 // workers will be created if necessary.
1622 cap->spare_workers = NULL;
1623 cap->n_spare_workers = 0;
1624 cap->returning_tasks_hd = NULL;
1625 cap->returning_tasks_tl = NULL;
1628 // On Unix, all timers are reset in the child, so we need to start
1633 #if defined(THREADED_RTS)
1634 cap = ioManagerStartCap(cap);
1637 cap = rts_evalStableIO(cap, entry, NULL); // run the action
1638 rts_checkSchedStatus("forkProcess",cap);
1641 hs_exit(); // clean up and exit
1642 stg_exit(EXIT_SUCCESS);
1644 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
1645 barf("forkProcess#: primop not supported on this platform, sorry!\n");
1649 /* ---------------------------------------------------------------------------
1650 * Delete all the threads in the system
1651 * ------------------------------------------------------------------------- */
1654 deleteAllThreads ( Capability *cap )
1656 // NOTE: only safe to call if we own all capabilities.
1661 debugTrace(DEBUG_sched,"deleting all threads");
1662 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1663 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
1664 if (t->what_next == ThreadRelocated) {
1667 next = t->global_link;
1668 deleteThread(cap,t);
1673 // The run queue now contains a bunch of ThreadKilled threads. We
1674 // must not throw these away: the main thread(s) will be in there
1675 // somewhere, and the main scheduler loop has to deal with it.
1676 // Also, the run queue is the only thing keeping these threads from
1677 // being GC'd, and we don't want the "main thread has been GC'd" panic.
1679 #if !defined(THREADED_RTS)
1680 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
1681 ASSERT(sleeping_queue == END_TSO_QUEUE);
1685 /* -----------------------------------------------------------------------------
1686 Managing the suspended_ccalls list.
1687 Locks required: sched_mutex
1688 -------------------------------------------------------------------------- */
1691 suspendTask (Capability *cap, Task *task)
1695 incall = task->incall;
1696 ASSERT(incall->next == NULL && incall->prev == NULL);
1697 incall->next = cap->suspended_ccalls;
1698 incall->prev = NULL;
1699 if (cap->suspended_ccalls) {
1700 cap->suspended_ccalls->prev = incall;
1702 cap->suspended_ccalls = incall;
1706 recoverSuspendedTask (Capability *cap, Task *task)
1710 incall = task->incall;
1712 incall->prev->next = incall->next;
1714 ASSERT(cap->suspended_ccalls == incall);
1715 cap->suspended_ccalls = incall->next;
1718 incall->next->prev = incall->prev;
1720 incall->next = incall->prev = NULL;
1723 /* ---------------------------------------------------------------------------
1724 * Suspending & resuming Haskell threads.
1726 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1727 * its capability before calling the C function. This allows another
1728 * task to pick up the capability and carry on running Haskell
1729 * threads. It also means that if the C call blocks, it won't lock
1732 * The Haskell thread making the C call is put to sleep for the
1733 * duration of the call, on the suspended_ccalling_threads queue. We
1734 * give out a token to the task, which it can use to resume the thread
1735 * on return from the C function.
1737 * If this is an interruptible C call, this means that the FFI call may be
1738 * unceremoniously terminated and should be scheduled on an
1739 * unbound worker thread.
1740 * ------------------------------------------------------------------------- */
1743 suspendThread (StgRegTable *reg, rtsBool interruptible)
1750 StgWord32 saved_winerror;
1753 saved_errno = errno;
1755 saved_winerror = GetLastError();
1758 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
1760 cap = regTableToCapability(reg);
1762 task = cap->running_task;
1763 tso = cap->r.rCurrentTSO;
1765 traceEventStopThread(cap, tso, THREAD_SUSPENDED_FOREIGN_CALL);
1767 // XXX this might not be necessary --SDM
1768 tso->what_next = ThreadRunGHC;
1770 threadPaused(cap,tso);
1772 if (interruptible) {
1773 tso->why_blocked = BlockedOnCCall_Interruptible;
1775 tso->why_blocked = BlockedOnCCall;
1778 // Hand back capability
1779 task->incall->suspended_tso = tso;
1780 task->incall->suspended_cap = cap;
1782 ACQUIRE_LOCK(&cap->lock);
1784 suspendTask(cap,task);
1785 cap->in_haskell = rtsFalse;
1786 releaseCapability_(cap,rtsFalse);
1788 RELEASE_LOCK(&cap->lock);
1790 errno = saved_errno;
1792 SetLastError(saved_winerror);
1798 resumeThread (void *task_)
1806 StgWord32 saved_winerror;
1809 saved_errno = errno;
1811 saved_winerror = GetLastError();
1814 incall = task->incall;
1815 cap = incall->suspended_cap;
1818 // Wait for permission to re-enter the RTS with the result.
1819 waitForReturnCapability(&cap,task);
1820 // we might be on a different capability now... but if so, our
1821 // entry on the suspended_ccalls list will also have been
1824 // Remove the thread from the suspended list
1825 recoverSuspendedTask(cap,task);
1827 tso = incall->suspended_tso;
1828 incall->suspended_tso = NULL;
1829 incall->suspended_cap = NULL;
1830 tso->_link = END_TSO_QUEUE; // no write barrier reqd
1832 traceEventRunThread(cap, tso);
1834 /* Reset blocking status */
1835 tso->why_blocked = NotBlocked;
1837 if ((tso->flags & TSO_BLOCKEX) == 0) {
1838 // avoid locking the TSO if we don't have to
1839 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE) {
1840 maybePerformBlockedException(cap,tso);
1844 cap->r.rCurrentTSO = tso;
1845 cap->in_haskell = rtsTrue;
1846 errno = saved_errno;
1848 SetLastError(saved_winerror);
1851 /* We might have GC'd, mark the TSO dirty again */
1854 IF_DEBUG(sanity, checkTSO(tso));
1859 /* ---------------------------------------------------------------------------
1862 * scheduleThread puts a thread on the end of the runnable queue.
1863 * This will usually be done immediately after a thread is created.
1864 * The caller of scheduleThread must create the thread using e.g.
1865 * createThread and push an appropriate closure
1866 * on this thread's stack before the scheduler is invoked.
1867 * ------------------------------------------------------------------------ */
1870 scheduleThread(Capability *cap, StgTSO *tso)
1872 // The thread goes at the *end* of the run-queue, to avoid possible
1873 // starvation of any threads already on the queue.
1874 appendToRunQueue(cap,tso);
1878 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
1880 #if defined(THREADED_RTS)
1881 tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
1882 // move this thread from now on.
1883 cpu %= RtsFlags.ParFlags.nNodes;
1884 if (cpu == cap->no) {
1885 appendToRunQueue(cap,tso);
1887 migrateThread(cap, tso, &capabilities[cpu]);
1890 appendToRunQueue(cap,tso);
1895 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
1900 // We already created/initialised the Task
1901 task = cap->running_task;
1903 // This TSO is now a bound thread; make the Task and TSO
1904 // point to each other.
1905 tso->bound = task->incall;
1908 task->incall->tso = tso;
1909 task->incall->ret = ret;
1910 task->incall->stat = NoStatus;
1912 appendToRunQueue(cap,tso);
1915 debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)id);
1917 cap = schedule(cap,task);
1919 ASSERT(task->incall->stat != NoStatus);
1920 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
1922 debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)id);
1926 /* ----------------------------------------------------------------------------
1928 * ------------------------------------------------------------------------- */
1930 #if defined(THREADED_RTS)
1931 void scheduleWorker (Capability *cap, Task *task)
1933 // schedule() runs without a lock.
1934 cap = schedule(cap,task);
1936 // On exit from schedule(), we have a Capability, but possibly not
1937 // the same one we started with.
1939 // During shutdown, the requirement is that after all the
1940 // Capabilities are shut down, all workers that are shutting down
1941 // have finished workerTaskStop(). This is why we hold on to
1942 // cap->lock until we've finished workerTaskStop() below.
1944 // There may be workers still involved in foreign calls; those
1945 // will just block in waitForReturnCapability() because the
1946 // Capability has been shut down.
1948 ACQUIRE_LOCK(&cap->lock);
1949 releaseCapability_(cap,rtsFalse);
1950 workerTaskStop(task);
1951 RELEASE_LOCK(&cap->lock);
1955 /* ---------------------------------------------------------------------------
1958 * Initialise the scheduler. This resets all the queues - if the
1959 * queues contained any threads, they'll be garbage collected at the
1962 * ------------------------------------------------------------------------ */
1967 #if !defined(THREADED_RTS)
1968 blocked_queue_hd = END_TSO_QUEUE;
1969 blocked_queue_tl = END_TSO_QUEUE;
1970 sleeping_queue = END_TSO_QUEUE;
1973 sched_state = SCHED_RUNNING;
1974 recent_activity = ACTIVITY_YES;
1976 #if defined(THREADED_RTS)
1977 /* Initialise the mutex and condition variables used by
1979 initMutex(&sched_mutex);
1982 ACQUIRE_LOCK(&sched_mutex);
1984 /* A capability holds the state a native thread needs in
1985 * order to execute STG code. At least one capability is
1986 * floating around (only THREADED_RTS builds have more than one).
1992 #if defined(THREADED_RTS)
1996 RELEASE_LOCK(&sched_mutex);
1998 #if defined(THREADED_RTS)
2000 * Eagerly start one worker to run each Capability, except for
2001 * Capability 0. The idea is that we're probably going to start a
2002 * bound thread on Capability 0 pretty soon, so we don't want a
2003 * worker task hogging it.
2008 for (i = 1; i < n_capabilities; i++) {
2009 cap = &capabilities[i];
2010 ACQUIRE_LOCK(&cap->lock);
2011 startWorkerTask(cap);
2012 RELEASE_LOCK(&cap->lock);
2019 exitScheduler (rtsBool wait_foreign USED_IF_THREADS)
2020 /* see Capability.c, shutdownCapability() */
2024 task = newBoundTask();
2026 // If we haven't killed all the threads yet, do it now.
2027 if (sched_state < SCHED_SHUTTING_DOWN) {
2028 sched_state = SCHED_INTERRUPTING;
2029 waitForReturnCapability(&task->cap,task);
2030 scheduleDoGC(task->cap,task,rtsFalse);
2031 ASSERT(task->incall->tso == NULL);
2032 releaseCapability(task->cap);
2034 sched_state = SCHED_SHUTTING_DOWN;
2036 #if defined(THREADED_RTS)
2040 for (i = 0; i < n_capabilities; i++) {
2041 ASSERT(task->incall->tso == NULL);
2042 shutdownCapability(&capabilities[i], task, wait_foreign);
2047 boundTaskExiting(task);
2051 freeScheduler( void )
2055 ACQUIRE_LOCK(&sched_mutex);
2056 still_running = freeTaskManager();
2057 // We can only free the Capabilities if there are no Tasks still
2058 // running. We might have a Task about to return from a foreign
2059 // call into waitForReturnCapability(), for example (actually,
2060 // this should be the *only* thing that a still-running Task can
2061 // do at this point, and it will block waiting for the
2063 if (still_running == 0) {
2065 if (n_capabilities != 1) {
2066 stgFree(capabilities);
2069 RELEASE_LOCK(&sched_mutex);
2070 #if defined(THREADED_RTS)
2071 closeMutex(&sched_mutex);
2075 /* -----------------------------------------------------------------------------
2078 This is the interface to the garbage collector from Haskell land.
2079 We provide this so that external C code can allocate and garbage
2080 collect when called from Haskell via _ccall_GC.
2081 -------------------------------------------------------------------------- */
2084 performGC_(rtsBool force_major)
2088 // We must grab a new Task here, because the existing Task may be
2089 // associated with a particular Capability, and chained onto the
2090 // suspended_ccalls queue.
2091 task = newBoundTask();
2093 waitForReturnCapability(&task->cap,task);
2094 scheduleDoGC(task->cap,task,force_major);
2095 releaseCapability(task->cap);
2096 boundTaskExiting(task);
2102 performGC_(rtsFalse);
2106 performMajorGC(void)
2108 performGC_(rtsTrue);
2111 /* -----------------------------------------------------------------------------
2114 If the thread has reached its maximum stack size, then raise the
2115 StackOverflow exception in the offending thread. Otherwise
2116 relocate the TSO into a larger chunk of memory and adjust its stack
2118 -------------------------------------------------------------------------- */
2121 threadStackOverflow(Capability *cap, StgTSO *tso)
2123 nat new_stack_size, stack_words;
2128 IF_DEBUG(sanity,checkTSO(tso));
2130 if (tso->stack_size >= tso->max_stack_size
2131 && !(tso->flags & TSO_BLOCKEX)) {
2132 // NB. never raise a StackOverflow exception if the thread is
2133 // inside Control.Exceptino.block. It is impractical to protect
2134 // against stack overflow exceptions, since virtually anything
2135 // can raise one (even 'catch'), so this is the only sensible
2136 // thing to do here. See bug #767.
2139 if (tso->flags & TSO_SQUEEZED) {
2142 // #3677: In a stack overflow situation, stack squeezing may
2143 // reduce the stack size, but we don't know whether it has been
2144 // reduced enough for the stack check to succeed if we try
2145 // again. Fortunately stack squeezing is idempotent, so all we
2146 // need to do is record whether *any* squeezing happened. If we
2147 // are at the stack's absolute -K limit, and stack squeezing
2148 // happened, then we try running the thread again. The
2149 // TSO_SQUEEZED flag is set by threadPaused() to tell us whether
2150 // squeezing happened or not.
2152 debugTrace(DEBUG_gc,
2153 "threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)",
2154 (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
2156 /* If we're debugging, just print out the top of the stack */
2157 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2160 // Send this thread the StackOverflow exception
2161 throwToSingleThreaded(cap, tso, (StgClosure *)stackOverflow_closure);
2166 // We also want to avoid enlarging the stack if squeezing has
2167 // already released some of it. However, we don't want to get into
2168 // a pathalogical situation where a thread has a nearly full stack
2169 // (near its current limit, but not near the absolute -K limit),
2170 // keeps allocating a little bit, squeezing removes a little bit,
2171 // and then it runs again. So to avoid this, if we squeezed *and*
2172 // there is still less than BLOCK_SIZE_W words free, then we enlarge
2173 // the stack anyway.
2174 if ((tso->flags & TSO_SQUEEZED) &&
2175 ((W_)(tso->sp - tso->stack) >= BLOCK_SIZE_W)) {
2179 /* Try to double the current stack size. If that takes us over the
2180 * maximum stack size for this thread, then use the maximum instead
2181 * (that is, unless we're already at or over the max size and we
2182 * can't raise the StackOverflow exception (see above), in which
2183 * case just double the size). Finally round up so the TSO ends up as
2184 * a whole number of blocks.
2186 if (tso->stack_size >= tso->max_stack_size) {
2187 new_stack_size = tso->stack_size * 2;
2189 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2191 new_tso_size = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2192 TSO_STRUCT_SIZE)/sizeof(W_);
2193 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2194 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2196 debugTrace(DEBUG_sched,
2197 "increasing stack size from %ld words to %d.",
2198 (long)tso->stack_size, new_stack_size);
2200 dest = (StgTSO *)allocate(cap,new_tso_size);
2201 TICK_ALLOC_TSO(new_stack_size,0);
2203 /* copy the TSO block and the old stack into the new area */
2204 memcpy(dest,tso,TSO_STRUCT_SIZE);
2205 stack_words = tso->stack + tso->stack_size - tso->sp;
2206 new_sp = (P_)dest + new_tso_size - stack_words;
2207 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2209 /* relocate the stack pointers... */
2211 dest->stack_size = new_stack_size;
2213 /* Mark the old TSO as relocated. We have to check for relocated
2214 * TSOs in the garbage collector and any primops that deal with TSOs.
2216 * It's important to set the sp value to just beyond the end
2217 * of the stack, so we don't attempt to scavenge any part of the
2220 setTSOLink(cap,tso,dest);
2221 write_barrier(); // other threads seeing ThreadRelocated will look at _link
2222 tso->what_next = ThreadRelocated;
2223 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2224 tso->why_blocked = NotBlocked;
2226 IF_DEBUG(sanity,checkTSO(dest));
2228 IF_DEBUG(scheduler,printTSO(dest));
2235 threadStackUnderflow (Capability *cap, Task *task, StgTSO *tso)
2237 bdescr *bd, *new_bd;
2238 lnat free_w, tso_size_w;
2241 tso_size_w = tso_sizeW(tso);
2243 if (tso_size_w < MBLOCK_SIZE_W ||
2244 // TSO is less than 2 mblocks (since the first mblock is
2245 // shorter than MBLOCK_SIZE_W)
2246 (tso_size_w - BLOCKS_PER_MBLOCK*BLOCK_SIZE_W) % MBLOCK_SIZE_W != 0 ||
2247 // or TSO is not a whole number of megablocks (ensuring
2248 // precondition of splitLargeBlock() below)
2249 (tso_size_w <= round_up_to_mblocks(RtsFlags.GcFlags.initialStkSize)) ||
2250 // or TSO is smaller than the minimum stack size (rounded up)
2251 (nat)(tso->stack + tso->stack_size - tso->sp) > tso->stack_size / 4)
2252 // or stack is using more than 1/4 of the available space
2258 // this is the number of words we'll free
2259 free_w = round_to_mblocks(tso_size_w/2);
2261 bd = Bdescr((StgPtr)tso);
2262 new_bd = splitLargeBlock(bd, free_w / BLOCK_SIZE_W);
2263 bd->free = bd->start + TSO_STRUCT_SIZEW;
2265 new_tso = (StgTSO *)new_bd->start;
2266 memcpy(new_tso,tso,TSO_STRUCT_SIZE);
2267 new_tso->stack_size = new_bd->free - new_tso->stack;
2269 // The original TSO was dirty and probably on the mutable
2270 // list. The new TSO is not yet on the mutable list, so we better
2273 new_tso->flags &= ~TSO_LINK_DIRTY;
2274 dirty_TSO(cap, new_tso);
2276 debugTrace(DEBUG_sched, "thread %ld: reducing TSO size from %lu words to %lu",
2277 (long)tso->id, tso_size_w, tso_sizeW(new_tso));
2279 tso->_link = new_tso; // no write barrier reqd: same generation
2280 write_barrier(); // other threads seeing ThreadRelocated will look at _link
2281 tso->what_next = ThreadRelocated;
2283 // The TSO attached to this Task may have moved, so update the
2285 if (task->incall->tso == tso) {
2286 task->incall->tso = new_tso;
2289 IF_DEBUG(sanity,checkTSO(new_tso));
2294 /* ---------------------------------------------------------------------------
2296 - usually called inside a signal handler so it mustn't do anything fancy.
2297 ------------------------------------------------------------------------ */
2300 interruptStgRts(void)
2302 sched_state = SCHED_INTERRUPTING;
2303 setContextSwitches();
2304 #if defined(THREADED_RTS)
2309 /* -----------------------------------------------------------------------------
2312 This function causes at least one OS thread to wake up and run the
2313 scheduler loop. It is invoked when the RTS might be deadlocked, or
2314 an external event has arrived that may need servicing (eg. a
2315 keyboard interrupt).
2317 In the single-threaded RTS we don't do anything here; we only have
2318 one thread anyway, and the event that caused us to want to wake up
2319 will have interrupted any blocking system call in progress anyway.
2320 -------------------------------------------------------------------------- */
2322 #if defined(THREADED_RTS)
2323 void wakeUpRts(void)
2325 // This forces the IO Manager thread to wakeup, which will
2326 // in turn ensure that some OS thread wakes up and runs the
2327 // scheduler loop, which will cause a GC and deadlock check.
2332 /* -----------------------------------------------------------------------------
2335 This is used for interruption (^C) and forking, and corresponds to
2336 raising an exception but without letting the thread catch the
2338 -------------------------------------------------------------------------- */
2341 deleteThread (Capability *cap STG_UNUSED, StgTSO *tso)
2343 // NOTE: must only be called on a TSO that we have exclusive
2344 // access to, because we will call throwToSingleThreaded() below.
2345 // The TSO must be on the run queue of the Capability we own, or
2346 // we must own all Capabilities.
2348 if (tso->why_blocked != BlockedOnCCall &&
2349 tso->why_blocked != BlockedOnCCall_Interruptible) {
2350 throwToSingleThreaded(tso->cap,tso,NULL);
2354 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2356 deleteThread_(Capability *cap, StgTSO *tso)
2357 { // for forkProcess only:
2358 // like deleteThread(), but we delete threads in foreign calls, too.
2360 if (tso->why_blocked == BlockedOnCCall ||
2361 tso->why_blocked == BlockedOnCCall_Interruptible) {
2362 tso->what_next = ThreadKilled;
2363 appendToRunQueue(tso->cap, tso);
2365 deleteThread(cap,tso);
2370 /* -----------------------------------------------------------------------------
2371 raiseExceptionHelper
2373 This function is called by the raise# primitve, just so that we can
2374 move some of the tricky bits of raising an exception from C-- into
2375 C. Who knows, it might be a useful re-useable thing here too.
2376 -------------------------------------------------------------------------- */
2379 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2381 Capability *cap = regTableToCapability(reg);
2382 StgThunk *raise_closure = NULL;
2384 StgRetInfoTable *info;
2386 // This closure represents the expression 'raise# E' where E
2387 // is the exception raise. It is used to overwrite all the
2388 // thunks which are currently under evaluataion.
2391 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2392 // LDV profiling: stg_raise_info has THUNK as its closure
2393 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2394 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
2395 // 1 does not cause any problem unless profiling is performed.
2396 // However, when LDV profiling goes on, we need to linearly scan
2397 // small object pool, where raise_closure is stored, so we should
2398 // use MIN_UPD_SIZE.
2400 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2401 // sizeofW(StgClosure)+1);
2405 // Walk up the stack, looking for the catch frame. On the way,
2406 // we update any closures pointed to from update frames with the
2407 // raise closure that we just built.
2411 info = get_ret_itbl((StgClosure *)p);
2412 next = p + stack_frame_sizeW((StgClosure *)p);
2413 switch (info->i.type) {
2416 // Only create raise_closure if we need to.
2417 if (raise_closure == NULL) {
2419 (StgThunk *)allocate(cap,sizeofW(StgThunk)+1);
2420 SET_HDR(raise_closure, &stg_raise_info, CCCS);
2421 raise_closure->payload[0] = exception;
2423 updateThunk(cap, tso, ((StgUpdateFrame *)p)->updatee,
2424 (StgClosure *)raise_closure);
2428 case ATOMICALLY_FRAME:
2429 debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
2431 return ATOMICALLY_FRAME;
2437 case CATCH_STM_FRAME:
2438 debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
2440 return CATCH_STM_FRAME;
2446 case CATCH_RETRY_FRAME:
2455 /* -----------------------------------------------------------------------------
2456 findRetryFrameHelper
2458 This function is called by the retry# primitive. It traverses the stack
2459 leaving tso->sp referring to the frame which should handle the retry.
2461 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
2462 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
2464 We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
2465 create) because retries are not considered to be exceptions, despite the
2466 similar implementation.
2468 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
2469 not be created within memory transactions.
2470 -------------------------------------------------------------------------- */
2473 findRetryFrameHelper (StgTSO *tso)
2476 StgRetInfoTable *info;
2480 info = get_ret_itbl((StgClosure *)p);
2481 next = p + stack_frame_sizeW((StgClosure *)p);
2482 switch (info->i.type) {
2484 case ATOMICALLY_FRAME:
2485 debugTrace(DEBUG_stm,
2486 "found ATOMICALLY_FRAME at %p during retry", p);
2488 return ATOMICALLY_FRAME;
2490 case CATCH_RETRY_FRAME:
2491 debugTrace(DEBUG_stm,
2492 "found CATCH_RETRY_FRAME at %p during retrry", p);
2494 return CATCH_RETRY_FRAME;
2496 case CATCH_STM_FRAME: {
2497 StgTRecHeader *trec = tso -> trec;
2498 StgTRecHeader *outer = trec -> enclosing_trec;
2499 debugTrace(DEBUG_stm,
2500 "found CATCH_STM_FRAME at %p during retry", p);
2501 debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
2502 stmAbortTransaction(tso -> cap, trec);
2503 stmFreeAbortedTRec(tso -> cap, trec);
2504 tso -> trec = outer;
2511 ASSERT(info->i.type != CATCH_FRAME);
2512 ASSERT(info->i.type != STOP_FRAME);
2519 /* -----------------------------------------------------------------------------
2520 resurrectThreads is called after garbage collection on the list of
2521 threads found to be garbage. Each of these threads will be woken
2522 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2523 on an MVar, or NonTermination if the thread was blocked on a Black
2526 Locks: assumes we hold *all* the capabilities.
2527 -------------------------------------------------------------------------- */
2530 resurrectThreads (StgTSO *threads)
2536 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2537 next = tso->global_link;
2539 gen = Bdescr((P_)tso)->gen;
2540 tso->global_link = gen->threads;
2543 debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
2545 // Wake up the thread on the Capability it was last on
2548 switch (tso->why_blocked) {
2550 /* Called by GC - sched_mutex lock is currently held. */
2551 throwToSingleThreaded(cap, tso,
2552 (StgClosure *)blockedIndefinitelyOnMVar_closure);
2554 case BlockedOnBlackHole:
2555 throwToSingleThreaded(cap, tso,
2556 (StgClosure *)nonTermination_closure);
2559 throwToSingleThreaded(cap, tso,
2560 (StgClosure *)blockedIndefinitelyOnSTM_closure);
2563 /* This might happen if the thread was blocked on a black hole
2564 * belonging to a thread that we've just woken up (raiseAsync
2565 * can wake up threads, remember...).
2568 case BlockedOnMsgThrowTo:
2569 // This can happen if the target is masking, blocks on a
2570 // black hole, and then is found to be unreachable. In
2571 // this case, we want to let the target wake up and carry
2572 // on, and do nothing to this thread.
2575 barf("resurrectThreads: thread blocked in a strange way: %d",