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 rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
144 nat prev_what_next );
145 static void scheduleHandleThreadBlocked( StgTSO *t );
146 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
148 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
149 static Capability *scheduleDoGC(Capability *cap, Task *task,
150 rtsBool force_major);
152 static void deleteThread (Capability *cap, StgTSO *tso);
153 static void deleteAllThreads (Capability *cap);
155 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
156 static void deleteThread_(Capability *cap, StgTSO *tso);
159 /* ---------------------------------------------------------------------------
160 Main scheduling loop.
162 We use round-robin scheduling, each thread returning to the
163 scheduler loop when one of these conditions is detected:
166 * timer expires (thread yields)
172 In a GranSim setup this loop iterates over the global event queue.
173 This revolves around the global event queue, which determines what
174 to do next. Therefore, it's more complicated than either the
175 concurrent or the parallel (GUM) setup.
176 This version has been entirely removed (JB 2008/08).
179 GUM iterates over incoming messages.
180 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
181 and sends out a fish whenever it has nothing to do; in-between
182 doing the actual reductions (shared code below) it processes the
183 incoming messages and deals with delayed operations
184 (see PendingFetches).
185 This is not the ugliest code you could imagine, but it's bloody close.
187 (JB 2008/08) This version was formerly indicated by a PP-Flag PAR,
188 now by PP-flag PARALLEL_HASKELL. The Eden RTS (in GHC-6.x) uses it,
189 as well as future GUM versions. This file has been refurbished to
190 only contain valid code, which is however incomplete, refers to
191 invalid includes etc.
193 ------------------------------------------------------------------------ */
196 schedule (Capability *initialCapability, Task *task)
200 StgThreadReturnCode ret;
203 #if defined(THREADED_RTS)
204 rtsBool first = rtsTrue;
207 cap = initialCapability;
209 // Pre-condition: this task owns initialCapability.
210 // The sched_mutex is *NOT* held
211 // NB. on return, we still hold a capability.
213 debugTrace (DEBUG_sched, "cap %d: schedule()", initialCapability->no);
217 // -----------------------------------------------------------
218 // Scheduler loop starts here:
222 // Check whether we have re-entered the RTS from Haskell without
223 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
225 if (cap->in_haskell) {
226 errorBelch("schedule: re-entered unsafely.\n"
227 " Perhaps a 'foreign import unsafe' should be 'safe'?");
228 stg_exit(EXIT_FAILURE);
231 // The interruption / shutdown sequence.
233 // In order to cleanly shut down the runtime, we want to:
234 // * make sure that all main threads return to their callers
235 // with the state 'Interrupted'.
236 // * clean up all OS threads assocated with the runtime
237 // * free all memory etc.
239 // So the sequence for ^C goes like this:
241 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
242 // arranges for some Capability to wake up
244 // * all threads in the system are halted, and the zombies are
245 // placed on the run queue for cleaning up. We acquire all
246 // the capabilities in order to delete the threads, this is
247 // done by scheduleDoGC() for convenience (because GC already
248 // needs to acquire all the capabilities). We can't kill
249 // threads involved in foreign calls.
251 // * somebody calls shutdownHaskell(), which calls exitScheduler()
253 // * sched_state := SCHED_SHUTTING_DOWN
255 // * all workers exit when the run queue on their capability
256 // drains. All main threads will also exit when their TSO
257 // reaches the head of the run queue and they can return.
259 // * eventually all Capabilities will shut down, and the RTS can
262 // * We might be left with threads blocked in foreign calls,
263 // we should really attempt to kill these somehow (TODO);
265 switch (sched_state) {
268 case SCHED_INTERRUPTING:
269 debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
270 #if defined(THREADED_RTS)
271 discardSparksCap(cap);
273 /* scheduleDoGC() deletes all the threads */
274 cap = scheduleDoGC(cap,task,rtsFalse);
276 // after scheduleDoGC(), we must be shutting down. Either some
277 // other Capability did the final GC, or we did it above,
278 // either way we can fall through to the SCHED_SHUTTING_DOWN
280 ASSERT(sched_state == SCHED_SHUTTING_DOWN);
283 case SCHED_SHUTTING_DOWN:
284 debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
285 // If we are a worker, just exit. If we're a bound thread
286 // then we will exit below when we've removed our TSO from
288 if (!isBoundTask(task) && emptyRunQueue(cap)) {
293 barf("sched_state: %d", sched_state);
296 scheduleFindWork(cap);
298 /* work pushing, currently relevant only for THREADED_RTS:
299 (pushes threads, wakes up idle capabilities for stealing) */
300 schedulePushWork(cap,task);
302 scheduleDetectDeadlock(cap,task);
304 #if defined(THREADED_RTS)
305 cap = task->cap; // reload cap, it might have changed
308 // Normally, the only way we can get here with no threads to
309 // run is if a keyboard interrupt received during
310 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
311 // Additionally, it is not fatal for the
312 // threaded RTS to reach here with no threads to run.
314 // win32: might be here due to awaitEvent() being abandoned
315 // as a result of a console event having been delivered.
317 #if defined(THREADED_RTS)
321 // // don't yield the first time, we want a chance to run this
322 // // thread for a bit, even if there are others banging at the
325 // ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
328 scheduleYield(&cap,task);
330 if (emptyRunQueue(cap)) continue; // look for work again
333 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
334 if ( emptyRunQueue(cap) ) {
335 ASSERT(sched_state >= SCHED_INTERRUPTING);
340 // Get a thread to run
342 t = popRunQueue(cap);
344 // Sanity check the thread we're about to run. This can be
345 // expensive if there is lots of thread switching going on...
346 IF_DEBUG(sanity,checkTSO(t));
348 #if defined(THREADED_RTS)
349 // Check whether we can run this thread in the current task.
350 // If not, we have to pass our capability to the right task.
352 InCall *bound = t->bound;
355 if (bound->task == task) {
356 // yes, the Haskell thread is bound to the current native thread
358 debugTrace(DEBUG_sched,
359 "thread %lu bound to another OS thread",
360 (unsigned long)t->id);
361 // no, bound to a different Haskell thread: pass to that thread
362 pushOnRunQueue(cap,t);
366 // The thread we want to run is unbound.
367 if (task->incall->tso) {
368 debugTrace(DEBUG_sched,
369 "this OS thread cannot run thread %lu",
370 (unsigned long)t->id);
371 // no, the current native thread is bound to a different
372 // Haskell thread, so pass it to any worker thread
373 pushOnRunQueue(cap,t);
380 // If we're shutting down, and this thread has not yet been
381 // killed, kill it now. This sometimes happens when a finalizer
382 // thread is created by the final GC, or a thread previously
383 // in a foreign call returns.
384 if (sched_state >= SCHED_INTERRUPTING &&
385 !(t->what_next == ThreadComplete || t->what_next == ThreadKilled)) {
389 /* context switches are initiated by the timer signal, unless
390 * the user specified "context switch as often as possible", with
393 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
394 && !emptyThreadQueues(cap)) {
395 cap->context_switch = 1;
400 // CurrentTSO is the thread to run. t might be different if we
401 // loop back to run_thread, so make sure to set CurrentTSO after
403 cap->r.rCurrentTSO = t;
405 startHeapProfTimer();
407 // ----------------------------------------------------------------------
408 // Run the current thread
410 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
411 ASSERT(t->cap == cap);
412 ASSERT(t->bound ? t->bound->task->cap == cap : 1);
414 prev_what_next = t->what_next;
416 errno = t->saved_errno;
418 SetLastError(t->saved_winerror);
421 cap->in_haskell = rtsTrue;
424 dirty_STACK(cap,t->stackobj);
426 #if defined(THREADED_RTS)
427 if (recent_activity == ACTIVITY_DONE_GC) {
428 // ACTIVITY_DONE_GC means we turned off the timer signal to
429 // conserve power (see #1623). Re-enable it here.
431 prev = xchg((P_)&recent_activity, ACTIVITY_YES);
432 if (prev == ACTIVITY_DONE_GC) {
435 } else if (recent_activity != ACTIVITY_INACTIVE) {
436 // If we reached ACTIVITY_INACTIVE, then don't reset it until
437 // we've done the GC. The thread running here might just be
438 // the IO manager thread that handle_tick() woke up via
440 recent_activity = ACTIVITY_YES;
444 traceEventRunThread(cap, t);
446 switch (prev_what_next) {
450 /* Thread already finished, return to scheduler. */
451 ret = ThreadFinished;
457 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
458 cap = regTableToCapability(r);
463 case ThreadInterpret:
464 cap = interpretBCO(cap);
469 barf("schedule: invalid what_next field");
472 cap->in_haskell = rtsFalse;
474 // The TSO might have moved, eg. if it re-entered the RTS and a GC
475 // happened. So find the new location:
476 t = cap->r.rCurrentTSO;
478 // And save the current errno in this thread.
479 // XXX: possibly bogus for SMP because this thread might already
480 // be running again, see code below.
481 t->saved_errno = errno;
483 // Similarly for Windows error code
484 t->saved_winerror = GetLastError();
487 traceEventStopThread(cap, t, ret);
489 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
490 ASSERT(t->cap == cap);
492 // ----------------------------------------------------------------------
494 // Costs for the scheduler are assigned to CCS_SYSTEM
496 #if defined(PROFILING)
500 schedulePostRunThread(cap,t);
502 ready_to_gc = rtsFalse;
506 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
510 // just adjust the stack for this thread, then pop it back
512 threadStackOverflow(cap, t);
513 pushOnRunQueue(cap,t);
517 if (scheduleHandleYield(cap, t, prev_what_next)) {
518 // shortcut for switching between compiler/interpreter:
524 scheduleHandleThreadBlocked(t);
528 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
529 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
533 barf("schedule: invalid thread return code %d", (int)ret);
536 if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
537 cap = scheduleDoGC(cap,task,rtsFalse);
539 } /* end of while() */
542 /* -----------------------------------------------------------------------------
543 * Run queue operations
544 * -------------------------------------------------------------------------- */
547 removeFromRunQueue (Capability *cap, StgTSO *tso)
549 if (tso->block_info.prev == END_TSO_QUEUE) {
550 ASSERT(cap->run_queue_hd == tso);
551 cap->run_queue_hd = tso->_link;
553 setTSOLink(cap, tso->block_info.prev, tso->_link);
555 if (tso->_link == END_TSO_QUEUE) {
556 ASSERT(cap->run_queue_tl == tso);
557 cap->run_queue_tl = tso->block_info.prev;
559 setTSOPrev(cap, tso->_link, tso->block_info.prev);
561 tso->_link = tso->block_info.prev = END_TSO_QUEUE;
563 IF_DEBUG(sanity, checkRunQueue(cap));
566 /* ----------------------------------------------------------------------------
567 * Setting up the scheduler loop
568 * ------------------------------------------------------------------------- */
571 schedulePreLoop(void)
573 // initialisation for scheduler - what cannot go into initScheduler()
576 /* -----------------------------------------------------------------------------
579 * Search for work to do, and handle messages from elsewhere.
580 * -------------------------------------------------------------------------- */
583 scheduleFindWork (Capability *cap)
585 scheduleStartSignalHandlers(cap);
587 scheduleProcessInbox(cap);
589 scheduleCheckBlockedThreads(cap);
591 #if defined(THREADED_RTS)
592 if (emptyRunQueue(cap)) { scheduleActivateSpark(cap); }
596 #if defined(THREADED_RTS)
597 STATIC_INLINE rtsBool
598 shouldYieldCapability (Capability *cap, Task *task)
600 // we need to yield this capability to someone else if..
601 // - another thread is initiating a GC
602 // - another Task is returning from a foreign call
603 // - the thread at the head of the run queue cannot be run
604 // by this Task (it is bound to another Task, or it is unbound
605 // and this task it bound).
606 return (waiting_for_gc ||
607 cap->returning_tasks_hd != NULL ||
608 (!emptyRunQueue(cap) && (task->incall->tso == NULL
609 ? cap->run_queue_hd->bound != NULL
610 : cap->run_queue_hd->bound != task->incall)));
613 // This is the single place where a Task goes to sleep. There are
614 // two reasons it might need to sleep:
615 // - there are no threads to run
616 // - we need to yield this Capability to someone else
617 // (see shouldYieldCapability())
619 // Careful: the scheduler loop is quite delicate. Make sure you run
620 // the tests in testsuite/concurrent (all ways) after modifying this,
621 // and also check the benchmarks in nofib/parallel for regressions.
624 scheduleYield (Capability **pcap, Task *task)
626 Capability *cap = *pcap;
628 // if we have work, and we don't need to give up the Capability, continue.
630 if (!shouldYieldCapability(cap,task) &&
631 (!emptyRunQueue(cap) ||
633 sched_state >= SCHED_INTERRUPTING))
636 // otherwise yield (sleep), and keep yielding if necessary.
638 yieldCapability(&cap,task);
640 while (shouldYieldCapability(cap,task));
642 // note there may still be no threads on the run queue at this
643 // point, the caller has to check.
650 /* -----------------------------------------------------------------------------
653 * Push work to other Capabilities if we have some.
654 * -------------------------------------------------------------------------- */
657 schedulePushWork(Capability *cap USED_IF_THREADS,
658 Task *task USED_IF_THREADS)
660 /* following code not for PARALLEL_HASKELL. I kept the call general,
661 future GUM versions might use pushing in a distributed setup */
662 #if defined(THREADED_RTS)
664 Capability *free_caps[n_capabilities], *cap0;
667 // migration can be turned off with +RTS -qm
668 if (!RtsFlags.ParFlags.migrate) return;
670 // Check whether we have more threads on our run queue, or sparks
671 // in our pool, that we could hand to another Capability.
672 if (cap->run_queue_hd == END_TSO_QUEUE) {
673 if (sparkPoolSizeCap(cap) < 2) return;
675 if (cap->run_queue_hd->_link == END_TSO_QUEUE &&
676 sparkPoolSizeCap(cap) < 1) return;
679 // First grab as many free Capabilities as we can.
680 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
681 cap0 = &capabilities[i];
682 if (cap != cap0 && tryGrabCapability(cap0,task)) {
683 if (!emptyRunQueue(cap0)
684 || cap->returning_tasks_hd != NULL
685 || cap->inbox != (Message*)END_TSO_QUEUE) {
686 // it already has some work, we just grabbed it at
687 // the wrong moment. Or maybe it's deadlocked!
688 releaseCapability(cap0);
690 free_caps[n_free_caps++] = cap0;
695 // we now have n_free_caps free capabilities stashed in
696 // free_caps[]. Share our run queue equally with them. This is
697 // probably the simplest thing we could do; improvements we might
698 // want to do include:
700 // - giving high priority to moving relatively new threads, on
701 // the gournds that they haven't had time to build up a
702 // working set in the cache on this CPU/Capability.
704 // - giving low priority to moving long-lived threads
706 if (n_free_caps > 0) {
707 StgTSO *prev, *t, *next;
708 rtsBool pushed_to_all;
710 debugTrace(DEBUG_sched,
711 "cap %d: %s and %d free capabilities, sharing...",
713 (!emptyRunQueue(cap) && cap->run_queue_hd->_link != END_TSO_QUEUE)?
714 "excess threads on run queue":"sparks to share (>=2)",
718 pushed_to_all = rtsFalse;
720 if (cap->run_queue_hd != END_TSO_QUEUE) {
721 prev = cap->run_queue_hd;
723 prev->_link = END_TSO_QUEUE;
724 for (; t != END_TSO_QUEUE; t = next) {
726 t->_link = END_TSO_QUEUE;
727 if (t->bound == task->incall // don't move my bound thread
728 || tsoLocked(t)) { // don't move a locked thread
729 setTSOLink(cap, prev, t);
730 setTSOPrev(cap, t, prev);
732 } else if (i == n_free_caps) {
733 pushed_to_all = rtsTrue;
736 setTSOLink(cap, prev, t);
737 setTSOPrev(cap, t, prev);
740 appendToRunQueue(free_caps[i],t);
742 traceEventMigrateThread (cap, t, free_caps[i]->no);
744 if (t->bound) { t->bound->task->cap = free_caps[i]; }
745 t->cap = free_caps[i];
749 cap->run_queue_tl = prev;
751 IF_DEBUG(sanity, checkRunQueue(cap));
755 /* JB I left this code in place, it would work but is not necessary */
757 // If there are some free capabilities that we didn't push any
758 // threads to, then try to push a spark to each one.
759 if (!pushed_to_all) {
761 // i is the next free capability to push to
762 for (; i < n_free_caps; i++) {
763 if (emptySparkPoolCap(free_caps[i])) {
764 spark = tryStealSpark(cap->sparks);
766 debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
768 traceEventStealSpark(free_caps[i], t, cap->no);
770 newSpark(&(free_caps[i]->r), spark);
775 #endif /* SPARK_PUSHING */
777 // release the capabilities
778 for (i = 0; i < n_free_caps; i++) {
779 task->cap = free_caps[i];
780 releaseAndWakeupCapability(free_caps[i]);
783 task->cap = cap; // reset to point to our Capability.
785 #endif /* THREADED_RTS */
789 /* ----------------------------------------------------------------------------
790 * Start any pending signal handlers
791 * ------------------------------------------------------------------------- */
793 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
795 scheduleStartSignalHandlers(Capability *cap)
797 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
798 // safe outside the lock
799 startSignalHandlers(cap);
804 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
809 /* ----------------------------------------------------------------------------
810 * Check for blocked threads that can be woken up.
811 * ------------------------------------------------------------------------- */
814 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
816 #if !defined(THREADED_RTS)
818 // Check whether any waiting threads need to be woken up. If the
819 // run queue is empty, and there are no other tasks running, we
820 // can wait indefinitely for something to happen.
822 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
824 awaitEvent (emptyRunQueue(cap));
829 /* ----------------------------------------------------------------------------
830 * Detect deadlock conditions and attempt to resolve them.
831 * ------------------------------------------------------------------------- */
834 scheduleDetectDeadlock (Capability *cap, Task *task)
837 * Detect deadlock: when we have no threads to run, there are no
838 * threads blocked, waiting for I/O, or sleeping, and all the
839 * other tasks are waiting for work, we must have a deadlock of
842 if ( emptyThreadQueues(cap) )
844 #if defined(THREADED_RTS)
846 * In the threaded RTS, we only check for deadlock if there
847 * has been no activity in a complete timeslice. This means
848 * we won't eagerly start a full GC just because we don't have
849 * any threads to run currently.
851 if (recent_activity != ACTIVITY_INACTIVE) return;
854 debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
856 // Garbage collection can release some new threads due to
857 // either (a) finalizers or (b) threads resurrected because
858 // they are unreachable and will therefore be sent an
859 // exception. Any threads thus released will be immediately
861 cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/);
862 // when force_major == rtsTrue. scheduleDoGC sets
863 // recent_activity to ACTIVITY_DONE_GC and turns off the timer
866 if ( !emptyRunQueue(cap) ) return;
868 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
869 /* If we have user-installed signal handlers, then wait
870 * for signals to arrive rather then bombing out with a
873 if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
874 debugTrace(DEBUG_sched,
875 "still deadlocked, waiting for signals...");
879 if (signals_pending()) {
880 startSignalHandlers(cap);
883 // either we have threads to run, or we were interrupted:
884 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
890 #if !defined(THREADED_RTS)
891 /* Probably a real deadlock. Send the current main thread the
892 * Deadlock exception.
894 if (task->incall->tso) {
895 switch (task->incall->tso->why_blocked) {
897 case BlockedOnBlackHole:
898 case BlockedOnMsgThrowTo:
900 throwToSingleThreaded(cap, task->incall->tso,
901 (StgClosure *)nonTermination_closure);
904 barf("deadlock: main thread blocked in a strange way");
913 /* ----------------------------------------------------------------------------
914 * Send pending messages (PARALLEL_HASKELL only)
915 * ------------------------------------------------------------------------- */
917 #if defined(PARALLEL_HASKELL)
919 scheduleSendPendingMessages(void)
922 # if defined(PAR) // global Mem.Mgmt., omit for now
923 if (PendingFetches != END_BF_QUEUE) {
928 if (RtsFlags.ParFlags.BufferTime) {
929 // if we use message buffering, we must send away all message
930 // packets which have become too old...
936 /* ----------------------------------------------------------------------------
937 * Process message in the current Capability's inbox
938 * ------------------------------------------------------------------------- */
941 scheduleProcessInbox (Capability *cap USED_IF_THREADS)
943 #if defined(THREADED_RTS)
946 while (!emptyInbox(cap)) {
947 ACQUIRE_LOCK(&cap->lock);
949 cap->inbox = m->link;
950 RELEASE_LOCK(&cap->lock);
951 executeMessage(cap, (Message *)m);
956 /* ----------------------------------------------------------------------------
957 * Activate spark threads (PARALLEL_HASKELL and THREADED_RTS)
958 * ------------------------------------------------------------------------- */
960 #if defined(THREADED_RTS)
962 scheduleActivateSpark(Capability *cap)
966 createSparkThread(cap);
967 debugTrace(DEBUG_sched, "creating a spark thread");
970 #endif // PARALLEL_HASKELL || THREADED_RTS
972 /* ----------------------------------------------------------------------------
973 * After running a thread...
974 * ------------------------------------------------------------------------- */
977 schedulePostRunThread (Capability *cap, StgTSO *t)
979 // We have to be able to catch transactions that are in an
980 // infinite loop as a result of seeing an inconsistent view of
984 // [a,b] <- mapM readTVar [ta,tb]
985 // when (a == b) loop
987 // and a is never equal to b given a consistent view of memory.
989 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
990 if (!stmValidateNestOfTransactions (t -> trec)) {
991 debugTrace(DEBUG_sched | DEBUG_stm,
992 "trec %p found wasting its time", t);
994 // strip the stack back to the
995 // ATOMICALLY_FRAME, aborting the (nested)
996 // transaction, and saving the stack of any
997 // partially-evaluated thunks on the heap.
998 throwToSingleThreaded_(cap, t, NULL, rtsTrue);
1000 // ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1004 /* some statistics gathering in the parallel case */
1007 /* -----------------------------------------------------------------------------
1008 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1009 * -------------------------------------------------------------------------- */
1012 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1014 // did the task ask for a large block?
1015 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1016 // if so, get one and push it on the front of the nursery.
1020 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1022 if (blocks > BLOCKS_PER_MBLOCK) {
1023 barf("allocation of %ld bytes too large (GHC should have complained at compile-time)", (long)cap->r.rHpAlloc);
1026 debugTrace(DEBUG_sched,
1027 "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1028 (long)t->id, what_next_strs[t->what_next], blocks);
1030 // don't do this if the nursery is (nearly) full, we'll GC first.
1031 if (cap->r.rCurrentNursery->link != NULL ||
1032 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1033 // if the nursery has only one block.
1035 bd = allocGroup_lock(blocks);
1036 cap->r.rNursery->n_blocks += blocks;
1038 // link the new group into the list
1039 bd->link = cap->r.rCurrentNursery;
1040 bd->u.back = cap->r.rCurrentNursery->u.back;
1041 if (cap->r.rCurrentNursery->u.back != NULL) {
1042 cap->r.rCurrentNursery->u.back->link = bd;
1044 cap->r.rNursery->blocks = bd;
1046 cap->r.rCurrentNursery->u.back = bd;
1048 // initialise it as a nursery block. We initialise the
1049 // step, gen_no, and flags field of *every* sub-block in
1050 // this large block, because this is easier than making
1051 // sure that we always find the block head of a large
1052 // block whenever we call Bdescr() (eg. evacuate() and
1053 // isAlive() in the GC would both have to do this, at
1057 for (x = bd; x < bd + blocks; x++) {
1058 initBdescr(x,g0,g0);
1064 // This assert can be a killer if the app is doing lots
1065 // of large block allocations.
1066 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1068 // now update the nursery to point to the new block
1069 cap->r.rCurrentNursery = bd;
1071 // we might be unlucky and have another thread get on the
1072 // run queue before us and steal the large block, but in that
1073 // case the thread will just end up requesting another large
1075 pushOnRunQueue(cap,t);
1076 return rtsFalse; /* not actually GC'ing */
1080 if (cap->r.rHpLim == NULL || cap->context_switch) {
1081 // Sometimes we miss a context switch, e.g. when calling
1082 // primitives in a tight loop, MAYBE_GC() doesn't check the
1083 // context switch flag, and we end up waiting for a GC.
1084 // See #1984, and concurrent/should_run/1984
1085 cap->context_switch = 0;
1086 appendToRunQueue(cap,t);
1088 pushOnRunQueue(cap,t);
1091 /* actual GC is done at the end of the while loop in schedule() */
1094 /* -----------------------------------------------------------------------------
1095 * Handle a thread that returned to the scheduler with ThreadYielding
1096 * -------------------------------------------------------------------------- */
1099 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1101 /* put the thread back on the run queue. Then, if we're ready to
1102 * GC, check whether this is the last task to stop. If so, wake
1103 * up the GC thread. getThread will block during a GC until the
1107 ASSERT(t->_link == END_TSO_QUEUE);
1109 // Shortcut if we're just switching evaluators: don't bother
1110 // doing stack squeezing (which can be expensive), just run the
1112 if (cap->context_switch == 0 && t->what_next != prev_what_next) {
1113 debugTrace(DEBUG_sched,
1114 "--<< thread %ld (%s) stopped to switch evaluators",
1115 (long)t->id, what_next_strs[t->what_next]);
1119 // Reset the context switch flag. We don't do this just before
1120 // running the thread, because that would mean we would lose ticks
1121 // during GC, which can lead to unfair scheduling (a thread hogs
1122 // the CPU because the tick always arrives during GC). This way
1123 // penalises threads that do a lot of allocation, but that seems
1124 // better than the alternative.
1125 cap->context_switch = 0;
1128 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1131 appendToRunQueue(cap,t);
1136 /* -----------------------------------------------------------------------------
1137 * Handle a thread that returned to the scheduler with ThreadBlocked
1138 * -------------------------------------------------------------------------- */
1141 scheduleHandleThreadBlocked( StgTSO *t
1148 // We don't need to do anything. The thread is blocked, and it
1149 // has tidied up its stack and placed itself on whatever queue
1150 // it needs to be on.
1152 // ASSERT(t->why_blocked != NotBlocked);
1153 // Not true: for example,
1154 // - the thread may have woken itself up already, because
1155 // threadPaused() might have raised a blocked throwTo
1156 // exception, see maybePerformBlockedException().
1159 traceThreadStatus(DEBUG_sched, t);
1163 /* -----------------------------------------------------------------------------
1164 * Handle a thread that returned to the scheduler with ThreadFinished
1165 * -------------------------------------------------------------------------- */
1168 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1170 /* Need to check whether this was a main thread, and if so,
1171 * return with the return value.
1173 * We also end up here if the thread kills itself with an
1174 * uncaught exception, see Exception.cmm.
1177 // blocked exceptions can now complete, even if the thread was in
1178 // blocked mode (see #2910).
1179 awakenBlockedExceptionQueue (cap, t);
1182 // Check whether the thread that just completed was a bound
1183 // thread, and if so return with the result.
1185 // There is an assumption here that all thread completion goes
1186 // through this point; we need to make sure that if a thread
1187 // ends up in the ThreadKilled state, that it stays on the run
1188 // queue so it can be dealt with here.
1193 if (t->bound != task->incall) {
1194 #if !defined(THREADED_RTS)
1195 // Must be a bound thread that is not the topmost one. Leave
1196 // it on the run queue until the stack has unwound to the
1197 // point where we can deal with this. Leaving it on the run
1198 // queue also ensures that the garbage collector knows about
1199 // this thread and its return value (it gets dropped from the
1200 // step->threads list so there's no other way to find it).
1201 appendToRunQueue(cap,t);
1204 // this cannot happen in the threaded RTS, because a
1205 // bound thread can only be run by the appropriate Task.
1206 barf("finished bound thread that isn't mine");
1210 ASSERT(task->incall->tso == t);
1212 if (t->what_next == ThreadComplete) {
1213 if (task->incall->ret) {
1214 // NOTE: return val is stack->sp[1] (see StgStartup.hc)
1215 *(task->incall->ret) = (StgClosure *)task->incall->tso->stackobj->sp[1];
1217 task->incall->stat = Success;
1219 if (task->incall->ret) {
1220 *(task->incall->ret) = NULL;
1222 if (sched_state >= SCHED_INTERRUPTING) {
1223 if (heap_overflow) {
1224 task->incall->stat = HeapExhausted;
1226 task->incall->stat = Interrupted;
1229 task->incall->stat = Killed;
1233 removeThreadLabel((StgWord)task->incall->tso->id);
1236 // We no longer consider this thread and task to be bound to
1237 // each other. The TSO lives on until it is GC'd, but the
1238 // task is about to be released by the caller, and we don't
1239 // want anyone following the pointer from the TSO to the
1240 // defunct task (which might have already been
1241 // re-used). This was a real bug: the GC updated
1242 // tso->bound->tso which lead to a deadlock.
1244 task->incall->tso = NULL;
1246 return rtsTrue; // tells schedule() to return
1252 /* -----------------------------------------------------------------------------
1253 * Perform a heap census
1254 * -------------------------------------------------------------------------- */
1257 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1259 // When we have +RTS -i0 and we're heap profiling, do a census at
1260 // every GC. This lets us get repeatable runs for debugging.
1261 if (performHeapProfile ||
1262 (RtsFlags.ProfFlags.profileInterval==0 &&
1263 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1270 /* -----------------------------------------------------------------------------
1271 * Perform a garbage collection if necessary
1272 * -------------------------------------------------------------------------- */
1275 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS, rtsBool force_major)
1277 rtsBool heap_census;
1279 /* extern static volatile StgWord waiting_for_gc;
1280 lives inside capability.c */
1281 rtsBool gc_type, prev_pending_gc;
1285 if (sched_state == SCHED_SHUTTING_DOWN) {
1286 // The final GC has already been done, and the system is
1287 // shutting down. We'll probably deadlock if we try to GC
1293 if (sched_state < SCHED_INTERRUPTING
1294 && RtsFlags.ParFlags.parGcEnabled
1295 && N >= RtsFlags.ParFlags.parGcGen
1296 && ! oldest_gen->mark)
1298 gc_type = PENDING_GC_PAR;
1300 gc_type = PENDING_GC_SEQ;
1303 // In order to GC, there must be no threads running Haskell code.
1304 // Therefore, the GC thread needs to hold *all* the capabilities,
1305 // and release them after the GC has completed.
1307 // This seems to be the simplest way: previous attempts involved
1308 // making all the threads with capabilities give up their
1309 // capabilities and sleep except for the *last* one, which
1310 // actually did the GC. But it's quite hard to arrange for all
1311 // the other tasks to sleep and stay asleep.
1314 /* Other capabilities are prevented from running yet more Haskell
1315 threads if waiting_for_gc is set. Tested inside
1316 yieldCapability() and releaseCapability() in Capability.c */
1318 prev_pending_gc = cas(&waiting_for_gc, 0, gc_type);
1319 if (prev_pending_gc) {
1321 debugTrace(DEBUG_sched, "someone else is trying to GC (%d)...",
1324 yieldCapability(&cap,task);
1325 } while (waiting_for_gc);
1326 return cap; // NOTE: task->cap might have changed here
1329 setContextSwitches();
1331 // The final shutdown GC is always single-threaded, because it's
1332 // possible that some of the Capabilities have no worker threads.
1334 if (gc_type == PENDING_GC_SEQ)
1336 traceEventRequestSeqGc(cap);
1340 traceEventRequestParGc(cap);
1341 debugTrace(DEBUG_sched, "ready_to_gc, grabbing GC threads");
1344 if (gc_type == PENDING_GC_SEQ)
1346 // single-threaded GC: grab all the capabilities
1347 for (i=0; i < n_capabilities; i++) {
1348 debugTrace(DEBUG_sched, "ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities);
1349 if (cap != &capabilities[i]) {
1350 Capability *pcap = &capabilities[i];
1351 // we better hope this task doesn't get migrated to
1352 // another Capability while we're waiting for this one.
1353 // It won't, because load balancing happens while we have
1354 // all the Capabilities, but even so it's a slightly
1355 // unsavoury invariant.
1357 waitForReturnCapability(&pcap, task);
1358 if (pcap != &capabilities[i]) {
1359 barf("scheduleDoGC: got the wrong capability");
1366 // multi-threaded GC: make sure all the Capabilities donate one
1368 waitForGcThreads(cap);
1373 IF_DEBUG(scheduler, printAllThreads());
1375 delete_threads_and_gc:
1377 * We now have all the capabilities; if we're in an interrupting
1378 * state, then we should take the opportunity to delete all the
1379 * threads in the system.
1381 if (sched_state == SCHED_INTERRUPTING) {
1382 deleteAllThreads(cap);
1383 sched_state = SCHED_SHUTTING_DOWN;
1386 heap_census = scheduleNeedHeapProfile(rtsTrue);
1388 traceEventGcStart(cap);
1389 #if defined(THREADED_RTS)
1390 // reset waiting_for_gc *before* GC, so that when the GC threads
1391 // emerge they don't immediately re-enter the GC.
1393 GarbageCollect(force_major || heap_census, gc_type, cap);
1395 GarbageCollect(force_major || heap_census, 0, cap);
1397 traceEventGcEnd(cap);
1399 if (recent_activity == ACTIVITY_INACTIVE && force_major)
1401 // We are doing a GC because the system has been idle for a
1402 // timeslice and we need to check for deadlock. Record the
1403 // fact that we've done a GC and turn off the timer signal;
1404 // it will get re-enabled if we run any threads after the GC.
1405 recent_activity = ACTIVITY_DONE_GC;
1410 // the GC might have taken long enough for the timer to set
1411 // recent_activity = ACTIVITY_INACTIVE, but we aren't
1412 // necessarily deadlocked:
1413 recent_activity = ACTIVITY_YES;
1416 #if defined(THREADED_RTS)
1417 if (gc_type == PENDING_GC_PAR)
1419 releaseGCThreads(cap);
1424 debugTrace(DEBUG_sched, "performing heap census");
1426 performHeapProfile = rtsFalse;
1429 if (heap_overflow && sched_state < SCHED_INTERRUPTING) {
1430 // GC set the heap_overflow flag, so we should proceed with
1431 // an orderly shutdown now. Ultimately we want the main
1432 // thread to return to its caller with HeapExhausted, at which
1433 // point the caller should call hs_exit(). The first step is
1434 // to delete all the threads.
1436 // Another way to do this would be to raise an exception in
1437 // the main thread, which we really should do because it gives
1438 // the program a chance to clean up. But how do we find the
1439 // main thread? It should presumably be the same one that
1440 // gets ^C exceptions, but that's all done on the Haskell side
1441 // (GHC.TopHandler).
1442 sched_state = SCHED_INTERRUPTING;
1443 goto delete_threads_and_gc;
1448 Once we are all together... this would be the place to balance all
1449 spark pools. No concurrent stealing or adding of new sparks can
1450 occur. Should be defined in Sparks.c. */
1451 balanceSparkPoolsCaps(n_capabilities, capabilities);
1454 #if defined(THREADED_RTS)
1455 if (gc_type == PENDING_GC_SEQ) {
1456 // release our stash of capabilities.
1457 for (i = 0; i < n_capabilities; i++) {
1458 if (cap != &capabilities[i]) {
1459 task->cap = &capabilities[i];
1460 releaseCapability(&capabilities[i]);
1474 /* ---------------------------------------------------------------------------
1475 * Singleton fork(). Do not copy any running threads.
1476 * ------------------------------------------------------------------------- */
1479 forkProcess(HsStablePtr *entry
1480 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
1485 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
1491 #if defined(THREADED_RTS)
1492 if (RtsFlags.ParFlags.nNodes > 1) {
1493 errorBelch("forking not supported with +RTS -N<n> greater than 1");
1494 stg_exit(EXIT_FAILURE);
1498 debugTrace(DEBUG_sched, "forking!");
1500 // ToDo: for SMP, we should probably acquire *all* the capabilities
1503 // no funny business: hold locks while we fork, otherwise if some
1504 // other thread is holding a lock when the fork happens, the data
1505 // structure protected by the lock will forever be in an
1506 // inconsistent state in the child. See also #1391.
1507 ACQUIRE_LOCK(&sched_mutex);
1508 ACQUIRE_LOCK(&cap->lock);
1509 ACQUIRE_LOCK(&cap->running_task->lock);
1511 stopTimer(); // See #4074
1513 #if defined(TRACING)
1514 flushEventLog(); // so that child won't inherit dirty file buffers
1519 if (pid) { // parent
1521 startTimer(); // #4074
1523 RELEASE_LOCK(&sched_mutex);
1524 RELEASE_LOCK(&cap->lock);
1525 RELEASE_LOCK(&cap->running_task->lock);
1527 // just return the pid
1533 #if defined(THREADED_RTS)
1534 initMutex(&sched_mutex);
1535 initMutex(&cap->lock);
1536 initMutex(&cap->running_task->lock);
1543 // Now, all OS threads except the thread that forked are
1544 // stopped. We need to stop all Haskell threads, including
1545 // those involved in foreign calls. Also we need to delete
1546 // all Tasks, because they correspond to OS threads that are
1549 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1550 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
1551 next = t->global_link;
1552 // don't allow threads to catch the ThreadKilled
1553 // exception, but we do want to raiseAsync() because these
1554 // threads may be evaluating thunks that we need later.
1555 deleteThread_(cap,t);
1557 // stop the GC from updating the InCall to point to
1558 // the TSO. This is only necessary because the
1559 // OSThread bound to the TSO has been killed, and
1560 // won't get a chance to exit in the usual way (see
1561 // also scheduleHandleThreadFinished).
1566 // Empty the run queue. It seems tempting to let all the
1567 // killed threads stay on the run queue as zombies to be
1568 // cleaned up later, but some of them correspond to bound
1569 // threads for which the corresponding Task does not exist.
1570 cap->run_queue_hd = END_TSO_QUEUE;
1571 cap->run_queue_tl = END_TSO_QUEUE;
1573 // Any suspended C-calling Tasks are no more, their OS threads
1575 cap->suspended_ccalls = NULL;
1577 // Empty the threads lists. Otherwise, the garbage
1578 // collector may attempt to resurrect some of these threads.
1579 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1580 generations[g].threads = END_TSO_QUEUE;
1583 discardTasksExcept(cap->running_task);
1585 #if defined(THREADED_RTS)
1586 // Wipe our spare workers list, they no longer exist. New
1587 // workers will be created if necessary.
1588 cap->spare_workers = NULL;
1589 cap->n_spare_workers = 0;
1590 cap->returning_tasks_hd = NULL;
1591 cap->returning_tasks_tl = NULL;
1594 // On Unix, all timers are reset in the child, so we need to start
1599 #if defined(THREADED_RTS)
1600 cap = ioManagerStartCap(cap);
1603 cap = rts_evalStableIO(cap, entry, NULL); // run the action
1604 rts_checkSchedStatus("forkProcess",cap);
1607 hs_exit(); // clean up and exit
1608 stg_exit(EXIT_SUCCESS);
1610 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
1611 barf("forkProcess#: primop not supported on this platform, sorry!\n");
1615 /* ---------------------------------------------------------------------------
1616 * Delete all the threads in the system
1617 * ------------------------------------------------------------------------- */
1620 deleteAllThreads ( Capability *cap )
1622 // NOTE: only safe to call if we own all capabilities.
1627 debugTrace(DEBUG_sched,"deleting all threads");
1628 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1629 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
1630 next = t->global_link;
1631 deleteThread(cap,t);
1635 // The run queue now contains a bunch of ThreadKilled threads. We
1636 // must not throw these away: the main thread(s) will be in there
1637 // somewhere, and the main scheduler loop has to deal with it.
1638 // Also, the run queue is the only thing keeping these threads from
1639 // being GC'd, and we don't want the "main thread has been GC'd" panic.
1641 #if !defined(THREADED_RTS)
1642 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
1643 ASSERT(sleeping_queue == END_TSO_QUEUE);
1647 /* -----------------------------------------------------------------------------
1648 Managing the suspended_ccalls list.
1649 Locks required: sched_mutex
1650 -------------------------------------------------------------------------- */
1653 suspendTask (Capability *cap, Task *task)
1657 incall = task->incall;
1658 ASSERT(incall->next == NULL && incall->prev == NULL);
1659 incall->next = cap->suspended_ccalls;
1660 incall->prev = NULL;
1661 if (cap->suspended_ccalls) {
1662 cap->suspended_ccalls->prev = incall;
1664 cap->suspended_ccalls = incall;
1668 recoverSuspendedTask (Capability *cap, Task *task)
1672 incall = task->incall;
1674 incall->prev->next = incall->next;
1676 ASSERT(cap->suspended_ccalls == incall);
1677 cap->suspended_ccalls = incall->next;
1680 incall->next->prev = incall->prev;
1682 incall->next = incall->prev = NULL;
1685 /* ---------------------------------------------------------------------------
1686 * Suspending & resuming Haskell threads.
1688 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1689 * its capability before calling the C function. This allows another
1690 * task to pick up the capability and carry on running Haskell
1691 * threads. It also means that if the C call blocks, it won't lock
1694 * The Haskell thread making the C call is put to sleep for the
1695 * duration of the call, on the suspended_ccalling_threads queue. We
1696 * give out a token to the task, which it can use to resume the thread
1697 * on return from the C function.
1699 * If this is an interruptible C call, this means that the FFI call may be
1700 * unceremoniously terminated and should be scheduled on an
1701 * unbound worker thread.
1702 * ------------------------------------------------------------------------- */
1705 suspendThread (StgRegTable *reg, rtsBool interruptible)
1712 StgWord32 saved_winerror;
1715 saved_errno = errno;
1717 saved_winerror = GetLastError();
1720 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
1722 cap = regTableToCapability(reg);
1724 task = cap->running_task;
1725 tso = cap->r.rCurrentTSO;
1727 traceEventStopThread(cap, tso, THREAD_SUSPENDED_FOREIGN_CALL);
1729 // XXX this might not be necessary --SDM
1730 tso->what_next = ThreadRunGHC;
1732 threadPaused(cap,tso);
1734 if (interruptible) {
1735 tso->why_blocked = BlockedOnCCall_Interruptible;
1737 tso->why_blocked = BlockedOnCCall;
1740 // Hand back capability
1741 task->incall->suspended_tso = tso;
1742 task->incall->suspended_cap = cap;
1744 ACQUIRE_LOCK(&cap->lock);
1746 suspendTask(cap,task);
1747 cap->in_haskell = rtsFalse;
1748 releaseCapability_(cap,rtsFalse);
1750 RELEASE_LOCK(&cap->lock);
1752 errno = saved_errno;
1754 SetLastError(saved_winerror);
1760 resumeThread (void *task_)
1768 StgWord32 saved_winerror;
1771 saved_errno = errno;
1773 saved_winerror = GetLastError();
1776 incall = task->incall;
1777 cap = incall->suspended_cap;
1780 // Wait for permission to re-enter the RTS with the result.
1781 waitForReturnCapability(&cap,task);
1782 // we might be on a different capability now... but if so, our
1783 // entry on the suspended_ccalls list will also have been
1786 // Remove the thread from the suspended list
1787 recoverSuspendedTask(cap,task);
1789 tso = incall->suspended_tso;
1790 incall->suspended_tso = NULL;
1791 incall->suspended_cap = NULL;
1792 tso->_link = END_TSO_QUEUE; // no write barrier reqd
1794 traceEventRunThread(cap, tso);
1796 /* Reset blocking status */
1797 tso->why_blocked = NotBlocked;
1799 if ((tso->flags & TSO_BLOCKEX) == 0) {
1800 // avoid locking the TSO if we don't have to
1801 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE) {
1802 maybePerformBlockedException(cap,tso);
1806 cap->r.rCurrentTSO = tso;
1807 cap->in_haskell = rtsTrue;
1808 errno = saved_errno;
1810 SetLastError(saved_winerror);
1813 /* We might have GC'd, mark the TSO dirty again */
1815 dirty_STACK(cap,tso->stackobj);
1817 IF_DEBUG(sanity, checkTSO(tso));
1822 /* ---------------------------------------------------------------------------
1825 * scheduleThread puts a thread on the end of the runnable queue.
1826 * This will usually be done immediately after a thread is created.
1827 * The caller of scheduleThread must create the thread using e.g.
1828 * createThread and push an appropriate closure
1829 * on this thread's stack before the scheduler is invoked.
1830 * ------------------------------------------------------------------------ */
1833 scheduleThread(Capability *cap, StgTSO *tso)
1835 // The thread goes at the *end* of the run-queue, to avoid possible
1836 // starvation of any threads already on the queue.
1837 appendToRunQueue(cap,tso);
1841 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
1843 #if defined(THREADED_RTS)
1844 tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
1845 // move this thread from now on.
1846 cpu %= RtsFlags.ParFlags.nNodes;
1847 if (cpu == cap->no) {
1848 appendToRunQueue(cap,tso);
1850 migrateThread(cap, tso, &capabilities[cpu]);
1853 appendToRunQueue(cap,tso);
1858 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
1863 // We already created/initialised the Task
1864 task = cap->running_task;
1866 // This TSO is now a bound thread; make the Task and TSO
1867 // point to each other.
1868 tso->bound = task->incall;
1871 task->incall->tso = tso;
1872 task->incall->ret = ret;
1873 task->incall->stat = NoStatus;
1875 appendToRunQueue(cap,tso);
1878 debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)id);
1880 cap = schedule(cap,task);
1882 ASSERT(task->incall->stat != NoStatus);
1883 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
1885 debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)id);
1889 /* ----------------------------------------------------------------------------
1891 * ------------------------------------------------------------------------- */
1893 #if defined(THREADED_RTS)
1894 void scheduleWorker (Capability *cap, Task *task)
1896 // schedule() runs without a lock.
1897 cap = schedule(cap,task);
1899 // On exit from schedule(), we have a Capability, but possibly not
1900 // the same one we started with.
1902 // During shutdown, the requirement is that after all the
1903 // Capabilities are shut down, all workers that are shutting down
1904 // have finished workerTaskStop(). This is why we hold on to
1905 // cap->lock until we've finished workerTaskStop() below.
1907 // There may be workers still involved in foreign calls; those
1908 // will just block in waitForReturnCapability() because the
1909 // Capability has been shut down.
1911 ACQUIRE_LOCK(&cap->lock);
1912 releaseCapability_(cap,rtsFalse);
1913 workerTaskStop(task);
1914 RELEASE_LOCK(&cap->lock);
1918 /* ---------------------------------------------------------------------------
1921 * Initialise the scheduler. This resets all the queues - if the
1922 * queues contained any threads, they'll be garbage collected at the
1925 * ------------------------------------------------------------------------ */
1930 #if !defined(THREADED_RTS)
1931 blocked_queue_hd = END_TSO_QUEUE;
1932 blocked_queue_tl = END_TSO_QUEUE;
1933 sleeping_queue = END_TSO_QUEUE;
1936 sched_state = SCHED_RUNNING;
1937 recent_activity = ACTIVITY_YES;
1939 #if defined(THREADED_RTS)
1940 /* Initialise the mutex and condition variables used by
1942 initMutex(&sched_mutex);
1945 ACQUIRE_LOCK(&sched_mutex);
1947 /* A capability holds the state a native thread needs in
1948 * order to execute STG code. At least one capability is
1949 * floating around (only THREADED_RTS builds have more than one).
1955 #if defined(THREADED_RTS)
1959 RELEASE_LOCK(&sched_mutex);
1961 #if defined(THREADED_RTS)
1963 * Eagerly start one worker to run each Capability, except for
1964 * Capability 0. The idea is that we're probably going to start a
1965 * bound thread on Capability 0 pretty soon, so we don't want a
1966 * worker task hogging it.
1971 for (i = 1; i < n_capabilities; i++) {
1972 cap = &capabilities[i];
1973 ACQUIRE_LOCK(&cap->lock);
1974 startWorkerTask(cap);
1975 RELEASE_LOCK(&cap->lock);
1982 exitScheduler (rtsBool wait_foreign USED_IF_THREADS)
1983 /* see Capability.c, shutdownCapability() */
1987 task = newBoundTask();
1989 // If we haven't killed all the threads yet, do it now.
1990 if (sched_state < SCHED_SHUTTING_DOWN) {
1991 sched_state = SCHED_INTERRUPTING;
1992 waitForReturnCapability(&task->cap,task);
1993 scheduleDoGC(task->cap,task,rtsFalse);
1994 ASSERT(task->incall->tso == NULL);
1995 releaseCapability(task->cap);
1997 sched_state = SCHED_SHUTTING_DOWN;
1999 #if defined(THREADED_RTS)
2003 for (i = 0; i < n_capabilities; i++) {
2004 ASSERT(task->incall->tso == NULL);
2005 shutdownCapability(&capabilities[i], task, wait_foreign);
2010 boundTaskExiting(task);
2014 freeScheduler( void )
2018 ACQUIRE_LOCK(&sched_mutex);
2019 still_running = freeTaskManager();
2020 // We can only free the Capabilities if there are no Tasks still
2021 // running. We might have a Task about to return from a foreign
2022 // call into waitForReturnCapability(), for example (actually,
2023 // this should be the *only* thing that a still-running Task can
2024 // do at this point, and it will block waiting for the
2026 if (still_running == 0) {
2028 if (n_capabilities != 1) {
2029 stgFree(capabilities);
2032 RELEASE_LOCK(&sched_mutex);
2033 #if defined(THREADED_RTS)
2034 closeMutex(&sched_mutex);
2038 /* -----------------------------------------------------------------------------
2041 This is the interface to the garbage collector from Haskell land.
2042 We provide this so that external C code can allocate and garbage
2043 collect when called from Haskell via _ccall_GC.
2044 -------------------------------------------------------------------------- */
2047 performGC_(rtsBool force_major)
2051 // We must grab a new Task here, because the existing Task may be
2052 // associated with a particular Capability, and chained onto the
2053 // suspended_ccalls queue.
2054 task = newBoundTask();
2056 waitForReturnCapability(&task->cap,task);
2057 scheduleDoGC(task->cap,task,force_major);
2058 releaseCapability(task->cap);
2059 boundTaskExiting(task);
2065 performGC_(rtsFalse);
2069 performMajorGC(void)
2071 performGC_(rtsTrue);
2074 /* ---------------------------------------------------------------------------
2076 - usually called inside a signal handler so it mustn't do anything fancy.
2077 ------------------------------------------------------------------------ */
2080 interruptStgRts(void)
2082 sched_state = SCHED_INTERRUPTING;
2083 setContextSwitches();
2084 #if defined(THREADED_RTS)
2089 /* -----------------------------------------------------------------------------
2092 This function causes at least one OS thread to wake up and run the
2093 scheduler loop. It is invoked when the RTS might be deadlocked, or
2094 an external event has arrived that may need servicing (eg. a
2095 keyboard interrupt).
2097 In the single-threaded RTS we don't do anything here; we only have
2098 one thread anyway, and the event that caused us to want to wake up
2099 will have interrupted any blocking system call in progress anyway.
2100 -------------------------------------------------------------------------- */
2102 #if defined(THREADED_RTS)
2103 void wakeUpRts(void)
2105 // This forces the IO Manager thread to wakeup, which will
2106 // in turn ensure that some OS thread wakes up and runs the
2107 // scheduler loop, which will cause a GC and deadlock check.
2112 /* -----------------------------------------------------------------------------
2115 This is used for interruption (^C) and forking, and corresponds to
2116 raising an exception but without letting the thread catch the
2118 -------------------------------------------------------------------------- */
2121 deleteThread (Capability *cap STG_UNUSED, StgTSO *tso)
2123 // NOTE: must only be called on a TSO that we have exclusive
2124 // access to, because we will call throwToSingleThreaded() below.
2125 // The TSO must be on the run queue of the Capability we own, or
2126 // we must own all Capabilities.
2128 if (tso->why_blocked != BlockedOnCCall &&
2129 tso->why_blocked != BlockedOnCCall_Interruptible) {
2130 throwToSingleThreaded(tso->cap,tso,NULL);
2134 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2136 deleteThread_(Capability *cap, StgTSO *tso)
2137 { // for forkProcess only:
2138 // like deleteThread(), but we delete threads in foreign calls, too.
2140 if (tso->why_blocked == BlockedOnCCall ||
2141 tso->why_blocked == BlockedOnCCall_Interruptible) {
2142 tso->what_next = ThreadKilled;
2143 appendToRunQueue(tso->cap, tso);
2145 deleteThread(cap,tso);
2150 /* -----------------------------------------------------------------------------
2151 raiseExceptionHelper
2153 This function is called by the raise# primitve, just so that we can
2154 move some of the tricky bits of raising an exception from C-- into
2155 C. Who knows, it might be a useful re-useable thing here too.
2156 -------------------------------------------------------------------------- */
2159 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2161 Capability *cap = regTableToCapability(reg);
2162 StgThunk *raise_closure = NULL;
2164 StgRetInfoTable *info;
2166 // This closure represents the expression 'raise# E' where E
2167 // is the exception raise. It is used to overwrite all the
2168 // thunks which are currently under evaluataion.
2171 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2172 // LDV profiling: stg_raise_info has THUNK as its closure
2173 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2174 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
2175 // 1 does not cause any problem unless profiling is performed.
2176 // However, when LDV profiling goes on, we need to linearly scan
2177 // small object pool, where raise_closure is stored, so we should
2178 // use MIN_UPD_SIZE.
2180 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2181 // sizeofW(StgClosure)+1);
2185 // Walk up the stack, looking for the catch frame. On the way,
2186 // we update any closures pointed to from update frames with the
2187 // raise closure that we just built.
2189 p = tso->stackobj->sp;
2191 info = get_ret_itbl((StgClosure *)p);
2192 next = p + stack_frame_sizeW((StgClosure *)p);
2193 switch (info->i.type) {
2196 // Only create raise_closure if we need to.
2197 if (raise_closure == NULL) {
2199 (StgThunk *)allocate(cap,sizeofW(StgThunk)+1);
2200 SET_HDR(raise_closure, &stg_raise_info, CCCS);
2201 raise_closure->payload[0] = exception;
2203 updateThunk(cap, tso, ((StgUpdateFrame *)p)->updatee,
2204 (StgClosure *)raise_closure);
2208 case ATOMICALLY_FRAME:
2209 debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
2210 tso->stackobj->sp = p;
2211 return ATOMICALLY_FRAME;
2214 tso->stackobj->sp = p;
2217 case CATCH_STM_FRAME:
2218 debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
2219 tso->stackobj->sp = p;
2220 return CATCH_STM_FRAME;
2222 case UNDERFLOW_FRAME:
2223 tso->stackobj->sp = p;
2224 threadStackUnderflow(cap,tso);
2225 p = tso->stackobj->sp;
2229 tso->stackobj->sp = p;
2232 case CATCH_RETRY_FRAME:
2241 /* -----------------------------------------------------------------------------
2242 findRetryFrameHelper
2244 This function is called by the retry# primitive. It traverses the stack
2245 leaving tso->sp referring to the frame which should handle the retry.
2247 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
2248 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
2250 We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
2251 create) because retries are not considered to be exceptions, despite the
2252 similar implementation.
2254 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
2255 not be created within memory transactions.
2256 -------------------------------------------------------------------------- */
2259 findRetryFrameHelper (Capability *cap, StgTSO *tso)
2262 StgRetInfoTable *info;
2264 p = tso->stackobj->sp;
2266 info = get_ret_itbl((StgClosure *)p);
2267 next = p + stack_frame_sizeW((StgClosure *)p);
2268 switch (info->i.type) {
2270 case ATOMICALLY_FRAME:
2271 debugTrace(DEBUG_stm,
2272 "found ATOMICALLY_FRAME at %p during retry", p);
2273 tso->stackobj->sp = p;
2274 return ATOMICALLY_FRAME;
2276 case CATCH_RETRY_FRAME:
2277 debugTrace(DEBUG_stm,
2278 "found CATCH_RETRY_FRAME at %p during retrry", p);
2279 tso->stackobj->sp = p;
2280 return CATCH_RETRY_FRAME;
2282 case CATCH_STM_FRAME: {
2283 StgTRecHeader *trec = tso -> trec;
2284 StgTRecHeader *outer = trec -> enclosing_trec;
2285 debugTrace(DEBUG_stm,
2286 "found CATCH_STM_FRAME at %p during retry", p);
2287 debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
2288 stmAbortTransaction(cap, trec);
2289 stmFreeAbortedTRec(cap, trec);
2290 tso -> trec = outer;
2295 case UNDERFLOW_FRAME:
2296 threadStackUnderflow(cap,tso);
2297 p = tso->stackobj->sp;
2301 ASSERT(info->i.type != CATCH_FRAME);
2302 ASSERT(info->i.type != STOP_FRAME);
2309 /* -----------------------------------------------------------------------------
2310 resurrectThreads is called after garbage collection on the list of
2311 threads found to be garbage. Each of these threads will be woken
2312 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2313 on an MVar, or NonTermination if the thread was blocked on a Black
2316 Locks: assumes we hold *all* the capabilities.
2317 -------------------------------------------------------------------------- */
2320 resurrectThreads (StgTSO *threads)
2326 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2327 next = tso->global_link;
2329 gen = Bdescr((P_)tso)->gen;
2330 tso->global_link = gen->threads;
2333 debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
2335 // Wake up the thread on the Capability it was last on
2338 switch (tso->why_blocked) {
2340 /* Called by GC - sched_mutex lock is currently held. */
2341 throwToSingleThreaded(cap, tso,
2342 (StgClosure *)blockedIndefinitelyOnMVar_closure);
2344 case BlockedOnBlackHole:
2345 throwToSingleThreaded(cap, tso,
2346 (StgClosure *)nonTermination_closure);
2349 throwToSingleThreaded(cap, tso,
2350 (StgClosure *)blockedIndefinitelyOnSTM_closure);
2353 /* This might happen if the thread was blocked on a black hole
2354 * belonging to a thread that we've just woken up (raiseAsync
2355 * can wake up threads, remember...).
2358 case BlockedOnMsgThrowTo:
2359 // This can happen if the target is masking, blocks on a
2360 // black hole, and then is found to be unreachable. In
2361 // this case, we want to let the target wake up and carry
2362 // on, and do nothing to this thread.
2365 barf("resurrectThreads: thread blocked in a strange way: %d",