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"
24 #include "ThreadLabels.h"
26 #include "Proftimer.h"
29 #include "eventlog/EventLog.h"
30 #include "sm/GC.h" // waitForGcThreads, releaseGCThreads, N
32 #include "Capability.h"
34 #include "AwaitEvent.h"
35 #if defined(mingw32_HOST_OS)
36 #include "win32/IOManager.h"
39 #include "RaiseAsync.h"
42 #include "ThreadPaused.h"
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
59 /* -----------------------------------------------------------------------------
61 * -------------------------------------------------------------------------- */
63 #if !defined(THREADED_RTS)
64 // Blocked/sleeping thrads
65 StgTSO *blocked_queue_hd = NULL;
66 StgTSO *blocked_queue_tl = NULL;
67 StgTSO *sleeping_queue = NULL; // perhaps replace with a hash table?
70 /* Threads blocked on blackholes.
71 * LOCK: sched_mutex+capability, or all capabilities
73 StgTSO *blackhole_queue = NULL;
75 /* The blackhole_queue should be checked for threads to wake up. See
76 * Schedule.h for more thorough comment.
77 * LOCK: none (doesn't matter if we miss an update)
79 rtsBool blackholes_need_checking = rtsFalse;
81 /* Set to true when the latest garbage collection failed to reclaim
82 * enough space, and the runtime should proceed to shut itself down in
83 * an orderly fashion (emitting profiling info etc.)
85 rtsBool heap_overflow = rtsFalse;
87 /* flag that tracks whether we have done any execution in this time slice.
88 * LOCK: currently none, perhaps we should lock (but needs to be
89 * updated in the fast path of the scheduler).
91 * NB. must be StgWord, we do xchg() on it.
93 volatile StgWord recent_activity = ACTIVITY_YES;
95 /* if this flag is set as well, give up execution
96 * LOCK: none (changes monotonically)
98 volatile StgWord sched_state = SCHED_RUNNING;
100 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
101 * exists - earlier gccs apparently didn't.
107 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
108 * in an MT setting, needed to signal that a worker thread shouldn't hang around
109 * in the scheduler when it is out of work.
111 rtsBool shutting_down_scheduler = rtsFalse;
114 * This mutex protects most of the global scheduler data in
115 * the THREADED_RTS runtime.
117 #if defined(THREADED_RTS)
121 #if !defined(mingw32_HOST_OS)
122 #define FORKPROCESS_PRIMOP_SUPPORTED
125 /* -----------------------------------------------------------------------------
126 * static function prototypes
127 * -------------------------------------------------------------------------- */
129 static Capability *schedule (Capability *initialCapability, Task *task);
132 // These function all encapsulate parts of the scheduler loop, and are
133 // abstracted only to make the structure and control flow of the
134 // scheduler clearer.
136 static void schedulePreLoop (void);
137 static void scheduleFindWork (Capability *cap);
138 #if defined(THREADED_RTS)
139 static void scheduleYield (Capability **pcap, Task *task, rtsBool);
141 static void scheduleStartSignalHandlers (Capability *cap);
142 static void scheduleCheckBlockedThreads (Capability *cap);
143 static void scheduleCheckWakeupThreads(Capability *cap USED_IF_NOT_THREADS);
144 static void scheduleCheckBlackHoles (Capability *cap);
145 static void scheduleDetectDeadlock (Capability *cap, Task *task);
146 static void schedulePushWork(Capability *cap, Task *task);
147 #if defined(THREADED_RTS)
148 static void scheduleActivateSpark(Capability *cap);
150 static void schedulePostRunThread(Capability *cap, StgTSO *t);
151 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
152 static void scheduleHandleStackOverflow( Capability *cap, Task *task,
154 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
155 nat prev_what_next );
156 static void scheduleHandleThreadBlocked( StgTSO *t );
157 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
159 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
160 static Capability *scheduleDoGC(Capability *cap, Task *task,
161 rtsBool force_major);
163 static rtsBool checkBlackHoles(Capability *cap);
165 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
166 static StgTSO *threadStackUnderflow(Task *task, StgTSO *tso);
168 static void deleteThread (Capability *cap, StgTSO *tso);
169 static void deleteAllThreads (Capability *cap);
171 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
172 static void deleteThread_(Capability *cap, StgTSO *tso);
176 static char *whatNext_strs[] = {
178 [ThreadRunGHC] = "ThreadRunGHC",
179 [ThreadInterpret] = "ThreadInterpret",
180 [ThreadKilled] = "ThreadKilled",
181 [ThreadRelocated] = "ThreadRelocated",
182 [ThreadComplete] = "ThreadComplete"
186 /* -----------------------------------------------------------------------------
187 * Putting a thread on the run queue: different scheduling policies
188 * -------------------------------------------------------------------------- */
191 addToRunQueue( Capability *cap, StgTSO *t )
193 // this does round-robin scheduling; good for concurrency
194 appendToRunQueue(cap,t);
197 /* ---------------------------------------------------------------------------
198 Main scheduling loop.
200 We use round-robin scheduling, each thread returning to the
201 scheduler loop when one of these conditions is detected:
204 * timer expires (thread yields)
210 In a GranSim setup this loop iterates over the global event queue.
211 This revolves around the global event queue, which determines what
212 to do next. Therefore, it's more complicated than either the
213 concurrent or the parallel (GUM) setup.
214 This version has been entirely removed (JB 2008/08).
217 GUM iterates over incoming messages.
218 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
219 and sends out a fish whenever it has nothing to do; in-between
220 doing the actual reductions (shared code below) it processes the
221 incoming messages and deals with delayed operations
222 (see PendingFetches).
223 This is not the ugliest code you could imagine, but it's bloody close.
225 (JB 2008/08) This version was formerly indicated by a PP-Flag PAR,
226 now by PP-flag PARALLEL_HASKELL. The Eden RTS (in GHC-6.x) uses it,
227 as well as future GUM versions. This file has been refurbished to
228 only contain valid code, which is however incomplete, refers to
229 invalid includes etc.
231 ------------------------------------------------------------------------ */
234 schedule (Capability *initialCapability, Task *task)
238 StgThreadReturnCode ret;
241 #if defined(THREADED_RTS)
242 rtsBool first = rtsTrue;
243 rtsBool force_yield = rtsFalse;
246 cap = initialCapability;
248 // Pre-condition: this task owns initialCapability.
249 // The sched_mutex is *NOT* held
250 // NB. on return, we still hold a capability.
252 debugTrace (DEBUG_sched,
253 "### NEW SCHEDULER LOOP (task: %p, cap: %p)",
254 task, initialCapability);
258 // -----------------------------------------------------------
259 // Scheduler loop starts here:
263 // Check whether we have re-entered the RTS from Haskell without
264 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
266 if (cap->in_haskell) {
267 errorBelch("schedule: re-entered unsafely.\n"
268 " Perhaps a 'foreign import unsafe' should be 'safe'?");
269 stg_exit(EXIT_FAILURE);
272 // The interruption / shutdown sequence.
274 // In order to cleanly shut down the runtime, we want to:
275 // * make sure that all main threads return to their callers
276 // with the state 'Interrupted'.
277 // * clean up all OS threads assocated with the runtime
278 // * free all memory etc.
280 // So the sequence for ^C goes like this:
282 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
283 // arranges for some Capability to wake up
285 // * all threads in the system are halted, and the zombies are
286 // placed on the run queue for cleaning up. We acquire all
287 // the capabilities in order to delete the threads, this is
288 // done by scheduleDoGC() for convenience (because GC already
289 // needs to acquire all the capabilities). We can't kill
290 // threads involved in foreign calls.
292 // * somebody calls shutdownHaskell(), which calls exitScheduler()
294 // * sched_state := SCHED_SHUTTING_DOWN
296 // * all workers exit when the run queue on their capability
297 // drains. All main threads will also exit when their TSO
298 // reaches the head of the run queue and they can return.
300 // * eventually all Capabilities will shut down, and the RTS can
303 // * We might be left with threads blocked in foreign calls,
304 // we should really attempt to kill these somehow (TODO);
306 switch (sched_state) {
309 case SCHED_INTERRUPTING:
310 debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
311 #if defined(THREADED_RTS)
312 discardSparksCap(cap);
314 /* scheduleDoGC() deletes all the threads */
315 cap = scheduleDoGC(cap,task,rtsFalse);
317 // after scheduleDoGC(), we must be shutting down. Either some
318 // other Capability did the final GC, or we did it above,
319 // either way we can fall through to the SCHED_SHUTTING_DOWN
321 ASSERT(sched_state == SCHED_SHUTTING_DOWN);
324 case SCHED_SHUTTING_DOWN:
325 debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
326 // If we are a worker, just exit. If we're a bound thread
327 // then we will exit below when we've removed our TSO from
329 if (task->tso == NULL && emptyRunQueue(cap)) {
334 barf("sched_state: %d", sched_state);
337 scheduleFindWork(cap);
339 /* work pushing, currently relevant only for THREADED_RTS:
340 (pushes threads, wakes up idle capabilities for stealing) */
341 schedulePushWork(cap,task);
343 scheduleDetectDeadlock(cap,task);
345 #if defined(THREADED_RTS)
346 cap = task->cap; // reload cap, it might have changed
349 // Normally, the only way we can get here with no threads to
350 // run is if a keyboard interrupt received during
351 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
352 // Additionally, it is not fatal for the
353 // threaded RTS to reach here with no threads to run.
355 // win32: might be here due to awaitEvent() being abandoned
356 // as a result of a console event having been delivered.
358 #if defined(THREADED_RTS)
362 // // don't yield the first time, we want a chance to run this
363 // // thread for a bit, even if there are others banging at the
366 // ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
370 scheduleYield(&cap,task,force_yield);
371 force_yield = rtsFalse;
373 if (emptyRunQueue(cap)) continue; // look for work again
376 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
377 if ( emptyRunQueue(cap) ) {
378 ASSERT(sched_state >= SCHED_INTERRUPTING);
383 // Get a thread to run
385 t = popRunQueue(cap);
387 // Sanity check the thread we're about to run. This can be
388 // expensive if there is lots of thread switching going on...
389 IF_DEBUG(sanity,checkTSO(t));
391 #if defined(THREADED_RTS)
392 // Check whether we can run this thread in the current task.
393 // If not, we have to pass our capability to the right task.
395 Task *bound = t->bound;
399 debugTrace(DEBUG_sched,
400 "### Running thread %lu in bound thread", (unsigned long)t->id);
401 // yes, the Haskell thread is bound to the current native thread
403 debugTrace(DEBUG_sched,
404 "### thread %lu bound to another OS thread", (unsigned long)t->id);
405 // no, bound to a different Haskell thread: pass to that thread
406 pushOnRunQueue(cap,t);
410 // The thread we want to run is unbound.
412 debugTrace(DEBUG_sched,
413 "### this OS thread cannot run thread %lu", (unsigned long)t->id);
414 // no, the current native thread is bound to a different
415 // Haskell thread, so pass it to any worker thread
416 pushOnRunQueue(cap,t);
423 // If we're shutting down, and this thread has not yet been
424 // killed, kill it now. This sometimes happens when a finalizer
425 // thread is created by the final GC, or a thread previously
426 // in a foreign call returns.
427 if (sched_state >= SCHED_INTERRUPTING &&
428 !(t->what_next == ThreadComplete || t->what_next == ThreadKilled)) {
432 /* context switches are initiated by the timer signal, unless
433 * the user specified "context switch as often as possible", with
436 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
437 && !emptyThreadQueues(cap)) {
438 cap->context_switch = 1;
443 // CurrentTSO is the thread to run. t might be different if we
444 // loop back to run_thread, so make sure to set CurrentTSO after
446 cap->r.rCurrentTSO = t;
448 debugTrace(DEBUG_sched, "-->> running thread %ld %s ...",
449 (long)t->id, whatNext_strs[t->what_next]);
451 startHeapProfTimer();
453 // Check for exceptions blocked on this thread
454 maybePerformBlockedException (cap, t);
456 // ----------------------------------------------------------------------
457 // Run the current thread
459 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
460 ASSERT(t->cap == cap);
461 ASSERT(t->bound ? t->bound->cap == cap : 1);
463 prev_what_next = t->what_next;
465 errno = t->saved_errno;
467 SetLastError(t->saved_winerror);
470 cap->in_haskell = rtsTrue;
474 #if defined(THREADED_RTS)
475 if (recent_activity == ACTIVITY_DONE_GC) {
476 // ACTIVITY_DONE_GC means we turned off the timer signal to
477 // conserve power (see #1623). Re-enable it here.
479 prev = xchg((P_)&recent_activity, ACTIVITY_YES);
480 if (prev == ACTIVITY_DONE_GC) {
484 recent_activity = ACTIVITY_YES;
488 postEvent(cap, EVENT_RUN_THREAD, t->id, 0);
490 switch (prev_what_next) {
494 /* Thread already finished, return to scheduler. */
495 ret = ThreadFinished;
501 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
502 cap = regTableToCapability(r);
507 case ThreadInterpret:
508 cap = interpretBCO(cap);
513 barf("schedule: invalid what_next field");
516 cap->in_haskell = rtsFalse;
518 // The TSO might have moved, eg. if it re-entered the RTS and a GC
519 // happened. So find the new location:
520 t = cap->r.rCurrentTSO;
522 // We have run some Haskell code: there might be blackhole-blocked
523 // threads to wake up now.
524 // Lock-free test here should be ok, we're just setting a flag.
525 if ( blackhole_queue != END_TSO_QUEUE ) {
526 blackholes_need_checking = rtsTrue;
529 // And save the current errno in this thread.
530 // XXX: possibly bogus for SMP because this thread might already
531 // be running again, see code below.
532 t->saved_errno = errno;
534 // Similarly for Windows error code
535 t->saved_winerror = GetLastError();
538 postEvent (cap, EVENT_STOP_THREAD, t->id, ret);
540 #if defined(THREADED_RTS)
541 // If ret is ThreadBlocked, and this Task is bound to the TSO that
542 // blocked, we are in limbo - the TSO is now owned by whatever it
543 // is blocked on, and may in fact already have been woken up,
544 // perhaps even on a different Capability. It may be the case
545 // that task->cap != cap. We better yield this Capability
546 // immediately and return to normaility.
547 if (ret == ThreadBlocked) {
548 debugTrace(DEBUG_sched,
549 "--<< thread %lu (%s) stopped: blocked",
550 (unsigned long)t->id, whatNext_strs[t->what_next]);
551 force_yield = rtsTrue;
556 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
557 ASSERT(t->cap == cap);
559 // ----------------------------------------------------------------------
561 // Costs for the scheduler are assigned to CCS_SYSTEM
563 #if defined(PROFILING)
567 schedulePostRunThread(cap,t);
569 if (ret != StackOverflow) {
570 t = threadStackUnderflow(task,t);
573 ready_to_gc = rtsFalse;
577 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
581 scheduleHandleStackOverflow(cap,task,t);
585 if (scheduleHandleYield(cap, t, prev_what_next)) {
586 // shortcut for switching between compiler/interpreter:
592 scheduleHandleThreadBlocked(t);
596 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
597 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
601 barf("schedule: invalid thread return code %d", (int)ret);
604 if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
605 cap = scheduleDoGC(cap,task,rtsFalse);
607 } /* end of while() */
610 /* ----------------------------------------------------------------------------
611 * Setting up the scheduler loop
612 * ------------------------------------------------------------------------- */
615 schedulePreLoop(void)
617 // initialisation for scheduler - what cannot go into initScheduler()
620 /* -----------------------------------------------------------------------------
623 * Search for work to do, and handle messages from elsewhere.
624 * -------------------------------------------------------------------------- */
627 scheduleFindWork (Capability *cap)
629 scheduleStartSignalHandlers(cap);
631 // Only check the black holes here if we've nothing else to do.
632 // During normal execution, the black hole list only gets checked
633 // at GC time, to avoid repeatedly traversing this possibly long
634 // list each time around the scheduler.
635 if (emptyRunQueue(cap)) { scheduleCheckBlackHoles(cap); }
637 scheduleCheckWakeupThreads(cap);
639 scheduleCheckBlockedThreads(cap);
641 #if defined(THREADED_RTS)
642 if (emptyRunQueue(cap)) { scheduleActivateSpark(cap); }
646 #if defined(THREADED_RTS)
647 STATIC_INLINE rtsBool
648 shouldYieldCapability (Capability *cap, Task *task)
650 // we need to yield this capability to someone else if..
651 // - another thread is initiating a GC
652 // - another Task is returning from a foreign call
653 // - the thread at the head of the run queue cannot be run
654 // by this Task (it is bound to another Task, or it is unbound
655 // and this task it bound).
656 return (waiting_for_gc ||
657 cap->returning_tasks_hd != NULL ||
658 (!emptyRunQueue(cap) && (task->tso == NULL
659 ? cap->run_queue_hd->bound != NULL
660 : cap->run_queue_hd->bound != task)));
663 // This is the single place where a Task goes to sleep. There are
664 // two reasons it might need to sleep:
665 // - there are no threads to run
666 // - we need to yield this Capability to someone else
667 // (see shouldYieldCapability())
669 // Careful: the scheduler loop is quite delicate. Make sure you run
670 // the tests in testsuite/concurrent (all ways) after modifying this,
671 // and also check the benchmarks in nofib/parallel for regressions.
674 scheduleYield (Capability **pcap, Task *task, rtsBool force_yield)
676 Capability *cap = *pcap;
678 // if we have work, and we don't need to give up the Capability, continue.
680 // The force_yield flag is used when a bound thread blocks. This
681 // is a particularly tricky situation: the current Task does not
682 // own the TSO any more, since it is on some queue somewhere, and
683 // might be woken up or manipulated by another thread at any time.
684 // The TSO and Task might be migrated to another Capability.
685 // Certain invariants might be in doubt, such as task->bound->cap
686 // == cap. We have to yield the current Capability immediately,
687 // no messing around.
690 !shouldYieldCapability(cap,task) &&
691 (!emptyRunQueue(cap) ||
692 !emptyWakeupQueue(cap) ||
693 blackholes_need_checking ||
694 sched_state >= SCHED_INTERRUPTING))
697 // otherwise yield (sleep), and keep yielding if necessary.
699 yieldCapability(&cap,task);
701 while (shouldYieldCapability(cap,task));
703 // note there may still be no threads on the run queue at this
704 // point, the caller has to check.
711 /* -----------------------------------------------------------------------------
714 * Push work to other Capabilities if we have some.
715 * -------------------------------------------------------------------------- */
718 schedulePushWork(Capability *cap USED_IF_THREADS,
719 Task *task USED_IF_THREADS)
721 /* following code not for PARALLEL_HASKELL. I kept the call general,
722 future GUM versions might use pushing in a distributed setup */
723 #if defined(THREADED_RTS)
725 Capability *free_caps[n_capabilities], *cap0;
728 // migration can be turned off with +RTS -qg
729 if (!RtsFlags.ParFlags.migrate) return;
731 // Check whether we have more threads on our run queue, or sparks
732 // in our pool, that we could hand to another Capability.
733 if (cap->run_queue_hd == END_TSO_QUEUE) {
734 if (sparkPoolSizeCap(cap) < 2) return;
736 if (cap->run_queue_hd->_link == END_TSO_QUEUE &&
737 sparkPoolSizeCap(cap) < 1) return;
740 // First grab as many free Capabilities as we can.
741 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
742 cap0 = &capabilities[i];
743 if (cap != cap0 && tryGrabCapability(cap0,task)) {
744 if (!emptyRunQueue(cap0) || cap->returning_tasks_hd != NULL) {
745 // it already has some work, we just grabbed it at
746 // the wrong moment. Or maybe it's deadlocked!
747 releaseCapability(cap0);
749 free_caps[n_free_caps++] = cap0;
754 // we now have n_free_caps free capabilities stashed in
755 // free_caps[]. Share our run queue equally with them. This is
756 // probably the simplest thing we could do; improvements we might
757 // want to do include:
759 // - giving high priority to moving relatively new threads, on
760 // the gournds that they haven't had time to build up a
761 // working set in the cache on this CPU/Capability.
763 // - giving low priority to moving long-lived threads
765 if (n_free_caps > 0) {
766 StgTSO *prev, *t, *next;
767 rtsBool pushed_to_all;
769 debugTrace(DEBUG_sched,
770 "cap %d: %s and %d free capabilities, sharing...",
772 (!emptyRunQueue(cap) && cap->run_queue_hd->_link != END_TSO_QUEUE)?
773 "excess threads on run queue":"sparks to share (>=2)",
777 pushed_to_all = rtsFalse;
779 if (cap->run_queue_hd != END_TSO_QUEUE) {
780 prev = cap->run_queue_hd;
782 prev->_link = END_TSO_QUEUE;
783 for (; t != END_TSO_QUEUE; t = next) {
785 t->_link = END_TSO_QUEUE;
786 if (t->what_next == ThreadRelocated
787 || t->bound == task // don't move my bound thread
788 || tsoLocked(t)) { // don't move a locked thread
789 setTSOLink(cap, prev, t);
791 } else if (i == n_free_caps) {
792 pushed_to_all = rtsTrue;
795 setTSOLink(cap, prev, t);
798 debugTrace(DEBUG_sched, "pushing thread %lu to capability %d", (unsigned long)t->id, free_caps[i]->no);
799 appendToRunQueue(free_caps[i],t);
801 postEvent (cap, EVENT_MIGRATE_THREAD, t->id, free_caps[i]->no);
803 if (t->bound) { t->bound->cap = free_caps[i]; }
804 t->cap = free_caps[i];
808 cap->run_queue_tl = prev;
812 /* JB I left this code in place, it would work but is not necessary */
814 // If there are some free capabilities that we didn't push any
815 // threads to, then try to push a spark to each one.
816 if (!pushed_to_all) {
818 // i is the next free capability to push to
819 for (; i < n_free_caps; i++) {
820 if (emptySparkPoolCap(free_caps[i])) {
821 spark = tryStealSpark(cap->sparks);
823 debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
825 postEvent(free_caps[i], EVENT_STEAL_SPARK, t->id, cap->no);
827 newSpark(&(free_caps[i]->r), spark);
832 #endif /* SPARK_PUSHING */
834 // release the capabilities
835 for (i = 0; i < n_free_caps; i++) {
836 task->cap = free_caps[i];
837 releaseAndWakeupCapability(free_caps[i]);
840 task->cap = cap; // reset to point to our Capability.
842 #endif /* THREADED_RTS */
846 /* ----------------------------------------------------------------------------
847 * Start any pending signal handlers
848 * ------------------------------------------------------------------------- */
850 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
852 scheduleStartSignalHandlers(Capability *cap)
854 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
855 // safe outside the lock
856 startSignalHandlers(cap);
861 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
866 /* ----------------------------------------------------------------------------
867 * Check for blocked threads that can be woken up.
868 * ------------------------------------------------------------------------- */
871 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
873 #if !defined(THREADED_RTS)
875 // Check whether any waiting threads need to be woken up. If the
876 // run queue is empty, and there are no other tasks running, we
877 // can wait indefinitely for something to happen.
879 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
881 awaitEvent( emptyRunQueue(cap) && !blackholes_need_checking );
887 /* ----------------------------------------------------------------------------
888 * Check for threads woken up by other Capabilities
889 * ------------------------------------------------------------------------- */
892 scheduleCheckWakeupThreads(Capability *cap USED_IF_THREADS)
894 #if defined(THREADED_RTS)
895 // Any threads that were woken up by other Capabilities get
896 // appended to our run queue.
897 if (!emptyWakeupQueue(cap)) {
898 ACQUIRE_LOCK(&cap->lock);
899 if (emptyRunQueue(cap)) {
900 cap->run_queue_hd = cap->wakeup_queue_hd;
901 cap->run_queue_tl = cap->wakeup_queue_tl;
903 setTSOLink(cap, cap->run_queue_tl, cap->wakeup_queue_hd);
904 cap->run_queue_tl = cap->wakeup_queue_tl;
906 cap->wakeup_queue_hd = cap->wakeup_queue_tl = END_TSO_QUEUE;
907 RELEASE_LOCK(&cap->lock);
912 /* ----------------------------------------------------------------------------
913 * Check for threads blocked on BLACKHOLEs that can be woken up
914 * ------------------------------------------------------------------------- */
916 scheduleCheckBlackHoles (Capability *cap)
918 if ( blackholes_need_checking ) // check without the lock first
920 ACQUIRE_LOCK(&sched_mutex);
921 if ( blackholes_need_checking ) {
922 blackholes_need_checking = rtsFalse;
923 // important that we reset the flag *before* checking the
924 // blackhole queue, otherwise we could get deadlock. This
925 // happens as follows: we wake up a thread that
926 // immediately runs on another Capability, blocks on a
927 // blackhole, and then we reset the blackholes_need_checking flag.
928 checkBlackHoles(cap);
930 RELEASE_LOCK(&sched_mutex);
934 /* ----------------------------------------------------------------------------
935 * Detect deadlock conditions and attempt to resolve them.
936 * ------------------------------------------------------------------------- */
939 scheduleDetectDeadlock (Capability *cap, Task *task)
942 * Detect deadlock: when we have no threads to run, there are no
943 * threads blocked, waiting for I/O, or sleeping, and all the
944 * other tasks are waiting for work, we must have a deadlock of
947 if ( emptyThreadQueues(cap) )
949 #if defined(THREADED_RTS)
951 * In the threaded RTS, we only check for deadlock if there
952 * has been no activity in a complete timeslice. This means
953 * we won't eagerly start a full GC just because we don't have
954 * any threads to run currently.
956 if (recent_activity != ACTIVITY_INACTIVE) return;
959 debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
961 // Garbage collection can release some new threads due to
962 // either (a) finalizers or (b) threads resurrected because
963 // they are unreachable and will therefore be sent an
964 // exception. Any threads thus released will be immediately
966 cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/);
967 // when force_major == rtsTrue. scheduleDoGC sets
968 // recent_activity to ACTIVITY_DONE_GC and turns off the timer
971 if ( !emptyRunQueue(cap) ) return;
973 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
974 /* If we have user-installed signal handlers, then wait
975 * for signals to arrive rather then bombing out with a
978 if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
979 debugTrace(DEBUG_sched,
980 "still deadlocked, waiting for signals...");
984 if (signals_pending()) {
985 startSignalHandlers(cap);
988 // either we have threads to run, or we were interrupted:
989 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
995 #if !defined(THREADED_RTS)
996 /* Probably a real deadlock. Send the current main thread the
997 * Deadlock exception.
1000 switch (task->tso->why_blocked) {
1002 case BlockedOnBlackHole:
1003 case BlockedOnException:
1005 throwToSingleThreaded(cap, task->tso,
1006 (StgClosure *)nonTermination_closure);
1009 barf("deadlock: main thread blocked in a strange way");
1018 /* ----------------------------------------------------------------------------
1019 * Send pending messages (PARALLEL_HASKELL only)
1020 * ------------------------------------------------------------------------- */
1022 #if defined(PARALLEL_HASKELL)
1024 scheduleSendPendingMessages(void)
1027 # if defined(PAR) // global Mem.Mgmt., omit for now
1028 if (PendingFetches != END_BF_QUEUE) {
1033 if (RtsFlags.ParFlags.BufferTime) {
1034 // if we use message buffering, we must send away all message
1035 // packets which have become too old...
1041 /* ----------------------------------------------------------------------------
1042 * Activate spark threads (PARALLEL_HASKELL and THREADED_RTS)
1043 * ------------------------------------------------------------------------- */
1045 #if defined(THREADED_RTS)
1047 scheduleActivateSpark(Capability *cap)
1051 createSparkThread(cap);
1052 debugTrace(DEBUG_sched, "creating a spark thread");
1055 #endif // PARALLEL_HASKELL || THREADED_RTS
1057 /* ----------------------------------------------------------------------------
1058 * After running a thread...
1059 * ------------------------------------------------------------------------- */
1062 schedulePostRunThread (Capability *cap, StgTSO *t)
1064 // We have to be able to catch transactions that are in an
1065 // infinite loop as a result of seeing an inconsistent view of
1069 // [a,b] <- mapM readTVar [ta,tb]
1070 // when (a == b) loop
1072 // and a is never equal to b given a consistent view of memory.
1074 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
1075 if (!stmValidateNestOfTransactions (t -> trec)) {
1076 debugTrace(DEBUG_sched | DEBUG_stm,
1077 "trec %p found wasting its time", t);
1079 // strip the stack back to the
1080 // ATOMICALLY_FRAME, aborting the (nested)
1081 // transaction, and saving the stack of any
1082 // partially-evaluated thunks on the heap.
1083 throwToSingleThreaded_(cap, t, NULL, rtsTrue);
1085 ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1089 /* some statistics gathering in the parallel case */
1092 /* -----------------------------------------------------------------------------
1093 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1094 * -------------------------------------------------------------------------- */
1097 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1099 // did the task ask for a large block?
1100 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1101 // if so, get one and push it on the front of the nursery.
1105 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1107 debugTrace(DEBUG_sched,
1108 "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1109 (long)t->id, whatNext_strs[t->what_next], blocks);
1111 // don't do this if the nursery is (nearly) full, we'll GC first.
1112 if (cap->r.rCurrentNursery->link != NULL ||
1113 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1114 // if the nursery has only one block.
1117 bd = allocGroup( blocks );
1119 cap->r.rNursery->n_blocks += blocks;
1121 // link the new group into the list
1122 bd->link = cap->r.rCurrentNursery;
1123 bd->u.back = cap->r.rCurrentNursery->u.back;
1124 if (cap->r.rCurrentNursery->u.back != NULL) {
1125 cap->r.rCurrentNursery->u.back->link = bd;
1127 cap->r.rNursery->blocks = bd;
1129 cap->r.rCurrentNursery->u.back = bd;
1131 // initialise it as a nursery block. We initialise the
1132 // step, gen_no, and flags field of *every* sub-block in
1133 // this large block, because this is easier than making
1134 // sure that we always find the block head of a large
1135 // block whenever we call Bdescr() (eg. evacuate() and
1136 // isAlive() in the GC would both have to do this, at
1140 for (x = bd; x < bd + blocks; x++) {
1141 x->step = cap->r.rNursery;
1147 // This assert can be a killer if the app is doing lots
1148 // of large block allocations.
1149 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1151 // now update the nursery to point to the new block
1152 cap->r.rCurrentNursery = bd;
1154 // we might be unlucky and have another thread get on the
1155 // run queue before us and steal the large block, but in that
1156 // case the thread will just end up requesting another large
1158 pushOnRunQueue(cap,t);
1159 return rtsFalse; /* not actually GC'ing */
1163 debugTrace(DEBUG_sched,
1164 "--<< thread %ld (%s) stopped: HeapOverflow",
1165 (long)t->id, whatNext_strs[t->what_next]);
1167 if (cap->r.rHpLim == NULL || cap->context_switch) {
1168 // Sometimes we miss a context switch, e.g. when calling
1169 // primitives in a tight loop, MAYBE_GC() doesn't check the
1170 // context switch flag, and we end up waiting for a GC.
1171 // See #1984, and concurrent/should_run/1984
1172 cap->context_switch = 0;
1173 addToRunQueue(cap,t);
1175 pushOnRunQueue(cap,t);
1178 /* actual GC is done at the end of the while loop in schedule() */
1181 /* -----------------------------------------------------------------------------
1182 * Handle a thread that returned to the scheduler with ThreadStackOverflow
1183 * -------------------------------------------------------------------------- */
1186 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1188 debugTrace (DEBUG_sched,
1189 "--<< thread %ld (%s) stopped, StackOverflow",
1190 (long)t->id, whatNext_strs[t->what_next]);
1192 /* just adjust the stack for this thread, then pop it back
1196 /* enlarge the stack */
1197 StgTSO *new_t = threadStackOverflow(cap, t);
1199 /* The TSO attached to this Task may have moved, so update the
1202 if (task->tso == t) {
1205 pushOnRunQueue(cap,new_t);
1209 /* -----------------------------------------------------------------------------
1210 * Handle a thread that returned to the scheduler with ThreadYielding
1211 * -------------------------------------------------------------------------- */
1214 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1216 // Reset the context switch flag. We don't do this just before
1217 // running the thread, because that would mean we would lose ticks
1218 // during GC, which can lead to unfair scheduling (a thread hogs
1219 // the CPU because the tick always arrives during GC). This way
1220 // penalises threads that do a lot of allocation, but that seems
1221 // better than the alternative.
1222 cap->context_switch = 0;
1224 /* put the thread back on the run queue. Then, if we're ready to
1225 * GC, check whether this is the last task to stop. If so, wake
1226 * up the GC thread. getThread will block during a GC until the
1230 if (t->what_next != prev_what_next) {
1231 debugTrace(DEBUG_sched,
1232 "--<< thread %ld (%s) stopped to switch evaluators",
1233 (long)t->id, whatNext_strs[t->what_next]);
1235 debugTrace(DEBUG_sched,
1236 "--<< thread %ld (%s) stopped, yielding",
1237 (long)t->id, whatNext_strs[t->what_next]);
1242 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1244 ASSERT(t->_link == END_TSO_QUEUE);
1246 // Shortcut if we're just switching evaluators: don't bother
1247 // doing stack squeezing (which can be expensive), just run the
1249 if (t->what_next != prev_what_next) {
1253 addToRunQueue(cap,t);
1258 /* -----------------------------------------------------------------------------
1259 * Handle a thread that returned to the scheduler with ThreadBlocked
1260 * -------------------------------------------------------------------------- */
1263 scheduleHandleThreadBlocked( StgTSO *t
1270 // We don't need to do anything. The thread is blocked, and it
1271 // has tidied up its stack and placed itself on whatever queue
1272 // it needs to be on.
1274 // ASSERT(t->why_blocked != NotBlocked);
1275 // Not true: for example,
1276 // - in THREADED_RTS, the thread may already have been woken
1277 // up by another Capability. This actually happens: try
1278 // conc023 +RTS -N2.
1279 // - the thread may have woken itself up already, because
1280 // threadPaused() might have raised a blocked throwTo
1281 // exception, see maybePerformBlockedException().
1284 if (traceClass(DEBUG_sched)) {
1285 debugTraceBegin("--<< thread %lu (%s) stopped: ",
1286 (unsigned long)t->id, whatNext_strs[t->what_next]);
1287 printThreadBlockage(t);
1293 /* -----------------------------------------------------------------------------
1294 * Handle a thread that returned to the scheduler with ThreadFinished
1295 * -------------------------------------------------------------------------- */
1298 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1300 /* Need to check whether this was a main thread, and if so,
1301 * return with the return value.
1303 * We also end up here if the thread kills itself with an
1304 * uncaught exception, see Exception.cmm.
1306 debugTrace(DEBUG_sched, "--++ thread %lu (%s) finished",
1307 (unsigned long)t->id, whatNext_strs[t->what_next]);
1309 // blocked exceptions can now complete, even if the thread was in
1310 // blocked mode (see #2910). This unconditionally calls
1311 // lockTSO(), which ensures that we don't miss any threads that
1312 // are engaged in throwTo() with this thread as a target.
1313 awakenBlockedExceptionQueue (cap, t);
1316 // Check whether the thread that just completed was a bound
1317 // thread, and if so return with the result.
1319 // There is an assumption here that all thread completion goes
1320 // through this point; we need to make sure that if a thread
1321 // ends up in the ThreadKilled state, that it stays on the run
1322 // queue so it can be dealt with here.
1327 if (t->bound != task) {
1328 #if !defined(THREADED_RTS)
1329 // Must be a bound thread that is not the topmost one. Leave
1330 // it on the run queue until the stack has unwound to the
1331 // point where we can deal with this. Leaving it on the run
1332 // queue also ensures that the garbage collector knows about
1333 // this thread and its return value (it gets dropped from the
1334 // step->threads list so there's no other way to find it).
1335 appendToRunQueue(cap,t);
1338 // this cannot happen in the threaded RTS, because a
1339 // bound thread can only be run by the appropriate Task.
1340 barf("finished bound thread that isn't mine");
1344 ASSERT(task->tso == t);
1346 if (t->what_next == ThreadComplete) {
1348 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1349 *(task->ret) = (StgClosure *)task->tso->sp[1];
1351 task->stat = Success;
1354 *(task->ret) = NULL;
1356 if (sched_state >= SCHED_INTERRUPTING) {
1357 if (heap_overflow) {
1358 task->stat = HeapExhausted;
1360 task->stat = Interrupted;
1363 task->stat = Killed;
1367 removeThreadLabel((StgWord)task->tso->id);
1369 return rtsTrue; // tells schedule() to return
1375 /* -----------------------------------------------------------------------------
1376 * Perform a heap census
1377 * -------------------------------------------------------------------------- */
1380 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1382 // When we have +RTS -i0 and we're heap profiling, do a census at
1383 // every GC. This lets us get repeatable runs for debugging.
1384 if (performHeapProfile ||
1385 (RtsFlags.ProfFlags.profileInterval==0 &&
1386 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1393 /* -----------------------------------------------------------------------------
1394 * Perform a garbage collection if necessary
1395 * -------------------------------------------------------------------------- */
1398 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS, rtsBool force_major)
1400 rtsBool heap_census;
1402 /* extern static volatile StgWord waiting_for_gc;
1403 lives inside capability.c */
1404 rtsBool gc_type, prev_pending_gc;
1408 if (sched_state == SCHED_SHUTTING_DOWN) {
1409 // The final GC has already been done, and the system is
1410 // shutting down. We'll probably deadlock if we try to GC
1416 if (sched_state < SCHED_INTERRUPTING
1417 && RtsFlags.ParFlags.parGcEnabled
1418 && N >= RtsFlags.ParFlags.parGcGen
1419 && ! oldest_gen->steps[0].mark)
1421 gc_type = PENDING_GC_PAR;
1423 gc_type = PENDING_GC_SEQ;
1426 // In order to GC, there must be no threads running Haskell code.
1427 // Therefore, the GC thread needs to hold *all* the capabilities,
1428 // and release them after the GC has completed.
1430 // This seems to be the simplest way: previous attempts involved
1431 // making all the threads with capabilities give up their
1432 // capabilities and sleep except for the *last* one, which
1433 // actually did the GC. But it's quite hard to arrange for all
1434 // the other tasks to sleep and stay asleep.
1437 /* Other capabilities are prevented from running yet more Haskell
1438 threads if waiting_for_gc is set. Tested inside
1439 yieldCapability() and releaseCapability() in Capability.c */
1441 prev_pending_gc = cas(&waiting_for_gc, 0, gc_type);
1442 if (prev_pending_gc) {
1444 debugTrace(DEBUG_sched, "someone else is trying to GC (%d)...",
1447 yieldCapability(&cap,task);
1448 } while (waiting_for_gc);
1449 return cap; // NOTE: task->cap might have changed here
1452 setContextSwitches();
1454 // The final shutdown GC is always single-threaded, because it's
1455 // possible that some of the Capabilities have no worker threads.
1457 if (gc_type == PENDING_GC_SEQ)
1459 postEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
1460 // single-threaded GC: grab all the capabilities
1461 for (i=0; i < n_capabilities; i++) {
1462 debugTrace(DEBUG_sched, "ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities);
1463 if (cap != &capabilities[i]) {
1464 Capability *pcap = &capabilities[i];
1465 // we better hope this task doesn't get migrated to
1466 // another Capability while we're waiting for this one.
1467 // It won't, because load balancing happens while we have
1468 // all the Capabilities, but even so it's a slightly
1469 // unsavoury invariant.
1471 waitForReturnCapability(&pcap, task);
1472 if (pcap != &capabilities[i]) {
1473 barf("scheduleDoGC: got the wrong capability");
1480 // multi-threaded GC: make sure all the Capabilities donate one
1482 postEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
1483 debugTrace(DEBUG_sched, "ready_to_gc, grabbing GC threads");
1485 waitForGcThreads(cap);
1489 // so this happens periodically:
1490 if (cap) scheduleCheckBlackHoles(cap);
1492 IF_DEBUG(scheduler, printAllThreads());
1494 delete_threads_and_gc:
1496 * We now have all the capabilities; if we're in an interrupting
1497 * state, then we should take the opportunity to delete all the
1498 * threads in the system.
1500 if (sched_state == SCHED_INTERRUPTING) {
1501 deleteAllThreads(cap);
1502 sched_state = SCHED_SHUTTING_DOWN;
1505 heap_census = scheduleNeedHeapProfile(rtsTrue);
1507 #if defined(THREADED_RTS)
1508 postEvent(cap, EVENT_GC_START, 0, 0);
1509 debugTrace(DEBUG_sched, "doing GC");
1510 // reset waiting_for_gc *before* GC, so that when the GC threads
1511 // emerge they don't immediately re-enter the GC.
1513 GarbageCollect(force_major || heap_census, gc_type, cap);
1515 GarbageCollect(force_major || heap_census, 0, cap);
1517 postEvent(cap, EVENT_GC_END, 0, 0);
1519 if (recent_activity == ACTIVITY_INACTIVE && force_major)
1521 // We are doing a GC because the system has been idle for a
1522 // timeslice and we need to check for deadlock. Record the
1523 // fact that we've done a GC and turn off the timer signal;
1524 // it will get re-enabled if we run any threads after the GC.
1525 recent_activity = ACTIVITY_DONE_GC;
1530 // the GC might have taken long enough for the timer to set
1531 // recent_activity = ACTIVITY_INACTIVE, but we aren't
1532 // necessarily deadlocked:
1533 recent_activity = ACTIVITY_YES;
1536 #if defined(THREADED_RTS)
1537 if (gc_type == PENDING_GC_PAR)
1539 releaseGCThreads(cap);
1544 debugTrace(DEBUG_sched, "performing heap census");
1546 performHeapProfile = rtsFalse;
1549 if (heap_overflow && sched_state < SCHED_INTERRUPTING) {
1550 // GC set the heap_overflow flag, so we should proceed with
1551 // an orderly shutdown now. Ultimately we want the main
1552 // thread to return to its caller with HeapExhausted, at which
1553 // point the caller should call hs_exit(). The first step is
1554 // to delete all the threads.
1556 // Another way to do this would be to raise an exception in
1557 // the main thread, which we really should do because it gives
1558 // the program a chance to clean up. But how do we find the
1559 // main thread? It should presumably be the same one that
1560 // gets ^C exceptions, but that's all done on the Haskell side
1561 // (GHC.TopHandler).
1562 sched_state = SCHED_INTERRUPTING;
1563 goto delete_threads_and_gc;
1568 Once we are all together... this would be the place to balance all
1569 spark pools. No concurrent stealing or adding of new sparks can
1570 occur. Should be defined in Sparks.c. */
1571 balanceSparkPoolsCaps(n_capabilities, capabilities);
1574 #if defined(THREADED_RTS)
1575 if (gc_type == PENDING_GC_SEQ) {
1576 // release our stash of capabilities.
1577 for (i = 0; i < n_capabilities; i++) {
1578 if (cap != &capabilities[i]) {
1579 task->cap = &capabilities[i];
1580 releaseCapability(&capabilities[i]);
1594 /* ---------------------------------------------------------------------------
1595 * Singleton fork(). Do not copy any running threads.
1596 * ------------------------------------------------------------------------- */
1599 forkProcess(HsStablePtr *entry
1600 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
1605 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
1612 #if defined(THREADED_RTS)
1613 if (RtsFlags.ParFlags.nNodes > 1) {
1614 errorBelch("forking not supported with +RTS -N<n> greater than 1");
1615 stg_exit(EXIT_FAILURE);
1619 debugTrace(DEBUG_sched, "forking!");
1621 // ToDo: for SMP, we should probably acquire *all* the capabilities
1624 // no funny business: hold locks while we fork, otherwise if some
1625 // other thread is holding a lock when the fork happens, the data
1626 // structure protected by the lock will forever be in an
1627 // inconsistent state in the child. See also #1391.
1628 ACQUIRE_LOCK(&sched_mutex);
1629 ACQUIRE_LOCK(&cap->lock);
1630 ACQUIRE_LOCK(&cap->running_task->lock);
1634 if (pid) { // parent
1636 RELEASE_LOCK(&sched_mutex);
1637 RELEASE_LOCK(&cap->lock);
1638 RELEASE_LOCK(&cap->running_task->lock);
1640 // just return the pid
1646 #if defined(THREADED_RTS)
1647 initMutex(&sched_mutex);
1648 initMutex(&cap->lock);
1649 initMutex(&cap->running_task->lock);
1652 // Now, all OS threads except the thread that forked are
1653 // stopped. We need to stop all Haskell threads, including
1654 // those involved in foreign calls. Also we need to delete
1655 // all Tasks, because they correspond to OS threads that are
1658 for (s = 0; s < total_steps; s++) {
1659 for (t = all_steps[s].threads; t != END_TSO_QUEUE; t = next) {
1660 if (t->what_next == ThreadRelocated) {
1663 next = t->global_link;
1664 // don't allow threads to catch the ThreadKilled
1665 // exception, but we do want to raiseAsync() because these
1666 // threads may be evaluating thunks that we need later.
1667 deleteThread_(cap,t);
1672 // Empty the run queue. It seems tempting to let all the
1673 // killed threads stay on the run queue as zombies to be
1674 // cleaned up later, but some of them correspond to bound
1675 // threads for which the corresponding Task does not exist.
1676 cap->run_queue_hd = END_TSO_QUEUE;
1677 cap->run_queue_tl = END_TSO_QUEUE;
1679 // Any suspended C-calling Tasks are no more, their OS threads
1681 cap->suspended_ccalling_tasks = NULL;
1683 // Empty the threads lists. Otherwise, the garbage
1684 // collector may attempt to resurrect some of these threads.
1685 for (s = 0; s < total_steps; s++) {
1686 all_steps[s].threads = END_TSO_QUEUE;
1689 // Wipe the task list, except the current Task.
1690 ACQUIRE_LOCK(&sched_mutex);
1691 for (task = all_tasks; task != NULL; task=task->all_link) {
1692 if (task != cap->running_task) {
1693 #if defined(THREADED_RTS)
1694 initMutex(&task->lock); // see #1391
1699 RELEASE_LOCK(&sched_mutex);
1701 #if defined(THREADED_RTS)
1702 // Wipe our spare workers list, they no longer exist. New
1703 // workers will be created if necessary.
1704 cap->spare_workers = NULL;
1705 cap->returning_tasks_hd = NULL;
1706 cap->returning_tasks_tl = NULL;
1709 // On Unix, all timers are reset in the child, so we need to start
1714 cap = rts_evalStableIO(cap, entry, NULL); // run the action
1715 rts_checkSchedStatus("forkProcess",cap);
1718 hs_exit(); // clean up and exit
1719 stg_exit(EXIT_SUCCESS);
1721 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
1722 barf("forkProcess#: primop not supported on this platform, sorry!\n");
1726 /* ---------------------------------------------------------------------------
1727 * Delete all the threads in the system
1728 * ------------------------------------------------------------------------- */
1731 deleteAllThreads ( Capability *cap )
1733 // NOTE: only safe to call if we own all capabilities.
1738 debugTrace(DEBUG_sched,"deleting all threads");
1739 for (s = 0; s < total_steps; s++) {
1740 for (t = all_steps[s].threads; t != END_TSO_QUEUE; t = next) {
1741 if (t->what_next == ThreadRelocated) {
1744 next = t->global_link;
1745 deleteThread(cap,t);
1750 // The run queue now contains a bunch of ThreadKilled threads. We
1751 // must not throw these away: the main thread(s) will be in there
1752 // somewhere, and the main scheduler loop has to deal with it.
1753 // Also, the run queue is the only thing keeping these threads from
1754 // being GC'd, and we don't want the "main thread has been GC'd" panic.
1756 #if !defined(THREADED_RTS)
1757 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
1758 ASSERT(sleeping_queue == END_TSO_QUEUE);
1762 /* -----------------------------------------------------------------------------
1763 Managing the suspended_ccalling_tasks list.
1764 Locks required: sched_mutex
1765 -------------------------------------------------------------------------- */
1768 suspendTask (Capability *cap, Task *task)
1770 ASSERT(task->next == NULL && task->prev == NULL);
1771 task->next = cap->suspended_ccalling_tasks;
1773 if (cap->suspended_ccalling_tasks) {
1774 cap->suspended_ccalling_tasks->prev = task;
1776 cap->suspended_ccalling_tasks = task;
1780 recoverSuspendedTask (Capability *cap, Task *task)
1783 task->prev->next = task->next;
1785 ASSERT(cap->suspended_ccalling_tasks == task);
1786 cap->suspended_ccalling_tasks = task->next;
1789 task->next->prev = task->prev;
1791 task->next = task->prev = NULL;
1794 /* ---------------------------------------------------------------------------
1795 * Suspending & resuming Haskell threads.
1797 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1798 * its capability before calling the C function. This allows another
1799 * task to pick up the capability and carry on running Haskell
1800 * threads. It also means that if the C call blocks, it won't lock
1803 * The Haskell thread making the C call is put to sleep for the
1804 * duration of the call, on the susepended_ccalling_threads queue. We
1805 * give out a token to the task, which it can use to resume the thread
1806 * on return from the C function.
1807 * ------------------------------------------------------------------------- */
1810 suspendThread (StgRegTable *reg)
1817 StgWord32 saved_winerror;
1820 saved_errno = errno;
1822 saved_winerror = GetLastError();
1825 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
1827 cap = regTableToCapability(reg);
1829 task = cap->running_task;
1830 tso = cap->r.rCurrentTSO;
1832 postEvent(cap, EVENT_STOP_THREAD, tso->id, THREAD_SUSPENDED_FOREIGN_CALL);
1833 debugTrace(DEBUG_sched,
1834 "thread %lu did a safe foreign call",
1835 (unsigned long)cap->r.rCurrentTSO->id);
1837 // XXX this might not be necessary --SDM
1838 tso->what_next = ThreadRunGHC;
1840 threadPaused(cap,tso);
1842 if ((tso->flags & TSO_BLOCKEX) == 0) {
1843 tso->why_blocked = BlockedOnCCall;
1844 tso->flags |= TSO_BLOCKEX;
1845 tso->flags &= ~TSO_INTERRUPTIBLE;
1847 tso->why_blocked = BlockedOnCCall_NoUnblockExc;
1850 // Hand back capability
1851 task->suspended_tso = tso;
1853 ACQUIRE_LOCK(&cap->lock);
1855 suspendTask(cap,task);
1856 cap->in_haskell = rtsFalse;
1857 releaseCapability_(cap,rtsFalse);
1859 RELEASE_LOCK(&cap->lock);
1861 #if defined(THREADED_RTS)
1862 /* Preparing to leave the RTS, so ensure there's a native thread/task
1863 waiting to take over.
1865 debugTrace(DEBUG_sched, "thread %lu: leaving RTS", (unsigned long)tso->id);
1868 errno = saved_errno;
1870 SetLastError(saved_winerror);
1876 resumeThread (void *task_)
1883 StgWord32 saved_winerror;
1886 saved_errno = errno;
1888 saved_winerror = GetLastError();
1892 // Wait for permission to re-enter the RTS with the result.
1893 waitForReturnCapability(&cap,task);
1894 // we might be on a different capability now... but if so, our
1895 // entry on the suspended_ccalling_tasks list will also have been
1898 // Remove the thread from the suspended list
1899 recoverSuspendedTask(cap,task);
1901 tso = task->suspended_tso;
1902 task->suspended_tso = NULL;
1903 tso->_link = END_TSO_QUEUE; // no write barrier reqd
1905 postEvent(cap, EVENT_RUN_THREAD, tso->id, 0);
1906 debugTrace(DEBUG_sched, "thread %lu: re-entering RTS", (unsigned long)tso->id);
1908 if (tso->why_blocked == BlockedOnCCall) {
1909 // avoid locking the TSO if we don't have to
1910 if (tso->blocked_exceptions != END_TSO_QUEUE) {
1911 awakenBlockedExceptionQueue(cap,tso);
1913 tso->flags &= ~(TSO_BLOCKEX | TSO_INTERRUPTIBLE);
1916 /* Reset blocking status */
1917 tso->why_blocked = NotBlocked;
1919 cap->r.rCurrentTSO = tso;
1920 cap->in_haskell = rtsTrue;
1921 errno = saved_errno;
1923 SetLastError(saved_winerror);
1926 /* We might have GC'd, mark the TSO dirty again */
1929 IF_DEBUG(sanity, checkTSO(tso));
1934 /* ---------------------------------------------------------------------------
1937 * scheduleThread puts a thread on the end of the runnable queue.
1938 * This will usually be done immediately after a thread is created.
1939 * The caller of scheduleThread must create the thread using e.g.
1940 * createThread and push an appropriate closure
1941 * on this thread's stack before the scheduler is invoked.
1942 * ------------------------------------------------------------------------ */
1945 scheduleThread(Capability *cap, StgTSO *tso)
1947 // The thread goes at the *end* of the run-queue, to avoid possible
1948 // starvation of any threads already on the queue.
1949 appendToRunQueue(cap,tso);
1953 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
1955 #if defined(THREADED_RTS)
1956 tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
1957 // move this thread from now on.
1958 cpu %= RtsFlags.ParFlags.nNodes;
1959 if (cpu == cap->no) {
1960 appendToRunQueue(cap,tso);
1962 postEvent (cap, EVENT_MIGRATE_THREAD, tso->id, capabilities[cpu].no);
1963 wakeupThreadOnCapability(cap, &capabilities[cpu], tso);
1966 appendToRunQueue(cap,tso);
1971 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
1975 // We already created/initialised the Task
1976 task = cap->running_task;
1978 // This TSO is now a bound thread; make the Task and TSO
1979 // point to each other.
1985 task->stat = NoStatus;
1987 appendToRunQueue(cap,tso);
1989 debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)tso->id);
1991 cap = schedule(cap,task);
1993 ASSERT(task->stat != NoStatus);
1994 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
1996 debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)task->tso->id);
2000 /* ----------------------------------------------------------------------------
2002 * ------------------------------------------------------------------------- */
2004 #if defined(THREADED_RTS)
2005 void OSThreadProcAttr
2006 workerStart(Task *task)
2010 // See startWorkerTask().
2011 ACQUIRE_LOCK(&task->lock);
2013 RELEASE_LOCK(&task->lock);
2015 if (RtsFlags.ParFlags.setAffinity) {
2016 setThreadAffinity(cap->no, n_capabilities);
2019 // set the thread-local pointer to the Task:
2022 // schedule() runs without a lock.
2023 cap = schedule(cap,task);
2025 // On exit from schedule(), we have a Capability, but possibly not
2026 // the same one we started with.
2028 // During shutdown, the requirement is that after all the
2029 // Capabilities are shut down, all workers that are shutting down
2030 // have finished workerTaskStop(). This is why we hold on to
2031 // cap->lock until we've finished workerTaskStop() below.
2033 // There may be workers still involved in foreign calls; those
2034 // will just block in waitForReturnCapability() because the
2035 // Capability has been shut down.
2037 ACQUIRE_LOCK(&cap->lock);
2038 releaseCapability_(cap,rtsFalse);
2039 workerTaskStop(task);
2040 RELEASE_LOCK(&cap->lock);
2044 /* ---------------------------------------------------------------------------
2047 * Initialise the scheduler. This resets all the queues - if the
2048 * queues contained any threads, they'll be garbage collected at the
2051 * ------------------------------------------------------------------------ */
2056 #if !defined(THREADED_RTS)
2057 blocked_queue_hd = END_TSO_QUEUE;
2058 blocked_queue_tl = END_TSO_QUEUE;
2059 sleeping_queue = END_TSO_QUEUE;
2062 blackhole_queue = END_TSO_QUEUE;
2064 sched_state = SCHED_RUNNING;
2065 recent_activity = ACTIVITY_YES;
2067 #if defined(THREADED_RTS)
2068 /* Initialise the mutex and condition variables used by
2070 initMutex(&sched_mutex);
2073 ACQUIRE_LOCK(&sched_mutex);
2075 /* A capability holds the state a native thread needs in
2076 * order to execute STG code. At least one capability is
2077 * floating around (only THREADED_RTS builds have more than one).
2083 #if defined(THREADED_RTS)
2087 #if defined(THREADED_RTS)
2089 * Eagerly start one worker to run each Capability, except for
2090 * Capability 0. The idea is that we're probably going to start a
2091 * bound thread on Capability 0 pretty soon, so we don't want a
2092 * worker task hogging it.
2097 for (i = 1; i < n_capabilities; i++) {
2098 cap = &capabilities[i];
2099 ACQUIRE_LOCK(&cap->lock);
2100 startWorkerTask(cap, workerStart);
2101 RELEASE_LOCK(&cap->lock);
2106 RELEASE_LOCK(&sched_mutex);
2111 rtsBool wait_foreign
2112 #if !defined(THREADED_RTS)
2113 __attribute__((unused))
2116 /* see Capability.c, shutdownCapability() */
2120 task = newBoundTask();
2122 // If we haven't killed all the threads yet, do it now.
2123 if (sched_state < SCHED_SHUTTING_DOWN) {
2124 sched_state = SCHED_INTERRUPTING;
2125 waitForReturnCapability(&task->cap,task);
2126 scheduleDoGC(task->cap,task,rtsFalse);
2127 releaseCapability(task->cap);
2129 sched_state = SCHED_SHUTTING_DOWN;
2131 #if defined(THREADED_RTS)
2135 for (i = 0; i < n_capabilities; i++) {
2136 shutdownCapability(&capabilities[i], task, wait_foreign);
2138 boundTaskExiting(task);
2144 freeScheduler( void )
2148 ACQUIRE_LOCK(&sched_mutex);
2149 still_running = freeTaskManager();
2150 // We can only free the Capabilities if there are no Tasks still
2151 // running. We might have a Task about to return from a foreign
2152 // call into waitForReturnCapability(), for example (actually,
2153 // this should be the *only* thing that a still-running Task can
2154 // do at this point, and it will block waiting for the
2156 if (still_running == 0) {
2158 if (n_capabilities != 1) {
2159 stgFree(capabilities);
2162 RELEASE_LOCK(&sched_mutex);
2163 #if defined(THREADED_RTS)
2164 closeMutex(&sched_mutex);
2168 /* -----------------------------------------------------------------------------
2171 This is the interface to the garbage collector from Haskell land.
2172 We provide this so that external C code can allocate and garbage
2173 collect when called from Haskell via _ccall_GC.
2174 -------------------------------------------------------------------------- */
2177 performGC_(rtsBool force_major)
2181 // We must grab a new Task here, because the existing Task may be
2182 // associated with a particular Capability, and chained onto the
2183 // suspended_ccalling_tasks queue.
2184 task = newBoundTask();
2186 waitForReturnCapability(&task->cap,task);
2187 scheduleDoGC(task->cap,task,force_major);
2188 releaseCapability(task->cap);
2189 boundTaskExiting(task);
2195 performGC_(rtsFalse);
2199 performMajorGC(void)
2201 performGC_(rtsTrue);
2204 /* -----------------------------------------------------------------------------
2207 If the thread has reached its maximum stack size, then raise the
2208 StackOverflow exception in the offending thread. Otherwise
2209 relocate the TSO into a larger chunk of memory and adjust its stack
2211 -------------------------------------------------------------------------- */
2214 threadStackOverflow(Capability *cap, StgTSO *tso)
2216 nat new_stack_size, stack_words;
2221 IF_DEBUG(sanity,checkTSO(tso));
2223 // don't allow throwTo() to modify the blocked_exceptions queue
2224 // while we are moving the TSO:
2225 lockClosure((StgClosure *)tso);
2227 if (tso->stack_size >= tso->max_stack_size && !(tso->flags & TSO_BLOCKEX)) {
2228 // NB. never raise a StackOverflow exception if the thread is
2229 // inside Control.Exceptino.block. It is impractical to protect
2230 // against stack overflow exceptions, since virtually anything
2231 // can raise one (even 'catch'), so this is the only sensible
2232 // thing to do here. See bug #767.
2234 debugTrace(DEBUG_gc,
2235 "threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)",
2236 (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
2238 /* If we're debugging, just print out the top of the stack */
2239 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2242 // Send this thread the StackOverflow exception
2244 throwToSingleThreaded(cap, tso, (StgClosure *)stackOverflow_closure);
2248 /* Try to double the current stack size. If that takes us over the
2249 * maximum stack size for this thread, then use the maximum instead
2250 * (that is, unless we're already at or over the max size and we
2251 * can't raise the StackOverflow exception (see above), in which
2252 * case just double the size). Finally round up so the TSO ends up as
2253 * a whole number of blocks.
2255 if (tso->stack_size >= tso->max_stack_size) {
2256 new_stack_size = tso->stack_size * 2;
2258 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2260 new_tso_size = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2261 TSO_STRUCT_SIZE)/sizeof(W_);
2262 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2263 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2265 debugTrace(DEBUG_sched,
2266 "increasing stack size from %ld words to %d.",
2267 (long)tso->stack_size, new_stack_size);
2269 dest = (StgTSO *)allocateLocal(cap,new_tso_size);
2270 TICK_ALLOC_TSO(new_stack_size,0);
2272 /* copy the TSO block and the old stack into the new area */
2273 memcpy(dest,tso,TSO_STRUCT_SIZE);
2274 stack_words = tso->stack + tso->stack_size - tso->sp;
2275 new_sp = (P_)dest + new_tso_size - stack_words;
2276 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2278 /* relocate the stack pointers... */
2280 dest->stack_size = new_stack_size;
2282 /* Mark the old TSO as relocated. We have to check for relocated
2283 * TSOs in the garbage collector and any primops that deal with TSOs.
2285 * It's important to set the sp value to just beyond the end
2286 * of the stack, so we don't attempt to scavenge any part of the
2289 tso->what_next = ThreadRelocated;
2290 setTSOLink(cap,tso,dest);
2291 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2292 tso->why_blocked = NotBlocked;
2297 IF_DEBUG(sanity,checkTSO(dest));
2299 IF_DEBUG(scheduler,printTSO(dest));
2306 threadStackUnderflow (Task *task STG_UNUSED, StgTSO *tso)
2308 bdescr *bd, *new_bd;
2309 lnat free_w, tso_size_w;
2312 tso_size_w = tso_sizeW(tso);
2314 if (tso_size_w < MBLOCK_SIZE_W ||
2315 // TSO is less than 2 mblocks (since the first mblock is
2316 // shorter than MBLOCK_SIZE_W)
2317 (tso_size_w - BLOCKS_PER_MBLOCK*BLOCK_SIZE_W) % MBLOCK_SIZE_W != 0 ||
2318 // or TSO is not a whole number of megablocks (ensuring
2319 // precondition of splitLargeBlock() below)
2320 (tso_size_w <= round_up_to_mblocks(RtsFlags.GcFlags.initialStkSize)) ||
2321 // or TSO is smaller than the minimum stack size (rounded up)
2322 (nat)(tso->stack + tso->stack_size - tso->sp) > tso->stack_size / 4)
2323 // or stack is using more than 1/4 of the available space
2329 // don't allow throwTo() to modify the blocked_exceptions queue
2330 // while we are moving the TSO:
2331 lockClosure((StgClosure *)tso);
2333 // this is the number of words we'll free
2334 free_w = round_to_mblocks(tso_size_w/2);
2336 bd = Bdescr((StgPtr)tso);
2337 new_bd = splitLargeBlock(bd, free_w / BLOCK_SIZE_W);
2338 bd->free = bd->start + TSO_STRUCT_SIZEW;
2340 new_tso = (StgTSO *)new_bd->start;
2341 memcpy(new_tso,tso,TSO_STRUCT_SIZE);
2342 new_tso->stack_size = new_bd->free - new_tso->stack;
2344 debugTrace(DEBUG_sched, "thread %ld: reducing TSO size from %lu words to %lu",
2345 (long)tso->id, tso_size_w, tso_sizeW(new_tso));
2347 tso->what_next = ThreadRelocated;
2348 tso->_link = new_tso; // no write barrier reqd: same generation
2350 // The TSO attached to this Task may have moved, so update the
2352 if (task->tso == tso) {
2353 task->tso = new_tso;
2359 IF_DEBUG(sanity,checkTSO(new_tso));
2364 /* ---------------------------------------------------------------------------
2366 - usually called inside a signal handler so it mustn't do anything fancy.
2367 ------------------------------------------------------------------------ */
2370 interruptStgRts(void)
2372 sched_state = SCHED_INTERRUPTING;
2373 setContextSwitches();
2374 #if defined(THREADED_RTS)
2379 /* -----------------------------------------------------------------------------
2382 This function causes at least one OS thread to wake up and run the
2383 scheduler loop. It is invoked when the RTS might be deadlocked, or
2384 an external event has arrived that may need servicing (eg. a
2385 keyboard interrupt).
2387 In the single-threaded RTS we don't do anything here; we only have
2388 one thread anyway, and the event that caused us to want to wake up
2389 will have interrupted any blocking system call in progress anyway.
2390 -------------------------------------------------------------------------- */
2392 #if defined(THREADED_RTS)
2393 void wakeUpRts(void)
2395 // This forces the IO Manager thread to wakeup, which will
2396 // in turn ensure that some OS thread wakes up and runs the
2397 // scheduler loop, which will cause a GC and deadlock check.
2402 /* -----------------------------------------------------------------------------
2405 * Check the blackhole_queue for threads that can be woken up. We do
2406 * this periodically: before every GC, and whenever the run queue is
2409 * An elegant solution might be to just wake up all the blocked
2410 * threads with awakenBlockedQueue occasionally: they'll go back to
2411 * sleep again if the object is still a BLACKHOLE. Unfortunately this
2412 * doesn't give us a way to tell whether we've actually managed to
2413 * wake up any threads, so we would be busy-waiting.
2415 * -------------------------------------------------------------------------- */
2418 checkBlackHoles (Capability *cap)
2421 rtsBool any_woke_up = rtsFalse;
2424 // blackhole_queue is global:
2425 ASSERT_LOCK_HELD(&sched_mutex);
2427 debugTrace(DEBUG_sched, "checking threads blocked on black holes");
2429 // ASSUMES: sched_mutex
2430 prev = &blackhole_queue;
2431 t = blackhole_queue;
2432 while (t != END_TSO_QUEUE) {
2433 if (t->what_next == ThreadRelocated) {
2437 ASSERT(t->why_blocked == BlockedOnBlackHole);
2438 type = get_itbl(UNTAG_CLOSURE(t->block_info.closure))->type;
2439 if (type != BLACKHOLE && type != CAF_BLACKHOLE) {
2440 IF_DEBUG(sanity,checkTSO(t));
2441 t = unblockOne(cap, t);
2443 any_woke_up = rtsTrue;
2453 /* -----------------------------------------------------------------------------
2456 This is used for interruption (^C) and forking, and corresponds to
2457 raising an exception but without letting the thread catch the
2459 -------------------------------------------------------------------------- */
2462 deleteThread (Capability *cap, StgTSO *tso)
2464 // NOTE: must only be called on a TSO that we have exclusive
2465 // access to, because we will call throwToSingleThreaded() below.
2466 // The TSO must be on the run queue of the Capability we own, or
2467 // we must own all Capabilities.
2469 if (tso->why_blocked != BlockedOnCCall &&
2470 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
2471 throwToSingleThreaded(cap,tso,NULL);
2475 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2477 deleteThread_(Capability *cap, StgTSO *tso)
2478 { // for forkProcess only:
2479 // like deleteThread(), but we delete threads in foreign calls, too.
2481 if (tso->why_blocked == BlockedOnCCall ||
2482 tso->why_blocked == BlockedOnCCall_NoUnblockExc) {
2483 unblockOne(cap,tso);
2484 tso->what_next = ThreadKilled;
2486 deleteThread(cap,tso);
2491 /* -----------------------------------------------------------------------------
2492 raiseExceptionHelper
2494 This function is called by the raise# primitve, just so that we can
2495 move some of the tricky bits of raising an exception from C-- into
2496 C. Who knows, it might be a useful re-useable thing here too.
2497 -------------------------------------------------------------------------- */
2500 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2502 Capability *cap = regTableToCapability(reg);
2503 StgThunk *raise_closure = NULL;
2505 StgRetInfoTable *info;
2507 // This closure represents the expression 'raise# E' where E
2508 // is the exception raise. It is used to overwrite all the
2509 // thunks which are currently under evaluataion.
2512 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2513 // LDV profiling: stg_raise_info has THUNK as its closure
2514 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2515 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
2516 // 1 does not cause any problem unless profiling is performed.
2517 // However, when LDV profiling goes on, we need to linearly scan
2518 // small object pool, where raise_closure is stored, so we should
2519 // use MIN_UPD_SIZE.
2521 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2522 // sizeofW(StgClosure)+1);
2526 // Walk up the stack, looking for the catch frame. On the way,
2527 // we update any closures pointed to from update frames with the
2528 // raise closure that we just built.
2532 info = get_ret_itbl((StgClosure *)p);
2533 next = p + stack_frame_sizeW((StgClosure *)p);
2534 switch (info->i.type) {
2537 // Only create raise_closure if we need to.
2538 if (raise_closure == NULL) {
2540 (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
2541 SET_HDR(raise_closure, &stg_raise_info, CCCS);
2542 raise_closure->payload[0] = exception;
2544 UPD_IND(((StgUpdateFrame *)p)->updatee,(StgClosure *)raise_closure);
2548 case ATOMICALLY_FRAME:
2549 debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
2551 return ATOMICALLY_FRAME;
2557 case CATCH_STM_FRAME:
2558 debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
2560 return CATCH_STM_FRAME;
2566 case CATCH_RETRY_FRAME:
2575 /* -----------------------------------------------------------------------------
2576 findRetryFrameHelper
2578 This function is called by the retry# primitive. It traverses the stack
2579 leaving tso->sp referring to the frame which should handle the retry.
2581 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
2582 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
2584 We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
2585 create) because retries are not considered to be exceptions, despite the
2586 similar implementation.
2588 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
2589 not be created within memory transactions.
2590 -------------------------------------------------------------------------- */
2593 findRetryFrameHelper (StgTSO *tso)
2596 StgRetInfoTable *info;
2600 info = get_ret_itbl((StgClosure *)p);
2601 next = p + stack_frame_sizeW((StgClosure *)p);
2602 switch (info->i.type) {
2604 case ATOMICALLY_FRAME:
2605 debugTrace(DEBUG_stm,
2606 "found ATOMICALLY_FRAME at %p during retry", p);
2608 return ATOMICALLY_FRAME;
2610 case CATCH_RETRY_FRAME:
2611 debugTrace(DEBUG_stm,
2612 "found CATCH_RETRY_FRAME at %p during retrry", p);
2614 return CATCH_RETRY_FRAME;
2616 case CATCH_STM_FRAME: {
2617 StgTRecHeader *trec = tso -> trec;
2618 StgTRecHeader *outer = stmGetEnclosingTRec(trec);
2619 debugTrace(DEBUG_stm,
2620 "found CATCH_STM_FRAME at %p during retry", p);
2621 debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
2622 stmAbortTransaction(tso -> cap, trec);
2623 stmFreeAbortedTRec(tso -> cap, trec);
2624 tso -> trec = outer;
2631 ASSERT(info->i.type != CATCH_FRAME);
2632 ASSERT(info->i.type != STOP_FRAME);
2639 /* -----------------------------------------------------------------------------
2640 resurrectThreads is called after garbage collection on the list of
2641 threads found to be garbage. Each of these threads will be woken
2642 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2643 on an MVar, or NonTermination if the thread was blocked on a Black
2646 Locks: assumes we hold *all* the capabilities.
2647 -------------------------------------------------------------------------- */
2650 resurrectThreads (StgTSO *threads)
2656 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2657 next = tso->global_link;
2659 step = Bdescr((P_)tso)->step;
2660 tso->global_link = step->threads;
2661 step->threads = tso;
2663 debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
2665 // Wake up the thread on the Capability it was last on
2668 switch (tso->why_blocked) {
2670 case BlockedOnException:
2671 /* Called by GC - sched_mutex lock is currently held. */
2672 throwToSingleThreaded(cap, tso,
2673 (StgClosure *)blockedOnDeadMVar_closure);
2675 case BlockedOnBlackHole:
2676 throwToSingleThreaded(cap, tso,
2677 (StgClosure *)nonTermination_closure);
2680 throwToSingleThreaded(cap, tso,
2681 (StgClosure *)blockedIndefinitely_closure);
2684 /* This might happen if the thread was blocked on a black hole
2685 * belonging to a thread that we've just woken up (raiseAsync
2686 * can wake up threads, remember...).
2690 barf("resurrectThreads: thread blocked in a strange way");
2695 /* -----------------------------------------------------------------------------
2696 performPendingThrowTos is called after garbage collection, and
2697 passed a list of threads that were found to have pending throwTos
2698 (tso->blocked_exceptions was not empty), and were blocked.
2699 Normally this doesn't happen, because we would deliver the
2700 exception directly if the target thread is blocked, but there are
2701 small windows where it might occur on a multiprocessor (see
2704 NB. we must be holding all the capabilities at this point, just
2705 like resurrectThreads().
2706 -------------------------------------------------------------------------- */
2709 performPendingThrowTos (StgTSO *threads)
2715 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2716 next = tso->global_link;
2718 step = Bdescr((P_)tso)->step;
2719 tso->global_link = step->threads;
2720 step->threads = tso;
2722 debugTrace(DEBUG_sched, "performing blocked throwTo to thread %lu", (unsigned long)tso->id);
2725 maybePerformBlockedException(cap, tso);