1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2006
5 * The scheduler and thread-related functionality
7 * --------------------------------------------------------------------------*/
9 #include "PosixSource.h"
14 #include "OSThreads.h"
19 #include "StgMiscClosures.h"
20 #include "Interpreter.h"
22 #include "RtsSignals.h"
28 #include "ThreadLabels.h"
29 #include "LdvProfile.h"
31 #include "Proftimer.h"
33 #if defined(GRAN) || defined(PARALLEL_HASKELL)
34 # include "GranSimRts.h"
36 # include "ParallelRts.h"
37 # include "Parallel.h"
38 # include "ParallelDebug.h"
43 #include "Capability.h"
45 #include "AwaitEvent.h"
46 #if defined(mingw32_HOST_OS)
47 #include "win32/IOManager.h"
50 #include "RaiseAsync.h"
52 #include "ThrIOManager.h"
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
69 // Turn off inlining when debugging - it obfuscates things
72 # define STATIC_INLINE static
75 /* -----------------------------------------------------------------------------
77 * -------------------------------------------------------------------------- */
81 StgTSO* ActiveTSO = NULL; /* for assigning system costs; GranSim-Light only */
82 /* rtsTime TimeOfNextEvent, EndOfTimeSlice; now in GranSim.c */
85 In GranSim we have a runnable and a blocked queue for each processor.
86 In order to minimise code changes new arrays run_queue_hds/tls
87 are created. run_queue_hd is then a short cut (macro) for
88 run_queue_hds[CurrentProc] (see GranSim.h).
91 StgTSO *run_queue_hds[MAX_PROC], *run_queue_tls[MAX_PROC];
92 StgTSO *blocked_queue_hds[MAX_PROC], *blocked_queue_tls[MAX_PROC];
93 StgTSO *ccalling_threadss[MAX_PROC];
94 /* We use the same global list of threads (all_threads) in GranSim as in
95 the std RTS (i.e. we are cheating). However, we don't use this list in
96 the GranSim specific code at the moment (so we are only potentially
101 #if !defined(THREADED_RTS)
102 // Blocked/sleeping thrads
103 StgTSO *blocked_queue_hd = NULL;
104 StgTSO *blocked_queue_tl = NULL;
105 StgTSO *sleeping_queue = NULL; // perhaps replace with a hash table?
108 /* Threads blocked on blackholes.
109 * LOCK: sched_mutex+capability, or all capabilities
111 StgTSO *blackhole_queue = NULL;
114 /* The blackhole_queue should be checked for threads to wake up. See
115 * Schedule.h for more thorough comment.
116 * LOCK: none (doesn't matter if we miss an update)
118 rtsBool blackholes_need_checking = rtsFalse;
120 /* Linked list of all threads.
121 * Used for detecting garbage collected threads.
122 * LOCK: sched_mutex+capability, or all capabilities
124 StgTSO *all_threads = NULL;
126 /* flag set by signal handler to precipitate a context switch
127 * LOCK: none (just an advisory flag)
129 int context_switch = 0;
131 /* flag that tracks whether we have done any execution in this time slice.
132 * LOCK: currently none, perhaps we should lock (but needs to be
133 * updated in the fast path of the scheduler).
135 nat recent_activity = ACTIVITY_YES;
137 /* if this flag is set as well, give up execution
138 * LOCK: none (changes once, from false->true)
140 rtsBool sched_state = SCHED_RUNNING;
146 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
147 * exists - earlier gccs apparently didn't.
153 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
154 * in an MT setting, needed to signal that a worker thread shouldn't hang around
155 * in the scheduler when it is out of work.
157 rtsBool shutting_down_scheduler = rtsFalse;
160 * This mutex protects most of the global scheduler data in
161 * the THREADED_RTS runtime.
163 #if defined(THREADED_RTS)
167 #if defined(PARALLEL_HASKELL)
169 rtsTime TimeOfLastYield;
170 rtsBool emitSchedule = rtsTrue;
173 #if !defined(mingw32_HOST_OS)
174 #define FORKPROCESS_PRIMOP_SUPPORTED
177 /* -----------------------------------------------------------------------------
178 * static function prototypes
179 * -------------------------------------------------------------------------- */
181 static Capability *schedule (Capability *initialCapability, Task *task);
184 // These function all encapsulate parts of the scheduler loop, and are
185 // abstracted only to make the structure and control flow of the
186 // scheduler clearer.
188 static void schedulePreLoop (void);
189 #if defined(THREADED_RTS)
190 static void schedulePushWork(Capability *cap, Task *task);
192 static void scheduleStartSignalHandlers (Capability *cap);
193 static void scheduleCheckBlockedThreads (Capability *cap);
194 static void scheduleCheckWakeupThreads(Capability *cap USED_IF_NOT_THREADS);
195 static void scheduleCheckBlackHoles (Capability *cap);
196 static void scheduleDetectDeadlock (Capability *cap, Task *task);
198 static StgTSO *scheduleProcessEvent(rtsEvent *event);
200 #if defined(PARALLEL_HASKELL)
201 static StgTSO *scheduleSendPendingMessages(void);
202 static void scheduleActivateSpark(void);
203 static rtsBool scheduleGetRemoteWork(rtsBool *receivedFinish);
205 #if defined(PAR) || defined(GRAN)
206 static void scheduleGranParReport(void);
208 static void schedulePostRunThread(void);
209 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
210 static void scheduleHandleStackOverflow( Capability *cap, Task *task,
212 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
213 nat prev_what_next );
214 static void scheduleHandleThreadBlocked( StgTSO *t );
215 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
217 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
218 static Capability *scheduleDoGC(Capability *cap, Task *task,
219 rtsBool force_major);
221 static rtsBool checkBlackHoles(Capability *cap);
223 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
225 static void deleteThread (Capability *cap, StgTSO *tso);
226 static void deleteAllThreads (Capability *cap);
228 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
229 static void deleteThread_(Capability *cap, StgTSO *tso);
232 #if defined(PARALLEL_HASKELL)
233 StgTSO * createSparkThread(rtsSpark spark);
234 StgTSO * activateSpark (rtsSpark spark);
238 static char *whatNext_strs[] = {
248 /* -----------------------------------------------------------------------------
249 * Putting a thread on the run queue: different scheduling policies
250 * -------------------------------------------------------------------------- */
253 addToRunQueue( Capability *cap, StgTSO *t )
255 #if defined(PARALLEL_HASKELL)
256 if (RtsFlags.ParFlags.doFairScheduling) {
257 // this does round-robin scheduling; good for concurrency
258 appendToRunQueue(cap,t);
260 // this does unfair scheduling; good for parallelism
261 pushOnRunQueue(cap,t);
264 // this does round-robin scheduling; good for concurrency
265 appendToRunQueue(cap,t);
269 /* ---------------------------------------------------------------------------
270 Main scheduling loop.
272 We use round-robin scheduling, each thread returning to the
273 scheduler loop when one of these conditions is detected:
276 * timer expires (thread yields)
282 In a GranSim setup this loop iterates over the global event queue.
283 This revolves around the global event queue, which determines what
284 to do next. Therefore, it's more complicated than either the
285 concurrent or the parallel (GUM) setup.
288 GUM iterates over incoming messages.
289 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
290 and sends out a fish whenever it has nothing to do; in-between
291 doing the actual reductions (shared code below) it processes the
292 incoming messages and deals with delayed operations
293 (see PendingFetches).
294 This is not the ugliest code you could imagine, but it's bloody close.
296 ------------------------------------------------------------------------ */
299 schedule (Capability *initialCapability, Task *task)
303 StgThreadReturnCode ret;
306 #elif defined(PARALLEL_HASKELL)
309 rtsBool receivedFinish = rtsFalse;
311 nat tp_size, sp_size; // stats only
316 #if defined(THREADED_RTS)
317 rtsBool first = rtsTrue;
320 cap = initialCapability;
322 // Pre-condition: this task owns initialCapability.
323 // The sched_mutex is *NOT* held
324 // NB. on return, we still hold a capability.
326 debugTrace (DEBUG_sched,
327 "### NEW SCHEDULER LOOP (task: %p, cap: %p)",
328 task, initialCapability);
332 // -----------------------------------------------------------
333 // Scheduler loop starts here:
335 #if defined(PARALLEL_HASKELL)
336 #define TERMINATION_CONDITION (!receivedFinish)
338 #define TERMINATION_CONDITION ((event = get_next_event()) != (rtsEvent*)NULL)
340 #define TERMINATION_CONDITION rtsTrue
343 while (TERMINATION_CONDITION) {
346 /* Choose the processor with the next event */
347 CurrentProc = event->proc;
348 CurrentTSO = event->tso;
351 #if defined(THREADED_RTS)
353 // don't yield the first time, we want a chance to run this
354 // thread for a bit, even if there are others banging at the
357 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
359 // Yield the capability to higher-priority tasks if necessary.
360 yieldCapability(&cap, task);
364 #if defined(THREADED_RTS)
365 schedulePushWork(cap,task);
368 // Check whether we have re-entered the RTS from Haskell without
369 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
371 if (cap->in_haskell) {
372 errorBelch("schedule: re-entered unsafely.\n"
373 " Perhaps a 'foreign import unsafe' should be 'safe'?");
374 stg_exit(EXIT_FAILURE);
377 // The interruption / shutdown sequence.
379 // In order to cleanly shut down the runtime, we want to:
380 // * make sure that all main threads return to their callers
381 // with the state 'Interrupted'.
382 // * clean up all OS threads assocated with the runtime
383 // * free all memory etc.
385 // So the sequence for ^C goes like this:
387 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
388 // arranges for some Capability to wake up
390 // * all threads in the system are halted, and the zombies are
391 // placed on the run queue for cleaning up. We acquire all
392 // the capabilities in order to delete the threads, this is
393 // done by scheduleDoGC() for convenience (because GC already
394 // needs to acquire all the capabilities). We can't kill
395 // threads involved in foreign calls.
397 // * somebody calls shutdownHaskell(), which calls exitScheduler()
399 // * sched_state := SCHED_SHUTTING_DOWN
401 // * all workers exit when the run queue on their capability
402 // drains. All main threads will also exit when their TSO
403 // reaches the head of the run queue and they can return.
405 // * eventually all Capabilities will shut down, and the RTS can
408 // * We might be left with threads blocked in foreign calls,
409 // we should really attempt to kill these somehow (TODO);
411 switch (sched_state) {
414 case SCHED_INTERRUPTING:
415 debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
416 #if defined(THREADED_RTS)
417 discardSparksCap(cap);
419 /* scheduleDoGC() deletes all the threads */
420 cap = scheduleDoGC(cap,task,rtsFalse);
422 case SCHED_SHUTTING_DOWN:
423 debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
424 // If we are a worker, just exit. If we're a bound thread
425 // then we will exit below when we've removed our TSO from
427 if (task->tso == NULL && emptyRunQueue(cap)) {
432 barf("sched_state: %d", sched_state);
435 #if defined(THREADED_RTS)
436 // If the run queue is empty, take a spark and turn it into a thread.
438 if (emptyRunQueue(cap)) {
440 spark = findSpark(cap);
442 debugTrace(DEBUG_sched,
443 "turning spark of closure %p into a thread",
444 (StgClosure *)spark);
445 createSparkThread(cap,spark);
449 #endif // THREADED_RTS
451 scheduleStartSignalHandlers(cap);
453 // Only check the black holes here if we've nothing else to do.
454 // During normal execution, the black hole list only gets checked
455 // at GC time, to avoid repeatedly traversing this possibly long
456 // list each time around the scheduler.
457 if (emptyRunQueue(cap)) { scheduleCheckBlackHoles(cap); }
459 scheduleCheckWakeupThreads(cap);
461 scheduleCheckBlockedThreads(cap);
463 scheduleDetectDeadlock(cap,task);
464 #if defined(THREADED_RTS)
465 cap = task->cap; // reload cap, it might have changed
468 // Normally, the only way we can get here with no threads to
469 // run is if a keyboard interrupt received during
470 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
471 // Additionally, it is not fatal for the
472 // threaded RTS to reach here with no threads to run.
474 // win32: might be here due to awaitEvent() being abandoned
475 // as a result of a console event having been delivered.
476 if ( emptyRunQueue(cap) ) {
477 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
478 ASSERT(sched_state >= SCHED_INTERRUPTING);
480 continue; // nothing to do
483 #if defined(PARALLEL_HASKELL)
484 scheduleSendPendingMessages();
485 if (emptyRunQueue(cap) && scheduleActivateSpark())
489 ASSERT(next_fish_to_send_at==0); // i.e. no delayed fishes left!
492 /* If we still have no work we need to send a FISH to get a spark
494 if (emptyRunQueue(cap)) {
495 if (!scheduleGetRemoteWork(&receivedFinish)) continue;
496 ASSERT(rtsFalse); // should not happen at the moment
498 // from here: non-empty run queue.
499 // TODO: merge above case with this, only one call processMessages() !
500 if (PacketsWaiting()) { /* process incoming messages, if
501 any pending... only in else
502 because getRemoteWork waits for
504 receivedFinish = processMessages();
509 scheduleProcessEvent(event);
513 // Get a thread to run
515 t = popRunQueue(cap);
517 #if defined(GRAN) || defined(PAR)
518 scheduleGranParReport(); // some kind of debuging output
520 // Sanity check the thread we're about to run. This can be
521 // expensive if there is lots of thread switching going on...
522 IF_DEBUG(sanity,checkTSO(t));
525 #if defined(THREADED_RTS)
526 // Check whether we can run this thread in the current task.
527 // If not, we have to pass our capability to the right task.
529 Task *bound = t->bound;
533 debugTrace(DEBUG_sched,
534 "### Running thread %lu in bound thread", (unsigned long)t->id);
535 // yes, the Haskell thread is bound to the current native thread
537 debugTrace(DEBUG_sched,
538 "### thread %lu bound to another OS thread", (unsigned long)t->id);
539 // no, bound to a different Haskell thread: pass to that thread
540 pushOnRunQueue(cap,t);
544 // The thread we want to run is unbound.
546 debugTrace(DEBUG_sched,
547 "### this OS thread cannot run thread %lu", (unsigned long)t->id);
548 // no, the current native thread is bound to a different
549 // Haskell thread, so pass it to any worker thread
550 pushOnRunQueue(cap,t);
557 cap->r.rCurrentTSO = t;
559 /* context switches are initiated by the timer signal, unless
560 * the user specified "context switch as often as possible", with
563 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
564 && !emptyThreadQueues(cap)) {
570 debugTrace(DEBUG_sched, "-->> running thread %ld %s ...",
571 (long)t->id, whatNext_strs[t->what_next]);
573 startHeapProfTimer();
575 // Check for exceptions blocked on this thread
576 maybePerformBlockedException (cap, t);
578 // ----------------------------------------------------------------------
579 // Run the current thread
581 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
582 ASSERT(t->cap == cap);
584 prev_what_next = t->what_next;
586 errno = t->saved_errno;
588 SetLastError(t->saved_winerror);
591 cap->in_haskell = rtsTrue;
595 recent_activity = ACTIVITY_YES;
597 switch (prev_what_next) {
601 /* Thread already finished, return to scheduler. */
602 ret = ThreadFinished;
608 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
609 cap = regTableToCapability(r);
614 case ThreadInterpret:
615 cap = interpretBCO(cap);
620 barf("schedule: invalid what_next field");
623 cap->in_haskell = rtsFalse;
625 // The TSO might have moved, eg. if it re-entered the RTS and a GC
626 // happened. So find the new location:
627 t = cap->r.rCurrentTSO;
629 // We have run some Haskell code: there might be blackhole-blocked
630 // threads to wake up now.
631 // Lock-free test here should be ok, we're just setting a flag.
632 if ( blackhole_queue != END_TSO_QUEUE ) {
633 blackholes_need_checking = rtsTrue;
636 // And save the current errno in this thread.
637 // XXX: possibly bogus for SMP because this thread might already
638 // be running again, see code below.
639 t->saved_errno = errno;
641 // Similarly for Windows error code
642 t->saved_winerror = GetLastError();
645 #if defined(THREADED_RTS)
646 // If ret is ThreadBlocked, and this Task is bound to the TSO that
647 // blocked, we are in limbo - the TSO is now owned by whatever it
648 // is blocked on, and may in fact already have been woken up,
649 // perhaps even on a different Capability. It may be the case
650 // that task->cap != cap. We better yield this Capability
651 // immediately and return to normaility.
652 if (ret == ThreadBlocked) {
653 debugTrace(DEBUG_sched,
654 "--<< thread %lu (%s) stopped: blocked",
655 (unsigned long)t->id, whatNext_strs[t->what_next]);
660 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
661 ASSERT(t->cap == cap);
663 // ----------------------------------------------------------------------
665 // Costs for the scheduler are assigned to CCS_SYSTEM
667 #if defined(PROFILING)
671 schedulePostRunThread();
673 ready_to_gc = rtsFalse;
677 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
681 scheduleHandleStackOverflow(cap,task,t);
685 if (scheduleHandleYield(cap, t, prev_what_next)) {
686 // shortcut for switching between compiler/interpreter:
692 scheduleHandleThreadBlocked(t);
696 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
697 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
701 barf("schedule: invalid thread return code %d", (int)ret);
704 if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
705 cap = scheduleDoGC(cap,task,rtsFalse);
707 } /* end of while() */
709 debugTrace(PAR_DEBUG_verbose,
710 "== Leaving schedule() after having received Finish");
713 /* ----------------------------------------------------------------------------
714 * Setting up the scheduler loop
715 * ------------------------------------------------------------------------- */
718 schedulePreLoop(void)
721 /* set up first event to get things going */
722 /* ToDo: assign costs for system setup and init MainTSO ! */
723 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
725 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
727 debugTrace (DEBUG_gran,
728 "GRAN: Init CurrentTSO (in schedule) = %p",
730 IF_DEBUG(gran, G_TSO(CurrentTSO, 5));
732 if (RtsFlags.GranFlags.Light) {
733 /* Save current time; GranSim Light only */
734 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
739 /* -----------------------------------------------------------------------------
742 * Push work to other Capabilities if we have some.
743 * -------------------------------------------------------------------------- */
745 #if defined(THREADED_RTS)
747 schedulePushWork(Capability *cap USED_IF_THREADS,
748 Task *task USED_IF_THREADS)
750 Capability *free_caps[n_capabilities], *cap0;
753 // migration can be turned off with +RTS -qg
754 if (!RtsFlags.ParFlags.migrate) return;
756 // Check whether we have more threads on our run queue, or sparks
757 // in our pool, that we could hand to another Capability.
758 if ((emptyRunQueue(cap) || cap->run_queue_hd->link == END_TSO_QUEUE)
759 && sparkPoolSizeCap(cap) < 2) {
763 // First grab as many free Capabilities as we can.
764 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
765 cap0 = &capabilities[i];
766 if (cap != cap0 && tryGrabCapability(cap0,task)) {
767 if (!emptyRunQueue(cap0) || cap->returning_tasks_hd != NULL) {
768 // it already has some work, we just grabbed it at
769 // the wrong moment. Or maybe it's deadlocked!
770 releaseCapability(cap0);
772 free_caps[n_free_caps++] = cap0;
777 // we now have n_free_caps free capabilities stashed in
778 // free_caps[]. Share our run queue equally with them. This is
779 // probably the simplest thing we could do; improvements we might
780 // want to do include:
782 // - giving high priority to moving relatively new threads, on
783 // the gournds that they haven't had time to build up a
784 // working set in the cache on this CPU/Capability.
786 // - giving low priority to moving long-lived threads
788 if (n_free_caps > 0) {
789 StgTSO *prev, *t, *next;
790 rtsBool pushed_to_all;
792 debugTrace(DEBUG_sched, "excess threads on run queue and %d free capabilities, sharing...", n_free_caps);
795 pushed_to_all = rtsFalse;
797 if (cap->run_queue_hd != END_TSO_QUEUE) {
798 prev = cap->run_queue_hd;
800 prev->link = END_TSO_QUEUE;
801 for (; t != END_TSO_QUEUE; t = next) {
803 t->link = END_TSO_QUEUE;
804 if (t->what_next == ThreadRelocated
805 || t->bound == task // don't move my bound thread
806 || tsoLocked(t)) { // don't move a locked thread
809 } else if (i == n_free_caps) {
810 pushed_to_all = rtsTrue;
816 debugTrace(DEBUG_sched, "pushing thread %lu to capability %d", (unsigned long)t->id, free_caps[i]->no);
817 appendToRunQueue(free_caps[i],t);
818 if (t->bound) { t->bound->cap = free_caps[i]; }
819 t->cap = free_caps[i];
823 cap->run_queue_tl = prev;
826 // If there are some free capabilities that we didn't push any
827 // threads to, then try to push a spark to each one.
828 if (!pushed_to_all) {
830 // i is the next free capability to push to
831 for (; i < n_free_caps; i++) {
832 if (emptySparkPoolCap(free_caps[i])) {
833 spark = findSpark(cap);
835 debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
836 newSpark(&(free_caps[i]->r), spark);
842 // release the capabilities
843 for (i = 0; i < n_free_caps; i++) {
844 task->cap = free_caps[i];
845 releaseCapability(free_caps[i]);
848 task->cap = cap; // reset to point to our Capability.
852 /* ----------------------------------------------------------------------------
853 * Start any pending signal handlers
854 * ------------------------------------------------------------------------- */
856 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
858 scheduleStartSignalHandlers(Capability *cap)
860 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
861 // safe outside the lock
862 startSignalHandlers(cap);
867 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
872 /* ----------------------------------------------------------------------------
873 * Check for blocked threads that can be woken up.
874 * ------------------------------------------------------------------------- */
877 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
879 #if !defined(THREADED_RTS)
881 // Check whether any waiting threads need to be woken up. If the
882 // run queue is empty, and there are no other tasks running, we
883 // can wait indefinitely for something to happen.
885 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
887 awaitEvent( emptyRunQueue(cap) && !blackholes_need_checking );
893 /* ----------------------------------------------------------------------------
894 * Check for threads woken up by other Capabilities
895 * ------------------------------------------------------------------------- */
898 scheduleCheckWakeupThreads(Capability *cap USED_IF_THREADS)
900 #if defined(THREADED_RTS)
901 // Any threads that were woken up by other Capabilities get
902 // appended to our run queue.
903 if (!emptyWakeupQueue(cap)) {
904 ACQUIRE_LOCK(&cap->lock);
905 if (emptyRunQueue(cap)) {
906 cap->run_queue_hd = cap->wakeup_queue_hd;
907 cap->run_queue_tl = cap->wakeup_queue_tl;
909 cap->run_queue_tl->link = cap->wakeup_queue_hd;
910 cap->run_queue_tl = cap->wakeup_queue_tl;
912 cap->wakeup_queue_hd = cap->wakeup_queue_tl = END_TSO_QUEUE;
913 RELEASE_LOCK(&cap->lock);
918 /* ----------------------------------------------------------------------------
919 * Check for threads blocked on BLACKHOLEs that can be woken up
920 * ------------------------------------------------------------------------- */
922 scheduleCheckBlackHoles (Capability *cap)
924 if ( blackholes_need_checking ) // check without the lock first
926 ACQUIRE_LOCK(&sched_mutex);
927 if ( blackholes_need_checking ) {
928 checkBlackHoles(cap);
929 blackholes_need_checking = rtsFalse;
931 RELEASE_LOCK(&sched_mutex);
935 /* ----------------------------------------------------------------------------
936 * Detect deadlock conditions and attempt to resolve them.
937 * ------------------------------------------------------------------------- */
940 scheduleDetectDeadlock (Capability *cap, Task *task)
943 #if defined(PARALLEL_HASKELL)
944 // ToDo: add deadlock detection in GUM (similar to THREADED_RTS) -- HWL
949 * Detect deadlock: when we have no threads to run, there are no
950 * threads blocked, waiting for I/O, or sleeping, and all the
951 * other tasks are waiting for work, we must have a deadlock of
954 if ( emptyThreadQueues(cap) )
956 #if defined(THREADED_RTS)
958 * In the threaded RTS, we only check for deadlock if there
959 * has been no activity in a complete timeslice. This means
960 * we won't eagerly start a full GC just because we don't have
961 * any threads to run currently.
963 if (recent_activity != ACTIVITY_INACTIVE) return;
966 debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
968 // Garbage collection can release some new threads due to
969 // either (a) finalizers or (b) threads resurrected because
970 // they are unreachable and will therefore be sent an
971 // exception. Any threads thus released will be immediately
973 cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/);
975 recent_activity = ACTIVITY_DONE_GC;
977 if ( !emptyRunQueue(cap) ) return;
979 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
980 /* If we have user-installed signal handlers, then wait
981 * for signals to arrive rather then bombing out with a
984 if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
985 debugTrace(DEBUG_sched,
986 "still deadlocked, waiting for signals...");
990 if (signals_pending()) {
991 startSignalHandlers(cap);
994 // either we have threads to run, or we were interrupted:
995 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
999 #if !defined(THREADED_RTS)
1000 /* Probably a real deadlock. Send the current main thread the
1001 * Deadlock exception.
1004 switch (task->tso->why_blocked) {
1006 case BlockedOnBlackHole:
1007 case BlockedOnException:
1009 throwToSingleThreaded(cap, task->tso,
1010 (StgClosure *)NonTermination_closure);
1013 barf("deadlock: main thread blocked in a strange way");
1021 /* ----------------------------------------------------------------------------
1022 * Process an event (GRAN only)
1023 * ------------------------------------------------------------------------- */
1027 scheduleProcessEvent(rtsEvent *event)
1031 if (RtsFlags.GranFlags.Light)
1032 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
1034 /* adjust time based on time-stamp */
1035 if (event->time > CurrentTime[CurrentProc] &&
1036 event->evttype != ContinueThread)
1037 CurrentTime[CurrentProc] = event->time;
1039 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
1040 if (!RtsFlags.GranFlags.Light)
1043 IF_DEBUG(gran, debugBelch("GRAN: switch by event-type\n"));
1045 /* main event dispatcher in GranSim */
1046 switch (event->evttype) {
1047 /* Should just be continuing execution */
1048 case ContinueThread:
1049 IF_DEBUG(gran, debugBelch("GRAN: doing ContinueThread\n"));
1050 /* ToDo: check assertion
1051 ASSERT(run_queue_hd != (StgTSO*)NULL &&
1052 run_queue_hd != END_TSO_QUEUE);
1054 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
1055 if (!RtsFlags.GranFlags.DoAsyncFetch &&
1056 procStatus[CurrentProc]==Fetching) {
1057 debugBelch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]\n",
1058 CurrentTSO->id, CurrentTSO, CurrentProc);
1061 /* Ignore ContinueThreads for completed threads */
1062 if (CurrentTSO->what_next == ThreadComplete) {
1063 debugBelch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)\n",
1064 CurrentTSO->id, CurrentTSO, CurrentProc);
1067 /* Ignore ContinueThreads for threads that are being migrated */
1068 if (PROCS(CurrentTSO)==Nowhere) {
1069 debugBelch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)\n",
1070 CurrentTSO->id, CurrentTSO, CurrentProc);
1073 /* The thread should be at the beginning of the run queue */
1074 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
1075 debugBelch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread\n",
1076 CurrentTSO->id, CurrentTSO, CurrentProc);
1077 break; // run the thread anyway
1080 new_event(proc, proc, CurrentTime[proc],
1082 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
1084 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
1085 break; // now actually run the thread; DaH Qu'vam yImuHbej
1088 do_the_fetchnode(event);
1089 goto next_thread; /* handle next event in event queue */
1092 do_the_globalblock(event);
1093 goto next_thread; /* handle next event in event queue */
1096 do_the_fetchreply(event);
1097 goto next_thread; /* handle next event in event queue */
1099 case UnblockThread: /* Move from the blocked queue to the tail of */
1100 do_the_unblock(event);
1101 goto next_thread; /* handle next event in event queue */
1103 case ResumeThread: /* Move from the blocked queue to the tail of */
1104 /* the runnable queue ( i.e. Qu' SImqa'lu') */
1105 event->tso->gran.blocktime +=
1106 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
1107 do_the_startthread(event);
1108 goto next_thread; /* handle next event in event queue */
1111 do_the_startthread(event);
1112 goto next_thread; /* handle next event in event queue */
1115 do_the_movethread(event);
1116 goto next_thread; /* handle next event in event queue */
1119 do_the_movespark(event);
1120 goto next_thread; /* handle next event in event queue */
1123 do_the_findwork(event);
1124 goto next_thread; /* handle next event in event queue */
1127 barf("Illegal event type %u\n", event->evttype);
1130 /* This point was scheduler_loop in the old RTS */
1132 IF_DEBUG(gran, debugBelch("GRAN: after main switch\n"));
1134 TimeOfLastEvent = CurrentTime[CurrentProc];
1135 TimeOfNextEvent = get_time_of_next_event();
1136 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
1137 // CurrentTSO = ThreadQueueHd;
1139 IF_DEBUG(gran, debugBelch("GRAN: time of next event is: %ld\n",
1142 if (RtsFlags.GranFlags.Light)
1143 GranSimLight_leave_system(event, &ActiveTSO);
1145 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
1148 debugBelch("GRAN: end of time-slice is %#lx\n", EndOfTimeSlice));
1150 /* in a GranSim setup the TSO stays on the run queue */
1152 /* Take a thread from the run queue. */
1153 POP_RUN_QUEUE(t); // take_off_run_queue(t);
1156 debugBelch("GRAN: About to run current thread, which is\n");
1159 context_switch = 0; // turned on via GranYield, checking events and time slice
1162 DumpGranEvent(GR_SCHEDULE, t));
1164 procStatus[CurrentProc] = Busy;
1168 /* ----------------------------------------------------------------------------
1169 * Send pending messages (PARALLEL_HASKELL only)
1170 * ------------------------------------------------------------------------- */
1172 #if defined(PARALLEL_HASKELL)
1174 scheduleSendPendingMessages(void)
1180 # if defined(PAR) // global Mem.Mgmt., omit for now
1181 if (PendingFetches != END_BF_QUEUE) {
1186 if (RtsFlags.ParFlags.BufferTime) {
1187 // if we use message buffering, we must send away all message
1188 // packets which have become too old...
1194 /* ----------------------------------------------------------------------------
1195 * Activate spark threads (PARALLEL_HASKELL only)
1196 * ------------------------------------------------------------------------- */
1198 #if defined(PARALLEL_HASKELL)
1200 scheduleActivateSpark(void)
1203 ASSERT(emptyRunQueue());
1204 /* We get here if the run queue is empty and want some work.
1205 We try to turn a spark into a thread, and add it to the run queue,
1206 from where it will be picked up in the next iteration of the scheduler
1210 /* :-[ no local threads => look out for local sparks */
1211 /* the spark pool for the current PE */
1212 pool = &(cap.r.rSparks); // JB: cap = (old) MainCap
1213 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
1214 pool->hd < pool->tl) {
1216 * ToDo: add GC code check that we really have enough heap afterwards!!
1218 * If we're here (no runnable threads) and we have pending
1219 * sparks, we must have a space problem. Get enough space
1220 * to turn one of those pending sparks into a
1224 spark = findSpark(rtsFalse); /* get a spark */
1225 if (spark != (rtsSpark) NULL) {
1226 tso = createThreadFromSpark(spark); /* turn the spark into a thread */
1227 IF_PAR_DEBUG(fish, // schedule,
1228 debugBelch("==== schedule: Created TSO %d (%p); %d threads active\n",
1229 tso->id, tso, advisory_thread_count));
1231 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
1232 IF_PAR_DEBUG(fish, // schedule,
1233 debugBelch("==^^ failed to create thread from spark @ %lx\n",
1235 return rtsFalse; /* failed to generate a thread */
1236 } /* otherwise fall through & pick-up new tso */
1238 IF_PAR_DEBUG(fish, // schedule,
1239 debugBelch("==^^ no local sparks (spark pool contains only NFs: %d)\n",
1240 spark_queue_len(pool)));
1241 return rtsFalse; /* failed to generate a thread */
1243 return rtsTrue; /* success in generating a thread */
1244 } else { /* no more threads permitted or pool empty */
1245 return rtsFalse; /* failed to generateThread */
1248 tso = NULL; // avoid compiler warning only
1249 return rtsFalse; /* dummy in non-PAR setup */
1252 #endif // PARALLEL_HASKELL
1254 /* ----------------------------------------------------------------------------
1255 * Get work from a remote node (PARALLEL_HASKELL only)
1256 * ------------------------------------------------------------------------- */
1258 #if defined(PARALLEL_HASKELL)
1260 scheduleGetRemoteWork(rtsBool *receivedFinish)
1262 ASSERT(emptyRunQueue());
1264 if (RtsFlags.ParFlags.BufferTime) {
1265 IF_PAR_DEBUG(verbose,
1266 debugBelch("...send all pending data,"));
1269 for (i=1; i<=nPEs; i++)
1270 sendImmediately(i); // send all messages away immediately
1274 //++EDEN++ idle() , i.e. send all buffers, wait for work
1275 // suppress fishing in EDEN... just look for incoming messages
1276 // (blocking receive)
1277 IF_PAR_DEBUG(verbose,
1278 debugBelch("...wait for incoming messages...\n"));
1279 *receivedFinish = processMessages(); // blocking receive...
1281 // and reenter scheduling loop after having received something
1282 // (return rtsFalse below)
1284 # else /* activate SPARKS machinery */
1285 /* We get here, if we have no work, tried to activate a local spark, but still
1286 have no work. We try to get a remote spark, by sending a FISH message.
1287 Thread migration should be added here, and triggered when a sequence of
1288 fishes returns without work. */
1289 delay = (RtsFlags.ParFlags.fishDelay!=0ll ? RtsFlags.ParFlags.fishDelay : 0ll);
1291 /* =8-[ no local sparks => look for work on other PEs */
1293 * We really have absolutely no work. Send out a fish
1294 * (there may be some out there already), and wait for
1295 * something to arrive. We clearly can't run any threads
1296 * until a SCHEDULE or RESUME arrives, and so that's what
1297 * we're hoping to see. (Of course, we still have to
1298 * respond to other types of messages.)
1300 rtsTime now = msTime() /*CURRENT_TIME*/;
1301 IF_PAR_DEBUG(verbose,
1302 debugBelch("-- now=%ld\n", now));
1303 IF_PAR_DEBUG(fish, // verbose,
1304 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1305 (last_fish_arrived_at!=0 &&
1306 last_fish_arrived_at+delay > now)) {
1307 debugBelch("--$$ <%llu> delaying FISH until %llu (last fish %llu, delay %llu)\n",
1308 now, last_fish_arrived_at+delay,
1309 last_fish_arrived_at,
1313 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1314 advisory_thread_count < RtsFlags.ParFlags.maxThreads) { // send a FISH, but when?
1315 if (last_fish_arrived_at==0 ||
1316 (last_fish_arrived_at+delay <= now)) { // send FISH now!
1317 /* outstandingFishes is set in sendFish, processFish;
1318 avoid flooding system with fishes via delay */
1319 next_fish_to_send_at = 0;
1321 /* ToDo: this should be done in the main scheduling loop to avoid the
1322 busy wait here; not so bad if fish delay is very small */
1323 int iq = 0; // DEBUGGING -- HWL
1324 next_fish_to_send_at = last_fish_arrived_at+delay; // remember when to send
1325 /* send a fish when ready, but process messages that arrive in the meantime */
1327 if (PacketsWaiting()) {
1329 *receivedFinish = processMessages();
1332 } while (!*receivedFinish || now<next_fish_to_send_at);
1333 // JB: This means the fish could become obsolete, if we receive
1334 // work. Better check for work again?
1335 // last line: while (!receivedFinish || !haveWork || now<...)
1336 // next line: if (receivedFinish || haveWork )
1338 if (*receivedFinish) // no need to send a FISH if we are finishing anyway
1339 return rtsFalse; // NB: this will leave scheduler loop
1340 // immediately after return!
1342 IF_PAR_DEBUG(fish, // verbose,
1343 debugBelch("--$$ <%llu> sent delayed fish (%d processMessages); active/total threads=%d/%d\n",now,iq,run_queue_len(),advisory_thread_count));
1347 // JB: IMHO, this should all be hidden inside sendFish(...)
1349 sendFish(pe, thisPE, NEW_FISH_AGE, NEW_FISH_HISTORY,
1352 // Global statistics: count no. of fishes
1353 if (RtsFlags.ParFlags.ParStats.Global &&
1354 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1355 globalParStats.tot_fish_mess++;
1359 /* delayed fishes must have been sent by now! */
1360 next_fish_to_send_at = 0;
1363 *receivedFinish = processMessages();
1364 # endif /* SPARKS */
1367 /* NB: this function always returns rtsFalse, meaning the scheduler
1368 loop continues with the next iteration;
1370 return code means success in finding work; we enter this function
1371 if there is no local work, thus have to send a fish which takes
1372 time until it arrives with work; in the meantime we should process
1373 messages in the main loop;
1376 #endif // PARALLEL_HASKELL
1378 /* ----------------------------------------------------------------------------
1379 * PAR/GRAN: Report stats & debugging info(?)
1380 * ------------------------------------------------------------------------- */
1382 #if defined(PAR) || defined(GRAN)
1384 scheduleGranParReport(void)
1386 ASSERT(run_queue_hd != END_TSO_QUEUE);
1388 /* Take a thread from the run queue, if we have work */
1389 POP_RUN_QUEUE(t); // take_off_run_queue(END_TSO_QUEUE);
1391 /* If this TSO has got its outport closed in the meantime,
1392 * it mustn't be run. Instead, we have to clean it up as if it was finished.
1393 * It has to be marked as TH_DEAD for this purpose.
1394 * If it is TH_TERM instead, it is supposed to have finished in the normal way.
1396 JB: TODO: investigate wether state change field could be nuked
1397 entirely and replaced by the normal tso state (whatnext
1398 field). All we want to do is to kill tsos from outside.
1401 /* ToDo: write something to the log-file
1402 if (RTSflags.ParFlags.granSimStats && !sameThread)
1403 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
1407 /* the spark pool for the current PE */
1408 pool = &(cap.r.rSparks); // cap = (old) MainCap
1411 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1412 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1415 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1416 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1418 if (RtsFlags.ParFlags.ParStats.Full &&
1419 (t->par.sparkname != (StgInt)0) && // only log spark generated threads
1420 (emitSchedule || // forced emit
1421 (t && LastTSO && t->id != LastTSO->id))) {
1423 we are running a different TSO, so write a schedule event to log file
1424 NB: If we use fair scheduling we also have to write a deschedule
1425 event for LastTSO; with unfair scheduling we know that the
1426 previous tso has blocked whenever we switch to another tso, so
1427 we don't need it in GUM for now
1429 IF_PAR_DEBUG(fish, // schedule,
1430 debugBelch("____ scheduling spark generated thread %d (%lx) (%lx) via a forced emit\n",t->id,t,t->par.sparkname));
1432 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1433 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
1434 emitSchedule = rtsFalse;
1439 /* ----------------------------------------------------------------------------
1440 * After running a thread...
1441 * ------------------------------------------------------------------------- */
1444 schedulePostRunThread(void)
1447 /* HACK 675: if the last thread didn't yield, make sure to print a
1448 SCHEDULE event to the log file when StgRunning the next thread, even
1449 if it is the same one as before */
1451 TimeOfLastYield = CURRENT_TIME;
1454 /* some statistics gathering in the parallel case */
1456 #if defined(GRAN) || defined(PAR) || defined(EDEN)
1460 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
1461 globalGranStats.tot_heapover++;
1463 globalParStats.tot_heapover++;
1470 DumpGranEvent(GR_DESCHEDULE, t));
1471 globalGranStats.tot_stackover++;
1474 // DumpGranEvent(GR_DESCHEDULE, t);
1475 globalParStats.tot_stackover++;
1479 case ThreadYielding:
1482 DumpGranEvent(GR_DESCHEDULE, t));
1483 globalGranStats.tot_yields++;
1486 // DumpGranEvent(GR_DESCHEDULE, t);
1487 globalParStats.tot_yields++;
1493 debugTrace(DEBUG_sched,
1494 "--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1495 t->id, t, whatNext_strs[t->what_next], t->block_info.closure,
1496 (t->block_info.closure==(StgClosure*)NULL ? 99 : where_is(t->block_info.closure)));
1497 if (t->block_info.closure!=(StgClosure*)NULL)
1498 print_bq(t->block_info.closure);
1501 // ??? needed; should emit block before
1503 DumpGranEvent(GR_DESCHEDULE, t));
1504 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1507 ASSERT(procStatus[CurrentProc]==Busy ||
1508 ((procStatus[CurrentProc]==Fetching) &&
1509 (t->block_info.closure!=(StgClosure*)NULL)));
1510 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1511 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1512 procStatus[CurrentProc]==Fetching))
1513 procStatus[CurrentProc] = Idle;
1516 //++PAR++ blockThread() writes the event (change?)
1520 case ThreadFinished:
1524 barf("parGlobalStats: unknown return code");
1530 /* -----------------------------------------------------------------------------
1531 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1532 * -------------------------------------------------------------------------- */
1535 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1537 // did the task ask for a large block?
1538 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1539 // if so, get one and push it on the front of the nursery.
1543 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1545 debugTrace(DEBUG_sched,
1546 "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1547 (long)t->id, whatNext_strs[t->what_next], blocks);
1549 // don't do this if the nursery is (nearly) full, we'll GC first.
1550 if (cap->r.rCurrentNursery->link != NULL ||
1551 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1552 // if the nursery has only one block.
1555 bd = allocGroup( blocks );
1557 cap->r.rNursery->n_blocks += blocks;
1559 // link the new group into the list
1560 bd->link = cap->r.rCurrentNursery;
1561 bd->u.back = cap->r.rCurrentNursery->u.back;
1562 if (cap->r.rCurrentNursery->u.back != NULL) {
1563 cap->r.rCurrentNursery->u.back->link = bd;
1565 #if !defined(THREADED_RTS)
1566 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
1567 g0s0 == cap->r.rNursery);
1569 cap->r.rNursery->blocks = bd;
1571 cap->r.rCurrentNursery->u.back = bd;
1573 // initialise it as a nursery block. We initialise the
1574 // step, gen_no, and flags field of *every* sub-block in
1575 // this large block, because this is easier than making
1576 // sure that we always find the block head of a large
1577 // block whenever we call Bdescr() (eg. evacuate() and
1578 // isAlive() in the GC would both have to do this, at
1582 for (x = bd; x < bd + blocks; x++) {
1583 x->step = cap->r.rNursery;
1589 // This assert can be a killer if the app is doing lots
1590 // of large block allocations.
1591 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1593 // now update the nursery to point to the new block
1594 cap->r.rCurrentNursery = bd;
1596 // we might be unlucky and have another thread get on the
1597 // run queue before us and steal the large block, but in that
1598 // case the thread will just end up requesting another large
1600 pushOnRunQueue(cap,t);
1601 return rtsFalse; /* not actually GC'ing */
1605 debugTrace(DEBUG_sched,
1606 "--<< thread %ld (%s) stopped: HeapOverflow\n",
1607 (long)t->id, whatNext_strs[t->what_next]);
1610 ASSERT(!is_on_queue(t,CurrentProc));
1611 #elif defined(PARALLEL_HASKELL)
1612 /* Currently we emit a DESCHEDULE event before GC in GUM.
1613 ToDo: either add separate event to distinguish SYSTEM time from rest
1614 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1615 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1616 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1617 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1618 emitSchedule = rtsTrue;
1622 pushOnRunQueue(cap,t);
1624 /* actual GC is done at the end of the while loop in schedule() */
1627 /* -----------------------------------------------------------------------------
1628 * Handle a thread that returned to the scheduler with ThreadStackOverflow
1629 * -------------------------------------------------------------------------- */
1632 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1634 debugTrace (DEBUG_sched,
1635 "--<< thread %ld (%s) stopped, StackOverflow",
1636 (long)t->id, whatNext_strs[t->what_next]);
1638 /* just adjust the stack for this thread, then pop it back
1642 /* enlarge the stack */
1643 StgTSO *new_t = threadStackOverflow(cap, t);
1645 /* The TSO attached to this Task may have moved, so update the
1648 if (task->tso == t) {
1651 pushOnRunQueue(cap,new_t);
1655 /* -----------------------------------------------------------------------------
1656 * Handle a thread that returned to the scheduler with ThreadYielding
1657 * -------------------------------------------------------------------------- */
1660 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1662 // Reset the context switch flag. We don't do this just before
1663 // running the thread, because that would mean we would lose ticks
1664 // during GC, which can lead to unfair scheduling (a thread hogs
1665 // the CPU because the tick always arrives during GC). This way
1666 // penalises threads that do a lot of allocation, but that seems
1667 // better than the alternative.
1670 /* put the thread back on the run queue. Then, if we're ready to
1671 * GC, check whether this is the last task to stop. If so, wake
1672 * up the GC thread. getThread will block during a GC until the
1676 if (t->what_next != prev_what_next) {
1677 debugTrace(DEBUG_sched,
1678 "--<< thread %ld (%s) stopped to switch evaluators",
1679 (long)t->id, whatNext_strs[t->what_next]);
1681 debugTrace(DEBUG_sched,
1682 "--<< thread %ld (%s) stopped, yielding",
1683 (long)t->id, whatNext_strs[t->what_next]);
1688 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1690 ASSERT(t->link == END_TSO_QUEUE);
1692 // Shortcut if we're just switching evaluators: don't bother
1693 // doing stack squeezing (which can be expensive), just run the
1695 if (t->what_next != prev_what_next) {
1700 ASSERT(!is_on_queue(t,CurrentProc));
1703 //debugBelch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1704 checkThreadQsSanity(rtsTrue));
1708 addToRunQueue(cap,t);
1711 /* add a ContinueThread event to actually process the thread */
1712 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1714 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1716 debugBelch("GRAN: eventq and runnableq after adding yielded thread to queue again:\n");
1723 /* -----------------------------------------------------------------------------
1724 * Handle a thread that returned to the scheduler with ThreadBlocked
1725 * -------------------------------------------------------------------------- */
1728 scheduleHandleThreadBlocked( StgTSO *t
1729 #if !defined(GRAN) && !defined(DEBUG)
1736 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: \n",
1737 t->id, t, whatNext_strs[t->what_next], t->block_info.closure, (t->block_info.closure==(StgClosure*)NULL ? 99 : where_is(t->block_info.closure)));
1738 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1740 // ??? needed; should emit block before
1742 DumpGranEvent(GR_DESCHEDULE, t));
1743 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1746 ASSERT(procStatus[CurrentProc]==Busy ||
1747 ((procStatus[CurrentProc]==Fetching) &&
1748 (t->block_info.closure!=(StgClosure*)NULL)));
1749 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1750 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1751 procStatus[CurrentProc]==Fetching))
1752 procStatus[CurrentProc] = Idle;
1756 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: \n",
1757 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1760 if (t->block_info.closure!=(StgClosure*)NULL)
1761 print_bq(t->block_info.closure));
1763 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1766 /* whatever we schedule next, we must log that schedule */
1767 emitSchedule = rtsTrue;
1771 // We don't need to do anything. The thread is blocked, and it
1772 // has tidied up its stack and placed itself on whatever queue
1773 // it needs to be on.
1775 // ASSERT(t->why_blocked != NotBlocked);
1776 // Not true: for example,
1777 // - in THREADED_RTS, the thread may already have been woken
1778 // up by another Capability. This actually happens: try
1779 // conc023 +RTS -N2.
1780 // - the thread may have woken itself up already, because
1781 // threadPaused() might have raised a blocked throwTo
1782 // exception, see maybePerformBlockedException().
1785 if (traceClass(DEBUG_sched)) {
1786 debugTraceBegin("--<< thread %lu (%s) stopped: ",
1787 (unsigned long)t->id, whatNext_strs[t->what_next]);
1788 printThreadBlockage(t);
1793 /* Only for dumping event to log file
1794 ToDo: do I need this in GranSim, too?
1800 /* -----------------------------------------------------------------------------
1801 * Handle a thread that returned to the scheduler with ThreadFinished
1802 * -------------------------------------------------------------------------- */
1805 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1807 /* Need to check whether this was a main thread, and if so,
1808 * return with the return value.
1810 * We also end up here if the thread kills itself with an
1811 * uncaught exception, see Exception.cmm.
1813 debugTrace(DEBUG_sched, "--++ thread %lu (%s) finished",
1814 (unsigned long)t->id, whatNext_strs[t->what_next]);
1817 endThread(t, CurrentProc); // clean-up the thread
1818 #elif defined(PARALLEL_HASKELL)
1819 /* For now all are advisory -- HWL */
1820 //if(t->priority==AdvisoryPriority) ??
1821 advisory_thread_count--; // JB: Caution with this counter, buggy!
1824 if(t->dist.priority==RevalPriority)
1828 # if defined(EDENOLD)
1829 // the thread could still have an outport... (BUG)
1830 if (t->eden.outport != -1) {
1831 // delete the outport for the tso which has finished...
1832 IF_PAR_DEBUG(eden_ports,
1833 debugBelch("WARNING: Scheduler removes outport %d for TSO %d.\n",
1834 t->eden.outport, t->id));
1837 // thread still in the process (HEAVY BUG! since outport has just been closed...)
1838 if (t->eden.epid != -1) {
1839 IF_PAR_DEBUG(eden_ports,
1840 debugBelch("WARNING: Scheduler removes TSO %d from process %d .\n",
1841 t->id, t->eden.epid));
1842 removeTSOfromProcess(t);
1847 if (RtsFlags.ParFlags.ParStats.Full &&
1848 !RtsFlags.ParFlags.ParStats.Suppressed)
1849 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1851 // t->par only contains statistics: left out for now...
1853 debugBelch("**** end thread: ended sparked thread %d (%lx); sparkname: %lx\n",
1854 t->id,t,t->par.sparkname));
1856 #endif // PARALLEL_HASKELL
1859 // Check whether the thread that just completed was a bound
1860 // thread, and if so return with the result.
1862 // There is an assumption here that all thread completion goes
1863 // through this point; we need to make sure that if a thread
1864 // ends up in the ThreadKilled state, that it stays on the run
1865 // queue so it can be dealt with here.
1870 if (t->bound != task) {
1871 #if !defined(THREADED_RTS)
1872 // Must be a bound thread that is not the topmost one. Leave
1873 // it on the run queue until the stack has unwound to the
1874 // point where we can deal with this. Leaving it on the run
1875 // queue also ensures that the garbage collector knows about
1876 // this thread and its return value (it gets dropped from the
1877 // all_threads list so there's no other way to find it).
1878 appendToRunQueue(cap,t);
1881 // this cannot happen in the threaded RTS, because a
1882 // bound thread can only be run by the appropriate Task.
1883 barf("finished bound thread that isn't mine");
1887 ASSERT(task->tso == t);
1889 if (t->what_next == ThreadComplete) {
1891 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1892 *(task->ret) = (StgClosure *)task->tso->sp[1];
1894 task->stat = Success;
1897 *(task->ret) = NULL;
1899 if (sched_state >= SCHED_INTERRUPTING) {
1900 task->stat = Interrupted;
1902 task->stat = Killed;
1906 removeThreadLabel((StgWord)task->tso->id);
1908 return rtsTrue; // tells schedule() to return
1914 /* -----------------------------------------------------------------------------
1915 * Perform a heap census
1916 * -------------------------------------------------------------------------- */
1919 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1921 // When we have +RTS -i0 and we're heap profiling, do a census at
1922 // every GC. This lets us get repeatable runs for debugging.
1923 if (performHeapProfile ||
1924 (RtsFlags.ProfFlags.profileInterval==0 &&
1925 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1932 /* -----------------------------------------------------------------------------
1933 * Perform a garbage collection if necessary
1934 * -------------------------------------------------------------------------- */
1937 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS, rtsBool force_major)
1940 rtsBool heap_census;
1942 static volatile StgWord waiting_for_gc;
1943 rtsBool was_waiting;
1948 // In order to GC, there must be no threads running Haskell code.
1949 // Therefore, the GC thread needs to hold *all* the capabilities,
1950 // and release them after the GC has completed.
1952 // This seems to be the simplest way: previous attempts involved
1953 // making all the threads with capabilities give up their
1954 // capabilities and sleep except for the *last* one, which
1955 // actually did the GC. But it's quite hard to arrange for all
1956 // the other tasks to sleep and stay asleep.
1959 was_waiting = cas(&waiting_for_gc, 0, 1);
1962 debugTrace(DEBUG_sched, "someone else is trying to GC...");
1963 if (cap) yieldCapability(&cap,task);
1964 } while (waiting_for_gc);
1965 return cap; // NOTE: task->cap might have changed here
1968 for (i=0; i < n_capabilities; i++) {
1969 debugTrace(DEBUG_sched, "ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities);
1970 if (cap != &capabilities[i]) {
1971 Capability *pcap = &capabilities[i];
1972 // we better hope this task doesn't get migrated to
1973 // another Capability while we're waiting for this one.
1974 // It won't, because load balancing happens while we have
1975 // all the Capabilities, but even so it's a slightly
1976 // unsavoury invariant.
1979 waitForReturnCapability(&pcap, task);
1980 if (pcap != &capabilities[i]) {
1981 barf("scheduleDoGC: got the wrong capability");
1986 waiting_for_gc = rtsFalse;
1989 /* Kick any transactions which are invalid back to their
1990 * atomically frames. When next scheduled they will try to
1991 * commit, this commit will fail and they will retry.
1996 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1997 if (t->what_next == ThreadRelocated) {
2000 next = t->global_link;
2002 // This is a good place to check for blocked
2003 // exceptions. It might be the case that a thread is
2004 // blocked on delivering an exception to a thread that
2005 // is also blocked - we try to ensure that this
2006 // doesn't happen in throwTo(), but it's too hard (or
2007 // impossible) to close all the race holes, so we
2008 // accept that some might get through and deal with
2009 // them here. A GC will always happen at some point,
2010 // even if the system is otherwise deadlocked.
2011 maybePerformBlockedException (&capabilities[0], t);
2013 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
2014 if (!stmValidateNestOfTransactions (t -> trec)) {
2015 debugTrace(DEBUG_sched | DEBUG_stm,
2016 "trec %p found wasting its time", t);
2018 // strip the stack back to the
2019 // ATOMICALLY_FRAME, aborting the (nested)
2020 // transaction, and saving the stack of any
2021 // partially-evaluated thunks on the heap.
2022 throwToSingleThreaded_(&capabilities[0], t,
2023 NULL, rtsTrue, NULL);
2026 ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
2034 // so this happens periodically:
2035 if (cap) scheduleCheckBlackHoles(cap);
2037 IF_DEBUG(scheduler, printAllThreads());
2040 * We now have all the capabilities; if we're in an interrupting
2041 * state, then we should take the opportunity to delete all the
2042 * threads in the system.
2044 if (sched_state >= SCHED_INTERRUPTING) {
2045 deleteAllThreads(&capabilities[0]);
2046 sched_state = SCHED_SHUTTING_DOWN;
2049 heap_census = scheduleNeedHeapProfile(rtsTrue);
2051 /* everybody back, start the GC.
2052 * Could do it in this thread, or signal a condition var
2053 * to do it in another thread. Either way, we need to
2054 * broadcast on gc_pending_cond afterward.
2056 #if defined(THREADED_RTS)
2057 debugTrace(DEBUG_sched, "doing GC");
2059 GarbageCollect(force_major || heap_census);
2062 debugTrace(DEBUG_sched, "performing heap census");
2064 performHeapProfile = rtsFalse;
2067 #if defined(THREADED_RTS)
2068 // release our stash of capabilities.
2069 for (i = 0; i < n_capabilities; i++) {
2070 if (cap != &capabilities[i]) {
2071 task->cap = &capabilities[i];
2072 releaseCapability(&capabilities[i]);
2083 /* add a ContinueThread event to continue execution of current thread */
2084 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
2086 t, (StgClosure*)NULL, (rtsSpark*)NULL);
2088 debugBelch("GRAN: eventq and runnableq after Garbage collection:\n\n");
2096 /* ---------------------------------------------------------------------------
2097 * Singleton fork(). Do not copy any running threads.
2098 * ------------------------------------------------------------------------- */
2101 forkProcess(HsStablePtr *entry
2102 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
2107 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2113 #if defined(THREADED_RTS)
2114 if (RtsFlags.ParFlags.nNodes > 1) {
2115 errorBelch("forking not supported with +RTS -N<n> greater than 1");
2116 stg_exit(EXIT_FAILURE);
2120 debugTrace(DEBUG_sched, "forking!");
2122 // ToDo: for SMP, we should probably acquire *all* the capabilities
2127 if (pid) { // parent
2129 // just return the pid
2135 // Now, all OS threads except the thread that forked are
2136 // stopped. We need to stop all Haskell threads, including
2137 // those involved in foreign calls. Also we need to delete
2138 // all Tasks, because they correspond to OS threads that are
2141 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
2142 if (t->what_next == ThreadRelocated) {
2145 next = t->global_link;
2146 // don't allow threads to catch the ThreadKilled
2147 // exception, but we do want to raiseAsync() because these
2148 // threads may be evaluating thunks that we need later.
2149 deleteThread_(cap,t);
2153 // Empty the run queue. It seems tempting to let all the
2154 // killed threads stay on the run queue as zombies to be
2155 // cleaned up later, but some of them correspond to bound
2156 // threads for which the corresponding Task does not exist.
2157 cap->run_queue_hd = END_TSO_QUEUE;
2158 cap->run_queue_tl = END_TSO_QUEUE;
2160 // Any suspended C-calling Tasks are no more, their OS threads
2162 cap->suspended_ccalling_tasks = NULL;
2164 // Empty the all_threads list. Otherwise, the garbage
2165 // collector may attempt to resurrect some of these threads.
2166 all_threads = END_TSO_QUEUE;
2168 // Wipe the task list, except the current Task.
2169 ACQUIRE_LOCK(&sched_mutex);
2170 for (task = all_tasks; task != NULL; task=task->all_link) {
2171 if (task != cap->running_task) {
2175 RELEASE_LOCK(&sched_mutex);
2177 #if defined(THREADED_RTS)
2178 // Wipe our spare workers list, they no longer exist. New
2179 // workers will be created if necessary.
2180 cap->spare_workers = NULL;
2181 cap->returning_tasks_hd = NULL;
2182 cap->returning_tasks_tl = NULL;
2185 // On Unix, all timers are reset in the child, so we need to start
2189 cap = rts_evalStableIO(cap, entry, NULL); // run the action
2190 rts_checkSchedStatus("forkProcess",cap);
2193 hs_exit(); // clean up and exit
2194 stg_exit(EXIT_SUCCESS);
2196 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
2197 barf("forkProcess#: primop not supported on this platform, sorry!\n");
2202 /* ---------------------------------------------------------------------------
2203 * Delete all the threads in the system
2204 * ------------------------------------------------------------------------- */
2207 deleteAllThreads ( Capability *cap )
2209 // NOTE: only safe to call if we own all capabilities.
2212 debugTrace(DEBUG_sched,"deleting all threads");
2213 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
2214 if (t->what_next == ThreadRelocated) {
2217 next = t->global_link;
2218 deleteThread(cap,t);
2222 // The run queue now contains a bunch of ThreadKilled threads. We
2223 // must not throw these away: the main thread(s) will be in there
2224 // somewhere, and the main scheduler loop has to deal with it.
2225 // Also, the run queue is the only thing keeping these threads from
2226 // being GC'd, and we don't want the "main thread has been GC'd" panic.
2228 #if !defined(THREADED_RTS)
2229 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
2230 ASSERT(sleeping_queue == END_TSO_QUEUE);
2234 /* -----------------------------------------------------------------------------
2235 Managing the suspended_ccalling_tasks list.
2236 Locks required: sched_mutex
2237 -------------------------------------------------------------------------- */
2240 suspendTask (Capability *cap, Task *task)
2242 ASSERT(task->next == NULL && task->prev == NULL);
2243 task->next = cap->suspended_ccalling_tasks;
2245 if (cap->suspended_ccalling_tasks) {
2246 cap->suspended_ccalling_tasks->prev = task;
2248 cap->suspended_ccalling_tasks = task;
2252 recoverSuspendedTask (Capability *cap, Task *task)
2255 task->prev->next = task->next;
2257 ASSERT(cap->suspended_ccalling_tasks == task);
2258 cap->suspended_ccalling_tasks = task->next;
2261 task->next->prev = task->prev;
2263 task->next = task->prev = NULL;
2266 /* ---------------------------------------------------------------------------
2267 * Suspending & resuming Haskell threads.
2269 * When making a "safe" call to C (aka _ccall_GC), the task gives back
2270 * its capability before calling the C function. This allows another
2271 * task to pick up the capability and carry on running Haskell
2272 * threads. It also means that if the C call blocks, it won't lock
2275 * The Haskell thread making the C call is put to sleep for the
2276 * duration of the call, on the susepended_ccalling_threads queue. We
2277 * give out a token to the task, which it can use to resume the thread
2278 * on return from the C function.
2279 * ------------------------------------------------------------------------- */
2282 suspendThread (StgRegTable *reg)
2289 StgWord32 saved_winerror;
2292 saved_errno = errno;
2294 saved_winerror = GetLastError();
2297 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
2299 cap = regTableToCapability(reg);
2301 task = cap->running_task;
2302 tso = cap->r.rCurrentTSO;
2304 debugTrace(DEBUG_sched,
2305 "thread %lu did a safe foreign call",
2306 (unsigned long)cap->r.rCurrentTSO->id);
2308 // XXX this might not be necessary --SDM
2309 tso->what_next = ThreadRunGHC;
2311 threadPaused(cap,tso);
2313 if ((tso->flags & TSO_BLOCKEX) == 0) {
2314 tso->why_blocked = BlockedOnCCall;
2315 tso->flags |= TSO_BLOCKEX;
2316 tso->flags &= ~TSO_INTERRUPTIBLE;
2318 tso->why_blocked = BlockedOnCCall_NoUnblockExc;
2321 // Hand back capability
2322 task->suspended_tso = tso;
2324 ACQUIRE_LOCK(&cap->lock);
2326 suspendTask(cap,task);
2327 cap->in_haskell = rtsFalse;
2328 releaseCapability_(cap);
2330 RELEASE_LOCK(&cap->lock);
2332 #if defined(THREADED_RTS)
2333 /* Preparing to leave the RTS, so ensure there's a native thread/task
2334 waiting to take over.
2336 debugTrace(DEBUG_sched, "thread %lu: leaving RTS", (unsigned long)tso->id);
2339 errno = saved_errno;
2341 SetLastError(saved_winerror);
2347 resumeThread (void *task_)
2354 StgWord32 saved_winerror;
2357 saved_errno = errno;
2359 saved_winerror = GetLastError();
2363 // Wait for permission to re-enter the RTS with the result.
2364 waitForReturnCapability(&cap,task);
2365 // we might be on a different capability now... but if so, our
2366 // entry on the suspended_ccalling_tasks list will also have been
2369 // Remove the thread from the suspended list
2370 recoverSuspendedTask(cap,task);
2372 tso = task->suspended_tso;
2373 task->suspended_tso = NULL;
2374 tso->link = END_TSO_QUEUE;
2375 debugTrace(DEBUG_sched, "thread %lu: re-entering RTS", (unsigned long)tso->id);
2377 if (tso->why_blocked == BlockedOnCCall) {
2378 awakenBlockedExceptionQueue(cap,tso);
2379 tso->flags &= ~(TSO_BLOCKEX | TSO_INTERRUPTIBLE);
2382 /* Reset blocking status */
2383 tso->why_blocked = NotBlocked;
2385 cap->r.rCurrentTSO = tso;
2386 cap->in_haskell = rtsTrue;
2387 errno = saved_errno;
2389 SetLastError(saved_winerror);
2392 /* We might have GC'd, mark the TSO dirty again */
2395 IF_DEBUG(sanity, checkTSO(tso));
2400 /* ---------------------------------------------------------------------------
2403 * scheduleThread puts a thread on the end of the runnable queue.
2404 * This will usually be done immediately after a thread is created.
2405 * The caller of scheduleThread must create the thread using e.g.
2406 * createThread and push an appropriate closure
2407 * on this thread's stack before the scheduler is invoked.
2408 * ------------------------------------------------------------------------ */
2411 scheduleThread(Capability *cap, StgTSO *tso)
2413 // The thread goes at the *end* of the run-queue, to avoid possible
2414 // starvation of any threads already on the queue.
2415 appendToRunQueue(cap,tso);
2419 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
2421 #if defined(THREADED_RTS)
2422 tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
2423 // move this thread from now on.
2424 cpu %= RtsFlags.ParFlags.nNodes;
2425 if (cpu == cap->no) {
2426 appendToRunQueue(cap,tso);
2428 migrateThreadToCapability_lock(&capabilities[cpu],tso);
2431 appendToRunQueue(cap,tso);
2436 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
2440 // We already created/initialised the Task
2441 task = cap->running_task;
2443 // This TSO is now a bound thread; make the Task and TSO
2444 // point to each other.
2450 task->stat = NoStatus;
2452 appendToRunQueue(cap,tso);
2454 debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)tso->id);
2457 /* GranSim specific init */
2458 CurrentTSO = m->tso; // the TSO to run
2459 procStatus[MainProc] = Busy; // status of main PE
2460 CurrentProc = MainProc; // PE to run it on
2463 cap = schedule(cap,task);
2465 ASSERT(task->stat != NoStatus);
2466 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
2468 debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)task->tso->id);
2472 /* ----------------------------------------------------------------------------
2474 * ------------------------------------------------------------------------- */
2476 #if defined(THREADED_RTS)
2478 workerStart(Task *task)
2482 // See startWorkerTask().
2483 ACQUIRE_LOCK(&task->lock);
2485 RELEASE_LOCK(&task->lock);
2487 // set the thread-local pointer to the Task:
2490 // schedule() runs without a lock.
2491 cap = schedule(cap,task);
2493 // On exit from schedule(), we have a Capability.
2494 releaseCapability(cap);
2495 workerTaskStop(task);
2499 /* ---------------------------------------------------------------------------
2502 * Initialise the scheduler. This resets all the queues - if the
2503 * queues contained any threads, they'll be garbage collected at the
2506 * ------------------------------------------------------------------------ */
2513 for (i=0; i<=MAX_PROC; i++) {
2514 run_queue_hds[i] = END_TSO_QUEUE;
2515 run_queue_tls[i] = END_TSO_QUEUE;
2516 blocked_queue_hds[i] = END_TSO_QUEUE;
2517 blocked_queue_tls[i] = END_TSO_QUEUE;
2518 ccalling_threadss[i] = END_TSO_QUEUE;
2519 blackhole_queue[i] = END_TSO_QUEUE;
2520 sleeping_queue = END_TSO_QUEUE;
2522 #elif !defined(THREADED_RTS)
2523 blocked_queue_hd = END_TSO_QUEUE;
2524 blocked_queue_tl = END_TSO_QUEUE;
2525 sleeping_queue = END_TSO_QUEUE;
2528 blackhole_queue = END_TSO_QUEUE;
2529 all_threads = END_TSO_QUEUE;
2532 sched_state = SCHED_RUNNING;
2534 #if defined(THREADED_RTS)
2535 /* Initialise the mutex and condition variables used by
2537 initMutex(&sched_mutex);
2540 ACQUIRE_LOCK(&sched_mutex);
2542 /* A capability holds the state a native thread needs in
2543 * order to execute STG code. At least one capability is
2544 * floating around (only THREADED_RTS builds have more than one).
2550 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
2554 #if defined(THREADED_RTS)
2556 * Eagerly start one worker to run each Capability, except for
2557 * Capability 0. The idea is that we're probably going to start a
2558 * bound thread on Capability 0 pretty soon, so we don't want a
2559 * worker task hogging it.
2564 for (i = 1; i < n_capabilities; i++) {
2565 cap = &capabilities[i];
2566 ACQUIRE_LOCK(&cap->lock);
2567 startWorkerTask(cap, workerStart);
2568 RELEASE_LOCK(&cap->lock);
2573 trace(TRACE_sched, "start: %d capabilities", n_capabilities);
2575 RELEASE_LOCK(&sched_mutex);
2579 exitScheduler( void )
2583 #if defined(THREADED_RTS)
2584 ACQUIRE_LOCK(&sched_mutex);
2585 task = newBoundTask();
2586 RELEASE_LOCK(&sched_mutex);
2589 // If we haven't killed all the threads yet, do it now.
2590 if (sched_state < SCHED_SHUTTING_DOWN) {
2591 sched_state = SCHED_INTERRUPTING;
2592 scheduleDoGC(NULL,task,rtsFalse);
2594 sched_state = SCHED_SHUTTING_DOWN;
2596 #if defined(THREADED_RTS)
2600 for (i = 0; i < n_capabilities; i++) {
2601 shutdownCapability(&capabilities[i], task);
2603 boundTaskExiting(task);
2607 freeCapability(&MainCapability);
2612 freeScheduler( void )
2615 if (n_capabilities != 1) {
2616 stgFree(capabilities);
2618 #if defined(THREADED_RTS)
2619 closeMutex(&sched_mutex);
2623 /* ---------------------------------------------------------------------------
2624 Where are the roots that we know about?
2626 - all the threads on the runnable queue
2627 - all the threads on the blocked queue
2628 - all the threads on the sleeping queue
2629 - all the thread currently executing a _ccall_GC
2630 - all the "main threads"
2632 ------------------------------------------------------------------------ */
2634 /* This has to be protected either by the scheduler monitor, or by the
2635 garbage collection monitor (probably the latter).
2640 GetRoots( evac_fn evac )
2647 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2648 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2649 evac((StgClosure **)&run_queue_hds[i]);
2650 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2651 evac((StgClosure **)&run_queue_tls[i]);
2653 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2654 evac((StgClosure **)&blocked_queue_hds[i]);
2655 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2656 evac((StgClosure **)&blocked_queue_tls[i]);
2657 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2658 evac((StgClosure **)&ccalling_threads[i]);
2665 for (i = 0; i < n_capabilities; i++) {
2666 cap = &capabilities[i];
2667 evac((StgClosure **)(void *)&cap->run_queue_hd);
2668 evac((StgClosure **)(void *)&cap->run_queue_tl);
2669 #if defined(THREADED_RTS)
2670 evac((StgClosure **)(void *)&cap->wakeup_queue_hd);
2671 evac((StgClosure **)(void *)&cap->wakeup_queue_tl);
2673 for (task = cap->suspended_ccalling_tasks; task != NULL;
2675 debugTrace(DEBUG_sched,
2676 "evac'ing suspended TSO %lu", (unsigned long)task->suspended_tso->id);
2677 evac((StgClosure **)(void *)&task->suspended_tso);
2683 #if !defined(THREADED_RTS)
2684 evac((StgClosure **)(void *)&blocked_queue_hd);
2685 evac((StgClosure **)(void *)&blocked_queue_tl);
2686 evac((StgClosure **)(void *)&sleeping_queue);
2690 // evac((StgClosure **)&blackhole_queue);
2692 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL) || defined(GRAN)
2693 markSparkQueue(evac);
2696 #if defined(RTS_USER_SIGNALS)
2697 // mark the signal handlers (signals should be already blocked)
2698 if (RtsFlags.MiscFlags.install_signal_handlers) {
2699 markSignalHandlers(evac);
2704 /* -----------------------------------------------------------------------------
2707 This is the interface to the garbage collector from Haskell land.
2708 We provide this so that external C code can allocate and garbage
2709 collect when called from Haskell via _ccall_GC.
2710 -------------------------------------------------------------------------- */
2713 performGC_(rtsBool force_major)
2716 // We must grab a new Task here, because the existing Task may be
2717 // associated with a particular Capability, and chained onto the
2718 // suspended_ccalling_tasks queue.
2719 ACQUIRE_LOCK(&sched_mutex);
2720 task = newBoundTask();
2721 RELEASE_LOCK(&sched_mutex);
2722 scheduleDoGC(NULL,task,force_major);
2723 boundTaskExiting(task);
2729 performGC_(rtsFalse);
2733 performMajorGC(void)
2735 performGC_(rtsTrue);
2738 /* -----------------------------------------------------------------------------
2741 If the thread has reached its maximum stack size, then raise the
2742 StackOverflow exception in the offending thread. Otherwise
2743 relocate the TSO into a larger chunk of memory and adjust its stack
2745 -------------------------------------------------------------------------- */
2748 threadStackOverflow(Capability *cap, StgTSO *tso)
2750 nat new_stack_size, stack_words;
2755 IF_DEBUG(sanity,checkTSO(tso));
2757 // don't allow throwTo() to modify the blocked_exceptions queue
2758 // while we are moving the TSO:
2759 lockClosure((StgClosure *)tso);
2761 if (tso->stack_size >= tso->max_stack_size && !(tso->flags & TSO_BLOCKEX)) {
2762 // NB. never raise a StackOverflow exception if the thread is
2763 // inside Control.Exceptino.block. It is impractical to protect
2764 // against stack overflow exceptions, since virtually anything
2765 // can raise one (even 'catch'), so this is the only sensible
2766 // thing to do here. See bug #767.
2768 debugTrace(DEBUG_gc,
2769 "threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)",
2770 (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
2772 /* If we're debugging, just print out the top of the stack */
2773 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2776 // Send this thread the StackOverflow exception
2778 throwToSingleThreaded(cap, tso, (StgClosure *)stackOverflow_closure);
2782 /* Try to double the current stack size. If that takes us over the
2783 * maximum stack size for this thread, then use the maximum instead.
2784 * Finally round up so the TSO ends up as a whole number of blocks.
2786 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2787 new_tso_size = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2788 TSO_STRUCT_SIZE)/sizeof(W_);
2789 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2790 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2792 debugTrace(DEBUG_sched,
2793 "increasing stack size from %ld words to %d.",
2794 (long)tso->stack_size, new_stack_size);
2796 dest = (StgTSO *)allocate(new_tso_size);
2797 TICK_ALLOC_TSO(new_stack_size,0);
2799 /* copy the TSO block and the old stack into the new area */
2800 memcpy(dest,tso,TSO_STRUCT_SIZE);
2801 stack_words = tso->stack + tso->stack_size - tso->sp;
2802 new_sp = (P_)dest + new_tso_size - stack_words;
2803 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2805 /* relocate the stack pointers... */
2807 dest->stack_size = new_stack_size;
2809 /* Mark the old TSO as relocated. We have to check for relocated
2810 * TSOs in the garbage collector and any primops that deal with TSOs.
2812 * It's important to set the sp value to just beyond the end
2813 * of the stack, so we don't attempt to scavenge any part of the
2816 tso->what_next = ThreadRelocated;
2818 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2819 tso->why_blocked = NotBlocked;
2821 IF_PAR_DEBUG(verbose,
2822 debugBelch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld\n",
2823 tso->id, tso, tso->stack_size);
2824 /* If we're debugging, just print out the top of the stack */
2825 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2831 IF_DEBUG(sanity,checkTSO(dest));
2833 IF_DEBUG(scheduler,printTSO(dest));
2839 /* ---------------------------------------------------------------------------
2841 - usually called inside a signal handler so it mustn't do anything fancy.
2842 ------------------------------------------------------------------------ */
2845 interruptStgRts(void)
2847 sched_state = SCHED_INTERRUPTING;
2852 /* -----------------------------------------------------------------------------
2855 This function causes at least one OS thread to wake up and run the
2856 scheduler loop. It is invoked when the RTS might be deadlocked, or
2857 an external event has arrived that may need servicing (eg. a
2858 keyboard interrupt).
2860 In the single-threaded RTS we don't do anything here; we only have
2861 one thread anyway, and the event that caused us to want to wake up
2862 will have interrupted any blocking system call in progress anyway.
2863 -------------------------------------------------------------------------- */
2868 #if defined(THREADED_RTS)
2869 // This forces the IO Manager thread to wakeup, which will
2870 // in turn ensure that some OS thread wakes up and runs the
2871 // scheduler loop, which will cause a GC and deadlock check.
2876 /* -----------------------------------------------------------------------------
2879 * Check the blackhole_queue for threads that can be woken up. We do
2880 * this periodically: before every GC, and whenever the run queue is
2883 * An elegant solution might be to just wake up all the blocked
2884 * threads with awakenBlockedQueue occasionally: they'll go back to
2885 * sleep again if the object is still a BLACKHOLE. Unfortunately this
2886 * doesn't give us a way to tell whether we've actually managed to
2887 * wake up any threads, so we would be busy-waiting.
2889 * -------------------------------------------------------------------------- */
2892 checkBlackHoles (Capability *cap)
2895 rtsBool any_woke_up = rtsFalse;
2898 // blackhole_queue is global:
2899 ASSERT_LOCK_HELD(&sched_mutex);
2901 debugTrace(DEBUG_sched, "checking threads blocked on black holes");
2903 // ASSUMES: sched_mutex
2904 prev = &blackhole_queue;
2905 t = blackhole_queue;
2906 while (t != END_TSO_QUEUE) {
2907 ASSERT(t->why_blocked == BlockedOnBlackHole);
2908 type = get_itbl(t->block_info.closure)->type;
2909 if (type != BLACKHOLE && type != CAF_BLACKHOLE) {
2910 IF_DEBUG(sanity,checkTSO(t));
2911 t = unblockOne(cap, t);
2912 // urk, the threads migrate to the current capability
2913 // here, but we'd like to keep them on the original one.
2915 any_woke_up = rtsTrue;
2925 /* -----------------------------------------------------------------------------
2928 This is used for interruption (^C) and forking, and corresponds to
2929 raising an exception but without letting the thread catch the
2931 -------------------------------------------------------------------------- */
2934 deleteThread (Capability *cap, StgTSO *tso)
2936 // NOTE: must only be called on a TSO that we have exclusive
2937 // access to, because we will call throwToSingleThreaded() below.
2938 // The TSO must be on the run queue of the Capability we own, or
2939 // we must own all Capabilities.
2941 if (tso->why_blocked != BlockedOnCCall &&
2942 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
2943 throwToSingleThreaded(cap,tso,NULL);
2947 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2949 deleteThread_(Capability *cap, StgTSO *tso)
2950 { // for forkProcess only:
2951 // like deleteThread(), but we delete threads in foreign calls, too.
2953 if (tso->why_blocked == BlockedOnCCall ||
2954 tso->why_blocked == BlockedOnCCall_NoUnblockExc) {
2955 unblockOne(cap,tso);
2956 tso->what_next = ThreadKilled;
2958 deleteThread(cap,tso);
2963 /* -----------------------------------------------------------------------------
2964 raiseExceptionHelper
2966 This function is called by the raise# primitve, just so that we can
2967 move some of the tricky bits of raising an exception from C-- into
2968 C. Who knows, it might be a useful re-useable thing here too.
2969 -------------------------------------------------------------------------- */
2972 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2974 Capability *cap = regTableToCapability(reg);
2975 StgThunk *raise_closure = NULL;
2977 StgRetInfoTable *info;
2979 // This closure represents the expression 'raise# E' where E
2980 // is the exception raise. It is used to overwrite all the
2981 // thunks which are currently under evaluataion.
2984 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2985 // LDV profiling: stg_raise_info has THUNK as its closure
2986 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2987 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
2988 // 1 does not cause any problem unless profiling is performed.
2989 // However, when LDV profiling goes on, we need to linearly scan
2990 // small object pool, where raise_closure is stored, so we should
2991 // use MIN_UPD_SIZE.
2993 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2994 // sizeofW(StgClosure)+1);
2998 // Walk up the stack, looking for the catch frame. On the way,
2999 // we update any closures pointed to from update frames with the
3000 // raise closure that we just built.
3004 info = get_ret_itbl((StgClosure *)p);
3005 next = p + stack_frame_sizeW((StgClosure *)p);
3006 switch (info->i.type) {
3009 // Only create raise_closure if we need to.
3010 if (raise_closure == NULL) {
3012 (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
3013 SET_HDR(raise_closure, &stg_raise_info, CCCS);
3014 raise_closure->payload[0] = exception;
3016 UPD_IND(((StgUpdateFrame *)p)->updatee,(StgClosure *)raise_closure);
3020 case ATOMICALLY_FRAME:
3021 debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
3023 return ATOMICALLY_FRAME;
3029 case CATCH_STM_FRAME:
3030 debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
3032 return CATCH_STM_FRAME;
3038 case CATCH_RETRY_FRAME:
3047 /* -----------------------------------------------------------------------------
3048 findRetryFrameHelper
3050 This function is called by the retry# primitive. It traverses the stack
3051 leaving tso->sp referring to the frame which should handle the retry.
3053 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
3054 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
3056 We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
3057 create) because retries are not considered to be exceptions, despite the
3058 similar implementation.
3060 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
3061 not be created within memory transactions.
3062 -------------------------------------------------------------------------- */
3065 findRetryFrameHelper (StgTSO *tso)
3068 StgRetInfoTable *info;
3072 info = get_ret_itbl((StgClosure *)p);
3073 next = p + stack_frame_sizeW((StgClosure *)p);
3074 switch (info->i.type) {
3076 case ATOMICALLY_FRAME:
3077 debugTrace(DEBUG_stm,
3078 "found ATOMICALLY_FRAME at %p during retry", p);
3080 return ATOMICALLY_FRAME;
3082 case CATCH_RETRY_FRAME:
3083 debugTrace(DEBUG_stm,
3084 "found CATCH_RETRY_FRAME at %p during retrry", p);
3086 return CATCH_RETRY_FRAME;
3088 case CATCH_STM_FRAME: {
3089 debugTrace(DEBUG_stm,
3090 "found CATCH_STM_FRAME at %p during retry", p);
3091 StgTRecHeader *trec = tso -> trec;
3092 StgTRecHeader *outer = stmGetEnclosingTRec(trec);
3093 debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
3094 stmAbortTransaction(tso -> cap, trec);
3095 stmFreeAbortedTRec(tso -> cap, trec);
3096 tso -> trec = outer;
3103 ASSERT(info->i.type != CATCH_FRAME);
3104 ASSERT(info->i.type != STOP_FRAME);
3111 /* -----------------------------------------------------------------------------
3112 resurrectThreads is called after garbage collection on the list of
3113 threads found to be garbage. Each of these threads will be woken
3114 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
3115 on an MVar, or NonTermination if the thread was blocked on a Black
3118 Locks: assumes we hold *all* the capabilities.
3119 -------------------------------------------------------------------------- */
3122 resurrectThreads (StgTSO *threads)
3127 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
3128 next = tso->global_link;
3129 tso->global_link = all_threads;
3131 debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
3133 // Wake up the thread on the Capability it was last on
3136 switch (tso->why_blocked) {
3138 case BlockedOnException:
3139 /* Called by GC - sched_mutex lock is currently held. */
3140 throwToSingleThreaded(cap, tso,
3141 (StgClosure *)BlockedOnDeadMVar_closure);
3143 case BlockedOnBlackHole:
3144 throwToSingleThreaded(cap, tso,
3145 (StgClosure *)NonTermination_closure);
3148 throwToSingleThreaded(cap, tso,
3149 (StgClosure *)BlockedIndefinitely_closure);
3152 /* This might happen if the thread was blocked on a black hole
3153 * belonging to a thread that we've just woken up (raiseAsync
3154 * can wake up threads, remember...).
3158 barf("resurrectThreads: thread blocked in a strange way");