1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2005
5 * The scheduler and thread-related functionality
7 * --------------------------------------------------------------------------*/
9 #include "PosixSource.h"
14 #include "BlockAlloc.h"
15 #include "OSThreads.h"
20 #include "StgMiscClosures.h"
21 #include "Interpreter.h"
22 #include "Exception.h"
24 #include "RtsSignals.h"
30 #include "ThreadLabels.h"
31 #include "LdvProfile.h"
34 #include "Proftimer.h"
37 #if defined(GRAN) || defined(PARALLEL_HASKELL)
38 # include "GranSimRts.h"
40 # include "ParallelRts.h"
41 # include "Parallel.h"
42 # include "ParallelDebug.h"
47 #include "Capability.h"
49 #include "AwaitEvent.h"
50 #if defined(mingw32_HOST_OS)
51 #include "win32/IOManager.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 interrupted = rtsFalse;
142 /* Next thread ID to allocate.
145 static StgThreadID next_thread_id = 1;
147 /* The smallest stack size that makes any sense is:
148 * RESERVED_STACK_WORDS (so we can get back from the stack overflow)
149 * + sizeofW(StgStopFrame) (the stg_stop_thread_info frame)
150 * + 1 (the closure to enter)
152 * + 1 (spare slot req'd by stg_ap_v_ret)
154 * A thread with this stack will bomb immediately with a stack
155 * overflow, which will increase its stack size.
157 #define MIN_STACK_WORDS (RESERVED_STACK_WORDS + sizeofW(StgStopFrame) + 3)
163 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
164 * exists - earlier gccs apparently didn't.
170 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
171 * in an MT setting, needed to signal that a worker thread shouldn't hang around
172 * in the scheduler when it is out of work.
174 rtsBool shutting_down_scheduler = rtsFalse;
177 * This mutex protects most of the global scheduler data in
178 * the THREADED_RTS runtime.
180 #if defined(THREADED_RTS)
184 #if defined(PARALLEL_HASKELL)
186 rtsTime TimeOfLastYield;
187 rtsBool emitSchedule = rtsTrue;
190 /* -----------------------------------------------------------------------------
191 * static function prototypes
192 * -------------------------------------------------------------------------- */
194 static Capability *schedule (Capability *initialCapability, Task *task);
197 // These function all encapsulate parts of the scheduler loop, and are
198 // abstracted only to make the structure and control flow of the
199 // scheduler clearer.
201 static void schedulePreLoop (void);
202 #if defined(THREADED_RTS)
203 static void schedulePushWork(Capability *cap, Task *task);
205 static void scheduleStartSignalHandlers (Capability *cap);
206 static void scheduleCheckBlockedThreads (Capability *cap);
207 static void scheduleCheckBlackHoles (Capability *cap);
208 static void scheduleDetectDeadlock (Capability *cap, Task *task);
210 static StgTSO *scheduleProcessEvent(rtsEvent *event);
212 #if defined(PARALLEL_HASKELL)
213 static StgTSO *scheduleSendPendingMessages(void);
214 static void scheduleActivateSpark(void);
215 static rtsBool scheduleGetRemoteWork(rtsBool *receivedFinish);
217 #if defined(PAR) || defined(GRAN)
218 static void scheduleGranParReport(void);
220 static void schedulePostRunThread(void);
221 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
222 static void scheduleHandleStackOverflow( Capability *cap, Task *task,
224 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
225 nat prev_what_next );
226 static void scheduleHandleThreadBlocked( StgTSO *t );
227 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
229 static rtsBool scheduleDoHeapProfile(rtsBool ready_to_gc);
230 static void scheduleDoGC(Capability *cap, Task *task, rtsBool force_major,
231 void (*get_roots)(evac_fn));
233 static void unblockThread(Capability *cap, StgTSO *tso);
234 static rtsBool checkBlackHoles(Capability *cap);
235 static void AllRoots(evac_fn evac);
237 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
239 static void raiseAsync_(Capability *cap, StgTSO *tso, StgClosure *exception,
240 rtsBool stop_at_atomically, StgPtr stop_here);
242 static void deleteThread (Capability *cap, StgTSO *tso);
243 static void deleteRunQueue (Capability *cap);
246 static void printThreadBlockage(StgTSO *tso);
247 static void printThreadStatus(StgTSO *tso);
248 void printThreadQueue(StgTSO *tso);
251 #if defined(PARALLEL_HASKELL)
252 StgTSO * createSparkThread(rtsSpark spark);
253 StgTSO * activateSpark (rtsSpark spark);
257 static char *whatNext_strs[] = {
267 /* -----------------------------------------------------------------------------
268 * Putting a thread on the run queue: different scheduling policies
269 * -------------------------------------------------------------------------- */
272 addToRunQueue( Capability *cap, StgTSO *t )
274 #if defined(PARALLEL_HASKELL)
275 if (RtsFlags.ParFlags.doFairScheduling) {
276 // this does round-robin scheduling; good for concurrency
277 appendToRunQueue(cap,t);
279 // this does unfair scheduling; good for parallelism
280 pushOnRunQueue(cap,t);
283 // this does round-robin scheduling; good for concurrency
284 appendToRunQueue(cap,t);
288 /* ---------------------------------------------------------------------------
289 Main scheduling loop.
291 We use round-robin scheduling, each thread returning to the
292 scheduler loop when one of these conditions is detected:
295 * timer expires (thread yields)
301 In a GranSim setup this loop iterates over the global event queue.
302 This revolves around the global event queue, which determines what
303 to do next. Therefore, it's more complicated than either the
304 concurrent or the parallel (GUM) setup.
307 GUM iterates over incoming messages.
308 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
309 and sends out a fish whenever it has nothing to do; in-between
310 doing the actual reductions (shared code below) it processes the
311 incoming messages and deals with delayed operations
312 (see PendingFetches).
313 This is not the ugliest code you could imagine, but it's bloody close.
315 ------------------------------------------------------------------------ */
318 schedule (Capability *initialCapability, Task *task)
322 StgThreadReturnCode ret;
325 #elif defined(PARALLEL_HASKELL)
328 rtsBool receivedFinish = rtsFalse;
330 nat tp_size, sp_size; // stats only
335 #if defined(THREADED_RTS)
336 rtsBool first = rtsTrue;
339 cap = initialCapability;
341 // Pre-condition: this task owns initialCapability.
342 // The sched_mutex is *NOT* held
343 // NB. on return, we still hold a capability.
346 sched_belch("### NEW SCHEDULER LOOP (task: %p, cap: %p)",
347 task, initialCapability);
352 // -----------------------------------------------------------
353 // Scheduler loop starts here:
355 #if defined(PARALLEL_HASKELL)
356 #define TERMINATION_CONDITION (!receivedFinish)
358 #define TERMINATION_CONDITION ((event = get_next_event()) != (rtsEvent*)NULL)
360 #define TERMINATION_CONDITION rtsTrue
363 while (TERMINATION_CONDITION) {
366 /* Choose the processor with the next event */
367 CurrentProc = event->proc;
368 CurrentTSO = event->tso;
371 #if defined(THREADED_RTS)
373 // don't yield the first time, we want a chance to run this
374 // thread for a bit, even if there are others banging at the
377 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
379 // Yield the capability to higher-priority tasks if necessary.
380 yieldCapability(&cap, task);
384 #if defined(THREADED_RTS)
385 schedulePushWork(cap,task);
388 // Check whether we have re-entered the RTS from Haskell without
389 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
391 if (cap->in_haskell) {
392 errorBelch("schedule: re-entered unsafely.\n"
393 " Perhaps a 'foreign import unsafe' should be 'safe'?");
394 stg_exit(EXIT_FAILURE);
398 // Test for interruption. If interrupted==rtsTrue, then either
399 // we received a keyboard interrupt (^C), or the scheduler is
400 // trying to shut down all the tasks (shutting_down_scheduler) in
405 #if defined(THREADED_RTS)
406 discardSparksCap(cap);
408 if (shutting_down_scheduler) {
409 IF_DEBUG(scheduler, sched_belch("shutting down"));
410 // If we are a worker, just exit. If we're a bound thread
411 // then we will exit below when we've removed our TSO from
413 if (task->tso == NULL && emptyRunQueue(cap)) {
417 IF_DEBUG(scheduler, sched_belch("interrupted"));
421 #if defined(THREADED_RTS)
422 // If the run queue is empty, take a spark and turn it into a thread.
424 if (emptyRunQueue(cap)) {
426 spark = findSpark(cap);
429 sched_belch("turning spark of closure %p into a thread",
430 (StgClosure *)spark));
431 createSparkThread(cap,spark);
435 #endif // THREADED_RTS
437 scheduleStartSignalHandlers(cap);
439 // Only check the black holes here if we've nothing else to do.
440 // During normal execution, the black hole list only gets checked
441 // at GC time, to avoid repeatedly traversing this possibly long
442 // list each time around the scheduler.
443 if (emptyRunQueue(cap)) { scheduleCheckBlackHoles(cap); }
445 scheduleCheckBlockedThreads(cap);
447 scheduleDetectDeadlock(cap,task);
448 #if defined(THREADED_RTS)
449 cap = task->cap; // reload cap, it might have changed
452 // Normally, the only way we can get here with no threads to
453 // run is if a keyboard interrupt received during
454 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
455 // Additionally, it is not fatal for the
456 // threaded RTS to reach here with no threads to run.
458 // win32: might be here due to awaitEvent() being abandoned
459 // as a result of a console event having been delivered.
460 if ( emptyRunQueue(cap) ) {
461 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
464 continue; // nothing to do
467 #if defined(PARALLEL_HASKELL)
468 scheduleSendPendingMessages();
469 if (emptyRunQueue(cap) && scheduleActivateSpark())
473 ASSERT(next_fish_to_send_at==0); // i.e. no delayed fishes left!
476 /* If we still have no work we need to send a FISH to get a spark
478 if (emptyRunQueue(cap)) {
479 if (!scheduleGetRemoteWork(&receivedFinish)) continue;
480 ASSERT(rtsFalse); // should not happen at the moment
482 // from here: non-empty run queue.
483 // TODO: merge above case with this, only one call processMessages() !
484 if (PacketsWaiting()) { /* process incoming messages, if
485 any pending... only in else
486 because getRemoteWork waits for
488 receivedFinish = processMessages();
493 scheduleProcessEvent(event);
497 // Get a thread to run
499 t = popRunQueue(cap);
501 #if defined(GRAN) || defined(PAR)
502 scheduleGranParReport(); // some kind of debuging output
504 // Sanity check the thread we're about to run. This can be
505 // expensive if there is lots of thread switching going on...
506 IF_DEBUG(sanity,checkTSO(t));
509 #if defined(THREADED_RTS)
510 // Check whether we can run this thread in the current task.
511 // If not, we have to pass our capability to the right task.
513 Task *bound = t->bound;
518 sched_belch("### Running thread %d in bound thread",
520 // yes, the Haskell thread is bound to the current native thread
523 sched_belch("### thread %d bound to another OS thread",
525 // no, bound to a different Haskell thread: pass to that thread
526 pushOnRunQueue(cap,t);
530 // The thread we want to run is unbound.
533 sched_belch("### this OS thread cannot run thread %d", t->id));
534 // no, the current native thread is bound to a different
535 // Haskell thread, so pass it to any worker thread
536 pushOnRunQueue(cap,t);
543 cap->r.rCurrentTSO = t;
545 /* context switches are initiated by the timer signal, unless
546 * the user specified "context switch as often as possible", with
549 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
550 && !emptyThreadQueues(cap)) {
556 IF_DEBUG(scheduler, sched_belch("-->> running thread %ld %s ...",
557 (long)t->id, whatNext_strs[t->what_next]));
559 #if defined(PROFILING)
560 startHeapProfTimer();
563 // ----------------------------------------------------------------------
564 // Run the current thread
566 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
568 prev_what_next = t->what_next;
570 errno = t->saved_errno;
571 cap->in_haskell = rtsTrue;
575 recent_activity = ACTIVITY_YES;
577 switch (prev_what_next) {
581 /* Thread already finished, return to scheduler. */
582 ret = ThreadFinished;
588 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
589 cap = regTableToCapability(r);
594 case ThreadInterpret:
595 cap = interpretBCO(cap);
600 barf("schedule: invalid what_next field");
603 cap->in_haskell = rtsFalse;
605 // The TSO might have moved, eg. if it re-entered the RTS and a GC
606 // happened. So find the new location:
607 t = cap->r.rCurrentTSO;
609 // We have run some Haskell code: there might be blackhole-blocked
610 // threads to wake up now.
611 // Lock-free test here should be ok, we're just setting a flag.
612 if ( blackhole_queue != END_TSO_QUEUE ) {
613 blackholes_need_checking = rtsTrue;
616 // And save the current errno in this thread.
617 // XXX: possibly bogus for SMP because this thread might already
618 // be running again, see code below.
619 t->saved_errno = errno;
621 #if defined(THREADED_RTS)
622 // If ret is ThreadBlocked, and this Task is bound to the TSO that
623 // blocked, we are in limbo - the TSO is now owned by whatever it
624 // is blocked on, and may in fact already have been woken up,
625 // perhaps even on a different Capability. It may be the case
626 // that task->cap != cap. We better yield this Capability
627 // immediately and return to normaility.
628 if (ret == ThreadBlocked) {
630 sched_belch("--<< thread %d (%s) stopped: blocked\n",
631 t->id, whatNext_strs[t->what_next]));
636 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
638 // ----------------------------------------------------------------------
640 // Costs for the scheduler are assigned to CCS_SYSTEM
641 #if defined(PROFILING)
646 #if defined(THREADED_RTS)
647 IF_DEBUG(scheduler,debugBelch("sched (task %p): ", (void *)(unsigned long)(unsigned int)osThreadId()););
648 #elif !defined(GRAN) && !defined(PARALLEL_HASKELL)
649 IF_DEBUG(scheduler,debugBelch("sched: "););
652 schedulePostRunThread();
654 ready_to_gc = rtsFalse;
658 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
662 scheduleHandleStackOverflow(cap,task,t);
666 if (scheduleHandleYield(cap, t, prev_what_next)) {
667 // shortcut for switching between compiler/interpreter:
673 scheduleHandleThreadBlocked(t);
677 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
678 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
682 barf("schedule: invalid thread return code %d", (int)ret);
685 if (scheduleDoHeapProfile(ready_to_gc)) { ready_to_gc = rtsFalse; }
687 scheduleDoGC(cap,task,rtsFalse,GetRoots);
688 #if defined(THREADED_RTS)
689 cap = task->cap; // reload cap, it might have changed
692 } /* end of while() */
694 IF_PAR_DEBUG(verbose,
695 debugBelch("== Leaving schedule() after having received Finish\n"));
698 /* ----------------------------------------------------------------------------
699 * Setting up the scheduler loop
700 * ------------------------------------------------------------------------- */
703 schedulePreLoop(void)
706 /* set up first event to get things going */
707 /* ToDo: assign costs for system setup and init MainTSO ! */
708 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
710 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
713 debugBelch("GRAN: Init CurrentTSO (in schedule) = %p\n",
715 G_TSO(CurrentTSO, 5));
717 if (RtsFlags.GranFlags.Light) {
718 /* Save current time; GranSim Light only */
719 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
724 /* -----------------------------------------------------------------------------
727 * Push work to other Capabilities if we have some.
728 * -------------------------------------------------------------------------- */
730 #if defined(THREADED_RTS)
732 schedulePushWork(Capability *cap USED_IF_THREADS,
733 Task *task USED_IF_THREADS)
735 Capability *free_caps[n_capabilities], *cap0;
738 // Check whether we have more threads on our run queue, or sparks
739 // in our pool, that we could hand to another Capability.
740 if ((emptyRunQueue(cap) || cap->run_queue_hd->link == END_TSO_QUEUE)
741 && sparkPoolSizeCap(cap) < 2) {
745 // First grab as many free Capabilities as we can.
746 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
747 cap0 = &capabilities[i];
748 if (cap != cap0 && tryGrabCapability(cap0,task)) {
749 if (!emptyRunQueue(cap0) || cap->returning_tasks_hd != NULL) {
750 // it already has some work, we just grabbed it at
751 // the wrong moment. Or maybe it's deadlocked!
752 releaseCapability(cap0);
754 free_caps[n_free_caps++] = cap0;
759 // we now have n_free_caps free capabilities stashed in
760 // free_caps[]. Share our run queue equally with them. This is
761 // probably the simplest thing we could do; improvements we might
762 // want to do include:
764 // - giving high priority to moving relatively new threads, on
765 // the gournds that they haven't had time to build up a
766 // working set in the cache on this CPU/Capability.
768 // - giving low priority to moving long-lived threads
770 if (n_free_caps > 0) {
771 StgTSO *prev, *t, *next;
772 rtsBool pushed_to_all;
774 IF_DEBUG(scheduler, sched_belch("excess threads on run queue and %d free capabilities, sharing...", n_free_caps));
777 pushed_to_all = rtsFalse;
779 if (cap->run_queue_hd != END_TSO_QUEUE) {
780 prev = cap->run_queue_hd;
782 prev->link = END_TSO_QUEUE;
783 for (; t != END_TSO_QUEUE; t = next) {
785 t->link = END_TSO_QUEUE;
786 if (t->what_next == ThreadRelocated
787 || t->bound == task) { // don't move my bound thread
790 } else if (i == n_free_caps) {
791 pushed_to_all = rtsTrue;
797 IF_DEBUG(scheduler, sched_belch("pushing thread %d to capability %d", t->id, free_caps[i]->no));
798 appendToRunQueue(free_caps[i],t);
799 if (t->bound) { t->bound->cap = free_caps[i]; }
803 cap->run_queue_tl = prev;
806 // If there are some free capabilities that we didn't push any
807 // threads to, then try to push a spark to each one.
808 if (!pushed_to_all) {
810 // i is the next free capability to push to
811 for (; i < n_free_caps; i++) {
812 if (emptySparkPoolCap(free_caps[i])) {
813 spark = findSpark(cap);
815 IF_DEBUG(scheduler, sched_belch("pushing spark %p to capability %d", spark, free_caps[i]->no));
816 newSpark(&(free_caps[i]->r), spark);
822 // release the capabilities
823 for (i = 0; i < n_free_caps; i++) {
824 task->cap = free_caps[i];
825 releaseCapability(free_caps[i]);
828 task->cap = cap; // reset to point to our Capability.
832 /* ----------------------------------------------------------------------------
833 * Start any pending signal handlers
834 * ------------------------------------------------------------------------- */
836 #if defined(RTS_USER_SIGNALS) && (!defined(THREADED_RTS) || defined(mingw32_HOST_OS))
838 scheduleStartSignalHandlers(Capability *cap)
840 if (signals_pending()) { // safe outside the lock
841 startSignalHandlers(cap);
846 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
851 /* ----------------------------------------------------------------------------
852 * Check for blocked threads that can be woken up.
853 * ------------------------------------------------------------------------- */
856 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
858 #if !defined(THREADED_RTS)
860 // Check whether any waiting threads need to be woken up. If the
861 // run queue is empty, and there are no other tasks running, we
862 // can wait indefinitely for something to happen.
864 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
866 awaitEvent( emptyRunQueue(cap) && !blackholes_need_checking );
872 /* ----------------------------------------------------------------------------
873 * Check for threads blocked on BLACKHOLEs that can be woken up
874 * ------------------------------------------------------------------------- */
876 scheduleCheckBlackHoles (Capability *cap)
878 if ( blackholes_need_checking ) // check without the lock first
880 ACQUIRE_LOCK(&sched_mutex);
881 if ( blackholes_need_checking ) {
882 checkBlackHoles(cap);
883 blackholes_need_checking = rtsFalse;
885 RELEASE_LOCK(&sched_mutex);
889 /* ----------------------------------------------------------------------------
890 * Detect deadlock conditions and attempt to resolve them.
891 * ------------------------------------------------------------------------- */
894 scheduleDetectDeadlock (Capability *cap, Task *task)
897 #if defined(PARALLEL_HASKELL)
898 // ToDo: add deadlock detection in GUM (similar to THREADED_RTS) -- HWL
903 * Detect deadlock: when we have no threads to run, there are no
904 * threads blocked, waiting for I/O, or sleeping, and all the
905 * other tasks are waiting for work, we must have a deadlock of
908 if ( emptyThreadQueues(cap) )
910 #if defined(THREADED_RTS)
912 * In the threaded RTS, we only check for deadlock if there
913 * has been no activity in a complete timeslice. This means
914 * we won't eagerly start a full GC just because we don't have
915 * any threads to run currently.
917 if (recent_activity != ACTIVITY_INACTIVE) return;
920 IF_DEBUG(scheduler, sched_belch("deadlocked, forcing major GC..."));
922 // Garbage collection can release some new threads due to
923 // either (a) finalizers or (b) threads resurrected because
924 // they are unreachable and will therefore be sent an
925 // exception. Any threads thus released will be immediately
927 scheduleDoGC( cap, task, rtsTrue/*force major GC*/, GetRoots );
928 #if defined(THREADED_RTS)
929 cap = task->cap; // reload cap, it might have changed
932 recent_activity = ACTIVITY_DONE_GC;
934 if ( !emptyRunQueue(cap) ) return;
936 #if defined(RTS_USER_SIGNALS) && (!defined(THREADED_RTS) || defined(mingw32_HOST_OS))
937 /* If we have user-installed signal handlers, then wait
938 * for signals to arrive rather then bombing out with a
941 if ( anyUserHandlers() ) {
943 sched_belch("still deadlocked, waiting for signals..."));
947 if (signals_pending()) {
948 startSignalHandlers(cap);
951 // either we have threads to run, or we were interrupted:
952 ASSERT(!emptyRunQueue(cap) || interrupted);
956 #if !defined(THREADED_RTS)
957 /* Probably a real deadlock. Send the current main thread the
958 * Deadlock exception.
961 switch (task->tso->why_blocked) {
963 case BlockedOnBlackHole:
964 case BlockedOnException:
966 raiseAsync(cap, task->tso, (StgClosure *)NonTermination_closure);
969 barf("deadlock: main thread blocked in a strange way");
977 /* ----------------------------------------------------------------------------
978 * Process an event (GRAN only)
979 * ------------------------------------------------------------------------- */
983 scheduleProcessEvent(rtsEvent *event)
987 if (RtsFlags.GranFlags.Light)
988 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
990 /* adjust time based on time-stamp */
991 if (event->time > CurrentTime[CurrentProc] &&
992 event->evttype != ContinueThread)
993 CurrentTime[CurrentProc] = event->time;
995 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
996 if (!RtsFlags.GranFlags.Light)
999 IF_DEBUG(gran, debugBelch("GRAN: switch by event-type\n"));
1001 /* main event dispatcher in GranSim */
1002 switch (event->evttype) {
1003 /* Should just be continuing execution */
1004 case ContinueThread:
1005 IF_DEBUG(gran, debugBelch("GRAN: doing ContinueThread\n"));
1006 /* ToDo: check assertion
1007 ASSERT(run_queue_hd != (StgTSO*)NULL &&
1008 run_queue_hd != END_TSO_QUEUE);
1010 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
1011 if (!RtsFlags.GranFlags.DoAsyncFetch &&
1012 procStatus[CurrentProc]==Fetching) {
1013 debugBelch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]\n",
1014 CurrentTSO->id, CurrentTSO, CurrentProc);
1017 /* Ignore ContinueThreads for completed threads */
1018 if (CurrentTSO->what_next == ThreadComplete) {
1019 debugBelch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)\n",
1020 CurrentTSO->id, CurrentTSO, CurrentProc);
1023 /* Ignore ContinueThreads for threads that are being migrated */
1024 if (PROCS(CurrentTSO)==Nowhere) {
1025 debugBelch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)\n",
1026 CurrentTSO->id, CurrentTSO, CurrentProc);
1029 /* The thread should be at the beginning of the run queue */
1030 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
1031 debugBelch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread\n",
1032 CurrentTSO->id, CurrentTSO, CurrentProc);
1033 break; // run the thread anyway
1036 new_event(proc, proc, CurrentTime[proc],
1038 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
1040 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
1041 break; // now actually run the thread; DaH Qu'vam yImuHbej
1044 do_the_fetchnode(event);
1045 goto next_thread; /* handle next event in event queue */
1048 do_the_globalblock(event);
1049 goto next_thread; /* handle next event in event queue */
1052 do_the_fetchreply(event);
1053 goto next_thread; /* handle next event in event queue */
1055 case UnblockThread: /* Move from the blocked queue to the tail of */
1056 do_the_unblock(event);
1057 goto next_thread; /* handle next event in event queue */
1059 case ResumeThread: /* Move from the blocked queue to the tail of */
1060 /* the runnable queue ( i.e. Qu' SImqa'lu') */
1061 event->tso->gran.blocktime +=
1062 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
1063 do_the_startthread(event);
1064 goto next_thread; /* handle next event in event queue */
1067 do_the_startthread(event);
1068 goto next_thread; /* handle next event in event queue */
1071 do_the_movethread(event);
1072 goto next_thread; /* handle next event in event queue */
1075 do_the_movespark(event);
1076 goto next_thread; /* handle next event in event queue */
1079 do_the_findwork(event);
1080 goto next_thread; /* handle next event in event queue */
1083 barf("Illegal event type %u\n", event->evttype);
1086 /* This point was scheduler_loop in the old RTS */
1088 IF_DEBUG(gran, debugBelch("GRAN: after main switch\n"));
1090 TimeOfLastEvent = CurrentTime[CurrentProc];
1091 TimeOfNextEvent = get_time_of_next_event();
1092 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
1093 // CurrentTSO = ThreadQueueHd;
1095 IF_DEBUG(gran, debugBelch("GRAN: time of next event is: %ld\n",
1098 if (RtsFlags.GranFlags.Light)
1099 GranSimLight_leave_system(event, &ActiveTSO);
1101 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
1104 debugBelch("GRAN: end of time-slice is %#lx\n", EndOfTimeSlice));
1106 /* in a GranSim setup the TSO stays on the run queue */
1108 /* Take a thread from the run queue. */
1109 POP_RUN_QUEUE(t); // take_off_run_queue(t);
1112 debugBelch("GRAN: About to run current thread, which is\n");
1115 context_switch = 0; // turned on via GranYield, checking events and time slice
1118 DumpGranEvent(GR_SCHEDULE, t));
1120 procStatus[CurrentProc] = Busy;
1124 /* ----------------------------------------------------------------------------
1125 * Send pending messages (PARALLEL_HASKELL only)
1126 * ------------------------------------------------------------------------- */
1128 #if defined(PARALLEL_HASKELL)
1130 scheduleSendPendingMessages(void)
1136 # if defined(PAR) // global Mem.Mgmt., omit for now
1137 if (PendingFetches != END_BF_QUEUE) {
1142 if (RtsFlags.ParFlags.BufferTime) {
1143 // if we use message buffering, we must send away all message
1144 // packets which have become too old...
1150 /* ----------------------------------------------------------------------------
1151 * Activate spark threads (PARALLEL_HASKELL only)
1152 * ------------------------------------------------------------------------- */
1154 #if defined(PARALLEL_HASKELL)
1156 scheduleActivateSpark(void)
1159 ASSERT(emptyRunQueue());
1160 /* We get here if the run queue is empty and want some work.
1161 We try to turn a spark into a thread, and add it to the run queue,
1162 from where it will be picked up in the next iteration of the scheduler
1166 /* :-[ no local threads => look out for local sparks */
1167 /* the spark pool for the current PE */
1168 pool = &(cap.r.rSparks); // JB: cap = (old) MainCap
1169 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
1170 pool->hd < pool->tl) {
1172 * ToDo: add GC code check that we really have enough heap afterwards!!
1174 * If we're here (no runnable threads) and we have pending
1175 * sparks, we must have a space problem. Get enough space
1176 * to turn one of those pending sparks into a
1180 spark = findSpark(rtsFalse); /* get a spark */
1181 if (spark != (rtsSpark) NULL) {
1182 tso = createThreadFromSpark(spark); /* turn the spark into a thread */
1183 IF_PAR_DEBUG(fish, // schedule,
1184 debugBelch("==== schedule: Created TSO %d (%p); %d threads active\n",
1185 tso->id, tso, advisory_thread_count));
1187 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
1188 IF_PAR_DEBUG(fish, // schedule,
1189 debugBelch("==^^ failed to create thread from spark @ %lx\n",
1191 return rtsFalse; /* failed to generate a thread */
1192 } /* otherwise fall through & pick-up new tso */
1194 IF_PAR_DEBUG(fish, // schedule,
1195 debugBelch("==^^ no local sparks (spark pool contains only NFs: %d)\n",
1196 spark_queue_len(pool)));
1197 return rtsFalse; /* failed to generate a thread */
1199 return rtsTrue; /* success in generating a thread */
1200 } else { /* no more threads permitted or pool empty */
1201 return rtsFalse; /* failed to generateThread */
1204 tso = NULL; // avoid compiler warning only
1205 return rtsFalse; /* dummy in non-PAR setup */
1208 #endif // PARALLEL_HASKELL
1210 /* ----------------------------------------------------------------------------
1211 * Get work from a remote node (PARALLEL_HASKELL only)
1212 * ------------------------------------------------------------------------- */
1214 #if defined(PARALLEL_HASKELL)
1216 scheduleGetRemoteWork(rtsBool *receivedFinish)
1218 ASSERT(emptyRunQueue());
1220 if (RtsFlags.ParFlags.BufferTime) {
1221 IF_PAR_DEBUG(verbose,
1222 debugBelch("...send all pending data,"));
1225 for (i=1; i<=nPEs; i++)
1226 sendImmediately(i); // send all messages away immediately
1230 //++EDEN++ idle() , i.e. send all buffers, wait for work
1231 // suppress fishing in EDEN... just look for incoming messages
1232 // (blocking receive)
1233 IF_PAR_DEBUG(verbose,
1234 debugBelch("...wait for incoming messages...\n"));
1235 *receivedFinish = processMessages(); // blocking receive...
1237 // and reenter scheduling loop after having received something
1238 // (return rtsFalse below)
1240 # else /* activate SPARKS machinery */
1241 /* We get here, if we have no work, tried to activate a local spark, but still
1242 have no work. We try to get a remote spark, by sending a FISH message.
1243 Thread migration should be added here, and triggered when a sequence of
1244 fishes returns without work. */
1245 delay = (RtsFlags.ParFlags.fishDelay!=0ll ? RtsFlags.ParFlags.fishDelay : 0ll);
1247 /* =8-[ no local sparks => look for work on other PEs */
1249 * We really have absolutely no work. Send out a fish
1250 * (there may be some out there already), and wait for
1251 * something to arrive. We clearly can't run any threads
1252 * until a SCHEDULE or RESUME arrives, and so that's what
1253 * we're hoping to see. (Of course, we still have to
1254 * respond to other types of messages.)
1256 rtsTime now = msTime() /*CURRENT_TIME*/;
1257 IF_PAR_DEBUG(verbose,
1258 debugBelch("-- now=%ld\n", now));
1259 IF_PAR_DEBUG(fish, // verbose,
1260 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1261 (last_fish_arrived_at!=0 &&
1262 last_fish_arrived_at+delay > now)) {
1263 debugBelch("--$$ <%llu> delaying FISH until %llu (last fish %llu, delay %llu)\n",
1264 now, last_fish_arrived_at+delay,
1265 last_fish_arrived_at,
1269 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1270 advisory_thread_count < RtsFlags.ParFlags.maxThreads) { // send a FISH, but when?
1271 if (last_fish_arrived_at==0 ||
1272 (last_fish_arrived_at+delay <= now)) { // send FISH now!
1273 /* outstandingFishes is set in sendFish, processFish;
1274 avoid flooding system with fishes via delay */
1275 next_fish_to_send_at = 0;
1277 /* ToDo: this should be done in the main scheduling loop to avoid the
1278 busy wait here; not so bad if fish delay is very small */
1279 int iq = 0; // DEBUGGING -- HWL
1280 next_fish_to_send_at = last_fish_arrived_at+delay; // remember when to send
1281 /* send a fish when ready, but process messages that arrive in the meantime */
1283 if (PacketsWaiting()) {
1285 *receivedFinish = processMessages();
1288 } while (!*receivedFinish || now<next_fish_to_send_at);
1289 // JB: This means the fish could become obsolete, if we receive
1290 // work. Better check for work again?
1291 // last line: while (!receivedFinish || !haveWork || now<...)
1292 // next line: if (receivedFinish || haveWork )
1294 if (*receivedFinish) // no need to send a FISH if we are finishing anyway
1295 return rtsFalse; // NB: this will leave scheduler loop
1296 // immediately after return!
1298 IF_PAR_DEBUG(fish, // verbose,
1299 debugBelch("--$$ <%llu> sent delayed fish (%d processMessages); active/total threads=%d/%d\n",now,iq,run_queue_len(),advisory_thread_count));
1303 // JB: IMHO, this should all be hidden inside sendFish(...)
1305 sendFish(pe, thisPE, NEW_FISH_AGE, NEW_FISH_HISTORY,
1308 // Global statistics: count no. of fishes
1309 if (RtsFlags.ParFlags.ParStats.Global &&
1310 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1311 globalParStats.tot_fish_mess++;
1315 /* delayed fishes must have been sent by now! */
1316 next_fish_to_send_at = 0;
1319 *receivedFinish = processMessages();
1320 # endif /* SPARKS */
1323 /* NB: this function always returns rtsFalse, meaning the scheduler
1324 loop continues with the next iteration;
1326 return code means success in finding work; we enter this function
1327 if there is no local work, thus have to send a fish which takes
1328 time until it arrives with work; in the meantime we should process
1329 messages in the main loop;
1332 #endif // PARALLEL_HASKELL
1334 /* ----------------------------------------------------------------------------
1335 * PAR/GRAN: Report stats & debugging info(?)
1336 * ------------------------------------------------------------------------- */
1338 #if defined(PAR) || defined(GRAN)
1340 scheduleGranParReport(void)
1342 ASSERT(run_queue_hd != END_TSO_QUEUE);
1344 /* Take a thread from the run queue, if we have work */
1345 POP_RUN_QUEUE(t); // take_off_run_queue(END_TSO_QUEUE);
1347 /* If this TSO has got its outport closed in the meantime,
1348 * it mustn't be run. Instead, we have to clean it up as if it was finished.
1349 * It has to be marked as TH_DEAD for this purpose.
1350 * If it is TH_TERM instead, it is supposed to have finished in the normal way.
1352 JB: TODO: investigate wether state change field could be nuked
1353 entirely and replaced by the normal tso state (whatnext
1354 field). All we want to do is to kill tsos from outside.
1357 /* ToDo: write something to the log-file
1358 if (RTSflags.ParFlags.granSimStats && !sameThread)
1359 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
1363 /* the spark pool for the current PE */
1364 pool = &(cap.r.rSparks); // cap = (old) MainCap
1367 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1368 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1371 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1372 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1374 if (RtsFlags.ParFlags.ParStats.Full &&
1375 (t->par.sparkname != (StgInt)0) && // only log spark generated threads
1376 (emitSchedule || // forced emit
1377 (t && LastTSO && t->id != LastTSO->id))) {
1379 we are running a different TSO, so write a schedule event to log file
1380 NB: If we use fair scheduling we also have to write a deschedule
1381 event for LastTSO; with unfair scheduling we know that the
1382 previous tso has blocked whenever we switch to another tso, so
1383 we don't need it in GUM for now
1385 IF_PAR_DEBUG(fish, // schedule,
1386 debugBelch("____ scheduling spark generated thread %d (%lx) (%lx) via a forced emit\n",t->id,t,t->par.sparkname));
1388 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1389 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
1390 emitSchedule = rtsFalse;
1395 /* ----------------------------------------------------------------------------
1396 * After running a thread...
1397 * ------------------------------------------------------------------------- */
1400 schedulePostRunThread(void)
1403 /* HACK 675: if the last thread didn't yield, make sure to print a
1404 SCHEDULE event to the log file when StgRunning the next thread, even
1405 if it is the same one as before */
1407 TimeOfLastYield = CURRENT_TIME;
1410 /* some statistics gathering in the parallel case */
1412 #if defined(GRAN) || defined(PAR) || defined(EDEN)
1416 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
1417 globalGranStats.tot_heapover++;
1419 globalParStats.tot_heapover++;
1426 DumpGranEvent(GR_DESCHEDULE, t));
1427 globalGranStats.tot_stackover++;
1430 // DumpGranEvent(GR_DESCHEDULE, t);
1431 globalParStats.tot_stackover++;
1435 case ThreadYielding:
1438 DumpGranEvent(GR_DESCHEDULE, t));
1439 globalGranStats.tot_yields++;
1442 // DumpGranEvent(GR_DESCHEDULE, t);
1443 globalParStats.tot_yields++;
1450 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1451 t->id, t, whatNext_strs[t->what_next], t->block_info.closure,
1452 (t->block_info.closure==(StgClosure*)NULL ? 99 : where_is(t->block_info.closure)));
1453 if (t->block_info.closure!=(StgClosure*)NULL)
1454 print_bq(t->block_info.closure);
1457 // ??? needed; should emit block before
1459 DumpGranEvent(GR_DESCHEDULE, t));
1460 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1463 ASSERT(procStatus[CurrentProc]==Busy ||
1464 ((procStatus[CurrentProc]==Fetching) &&
1465 (t->block_info.closure!=(StgClosure*)NULL)));
1466 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1467 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1468 procStatus[CurrentProc]==Fetching))
1469 procStatus[CurrentProc] = Idle;
1472 //++PAR++ blockThread() writes the event (change?)
1476 case ThreadFinished:
1480 barf("parGlobalStats: unknown return code");
1486 /* -----------------------------------------------------------------------------
1487 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1488 * -------------------------------------------------------------------------- */
1491 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1493 // did the task ask for a large block?
1494 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1495 // if so, get one and push it on the front of the nursery.
1499 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1502 debugBelch("--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1503 (long)t->id, whatNext_strs[t->what_next], blocks));
1505 // don't do this if the nursery is (nearly) full, we'll GC first.
1506 if (cap->r.rCurrentNursery->link != NULL ||
1507 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1508 // if the nursery has only one block.
1511 bd = allocGroup( blocks );
1513 cap->r.rNursery->n_blocks += blocks;
1515 // link the new group into the list
1516 bd->link = cap->r.rCurrentNursery;
1517 bd->u.back = cap->r.rCurrentNursery->u.back;
1518 if (cap->r.rCurrentNursery->u.back != NULL) {
1519 cap->r.rCurrentNursery->u.back->link = bd;
1521 #if !defined(THREADED_RTS)
1522 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
1523 g0s0 == cap->r.rNursery);
1525 cap->r.rNursery->blocks = bd;
1527 cap->r.rCurrentNursery->u.back = bd;
1529 // initialise it as a nursery block. We initialise the
1530 // step, gen_no, and flags field of *every* sub-block in
1531 // this large block, because this is easier than making
1532 // sure that we always find the block head of a large
1533 // block whenever we call Bdescr() (eg. evacuate() and
1534 // isAlive() in the GC would both have to do this, at
1538 for (x = bd; x < bd + blocks; x++) {
1539 x->step = cap->r.rNursery;
1545 // This assert can be a killer if the app is doing lots
1546 // of large block allocations.
1547 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1549 // now update the nursery to point to the new block
1550 cap->r.rCurrentNursery = bd;
1552 // we might be unlucky and have another thread get on the
1553 // run queue before us and steal the large block, but in that
1554 // case the thread will just end up requesting another large
1556 pushOnRunQueue(cap,t);
1557 return rtsFalse; /* not actually GC'ing */
1562 debugBelch("--<< thread %ld (%s) stopped: HeapOverflow\n",
1563 (long)t->id, whatNext_strs[t->what_next]));
1565 ASSERT(!is_on_queue(t,CurrentProc));
1566 #elif defined(PARALLEL_HASKELL)
1567 /* Currently we emit a DESCHEDULE event before GC in GUM.
1568 ToDo: either add separate event to distinguish SYSTEM time from rest
1569 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1570 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1571 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1572 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1573 emitSchedule = rtsTrue;
1577 pushOnRunQueue(cap,t);
1579 /* actual GC is done at the end of the while loop in schedule() */
1582 /* -----------------------------------------------------------------------------
1583 * Handle a thread that returned to the scheduler with ThreadStackOverflow
1584 * -------------------------------------------------------------------------- */
1587 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1589 IF_DEBUG(scheduler,debugBelch("--<< thread %ld (%s) stopped, StackOverflow\n",
1590 (long)t->id, whatNext_strs[t->what_next]));
1591 /* just adjust the stack for this thread, then pop it back
1595 /* enlarge the stack */
1596 StgTSO *new_t = threadStackOverflow(cap, t);
1598 /* The TSO attached to this Task may have moved, so update the
1601 if (task->tso == t) {
1604 pushOnRunQueue(cap,new_t);
1608 /* -----------------------------------------------------------------------------
1609 * Handle a thread that returned to the scheduler with ThreadYielding
1610 * -------------------------------------------------------------------------- */
1613 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1615 // Reset the context switch flag. We don't do this just before
1616 // running the thread, because that would mean we would lose ticks
1617 // during GC, which can lead to unfair scheduling (a thread hogs
1618 // the CPU because the tick always arrives during GC). This way
1619 // penalises threads that do a lot of allocation, but that seems
1620 // better than the alternative.
1623 /* put the thread back on the run queue. Then, if we're ready to
1624 * GC, check whether this is the last task to stop. If so, wake
1625 * up the GC thread. getThread will block during a GC until the
1629 if (t->what_next != prev_what_next) {
1630 debugBelch("--<< thread %ld (%s) stopped to switch evaluators\n",
1631 (long)t->id, whatNext_strs[t->what_next]);
1633 debugBelch("--<< thread %ld (%s) stopped, yielding\n",
1634 (long)t->id, whatNext_strs[t->what_next]);
1639 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1641 ASSERT(t->link == END_TSO_QUEUE);
1643 // Shortcut if we're just switching evaluators: don't bother
1644 // doing stack squeezing (which can be expensive), just run the
1646 if (t->what_next != prev_what_next) {
1651 ASSERT(!is_on_queue(t,CurrentProc));
1654 //debugBelch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1655 checkThreadQsSanity(rtsTrue));
1659 addToRunQueue(cap,t);
1662 /* add a ContinueThread event to actually process the thread */
1663 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1665 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1667 debugBelch("GRAN: eventq and runnableq after adding yielded thread to queue again:\n");
1674 /* -----------------------------------------------------------------------------
1675 * Handle a thread that returned to the scheduler with ThreadBlocked
1676 * -------------------------------------------------------------------------- */
1679 scheduleHandleThreadBlocked( StgTSO *t
1680 #if !defined(GRAN) && !defined(DEBUG)
1687 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: \n",
1688 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)));
1689 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1691 // ??? needed; should emit block before
1693 DumpGranEvent(GR_DESCHEDULE, t));
1694 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1697 ASSERT(procStatus[CurrentProc]==Busy ||
1698 ((procStatus[CurrentProc]==Fetching) &&
1699 (t->block_info.closure!=(StgClosure*)NULL)));
1700 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1701 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1702 procStatus[CurrentProc]==Fetching))
1703 procStatus[CurrentProc] = Idle;
1707 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: \n",
1708 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1711 if (t->block_info.closure!=(StgClosure*)NULL)
1712 print_bq(t->block_info.closure));
1714 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1717 /* whatever we schedule next, we must log that schedule */
1718 emitSchedule = rtsTrue;
1722 // We don't need to do anything. The thread is blocked, and it
1723 // has tidied up its stack and placed itself on whatever queue
1724 // it needs to be on.
1726 #if !defined(THREADED_RTS)
1727 ASSERT(t->why_blocked != NotBlocked);
1728 // This might not be true under THREADED_RTS: we don't have
1729 // exclusive access to this TSO, so someone might have
1730 // woken it up by now. This actually happens: try
1731 // conc023 +RTS -N2.
1735 debugBelch("--<< thread %d (%s) stopped: ",
1736 t->id, whatNext_strs[t->what_next]);
1737 printThreadBlockage(t);
1740 /* Only for dumping event to log file
1741 ToDo: do I need this in GranSim, too?
1747 /* -----------------------------------------------------------------------------
1748 * Handle a thread that returned to the scheduler with ThreadFinished
1749 * -------------------------------------------------------------------------- */
1752 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1754 /* Need to check whether this was a main thread, and if so,
1755 * return with the return value.
1757 * We also end up here if the thread kills itself with an
1758 * uncaught exception, see Exception.cmm.
1760 IF_DEBUG(scheduler,debugBelch("--++ thread %d (%s) finished\n",
1761 t->id, whatNext_strs[t->what_next]));
1764 endThread(t, CurrentProc); // clean-up the thread
1765 #elif defined(PARALLEL_HASKELL)
1766 /* For now all are advisory -- HWL */
1767 //if(t->priority==AdvisoryPriority) ??
1768 advisory_thread_count--; // JB: Caution with this counter, buggy!
1771 if(t->dist.priority==RevalPriority)
1775 # if defined(EDENOLD)
1776 // the thread could still have an outport... (BUG)
1777 if (t->eden.outport != -1) {
1778 // delete the outport for the tso which has finished...
1779 IF_PAR_DEBUG(eden_ports,
1780 debugBelch("WARNING: Scheduler removes outport %d for TSO %d.\n",
1781 t->eden.outport, t->id));
1784 // thread still in the process (HEAVY BUG! since outport has just been closed...)
1785 if (t->eden.epid != -1) {
1786 IF_PAR_DEBUG(eden_ports,
1787 debugBelch("WARNING: Scheduler removes TSO %d from process %d .\n",
1788 t->id, t->eden.epid));
1789 removeTSOfromProcess(t);
1794 if (RtsFlags.ParFlags.ParStats.Full &&
1795 !RtsFlags.ParFlags.ParStats.Suppressed)
1796 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1798 // t->par only contains statistics: left out for now...
1800 debugBelch("**** end thread: ended sparked thread %d (%lx); sparkname: %lx\n",
1801 t->id,t,t->par.sparkname));
1803 #endif // PARALLEL_HASKELL
1806 // Check whether the thread that just completed was a bound
1807 // thread, and if so return with the result.
1809 // There is an assumption here that all thread completion goes
1810 // through this point; we need to make sure that if a thread
1811 // ends up in the ThreadKilled state, that it stays on the run
1812 // queue so it can be dealt with here.
1817 if (t->bound != task) {
1818 #if !defined(THREADED_RTS)
1819 // Must be a bound thread that is not the topmost one. Leave
1820 // it on the run queue until the stack has unwound to the
1821 // point where we can deal with this. Leaving it on the run
1822 // queue also ensures that the garbage collector knows about
1823 // this thread and its return value (it gets dropped from the
1824 // all_threads list so there's no other way to find it).
1825 appendToRunQueue(cap,t);
1828 // this cannot happen in the threaded RTS, because a
1829 // bound thread can only be run by the appropriate Task.
1830 barf("finished bound thread that isn't mine");
1834 ASSERT(task->tso == t);
1836 if (t->what_next == ThreadComplete) {
1838 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1839 *(task->ret) = (StgClosure *)task->tso->sp[1];
1841 task->stat = Success;
1844 *(task->ret) = NULL;
1847 task->stat = Interrupted;
1849 task->stat = Killed;
1853 removeThreadLabel((StgWord)task->tso->id);
1855 return rtsTrue; // tells schedule() to return
1861 /* -----------------------------------------------------------------------------
1862 * Perform a heap census, if PROFILING
1863 * -------------------------------------------------------------------------- */
1866 scheduleDoHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1868 #if defined(PROFILING)
1869 // When we have +RTS -i0 and we're heap profiling, do a census at
1870 // every GC. This lets us get repeatable runs for debugging.
1871 if (performHeapProfile ||
1872 (RtsFlags.ProfFlags.profileInterval==0 &&
1873 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1875 // checking black holes is necessary before GC, otherwise
1876 // there may be threads that are unreachable except by the
1877 // blackhole queue, which the GC will consider to be
1879 scheduleCheckBlackHoles(&MainCapability);
1881 IF_DEBUG(scheduler, sched_belch("garbage collecting before heap census"));
1882 GarbageCollect(GetRoots, rtsTrue);
1884 IF_DEBUG(scheduler, sched_belch("performing heap census"));
1887 performHeapProfile = rtsFalse;
1888 return rtsTrue; // true <=> we already GC'd
1894 /* -----------------------------------------------------------------------------
1895 * Perform a garbage collection if necessary
1896 * -------------------------------------------------------------------------- */
1899 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS,
1900 rtsBool force_major, void (*get_roots)(evac_fn))
1904 static volatile StgWord waiting_for_gc;
1905 rtsBool was_waiting;
1910 // In order to GC, there must be no threads running Haskell code.
1911 // Therefore, the GC thread needs to hold *all* the capabilities,
1912 // and release them after the GC has completed.
1914 // This seems to be the simplest way: previous attempts involved
1915 // making all the threads with capabilities give up their
1916 // capabilities and sleep except for the *last* one, which
1917 // actually did the GC. But it's quite hard to arrange for all
1918 // the other tasks to sleep and stay asleep.
1921 was_waiting = cas(&waiting_for_gc, 0, 1);
1924 IF_DEBUG(scheduler, sched_belch("someone else is trying to GC..."));
1925 if (cap) yieldCapability(&cap,task);
1926 } while (waiting_for_gc);
1927 return; // NOTE: task->cap might have changed here
1930 for (i=0; i < n_capabilities; i++) {
1931 IF_DEBUG(scheduler, sched_belch("ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities));
1932 if (cap != &capabilities[i]) {
1933 Capability *pcap = &capabilities[i];
1934 // we better hope this task doesn't get migrated to
1935 // another Capability while we're waiting for this one.
1936 // It won't, because load balancing happens while we have
1937 // all the Capabilities, but even so it's a slightly
1938 // unsavoury invariant.
1941 waitForReturnCapability(&pcap, task);
1942 if (pcap != &capabilities[i]) {
1943 barf("scheduleDoGC: got the wrong capability");
1948 waiting_for_gc = rtsFalse;
1951 /* Kick any transactions which are invalid back to their
1952 * atomically frames. When next scheduled they will try to
1953 * commit, this commit will fail and they will retry.
1958 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1959 if (t->what_next == ThreadRelocated) {
1962 next = t->global_link;
1963 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
1964 if (!stmValidateNestOfTransactions (t -> trec)) {
1965 IF_DEBUG(stm, sched_belch("trec %p found wasting its time", t));
1967 // strip the stack back to the
1968 // ATOMICALLY_FRAME, aborting the (nested)
1969 // transaction, and saving the stack of any
1970 // partially-evaluated thunks on the heap.
1971 raiseAsync_(&capabilities[0], t, NULL, rtsTrue, NULL);
1974 ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1982 // so this happens periodically:
1983 if (cap) scheduleCheckBlackHoles(cap);
1985 IF_DEBUG(scheduler, printAllThreads());
1987 /* everybody back, start the GC.
1988 * Could do it in this thread, or signal a condition var
1989 * to do it in another thread. Either way, we need to
1990 * broadcast on gc_pending_cond afterward.
1992 #if defined(THREADED_RTS)
1993 IF_DEBUG(scheduler,sched_belch("doing GC"));
1995 GarbageCollect(get_roots, force_major);
1997 #if defined(THREADED_RTS)
1998 // release our stash of capabilities.
1999 for (i = 0; i < n_capabilities; i++) {
2000 if (cap != &capabilities[i]) {
2001 task->cap = &capabilities[i];
2002 releaseCapability(&capabilities[i]);
2013 /* add a ContinueThread event to continue execution of current thread */
2014 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
2016 t, (StgClosure*)NULL, (rtsSpark*)NULL);
2018 debugBelch("GRAN: eventq and runnableq after Garbage collection:\n\n");
2024 /* ---------------------------------------------------------------------------
2025 * rtsSupportsBoundThreads(): is the RTS built to support bound threads?
2026 * used by Control.Concurrent for error checking.
2027 * ------------------------------------------------------------------------- */
2030 rtsSupportsBoundThreads(void)
2032 #if defined(THREADED_RTS)
2039 /* ---------------------------------------------------------------------------
2040 * isThreadBound(tso): check whether tso is bound to an OS thread.
2041 * ------------------------------------------------------------------------- */
2044 isThreadBound(StgTSO* tso USED_IF_THREADS)
2046 #if defined(THREADED_RTS)
2047 return (tso->bound != NULL);
2052 /* ---------------------------------------------------------------------------
2053 * Singleton fork(). Do not copy any running threads.
2054 * ------------------------------------------------------------------------- */
2056 #if !defined(mingw32_HOST_OS)
2057 #define FORKPROCESS_PRIMOP_SUPPORTED
2060 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2062 deleteThreadImmediately(Capability *cap, StgTSO *tso);
2065 forkProcess(HsStablePtr *entry
2066 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
2071 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2077 #if defined(THREADED_RTS)
2078 if (RtsFlags.ParFlags.nNodes > 1) {
2079 errorBelch("forking not supported with +RTS -N<n> greater than 1");
2080 stg_exit(EXIT_FAILURE);
2084 IF_DEBUG(scheduler,sched_belch("forking!"));
2086 // ToDo: for SMP, we should probably acquire *all* the capabilities
2091 if (pid) { // parent
2093 // just return the pid
2099 // delete all threads
2100 cap->run_queue_hd = END_TSO_QUEUE;
2101 cap->run_queue_tl = END_TSO_QUEUE;
2103 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
2106 // don't allow threads to catch the ThreadKilled exception
2107 deleteThreadImmediately(cap,t);
2110 // wipe the task list
2111 ACQUIRE_LOCK(&sched_mutex);
2112 for (task = all_tasks; task != NULL; task=task->all_link) {
2113 if (task != cap->running_task) discardTask(task);
2115 RELEASE_LOCK(&sched_mutex);
2117 cap->suspended_ccalling_tasks = NULL;
2119 #if defined(THREADED_RTS)
2120 // wipe our spare workers list.
2121 cap->spare_workers = NULL;
2122 cap->returning_tasks_hd = NULL;
2123 cap->returning_tasks_tl = NULL;
2126 cap = rts_evalStableIO(cap, entry, NULL); // run the action
2127 rts_checkSchedStatus("forkProcess",cap);
2130 hs_exit(); // clean up and exit
2131 stg_exit(EXIT_SUCCESS);
2133 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
2134 barf("forkProcess#: primop not supported on this platform, sorry!\n");
2139 /* ---------------------------------------------------------------------------
2140 * Delete the threads on the run queue of the current capability.
2141 * ------------------------------------------------------------------------- */
2144 deleteRunQueue (Capability *cap)
2147 for (t = cap->run_queue_hd; t != END_TSO_QUEUE; t = next) {
2148 ASSERT(t->what_next != ThreadRelocated);
2150 deleteThread(cap, t);
2154 /* startThread and insertThread are now in GranSim.c -- HWL */
2157 /* -----------------------------------------------------------------------------
2158 Managing the suspended_ccalling_tasks list.
2159 Locks required: sched_mutex
2160 -------------------------------------------------------------------------- */
2163 suspendTask (Capability *cap, Task *task)
2165 ASSERT(task->next == NULL && task->prev == NULL);
2166 task->next = cap->suspended_ccalling_tasks;
2168 if (cap->suspended_ccalling_tasks) {
2169 cap->suspended_ccalling_tasks->prev = task;
2171 cap->suspended_ccalling_tasks = task;
2175 recoverSuspendedTask (Capability *cap, Task *task)
2178 task->prev->next = task->next;
2180 ASSERT(cap->suspended_ccalling_tasks == task);
2181 cap->suspended_ccalling_tasks = task->next;
2184 task->next->prev = task->prev;
2186 task->next = task->prev = NULL;
2189 /* ---------------------------------------------------------------------------
2190 * Suspending & resuming Haskell threads.
2192 * When making a "safe" call to C (aka _ccall_GC), the task gives back
2193 * its capability before calling the C function. This allows another
2194 * task to pick up the capability and carry on running Haskell
2195 * threads. It also means that if the C call blocks, it won't lock
2198 * The Haskell thread making the C call is put to sleep for the
2199 * duration of the call, on the susepended_ccalling_threads queue. We
2200 * give out a token to the task, which it can use to resume the thread
2201 * on return from the C function.
2202 * ------------------------------------------------------------------------- */
2205 suspendThread (StgRegTable *reg)
2208 int saved_errno = errno;
2212 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
2214 cap = regTableToCapability(reg);
2216 task = cap->running_task;
2217 tso = cap->r.rCurrentTSO;
2220 sched_belch("thread %d did a safe foreign call", cap->r.rCurrentTSO->id));
2222 // XXX this might not be necessary --SDM
2223 tso->what_next = ThreadRunGHC;
2225 threadPaused(cap,tso);
2227 if(tso->blocked_exceptions == NULL) {
2228 tso->why_blocked = BlockedOnCCall;
2229 tso->blocked_exceptions = END_TSO_QUEUE;
2231 tso->why_blocked = BlockedOnCCall_NoUnblockExc;
2234 // Hand back capability
2235 task->suspended_tso = tso;
2237 ACQUIRE_LOCK(&cap->lock);
2239 suspendTask(cap,task);
2240 cap->in_haskell = rtsFalse;
2241 releaseCapability_(cap);
2243 RELEASE_LOCK(&cap->lock);
2245 #if defined(THREADED_RTS)
2246 /* Preparing to leave the RTS, so ensure there's a native thread/task
2247 waiting to take over.
2249 IF_DEBUG(scheduler, sched_belch("thread %d: leaving RTS", tso->id));
2252 errno = saved_errno;
2257 resumeThread (void *task_)
2261 int saved_errno = errno;
2265 // Wait for permission to re-enter the RTS with the result.
2266 waitForReturnCapability(&cap,task);
2267 // we might be on a different capability now... but if so, our
2268 // entry on the suspended_ccalling_tasks list will also have been
2271 // Remove the thread from the suspended list
2272 recoverSuspendedTask(cap,task);
2274 tso = task->suspended_tso;
2275 task->suspended_tso = NULL;
2276 tso->link = END_TSO_QUEUE;
2277 IF_DEBUG(scheduler, sched_belch("thread %d: re-entering RTS", tso->id));
2279 if (tso->why_blocked == BlockedOnCCall) {
2280 awakenBlockedQueue(cap,tso->blocked_exceptions);
2281 tso->blocked_exceptions = NULL;
2284 /* Reset blocking status */
2285 tso->why_blocked = NotBlocked;
2287 cap->r.rCurrentTSO = tso;
2288 cap->in_haskell = rtsTrue;
2289 errno = saved_errno;
2291 /* We might have GC'd, mark the TSO dirty again */
2294 IF_DEBUG(sanity, checkTSO(tso));
2299 /* ---------------------------------------------------------------------------
2300 * Comparing Thread ids.
2302 * This is used from STG land in the implementation of the
2303 * instances of Eq/Ord for ThreadIds.
2304 * ------------------------------------------------------------------------ */
2307 cmp_thread(StgPtr tso1, StgPtr tso2)
2309 StgThreadID id1 = ((StgTSO *)tso1)->id;
2310 StgThreadID id2 = ((StgTSO *)tso2)->id;
2312 if (id1 < id2) return (-1);
2313 if (id1 > id2) return 1;
2317 /* ---------------------------------------------------------------------------
2318 * Fetching the ThreadID from an StgTSO.
2320 * This is used in the implementation of Show for ThreadIds.
2321 * ------------------------------------------------------------------------ */
2323 rts_getThreadId(StgPtr tso)
2325 return ((StgTSO *)tso)->id;
2330 labelThread(StgPtr tso, char *label)
2335 /* Caveat: Once set, you can only set the thread name to "" */
2336 len = strlen(label)+1;
2337 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
2338 strncpy(buf,label,len);
2339 /* Update will free the old memory for us */
2340 updateThreadLabel(((StgTSO *)tso)->id,buf);
2344 /* ---------------------------------------------------------------------------
2345 Create a new thread.
2347 The new thread starts with the given stack size. Before the
2348 scheduler can run, however, this thread needs to have a closure
2349 (and possibly some arguments) pushed on its stack. See
2350 pushClosure() in Schedule.h.
2352 createGenThread() and createIOThread() (in SchedAPI.h) are
2353 convenient packaged versions of this function.
2355 currently pri (priority) is only used in a GRAN setup -- HWL
2356 ------------------------------------------------------------------------ */
2358 /* currently pri (priority) is only used in a GRAN setup -- HWL */
2360 createThread(nat size, StgInt pri)
2363 createThread(Capability *cap, nat size)
2369 /* sched_mutex is *not* required */
2371 /* First check whether we should create a thread at all */
2372 #if defined(PARALLEL_HASKELL)
2373 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
2374 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
2376 debugBelch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)\n",
2377 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
2378 return END_TSO_QUEUE;
2384 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
2387 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
2389 /* catch ridiculously small stack sizes */
2390 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
2391 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
2394 stack_size = size - TSO_STRUCT_SIZEW;
2396 tso = (StgTSO *)allocateLocal(cap, size);
2397 TICK_ALLOC_TSO(stack_size, 0);
2399 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
2401 SET_GRAN_HDR(tso, ThisPE);
2404 // Always start with the compiled code evaluator
2405 tso->what_next = ThreadRunGHC;
2407 tso->why_blocked = NotBlocked;
2408 tso->blocked_exceptions = NULL;
2409 tso->flags = TSO_DIRTY;
2411 tso->saved_errno = 0;
2414 tso->stack_size = stack_size;
2415 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
2417 tso->sp = (P_)&(tso->stack) + stack_size;
2419 tso->trec = NO_TREC;
2422 tso->prof.CCCS = CCS_MAIN;
2425 /* put a stop frame on the stack */
2426 tso->sp -= sizeofW(StgStopFrame);
2427 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
2428 tso->link = END_TSO_QUEUE;
2432 /* uses more flexible routine in GranSim */
2433 insertThread(tso, CurrentProc);
2435 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
2441 if (RtsFlags.GranFlags.GranSimStats.Full)
2442 DumpGranEvent(GR_START,tso);
2443 #elif defined(PARALLEL_HASKELL)
2444 if (RtsFlags.ParFlags.ParStats.Full)
2445 DumpGranEvent(GR_STARTQ,tso);
2446 /* HACk to avoid SCHEDULE
2450 /* Link the new thread on the global thread list.
2452 ACQUIRE_LOCK(&sched_mutex);
2453 tso->id = next_thread_id++; // while we have the mutex
2454 tso->global_link = all_threads;
2456 RELEASE_LOCK(&sched_mutex);
2459 tso->dist.priority = MandatoryPriority; //by default that is...
2463 tso->gran.pri = pri;
2465 tso->gran.magic = TSO_MAGIC; // debugging only
2467 tso->gran.sparkname = 0;
2468 tso->gran.startedat = CURRENT_TIME;
2469 tso->gran.exported = 0;
2470 tso->gran.basicblocks = 0;
2471 tso->gran.allocs = 0;
2472 tso->gran.exectime = 0;
2473 tso->gran.fetchtime = 0;
2474 tso->gran.fetchcount = 0;
2475 tso->gran.blocktime = 0;
2476 tso->gran.blockcount = 0;
2477 tso->gran.blockedat = 0;
2478 tso->gran.globalsparks = 0;
2479 tso->gran.localsparks = 0;
2480 if (RtsFlags.GranFlags.Light)
2481 tso->gran.clock = Now; /* local clock */
2483 tso->gran.clock = 0;
2485 IF_DEBUG(gran,printTSO(tso));
2486 #elif defined(PARALLEL_HASKELL)
2488 tso->par.magic = TSO_MAGIC; // debugging only
2490 tso->par.sparkname = 0;
2491 tso->par.startedat = CURRENT_TIME;
2492 tso->par.exported = 0;
2493 tso->par.basicblocks = 0;
2494 tso->par.allocs = 0;
2495 tso->par.exectime = 0;
2496 tso->par.fetchtime = 0;
2497 tso->par.fetchcount = 0;
2498 tso->par.blocktime = 0;
2499 tso->par.blockcount = 0;
2500 tso->par.blockedat = 0;
2501 tso->par.globalsparks = 0;
2502 tso->par.localsparks = 0;
2506 globalGranStats.tot_threads_created++;
2507 globalGranStats.threads_created_on_PE[CurrentProc]++;
2508 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
2509 globalGranStats.tot_sq_probes++;
2510 #elif defined(PARALLEL_HASKELL)
2511 // collect parallel global statistics (currently done together with GC stats)
2512 if (RtsFlags.ParFlags.ParStats.Global &&
2513 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
2514 //debugBelch("Creating thread %d @ %11.2f\n", tso->id, usertime());
2515 globalParStats.tot_threads_created++;
2521 sched_belch("==__ schedule: Created TSO %d (%p);",
2522 CurrentProc, tso, tso->id));
2523 #elif defined(PARALLEL_HASKELL)
2524 IF_PAR_DEBUG(verbose,
2525 sched_belch("==__ schedule: Created TSO %d (%p); %d threads active",
2526 (long)tso->id, tso, advisory_thread_count));
2528 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
2529 (long)tso->id, (long)tso->stack_size));
2536 all parallel thread creation calls should fall through the following routine.
2539 createThreadFromSpark(rtsSpark spark)
2541 ASSERT(spark != (rtsSpark)NULL);
2542 // JB: TAKE CARE OF THIS COUNTER! BUGGY
2543 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads)
2545 barf("{createSparkThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
2546 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
2547 return END_TSO_QUEUE;
2551 tso = createThread(RtsFlags.GcFlags.initialStkSize);
2552 if (tso==END_TSO_QUEUE)
2553 barf("createSparkThread: Cannot create TSO");
2555 tso->priority = AdvisoryPriority;
2557 pushClosure(tso,spark);
2559 advisory_thread_count++; // JB: TAKE CARE OF THIS COUNTER! BUGGY
2566 Turn a spark into a thread.
2567 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
2571 activateSpark (rtsSpark spark)
2575 tso = createSparkThread(spark);
2576 if (RtsFlags.ParFlags.ParStats.Full) {
2577 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
2578 IF_PAR_DEBUG(verbose,
2579 debugBelch("==^^ activateSpark: turning spark of closure %p (%s) into a thread\n",
2580 (StgClosure *)spark, info_type((StgClosure *)spark)));
2582 // ToDo: fwd info on local/global spark to thread -- HWL
2583 // tso->gran.exported = spark->exported;
2584 // tso->gran.locked = !spark->global;
2585 // tso->gran.sparkname = spark->name;
2591 /* ---------------------------------------------------------------------------
2594 * scheduleThread puts a thread on the end of the runnable queue.
2595 * This will usually be done immediately after a thread is created.
2596 * The caller of scheduleThread must create the thread using e.g.
2597 * createThread and push an appropriate closure
2598 * on this thread's stack before the scheduler is invoked.
2599 * ------------------------------------------------------------------------ */
2602 scheduleThread(Capability *cap, StgTSO *tso)
2604 // The thread goes at the *end* of the run-queue, to avoid possible
2605 // starvation of any threads already on the queue.
2606 appendToRunQueue(cap,tso);
2610 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
2614 // We already created/initialised the Task
2615 task = cap->running_task;
2617 // This TSO is now a bound thread; make the Task and TSO
2618 // point to each other.
2623 task->stat = NoStatus;
2625 appendToRunQueue(cap,tso);
2627 IF_DEBUG(scheduler, sched_belch("new bound thread (%d)", tso->id));
2630 /* GranSim specific init */
2631 CurrentTSO = m->tso; // the TSO to run
2632 procStatus[MainProc] = Busy; // status of main PE
2633 CurrentProc = MainProc; // PE to run it on
2636 cap = schedule(cap,task);
2638 ASSERT(task->stat != NoStatus);
2639 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
2641 IF_DEBUG(scheduler, sched_belch("bound thread (%d) finished", task->tso->id));
2645 /* ----------------------------------------------------------------------------
2647 * ------------------------------------------------------------------------- */
2649 #if defined(THREADED_RTS)
2651 workerStart(Task *task)
2655 // See startWorkerTask().
2656 ACQUIRE_LOCK(&task->lock);
2658 RELEASE_LOCK(&task->lock);
2660 // set the thread-local pointer to the Task:
2663 // schedule() runs without a lock.
2664 cap = schedule(cap,task);
2666 // On exit from schedule(), we have a Capability.
2667 releaseCapability(cap);
2672 /* ---------------------------------------------------------------------------
2675 * Initialise the scheduler. This resets all the queues - if the
2676 * queues contained any threads, they'll be garbage collected at the
2679 * ------------------------------------------------------------------------ */
2686 for (i=0; i<=MAX_PROC; i++) {
2687 run_queue_hds[i] = END_TSO_QUEUE;
2688 run_queue_tls[i] = END_TSO_QUEUE;
2689 blocked_queue_hds[i] = END_TSO_QUEUE;
2690 blocked_queue_tls[i] = END_TSO_QUEUE;
2691 ccalling_threadss[i] = END_TSO_QUEUE;
2692 blackhole_queue[i] = END_TSO_QUEUE;
2693 sleeping_queue = END_TSO_QUEUE;
2695 #elif !defined(THREADED_RTS)
2696 blocked_queue_hd = END_TSO_QUEUE;
2697 blocked_queue_tl = END_TSO_QUEUE;
2698 sleeping_queue = END_TSO_QUEUE;
2701 blackhole_queue = END_TSO_QUEUE;
2702 all_threads = END_TSO_QUEUE;
2707 RtsFlags.ConcFlags.ctxtSwitchTicks =
2708 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
2710 #if defined(THREADED_RTS)
2711 /* Initialise the mutex and condition variables used by
2713 initMutex(&sched_mutex);
2716 ACQUIRE_LOCK(&sched_mutex);
2718 /* A capability holds the state a native thread needs in
2719 * order to execute STG code. At least one capability is
2720 * floating around (only THREADED_RTS builds have more than one).
2726 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
2730 #if defined(THREADED_RTS)
2732 * Eagerly start one worker to run each Capability, except for
2733 * Capability 0. The idea is that we're probably going to start a
2734 * bound thread on Capability 0 pretty soon, so we don't want a
2735 * worker task hogging it.
2740 for (i = 1; i < n_capabilities; i++) {
2741 cap = &capabilities[i];
2742 ACQUIRE_LOCK(&cap->lock);
2743 startWorkerTask(cap, workerStart);
2744 RELEASE_LOCK(&cap->lock);
2749 RELEASE_LOCK(&sched_mutex);
2753 exitScheduler( void )
2755 interrupted = rtsTrue;
2756 shutting_down_scheduler = rtsTrue;
2758 #if defined(THREADED_RTS)
2763 ACQUIRE_LOCK(&sched_mutex);
2764 task = newBoundTask();
2765 RELEASE_LOCK(&sched_mutex);
2767 for (i = 0; i < n_capabilities; i++) {
2768 shutdownCapability(&capabilities[i], task);
2770 boundTaskExiting(task);
2776 /* ---------------------------------------------------------------------------
2777 Where are the roots that we know about?
2779 - all the threads on the runnable queue
2780 - all the threads on the blocked queue
2781 - all the threads on the sleeping queue
2782 - all the thread currently executing a _ccall_GC
2783 - all the "main threads"
2785 ------------------------------------------------------------------------ */
2787 /* This has to be protected either by the scheduler monitor, or by the
2788 garbage collection monitor (probably the latter).
2793 GetRoots( evac_fn evac )
2800 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2801 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2802 evac((StgClosure **)&run_queue_hds[i]);
2803 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2804 evac((StgClosure **)&run_queue_tls[i]);
2806 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2807 evac((StgClosure **)&blocked_queue_hds[i]);
2808 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2809 evac((StgClosure **)&blocked_queue_tls[i]);
2810 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2811 evac((StgClosure **)&ccalling_threads[i]);
2818 for (i = 0; i < n_capabilities; i++) {
2819 cap = &capabilities[i];
2820 evac((StgClosure **)&cap->run_queue_hd);
2821 evac((StgClosure **)&cap->run_queue_tl);
2823 for (task = cap->suspended_ccalling_tasks; task != NULL;
2825 evac((StgClosure **)&task->suspended_tso);
2829 #if !defined(THREADED_RTS)
2830 evac((StgClosure **)(void *)&blocked_queue_hd);
2831 evac((StgClosure **)(void *)&blocked_queue_tl);
2832 evac((StgClosure **)(void *)&sleeping_queue);
2836 // evac((StgClosure **)&blackhole_queue);
2838 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL) || defined(GRAN)
2839 markSparkQueue(evac);
2842 #if defined(RTS_USER_SIGNALS)
2843 // mark the signal handlers (signals should be already blocked)
2844 markSignalHandlers(evac);
2848 /* -----------------------------------------------------------------------------
2851 This is the interface to the garbage collector from Haskell land.
2852 We provide this so that external C code can allocate and garbage
2853 collect when called from Haskell via _ccall_GC.
2855 It might be useful to provide an interface whereby the programmer
2856 can specify more roots (ToDo).
2858 This needs to be protected by the GC condition variable above. KH.
2859 -------------------------------------------------------------------------- */
2861 static void (*extra_roots)(evac_fn);
2864 performGC_(rtsBool force_major, void (*get_roots)(evac_fn))
2866 Task *task = myTask();
2869 ACQUIRE_LOCK(&sched_mutex);
2870 task = newBoundTask();
2871 RELEASE_LOCK(&sched_mutex);
2872 scheduleDoGC(NULL,task,force_major, get_roots);
2873 boundTaskExiting(task);
2875 scheduleDoGC(NULL,task,force_major, get_roots);
2882 performGC_(rtsFalse, GetRoots);
2886 performMajorGC(void)
2888 performGC_(rtsTrue, GetRoots);
2892 AllRoots(evac_fn evac)
2894 GetRoots(evac); // the scheduler's roots
2895 extra_roots(evac); // the user's roots
2899 performGCWithRoots(void (*get_roots)(evac_fn))
2901 extra_roots = get_roots;
2902 performGC_(rtsFalse, AllRoots);
2905 /* -----------------------------------------------------------------------------
2908 If the thread has reached its maximum stack size, then raise the
2909 StackOverflow exception in the offending thread. Otherwise
2910 relocate the TSO into a larger chunk of memory and adjust its stack
2912 -------------------------------------------------------------------------- */
2915 threadStackOverflow(Capability *cap, StgTSO *tso)
2917 nat new_stack_size, stack_words;
2922 IF_DEBUG(sanity,checkTSO(tso));
2923 if (tso->stack_size >= tso->max_stack_size) {
2926 debugBelch("@@ threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)\n",
2927 (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
2928 /* If we're debugging, just print out the top of the stack */
2929 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2932 /* Send this thread the StackOverflow exception */
2933 raiseAsync(cap, tso, (StgClosure *)stackOverflow_closure);
2937 /* Try to double the current stack size. If that takes us over the
2938 * maximum stack size for this thread, then use the maximum instead.
2939 * Finally round up so the TSO ends up as a whole number of blocks.
2941 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2942 new_tso_size = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2943 TSO_STRUCT_SIZE)/sizeof(W_);
2944 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2945 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2947 IF_DEBUG(scheduler, sched_belch("increasing stack size from %ld words to %d.\n", (long)tso->stack_size, new_stack_size));
2949 dest = (StgTSO *)allocate(new_tso_size);
2950 TICK_ALLOC_TSO(new_stack_size,0);
2952 /* copy the TSO block and the old stack into the new area */
2953 memcpy(dest,tso,TSO_STRUCT_SIZE);
2954 stack_words = tso->stack + tso->stack_size - tso->sp;
2955 new_sp = (P_)dest + new_tso_size - stack_words;
2956 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2958 /* relocate the stack pointers... */
2960 dest->stack_size = new_stack_size;
2962 /* Mark the old TSO as relocated. We have to check for relocated
2963 * TSOs in the garbage collector and any primops that deal with TSOs.
2965 * It's important to set the sp value to just beyond the end
2966 * of the stack, so we don't attempt to scavenge any part of the
2969 tso->what_next = ThreadRelocated;
2971 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2972 tso->why_blocked = NotBlocked;
2974 IF_PAR_DEBUG(verbose,
2975 debugBelch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld\n",
2976 tso->id, tso, tso->stack_size);
2977 /* If we're debugging, just print out the top of the stack */
2978 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2981 IF_DEBUG(sanity,checkTSO(tso));
2983 IF_DEBUG(scheduler,printTSO(dest));
2989 /* ---------------------------------------------------------------------------
2990 Wake up a queue that was blocked on some resource.
2991 ------------------------------------------------------------------------ */
2995 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2998 #elif defined(PARALLEL_HASKELL)
3000 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
3002 /* write RESUME events to log file and
3003 update blocked and fetch time (depending on type of the orig closure) */
3004 if (RtsFlags.ParFlags.ParStats.Full) {
3005 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
3006 GR_RESUMEQ, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
3007 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
3008 if (emptyRunQueue())
3009 emitSchedule = rtsTrue;
3011 switch (get_itbl(node)->type) {
3013 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
3018 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
3025 barf("{unblockOne}Daq Qagh: unexpected closure in blocking queue");
3032 StgBlockingQueueElement *
3033 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
3036 PEs node_loc, tso_loc;
3038 node_loc = where_is(node); // should be lifted out of loop
3039 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
3040 tso_loc = where_is((StgClosure *)tso);
3041 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
3042 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
3043 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
3044 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
3045 // insertThread(tso, node_loc);
3046 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
3048 tso, node, (rtsSpark*)NULL);
3049 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
3052 } else { // TSO is remote (actually should be FMBQ)
3053 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
3054 RtsFlags.GranFlags.Costs.gunblocktime +
3055 RtsFlags.GranFlags.Costs.latency;
3056 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
3058 tso, node, (rtsSpark*)NULL);
3059 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
3062 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
3064 debugBelch(" %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
3065 (node_loc==tso_loc ? "Local" : "Global"),
3066 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
3067 tso->block_info.closure = NULL;
3068 IF_DEBUG(scheduler,debugBelch("-- Waking up thread %ld (%p)\n",
3071 #elif defined(PARALLEL_HASKELL)
3072 StgBlockingQueueElement *
3073 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
3075 StgBlockingQueueElement *next;
3077 switch (get_itbl(bqe)->type) {
3079 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
3080 /* if it's a TSO just push it onto the run_queue */
3082 ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
3083 APPEND_TO_RUN_QUEUE((StgTSO *)bqe);
3085 unblockCount(bqe, node);
3086 /* reset blocking status after dumping event */
3087 ((StgTSO *)bqe)->why_blocked = NotBlocked;
3091 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
3093 bqe->link = (StgBlockingQueueElement *)PendingFetches;
3094 PendingFetches = (StgBlockedFetch *)bqe;
3098 /* can ignore this case in a non-debugging setup;
3099 see comments on RBHSave closures above */
3101 /* check that the closure is an RBHSave closure */
3102 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
3103 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
3104 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
3108 barf("{unblockOne}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
3109 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
3113 IF_PAR_DEBUG(bq, debugBelch(", %p (%s)\n", bqe, info_type((StgClosure*)bqe)));
3119 unblockOne(Capability *cap, StgTSO *tso)
3123 ASSERT(get_itbl(tso)->type == TSO);
3124 ASSERT(tso->why_blocked != NotBlocked);
3125 tso->why_blocked = NotBlocked;
3127 tso->link = END_TSO_QUEUE;
3129 // We might have just migrated this TSO to our Capability:
3131 tso->bound->cap = cap;
3134 appendToRunQueue(cap,tso);
3136 // we're holding a newly woken thread, make sure we context switch
3137 // quickly so we can migrate it if necessary.
3139 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", (long)tso->id));
3146 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
3148 StgBlockingQueueElement *bqe;
3153 debugBelch("##-_ AwBQ for node %p on PE %d @ %ld by TSO %d (%p): \n", \
3154 node, CurrentProc, CurrentTime[CurrentProc],
3155 CurrentTSO->id, CurrentTSO));
3157 node_loc = where_is(node);
3159 ASSERT(q == END_BQ_QUEUE ||
3160 get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
3161 get_itbl(q)->type == CONSTR); // closure (type constructor)
3162 ASSERT(is_unique(node));
3164 /* FAKE FETCH: magically copy the node to the tso's proc;
3165 no Fetch necessary because in reality the node should not have been
3166 moved to the other PE in the first place
3168 if (CurrentProc!=node_loc) {
3170 debugBelch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)\n",
3171 node, node_loc, CurrentProc, CurrentTSO->id,
3172 // CurrentTSO, where_is(CurrentTSO),
3173 node->header.gran.procs));
3174 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
3176 debugBelch("## new bitmask of node %p is %#x\n",
3177 node, node->header.gran.procs));
3178 if (RtsFlags.GranFlags.GranSimStats.Global) {
3179 globalGranStats.tot_fake_fetches++;
3184 // ToDo: check: ASSERT(CurrentProc==node_loc);
3185 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
3188 bqe points to the current element in the queue
3189 next points to the next element in the queue
3191 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
3192 //tso_loc = where_is(tso);
3194 bqe = unblockOne(bqe, node);
3197 /* if this is the BQ of an RBH, we have to put back the info ripped out of
3198 the closure to make room for the anchor of the BQ */
3199 if (bqe!=END_BQ_QUEUE) {
3200 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
3202 ASSERT((info_ptr==&RBH_Save_0_info) ||
3203 (info_ptr==&RBH_Save_1_info) ||
3204 (info_ptr==&RBH_Save_2_info));
3206 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
3207 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
3208 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
3211 debugBelch("## Filled in RBH_Save for %p (%s) at end of AwBQ\n",
3212 node, info_type(node)));
3215 /* statistics gathering */
3216 if (RtsFlags.GranFlags.GranSimStats.Global) {
3217 // globalGranStats.tot_bq_processing_time += bq_processing_time;
3218 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
3219 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
3220 globalGranStats.tot_awbq++; // total no. of bqs awakened
3223 debugBelch("## BQ Stats of %p: [%d entries] %s\n",
3224 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
3226 #elif defined(PARALLEL_HASKELL)
3228 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
3230 StgBlockingQueueElement *bqe;
3232 IF_PAR_DEBUG(verbose,
3233 debugBelch("##-_ AwBQ for node %p on [%x]: \n",
3237 if(get_itbl(q)->type == CONSTR || q==END_BQ_QUEUE) {
3238 IF_PAR_DEBUG(verbose, debugBelch("## ... nothing to unblock so lets just return. RFP (BUG?)\n"));
3243 ASSERT(q == END_BQ_QUEUE ||
3244 get_itbl(q)->type == TSO ||
3245 get_itbl(q)->type == BLOCKED_FETCH ||
3246 get_itbl(q)->type == CONSTR);
3249 while (get_itbl(bqe)->type==TSO ||
3250 get_itbl(bqe)->type==BLOCKED_FETCH) {
3251 bqe = unblockOne(bqe, node);
3255 #else /* !GRAN && !PARALLEL_HASKELL */
3258 awakenBlockedQueue(Capability *cap, StgTSO *tso)
3260 if (tso == NULL) return; // hack; see bug #1235728, and comments in
3262 while (tso != END_TSO_QUEUE) {
3263 tso = unblockOne(cap,tso);
3268 /* ---------------------------------------------------------------------------
3270 - usually called inside a signal handler so it mustn't do anything fancy.
3271 ------------------------------------------------------------------------ */
3274 interruptStgRts(void)
3278 #if defined(THREADED_RTS)
3279 prodAllCapabilities();
3283 /* -----------------------------------------------------------------------------
3286 This is for use when we raise an exception in another thread, which
3288 This has nothing to do with the UnblockThread event in GranSim. -- HWL
3289 -------------------------------------------------------------------------- */
3291 #if defined(GRAN) || defined(PARALLEL_HASKELL)
3293 NB: only the type of the blocking queue is different in GranSim and GUM
3294 the operations on the queue-elements are the same
3295 long live polymorphism!
3297 Locks: sched_mutex is held upon entry and exit.
3301 unblockThread(Capability *cap, StgTSO *tso)
3303 StgBlockingQueueElement *t, **last;
3305 switch (tso->why_blocked) {
3308 return; /* not blocked */
3311 // Be careful: nothing to do here! We tell the scheduler that the thread
3312 // is runnable and we leave it to the stack-walking code to abort the
3313 // transaction while unwinding the stack. We should perhaps have a debugging
3314 // test to make sure that this really happens and that the 'zombie' transaction
3315 // does not get committed.
3319 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3321 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
3322 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3324 last = (StgBlockingQueueElement **)&mvar->head;
3325 for (t = (StgBlockingQueueElement *)mvar->head;
3327 last = &t->link, last_tso = t, t = t->link) {
3328 if (t == (StgBlockingQueueElement *)tso) {
3329 *last = (StgBlockingQueueElement *)tso->link;
3330 if (mvar->tail == tso) {
3331 mvar->tail = (StgTSO *)last_tso;
3336 barf("unblockThread (MVAR): TSO not found");
3339 case BlockedOnBlackHole:
3340 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3342 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3344 last = &bq->blocking_queue;
3345 for (t = bq->blocking_queue;
3347 last = &t->link, t = t->link) {
3348 if (t == (StgBlockingQueueElement *)tso) {
3349 *last = (StgBlockingQueueElement *)tso->link;
3353 barf("unblockThread (BLACKHOLE): TSO not found");
3356 case BlockedOnException:
3358 StgTSO *target = tso->block_info.tso;
3360 ASSERT(get_itbl(target)->type == TSO);
3362 if (target->what_next == ThreadRelocated) {
3363 target = target->link;
3364 ASSERT(get_itbl(target)->type == TSO);
3367 ASSERT(target->blocked_exceptions != NULL);
3369 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
3370 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
3372 last = &t->link, t = t->link) {
3373 ASSERT(get_itbl(t)->type == TSO);
3374 if (t == (StgBlockingQueueElement *)tso) {
3375 *last = (StgBlockingQueueElement *)tso->link;
3379 barf("unblockThread (Exception): TSO not found");
3383 case BlockedOnWrite:
3384 #if defined(mingw32_HOST_OS)
3385 case BlockedOnDoProc:
3388 /* take TSO off blocked_queue */
3389 StgBlockingQueueElement *prev = NULL;
3390 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
3391 prev = t, t = t->link) {
3392 if (t == (StgBlockingQueueElement *)tso) {
3394 blocked_queue_hd = (StgTSO *)t->link;
3395 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3396 blocked_queue_tl = END_TSO_QUEUE;
3399 prev->link = t->link;
3400 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3401 blocked_queue_tl = (StgTSO *)prev;
3404 #if defined(mingw32_HOST_OS)
3405 /* (Cooperatively) signal that the worker thread should abort
3408 abandonWorkRequest(tso->block_info.async_result->reqID);
3413 barf("unblockThread (I/O): TSO not found");
3416 case BlockedOnDelay:
3418 /* take TSO off sleeping_queue */
3419 StgBlockingQueueElement *prev = NULL;
3420 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
3421 prev = t, t = t->link) {
3422 if (t == (StgBlockingQueueElement *)tso) {
3424 sleeping_queue = (StgTSO *)t->link;
3426 prev->link = t->link;
3431 barf("unblockThread (delay): TSO not found");
3435 barf("unblockThread");
3439 tso->link = END_TSO_QUEUE;
3440 tso->why_blocked = NotBlocked;
3441 tso->block_info.closure = NULL;
3442 pushOnRunQueue(cap,tso);
3446 unblockThread(Capability *cap, StgTSO *tso)
3450 /* To avoid locking unnecessarily. */
3451 if (tso->why_blocked == NotBlocked) {
3455 switch (tso->why_blocked) {
3458 // Be careful: nothing to do here! We tell the scheduler that the thread
3459 // is runnable and we leave it to the stack-walking code to abort the
3460 // transaction while unwinding the stack. We should perhaps have a debugging
3461 // test to make sure that this really happens and that the 'zombie' transaction
3462 // does not get committed.
3466 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3468 StgTSO *last_tso = END_TSO_QUEUE;
3469 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3472 for (t = mvar->head; t != END_TSO_QUEUE;
3473 last = &t->link, last_tso = t, t = t->link) {
3476 if (mvar->tail == tso) {
3477 mvar->tail = last_tso;
3482 barf("unblockThread (MVAR): TSO not found");
3485 case BlockedOnBlackHole:
3487 last = &blackhole_queue;
3488 for (t = blackhole_queue; t != END_TSO_QUEUE;
3489 last = &t->link, t = t->link) {
3495 barf("unblockThread (BLACKHOLE): TSO not found");
3498 case BlockedOnException:
3500 StgTSO *target = tso->block_info.tso;
3502 ASSERT(get_itbl(target)->type == TSO);
3504 while (target->what_next == ThreadRelocated) {
3505 target = target->link;
3506 ASSERT(get_itbl(target)->type == TSO);
3509 ASSERT(target->blocked_exceptions != NULL);
3511 last = &target->blocked_exceptions;
3512 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
3513 last = &t->link, t = t->link) {
3514 ASSERT(get_itbl(t)->type == TSO);
3520 barf("unblockThread (Exception): TSO not found");
3523 #if !defined(THREADED_RTS)
3525 case BlockedOnWrite:
3526 #if defined(mingw32_HOST_OS)
3527 case BlockedOnDoProc:
3530 StgTSO *prev = NULL;
3531 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
3532 prev = t, t = t->link) {
3535 blocked_queue_hd = t->link;
3536 if (blocked_queue_tl == t) {
3537 blocked_queue_tl = END_TSO_QUEUE;
3540 prev->link = t->link;
3541 if (blocked_queue_tl == t) {
3542 blocked_queue_tl = prev;
3545 #if defined(mingw32_HOST_OS)
3546 /* (Cooperatively) signal that the worker thread should abort
3549 abandonWorkRequest(tso->block_info.async_result->reqID);
3554 barf("unblockThread (I/O): TSO not found");
3557 case BlockedOnDelay:
3559 StgTSO *prev = NULL;
3560 for (t = sleeping_queue; t != END_TSO_QUEUE;
3561 prev = t, t = t->link) {
3564 sleeping_queue = t->link;
3566 prev->link = t->link;
3571 barf("unblockThread (delay): TSO not found");
3576 barf("unblockThread");
3580 tso->link = END_TSO_QUEUE;
3581 tso->why_blocked = NotBlocked;
3582 tso->block_info.closure = NULL;
3583 appendToRunQueue(cap,tso);
3587 /* -----------------------------------------------------------------------------
3590 * Check the blackhole_queue for threads that can be woken up. We do
3591 * this periodically: before every GC, and whenever the run queue is
3594 * An elegant solution might be to just wake up all the blocked
3595 * threads with awakenBlockedQueue occasionally: they'll go back to
3596 * sleep again if the object is still a BLACKHOLE. Unfortunately this
3597 * doesn't give us a way to tell whether we've actually managed to
3598 * wake up any threads, so we would be busy-waiting.
3600 * -------------------------------------------------------------------------- */
3603 checkBlackHoles (Capability *cap)
3606 rtsBool any_woke_up = rtsFalse;
3609 // blackhole_queue is global:
3610 ASSERT_LOCK_HELD(&sched_mutex);
3612 IF_DEBUG(scheduler, sched_belch("checking threads blocked on black holes"));
3614 // ASSUMES: sched_mutex
3615 prev = &blackhole_queue;
3616 t = blackhole_queue;
3617 while (t != END_TSO_QUEUE) {
3618 ASSERT(t->why_blocked == BlockedOnBlackHole);
3619 type = get_itbl(t->block_info.closure)->type;
3620 if (type != BLACKHOLE && type != CAF_BLACKHOLE) {
3621 IF_DEBUG(sanity,checkTSO(t));
3622 t = unblockOne(cap, t);
3623 // urk, the threads migrate to the current capability
3624 // here, but we'd like to keep them on the original one.
3626 any_woke_up = rtsTrue;
3636 /* -----------------------------------------------------------------------------
3639 * The following function implements the magic for raising an
3640 * asynchronous exception in an existing thread.
3642 * We first remove the thread from any queue on which it might be
3643 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
3645 * We strip the stack down to the innermost CATCH_FRAME, building
3646 * thunks in the heap for all the active computations, so they can
3647 * be restarted if necessary. When we reach a CATCH_FRAME, we build
3648 * an application of the handler to the exception, and push it on
3649 * the top of the stack.
3651 * How exactly do we save all the active computations? We create an
3652 * AP_STACK for every UpdateFrame on the stack. Entering one of these
3653 * AP_STACKs pushes everything from the corresponding update frame
3654 * upwards onto the stack. (Actually, it pushes everything up to the
3655 * next update frame plus a pointer to the next AP_STACK object.
3656 * Entering the next AP_STACK object pushes more onto the stack until we
3657 * reach the last AP_STACK object - at which point the stack should look
3658 * exactly as it did when we killed the TSO and we can continue
3659 * execution by entering the closure on top of the stack.
3661 * We can also kill a thread entirely - this happens if either (a) the
3662 * exception passed to raiseAsync is NULL, or (b) there's no
3663 * CATCH_FRAME on the stack. In either case, we strip the entire
3664 * stack and replace the thread with a zombie.
3666 * ToDo: in THREADED_RTS mode, this function is only safe if either
3667 * (a) we hold all the Capabilities (eg. in GC, or if there is only
3668 * one Capability), or (b) we own the Capability that the TSO is
3669 * currently blocked on or on the run queue of.
3671 * -------------------------------------------------------------------------- */
3674 raiseAsync(Capability *cap, StgTSO *tso, StgClosure *exception)
3676 raiseAsync_(cap, tso, exception, rtsFalse, NULL);
3680 suspendComputation(Capability *cap, StgTSO *tso, StgPtr stop_here)
3682 raiseAsync_(cap, tso, NULL, rtsFalse, stop_here);
3686 raiseAsync_(Capability *cap, StgTSO *tso, StgClosure *exception,
3687 rtsBool stop_at_atomically, StgPtr stop_here)
3689 StgRetInfoTable *info;
3693 // Thread already dead?
3694 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3699 sched_belch("raising exception in thread %ld.", (long)tso->id));
3701 // Remove it from any blocking queues
3702 unblockThread(cap,tso);
3704 // mark it dirty; we're about to change its stack.
3709 // The stack freezing code assumes there's a closure pointer on
3710 // the top of the stack, so we have to arrange that this is the case...
3712 if (sp[0] == (W_)&stg_enter_info) {
3716 sp[0] = (W_)&stg_dummy_ret_closure;
3720 while (stop_here == NULL || frame < stop_here) {
3722 // 1. Let the top of the stack be the "current closure"
3724 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
3727 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
3728 // current closure applied to the chunk of stack up to (but not
3729 // including) the update frame. This closure becomes the "current
3730 // closure". Go back to step 2.
3732 // 4. If it's a CATCH_FRAME, then leave the exception handler on
3733 // top of the stack applied to the exception.
3735 // 5. If it's a STOP_FRAME, then kill the thread.
3737 // NB: if we pass an ATOMICALLY_FRAME then abort the associated
3740 info = get_ret_itbl((StgClosure *)frame);
3742 switch (info->i.type) {
3749 // First build an AP_STACK consisting of the stack chunk above the
3750 // current update frame, with the top word on the stack as the
3753 words = frame - sp - 1;
3754 ap = (StgAP_STACK *)allocateLocal(cap,AP_STACK_sizeW(words));
3757 ap->fun = (StgClosure *)sp[0];
3759 for(i=0; i < (nat)words; ++i) {
3760 ap->payload[i] = (StgClosure *)*sp++;
3763 SET_HDR(ap,&stg_AP_STACK_info,
3764 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
3765 TICK_ALLOC_UP_THK(words+1,0);
3768 debugBelch("sched: Updating ");
3769 printPtr((P_)((StgUpdateFrame *)frame)->updatee);
3770 debugBelch(" with ");
3771 printObj((StgClosure *)ap);
3774 // Replace the updatee with an indirection
3776 // Warning: if we're in a loop, more than one update frame on
3777 // the stack may point to the same object. Be careful not to
3778 // overwrite an IND_OLDGEN in this case, because we'll screw
3779 // up the mutable lists. To be on the safe side, don't
3780 // overwrite any kind of indirection at all. See also
3781 // threadSqueezeStack in GC.c, where we have to make a similar
3784 if (!closure_IND(((StgUpdateFrame *)frame)->updatee)) {
3785 // revert the black hole
3786 UPD_IND_NOLOCK(((StgUpdateFrame *)frame)->updatee,
3789 sp += sizeofW(StgUpdateFrame) - 1;
3790 sp[0] = (W_)ap; // push onto stack
3792 continue; //no need to bump frame
3796 // We've stripped the entire stack, the thread is now dead.
3797 tso->what_next = ThreadKilled;
3798 tso->sp = frame + sizeofW(StgStopFrame);
3802 // If we find a CATCH_FRAME, and we've got an exception to raise,
3803 // then build the THUNK raise(exception), and leave it on
3804 // top of the CATCH_FRAME ready to enter.
3808 StgCatchFrame *cf = (StgCatchFrame *)frame;
3812 if (exception == NULL) break;
3814 // we've got an exception to raise, so let's pass it to the
3815 // handler in this frame.
3817 raise = (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
3818 TICK_ALLOC_SE_THK(1,0);
3819 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
3820 raise->payload[0] = exception;
3822 // throw away the stack from Sp up to the CATCH_FRAME.
3826 /* Ensure that async excpetions are blocked now, so we don't get
3827 * a surprise exception before we get around to executing the
3830 if (tso->blocked_exceptions == NULL) {
3831 tso->blocked_exceptions = END_TSO_QUEUE;
3834 /* Put the newly-built THUNK on top of the stack, ready to execute
3835 * when the thread restarts.
3838 sp[-1] = (W_)&stg_enter_info;
3840 tso->what_next = ThreadRunGHC;
3841 IF_DEBUG(sanity, checkTSO(tso));
3845 case ATOMICALLY_FRAME:
3846 if (stop_at_atomically) {
3847 ASSERT(stmGetEnclosingTRec(tso->trec) == NO_TREC);
3848 stmCondemnTransaction(cap, tso -> trec);
3852 // R1 is not a register: the return convention for IO in
3853 // this case puts the return value on the stack, so we
3854 // need to set up the stack to return to the atomically
3855 // frame properly...
3856 tso->sp = frame - 2;
3857 tso->sp[1] = (StgWord) &stg_NO_FINALIZER_closure; // why not?
3858 tso->sp[0] = (StgWord) &stg_ut_1_0_unreg_info;
3860 tso->what_next = ThreadRunGHC;
3863 // Not stop_at_atomically... fall through and abort the
3866 case CATCH_RETRY_FRAME:
3867 // IF we find an ATOMICALLY_FRAME then we abort the
3868 // current transaction and propagate the exception. In
3869 // this case (unlike ordinary exceptions) we do not care
3870 // whether the transaction is valid or not because its
3871 // possible validity cannot have caused the exception
3872 // and will not be visible after the abort.
3874 debugBelch("Found atomically block delivering async exception\n"));
3875 StgTRecHeader *trec = tso -> trec;
3876 StgTRecHeader *outer = stmGetEnclosingTRec(trec);
3877 stmAbortTransaction(cap, trec);
3878 tso -> trec = outer;
3885 // move on to the next stack frame
3886 frame += stack_frame_sizeW((StgClosure *)frame);
3889 // if we got here, then we stopped at stop_here
3890 ASSERT(stop_here != NULL);
3893 /* -----------------------------------------------------------------------------
3896 This is used for interruption (^C) and forking, and corresponds to
3897 raising an exception but without letting the thread catch the
3899 -------------------------------------------------------------------------- */
3902 deleteThread (Capability *cap, StgTSO *tso)
3904 if (tso->why_blocked != BlockedOnCCall &&
3905 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
3906 raiseAsync(cap,tso,NULL);
3910 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
3912 deleteThreadImmediately(Capability *cap, StgTSO *tso)
3913 { // for forkProcess only:
3914 // delete thread without giving it a chance to catch the KillThread exception
3916 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3920 if (tso->why_blocked != BlockedOnCCall &&
3921 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
3922 unblockThread(cap,tso);
3925 tso->what_next = ThreadKilled;
3929 /* -----------------------------------------------------------------------------
3930 raiseExceptionHelper
3932 This function is called by the raise# primitve, just so that we can
3933 move some of the tricky bits of raising an exception from C-- into
3934 C. Who knows, it might be a useful re-useable thing here too.
3935 -------------------------------------------------------------------------- */
3938 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
3940 Capability *cap = regTableToCapability(reg);
3941 StgThunk *raise_closure = NULL;
3943 StgRetInfoTable *info;
3945 // This closure represents the expression 'raise# E' where E
3946 // is the exception raise. It is used to overwrite all the
3947 // thunks which are currently under evaluataion.
3950 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
3951 // LDV profiling: stg_raise_info has THUNK as its closure
3952 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
3953 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
3954 // 1 does not cause any problem unless profiling is performed.
3955 // However, when LDV profiling goes on, we need to linearly scan
3956 // small object pool, where raise_closure is stored, so we should
3957 // use MIN_UPD_SIZE.
3959 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
3960 // sizeofW(StgClosure)+1);
3964 // Walk up the stack, looking for the catch frame. On the way,
3965 // we update any closures pointed to from update frames with the
3966 // raise closure that we just built.
3970 info = get_ret_itbl((StgClosure *)p);
3971 next = p + stack_frame_sizeW((StgClosure *)p);
3972 switch (info->i.type) {
3975 // Only create raise_closure if we need to.
3976 if (raise_closure == NULL) {
3978 (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
3979 SET_HDR(raise_closure, &stg_raise_info, CCCS);
3980 raise_closure->payload[0] = exception;
3982 UPD_IND(((StgUpdateFrame *)p)->updatee,(StgClosure *)raise_closure);
3986 case ATOMICALLY_FRAME:
3987 IF_DEBUG(stm, debugBelch("Found ATOMICALLY_FRAME at %p\n", p));
3989 return ATOMICALLY_FRAME;
3995 case CATCH_STM_FRAME:
3996 IF_DEBUG(stm, debugBelch("Found CATCH_STM_FRAME at %p\n", p));
3998 return CATCH_STM_FRAME;
4004 case CATCH_RETRY_FRAME:
4013 /* -----------------------------------------------------------------------------
4014 findRetryFrameHelper
4016 This function is called by the retry# primitive. It traverses the stack
4017 leaving tso->sp referring to the frame which should handle the retry.
4019 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
4020 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
4022 We skip CATCH_STM_FRAMEs because retries are not considered to be exceptions,
4023 despite the similar implementation.
4025 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
4026 not be created within memory transactions.
4027 -------------------------------------------------------------------------- */
4030 findRetryFrameHelper (StgTSO *tso)
4033 StgRetInfoTable *info;
4037 info = get_ret_itbl((StgClosure *)p);
4038 next = p + stack_frame_sizeW((StgClosure *)p);
4039 switch (info->i.type) {
4041 case ATOMICALLY_FRAME:
4042 IF_DEBUG(stm, debugBelch("Found ATOMICALLY_FRAME at %p during retrry\n", p));
4044 return ATOMICALLY_FRAME;
4046 case CATCH_RETRY_FRAME:
4047 IF_DEBUG(stm, debugBelch("Found CATCH_RETRY_FRAME at %p during retrry\n", p));
4049 return CATCH_RETRY_FRAME;
4051 case CATCH_STM_FRAME:
4053 ASSERT(info->i.type != CATCH_FRAME);
4054 ASSERT(info->i.type != STOP_FRAME);
4061 /* -----------------------------------------------------------------------------
4062 resurrectThreads is called after garbage collection on the list of
4063 threads found to be garbage. Each of these threads will be woken
4064 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
4065 on an MVar, or NonTermination if the thread was blocked on a Black
4068 Locks: assumes we hold *all* the capabilities.
4069 -------------------------------------------------------------------------- */
4072 resurrectThreads (StgTSO *threads)
4077 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
4078 next = tso->global_link;
4079 tso->global_link = all_threads;
4081 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
4083 // Wake up the thread on the Capability it was last on for a
4084 // bound thread, or last_free_capability otherwise.
4086 cap = tso->bound->cap;
4088 cap = last_free_capability;
4091 switch (tso->why_blocked) {
4093 case BlockedOnException:
4094 /* Called by GC - sched_mutex lock is currently held. */
4095 raiseAsync(cap, tso,(StgClosure *)BlockedOnDeadMVar_closure);
4097 case BlockedOnBlackHole:
4098 raiseAsync(cap, tso,(StgClosure *)NonTermination_closure);
4101 raiseAsync(cap, tso,(StgClosure *)BlockedIndefinitely_closure);
4104 /* This might happen if the thread was blocked on a black hole
4105 * belonging to a thread that we've just woken up (raiseAsync
4106 * can wake up threads, remember...).
4110 barf("resurrectThreads: thread blocked in a strange way");
4115 /* ----------------------------------------------------------------------------
4116 * Debugging: why is a thread blocked
4117 * [Also provides useful information when debugging threaded programs
4118 * at the Haskell source code level, so enable outside of DEBUG. --sof 7/02]
4119 ------------------------------------------------------------------------- */
4123 printThreadBlockage(StgTSO *tso)
4125 switch (tso->why_blocked) {
4127 debugBelch("is blocked on read from fd %d", (int)(tso->block_info.fd));
4129 case BlockedOnWrite:
4130 debugBelch("is blocked on write to fd %d", (int)(tso->block_info.fd));
4132 #if defined(mingw32_HOST_OS)
4133 case BlockedOnDoProc:
4134 debugBelch("is blocked on proc (request: %ld)", tso->block_info.async_result->reqID);
4137 case BlockedOnDelay:
4138 debugBelch("is blocked until %ld", (long)(tso->block_info.target));
4141 debugBelch("is blocked on an MVar @ %p", tso->block_info.closure);
4143 case BlockedOnException:
4144 debugBelch("is blocked on delivering an exception to thread %d",
4145 tso->block_info.tso->id);
4147 case BlockedOnBlackHole:
4148 debugBelch("is blocked on a black hole");
4151 debugBelch("is not blocked");
4153 #if defined(PARALLEL_HASKELL)
4155 debugBelch("is blocked on global address; local FM_BQ is %p (%s)",
4156 tso->block_info.closure, info_type(tso->block_info.closure));
4158 case BlockedOnGA_NoSend:
4159 debugBelch("is blocked on global address (no send); local FM_BQ is %p (%s)",
4160 tso->block_info.closure, info_type(tso->block_info.closure));
4163 case BlockedOnCCall:
4164 debugBelch("is blocked on an external call");
4166 case BlockedOnCCall_NoUnblockExc:
4167 debugBelch("is blocked on an external call (exceptions were already blocked)");
4170 debugBelch("is blocked on an STM operation");
4173 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
4174 tso->why_blocked, tso->id, tso);
4179 printThreadStatus(StgTSO *t)
4181 debugBelch("\tthread %4d @ %p ", t->id, (void *)t);
4183 void *label = lookupThreadLabel(t->id);
4184 if (label) debugBelch("[\"%s\"] ",(char *)label);
4186 if (t->what_next == ThreadRelocated) {
4187 debugBelch("has been relocated...\n");
4189 switch (t->what_next) {
4191 debugBelch("has been killed");
4193 case ThreadComplete:
4194 debugBelch("has completed");
4197 printThreadBlockage(t);
4204 printAllThreads(void)
4211 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
4212 ullong_format_string(TIME_ON_PROC(CurrentProc),
4213 time_string, rtsFalse/*no commas!*/);
4215 debugBelch("all threads at [%s]:\n", time_string);
4216 # elif defined(PARALLEL_HASKELL)
4217 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
4218 ullong_format_string(CURRENT_TIME,
4219 time_string, rtsFalse/*no commas!*/);
4221 debugBelch("all threads at [%s]:\n", time_string);
4223 debugBelch("all threads:\n");
4226 for (i = 0; i < n_capabilities; i++) {
4227 cap = &capabilities[i];
4228 debugBelch("threads on capability %d:\n", cap->no);
4229 for (t = cap->run_queue_hd; t != END_TSO_QUEUE; t = t->link) {
4230 printThreadStatus(t);
4234 debugBelch("other threads:\n");
4235 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
4236 if (t->why_blocked != NotBlocked) {
4237 printThreadStatus(t);
4239 if (t->what_next == ThreadRelocated) {
4242 next = t->global_link;
4249 printThreadQueue(StgTSO *t)
4252 for (; t != END_TSO_QUEUE; t = t->link) {
4253 printThreadStatus(t);
4256 debugBelch("%d threads on queue\n", i);
4260 Print a whole blocking queue attached to node (debugging only).
4262 # if defined(PARALLEL_HASKELL)
4264 print_bq (StgClosure *node)
4266 StgBlockingQueueElement *bqe;
4270 debugBelch("## BQ of closure %p (%s): ",
4271 node, info_type(node));
4273 /* should cover all closures that may have a blocking queue */
4274 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
4275 get_itbl(node)->type == FETCH_ME_BQ ||
4276 get_itbl(node)->type == RBH ||
4277 get_itbl(node)->type == MVAR);
4279 ASSERT(node!=(StgClosure*)NULL); // sanity check
4281 print_bqe(((StgBlockingQueue*)node)->blocking_queue);
4285 Print a whole blocking queue starting with the element bqe.
4288 print_bqe (StgBlockingQueueElement *bqe)
4293 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
4295 for (end = (bqe==END_BQ_QUEUE);
4296 !end; // iterate until bqe points to a CONSTR
4297 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE),
4298 bqe = end ? END_BQ_QUEUE : bqe->link) {
4299 ASSERT(bqe != END_BQ_QUEUE); // sanity check
4300 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
4301 /* types of closures that may appear in a blocking queue */
4302 ASSERT(get_itbl(bqe)->type == TSO ||
4303 get_itbl(bqe)->type == BLOCKED_FETCH ||
4304 get_itbl(bqe)->type == CONSTR);
4305 /* only BQs of an RBH end with an RBH_Save closure */
4306 //ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
4308 switch (get_itbl(bqe)->type) {
4310 debugBelch(" TSO %u (%x),",
4311 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
4314 debugBelch(" BF (node=%p, ga=((%x, %d, %x)),",
4315 ((StgBlockedFetch *)bqe)->node,
4316 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
4317 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
4318 ((StgBlockedFetch *)bqe)->ga.weight);
4321 debugBelch(" %s (IP %p),",
4322 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
4323 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
4324 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
4325 "RBH_Save_?"), get_itbl(bqe));
4328 barf("Unexpected closure type %s in blocking queue", // of %p (%s)",
4329 info_type((StgClosure *)bqe)); // , node, info_type(node));
4335 # elif defined(GRAN)
4337 print_bq (StgClosure *node)
4339 StgBlockingQueueElement *bqe;
4340 PEs node_loc, tso_loc;
4343 /* should cover all closures that may have a blocking queue */
4344 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
4345 get_itbl(node)->type == FETCH_ME_BQ ||
4346 get_itbl(node)->type == RBH);
4348 ASSERT(node!=(StgClosure*)NULL); // sanity check
4349 node_loc = where_is(node);
4351 debugBelch("## BQ of closure %p (%s) on [PE %d]: ",
4352 node, info_type(node), node_loc);
4355 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
4357 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
4358 !end; // iterate until bqe points to a CONSTR
4359 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
4360 ASSERT(bqe != END_BQ_QUEUE); // sanity check
4361 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
4362 /* types of closures that may appear in a blocking queue */
4363 ASSERT(get_itbl(bqe)->type == TSO ||
4364 get_itbl(bqe)->type == CONSTR);
4365 /* only BQs of an RBH end with an RBH_Save closure */
4366 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
4368 tso_loc = where_is((StgClosure *)bqe);
4369 switch (get_itbl(bqe)->type) {
4371 debugBelch(" TSO %d (%p) on [PE %d],",
4372 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
4375 debugBelch(" %s (IP %p),",
4376 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
4377 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
4378 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
4379 "RBH_Save_?"), get_itbl(bqe));
4382 barf("Unexpected closure type %s in blocking queue of %p (%s)",
4383 info_type((StgClosure *)bqe), node, info_type(node));
4391 #if defined(PARALLEL_HASKELL)
4398 for (i=0, tso=run_queue_hd;
4399 tso != END_TSO_QUEUE;
4400 i++, tso=tso->link) {
4409 sched_belch(char *s, ...)
4414 debugBelch("sched (task %p): ", (void *)(unsigned long)(unsigned int)osThreadId());
4415 #elif defined(PARALLEL_HASKELL)
4418 debugBelch("sched: ");