1 /* ---------------------------------------------------------------------------
2 * $Id: Schedule.c,v 1.193 2004/03/01 14:18:35 simonmar Exp $
4 * (c) The GHC Team, 1998-2003
8 * Different GHC ways use this scheduler quite differently (see comments below)
9 * Here is the global picture:
11 * WAY Name CPP flag What's it for
12 * --------------------------------------
13 * mp GUM PAR Parallel execution on a distrib. memory machine
14 * s SMP SMP Parallel execution on a shared memory machine
15 * mg GranSim GRAN Simulation of parallel execution
16 * md GUM/GdH DIST Distributed execution (based on GUM)
18 * --------------------------------------------------------------------------*/
21 * Version with support for distributed memory parallelism aka GUM (WAY=mp):
23 The main scheduling loop in GUM iterates until a finish message is received.
24 In that case a global flag @receivedFinish@ is set and this instance of
25 the RTS shuts down. See ghc/rts/parallel/HLComms.c:processMessages()
26 for the handling of incoming messages, such as PP_FINISH.
27 Note that in the parallel case we have a system manager that coordinates
28 different PEs, each of which are running one instance of the RTS.
29 See ghc/rts/parallel/SysMan.c for the main routine of the parallel program.
30 From this routine processes executing ghc/rts/Main.c are spawned. -- HWL
32 * Version with support for simulating parallel execution aka GranSim (WAY=mg):
34 The main scheduling code in GranSim is quite different from that in std
35 (concurrent) Haskell: while concurrent Haskell just iterates over the
36 threads in the runnable queue, GranSim is event driven, i.e. it iterates
37 over the events in the global event queue. -- HWL
40 #include "PosixSource.h"
47 #include "StgStartup.h"
49 #define COMPILING_SCHEDULER
51 #include "StgMiscClosures.h"
53 #include "Interpreter.h"
54 #include "Exception.h"
61 #include "ThreadLabels.h"
63 #include "Proftimer.h"
66 #if defined(GRAN) || defined(PAR)
67 # include "GranSimRts.h"
69 # include "ParallelRts.h"
70 # include "Parallel.h"
71 # include "ParallelDebug.h"
76 #include "Capability.h"
77 #include "OSThreads.h"
80 #ifdef HAVE_SYS_TYPES_H
81 #include <sys/types.h>
96 #define USED_IN_THREADED_RTS
98 #define USED_IN_THREADED_RTS STG_UNUSED
101 #ifdef RTS_SUPPORTS_THREADS
102 #define USED_WHEN_RTS_SUPPORTS_THREADS
104 #define USED_WHEN_RTS_SUPPORTS_THREADS STG_UNUSED
107 /* Main thread queue.
108 * Locks required: sched_mutex.
110 StgMainThread *main_threads = NULL;
113 * Locks required: sched_mutex.
117 StgTSO* ActiveTSO = NULL; /* for assigning system costs; GranSim-Light only */
118 /* rtsTime TimeOfNextEvent, EndOfTimeSlice; now in GranSim.c */
121 In GranSim we have a runnable and a blocked queue for each processor.
122 In order to minimise code changes new arrays run_queue_hds/tls
123 are created. run_queue_hd is then a short cut (macro) for
124 run_queue_hds[CurrentProc] (see GranSim.h).
127 StgTSO *run_queue_hds[MAX_PROC], *run_queue_tls[MAX_PROC];
128 StgTSO *blocked_queue_hds[MAX_PROC], *blocked_queue_tls[MAX_PROC];
129 StgTSO *ccalling_threadss[MAX_PROC];
130 /* We use the same global list of threads (all_threads) in GranSim as in
131 the std RTS (i.e. we are cheating). However, we don't use this list in
132 the GranSim specific code at the moment (so we are only potentially
137 StgTSO *run_queue_hd = NULL;
138 StgTSO *run_queue_tl = NULL;
139 StgTSO *blocked_queue_hd = NULL;
140 StgTSO *blocked_queue_tl = NULL;
141 StgTSO *sleeping_queue = NULL; /* perhaps replace with a hash table? */
145 /* Linked list of all threads.
146 * Used for detecting garbage collected threads.
148 StgTSO *all_threads = NULL;
150 /* When a thread performs a safe C call (_ccall_GC, using old
151 * terminology), it gets put on the suspended_ccalling_threads
152 * list. Used by the garbage collector.
154 static StgTSO *suspended_ccalling_threads;
156 static StgTSO *threadStackOverflow(StgTSO *tso);
158 /* KH: The following two flags are shared memory locations. There is no need
159 to lock them, since they are only unset at the end of a scheduler
163 /* flag set by signal handler to precipitate a context switch */
164 nat context_switch = 0;
166 /* if this flag is set as well, give up execution */
167 rtsBool interrupted = rtsFalse;
169 /* Next thread ID to allocate.
170 * Locks required: thread_id_mutex
172 static StgThreadID next_thread_id = 1;
175 * Pointers to the state of the current thread.
176 * Rule of thumb: if CurrentTSO != NULL, then we're running a Haskell
177 * thread. If CurrentTSO == NULL, then we're at the scheduler level.
180 /* The smallest stack size that makes any sense is:
181 * RESERVED_STACK_WORDS (so we can get back from the stack overflow)
182 * + sizeofW(StgStopFrame) (the stg_stop_thread_info frame)
183 * + 1 (the closure to enter)
185 * + 1 (spare slot req'd by stg_ap_v_ret)
187 * A thread with this stack will bomb immediately with a stack
188 * overflow, which will increase its stack size.
191 #define MIN_STACK_WORDS (RESERVED_STACK_WORDS + sizeofW(StgStopFrame) + 3)
198 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
199 * exists - earlier gccs apparently didn't.
204 static rtsBool ready_to_gc;
207 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
208 * in an MT setting, needed to signal that a worker thread shouldn't hang around
209 * in the scheduler when it is out of work.
211 static rtsBool shutting_down_scheduler = rtsFalse;
213 void addToBlockedQueue ( StgTSO *tso );
215 static void schedule ( StgMainThread *mainThread, Capability *initialCapability );
216 void interruptStgRts ( void );
218 static void detectBlackHoles ( void );
220 #if defined(RTS_SUPPORTS_THREADS)
221 /* ToDo: carefully document the invariants that go together
222 * with these synchronisation objects.
224 Mutex sched_mutex = INIT_MUTEX_VAR;
225 Mutex term_mutex = INIT_MUTEX_VAR;
227 #endif /* RTS_SUPPORTS_THREADS */
231 rtsTime TimeOfLastYield;
232 rtsBool emitSchedule = rtsTrue;
236 static char *whatNext_strs[] = {
246 StgTSO * createSparkThread(rtsSpark spark);
247 StgTSO * activateSpark (rtsSpark spark);
250 /* ----------------------------------------------------------------------------
252 * ------------------------------------------------------------------------- */
254 #if defined(RTS_SUPPORTS_THREADS)
255 static rtsBool startingWorkerThread = rtsFalse;
257 static void taskStart(void);
261 ACQUIRE_LOCK(&sched_mutex);
262 startingWorkerThread = rtsFalse;
264 RELEASE_LOCK(&sched_mutex);
268 startSchedulerTaskIfNecessary(void)
270 if(run_queue_hd != END_TSO_QUEUE
271 || blocked_queue_hd != END_TSO_QUEUE
272 || sleeping_queue != END_TSO_QUEUE)
274 if(!startingWorkerThread)
275 { // we don't want to start another worker thread
276 // just because the last one hasn't yet reached the
277 // "waiting for capability" state
278 startingWorkerThread = rtsTrue;
279 startTask(taskStart);
285 /* ---------------------------------------------------------------------------
286 Main scheduling loop.
288 We use round-robin scheduling, each thread returning to the
289 scheduler loop when one of these conditions is detected:
292 * timer expires (thread yields)
297 Locking notes: we acquire the scheduler lock once at the beginning
298 of the scheduler loop, and release it when
300 * running a thread, or
301 * waiting for work, or
302 * waiting for a GC to complete.
305 In a GranSim setup this loop iterates over the global event queue.
306 This revolves around the global event queue, which determines what
307 to do next. Therefore, it's more complicated than either the
308 concurrent or the parallel (GUM) setup.
311 GUM iterates over incoming messages.
312 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
313 and sends out a fish whenever it has nothing to do; in-between
314 doing the actual reductions (shared code below) it processes the
315 incoming messages and deals with delayed operations
316 (see PendingFetches).
317 This is not the ugliest code you could imagine, but it's bloody close.
319 ------------------------------------------------------------------------ */
321 schedule( StgMainThread *mainThread USED_WHEN_RTS_SUPPORTS_THREADS,
322 Capability *initialCapability )
326 StgThreadReturnCode ret;
334 rtsBool receivedFinish = rtsFalse;
336 nat tp_size, sp_size; // stats only
339 rtsBool was_interrupted = rtsFalse;
340 StgTSOWhatNext prev_what_next;
342 // Pre-condition: sched_mutex is held.
343 // We might have a capability, passed in as initialCapability.
344 cap = initialCapability;
346 #if defined(RTS_SUPPORTS_THREADS)
348 // in the threaded case, the capability is either passed in via the
349 // initialCapability parameter, or initialized inside the scheduler
353 sched_belch("### NEW SCHEDULER LOOP (main thr: %p, cap: %p)",
354 mainThread, initialCapability);
357 // simply initialise it in the non-threaded case
358 grabCapability(&cap);
362 /* set up first event to get things going */
363 /* ToDo: assign costs for system setup and init MainTSO ! */
364 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
366 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
369 fprintf(stderr, "GRAN: Init CurrentTSO (in schedule) = %p\n", CurrentTSO);
370 G_TSO(CurrentTSO, 5));
372 if (RtsFlags.GranFlags.Light) {
373 /* Save current time; GranSim Light only */
374 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
377 event = get_next_event();
379 while (event!=(rtsEvent*)NULL) {
380 /* Choose the processor with the next event */
381 CurrentProc = event->proc;
382 CurrentTSO = event->tso;
386 while (!receivedFinish) { /* set by processMessages */
387 /* when receiving PP_FINISH message */
389 #else // everything except GRAN and PAR
395 IF_DEBUG(scheduler, printAllThreads());
397 #if defined(RTS_SUPPORTS_THREADS)
398 // Yield the capability to higher-priority tasks if necessary.
401 yieldCapability(&cap);
404 // If we do not currently hold a capability, we wait for one
407 waitForCapability(&sched_mutex, &cap,
408 mainThread ? &mainThread->bound_thread_cond : NULL);
411 // We now have a capability...
415 // If we're interrupted (the user pressed ^C, or some other
416 // termination condition occurred), kill all the currently running
420 IF_DEBUG(scheduler, sched_belch("interrupted"));
421 interrupted = rtsFalse;
422 was_interrupted = rtsTrue;
423 #if defined(RTS_SUPPORTS_THREADS)
424 // In the threaded RTS, deadlock detection doesn't work,
425 // so just exit right away.
426 prog_belch("interrupted");
427 releaseCapability(cap);
428 RELEASE_LOCK(&sched_mutex);
429 shutdownHaskellAndExit(EXIT_SUCCESS);
435 #if defined(RTS_USER_SIGNALS)
436 // check for signals each time around the scheduler
437 if (signals_pending()) {
438 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
439 startSignalHandlers();
440 ACQUIRE_LOCK(&sched_mutex);
445 // Check whether any waiting threads need to be woken up. If the
446 // run queue is empty, and there are no other tasks running, we
447 // can wait indefinitely for something to happen.
449 if ( !EMPTY_QUEUE(blocked_queue_hd) || !EMPTY_QUEUE(sleeping_queue)
450 #if defined(RTS_SUPPORTS_THREADS)
455 awaitEvent( EMPTY_RUN_QUEUE() );
457 // we can be interrupted while waiting for I/O...
458 if (interrupted) continue;
461 * Detect deadlock: when we have no threads to run, there are no
462 * threads waiting on I/O or sleeping, and all the other tasks are
463 * waiting for work, we must have a deadlock of some description.
465 * We first try to find threads blocked on themselves (ie. black
466 * holes), and generate NonTermination exceptions where necessary.
468 * If no threads are black holed, we have a deadlock situation, so
469 * inform all the main threads.
471 #if !defined(PAR) && !defined(RTS_SUPPORTS_THREADS)
472 if ( EMPTY_THREAD_QUEUES() )
474 IF_DEBUG(scheduler, sched_belch("deadlocked, forcing major GC..."));
475 // Garbage collection can release some new threads due to
476 // either (a) finalizers or (b) threads resurrected because
477 // they are about to be send BlockedOnDeadMVar. Any threads
478 // thus released will be immediately runnable.
479 GarbageCollect(GetRoots,rtsTrue);
481 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
484 sched_belch("still deadlocked, checking for black holes..."));
487 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
489 #if defined(RTS_USER_SIGNALS)
490 /* If we have user-installed signal handlers, then wait
491 * for signals to arrive rather then bombing out with a
494 if ( anyUserHandlers() ) {
496 sched_belch("still deadlocked, waiting for signals..."));
500 // we might be interrupted...
501 if (interrupted) { continue; }
503 if (signals_pending()) {
504 RELEASE_LOCK(&sched_mutex);
505 startSignalHandlers();
506 ACQUIRE_LOCK(&sched_mutex);
508 ASSERT(!EMPTY_RUN_QUEUE());
513 /* Probably a real deadlock. Send the current main thread the
514 * Deadlock exception (or in the SMP build, send *all* main
515 * threads the deadlock exception, since none of them can make
521 switch (m->tso->why_blocked) {
522 case BlockedOnBlackHole:
523 case BlockedOnException:
525 raiseAsync(m->tso, (StgClosure *)NonTermination_closure);
528 barf("deadlock: main thread blocked in a strange way");
534 #elif defined(RTS_SUPPORTS_THREADS)
535 // ToDo: add deadlock detection in threaded RTS
537 // ToDo: add deadlock detection in GUM (similar to SMP) -- HWL
540 #if defined(RTS_SUPPORTS_THREADS)
541 if ( EMPTY_RUN_QUEUE() ) {
542 continue; // nothing to do
547 if (RtsFlags.GranFlags.Light)
548 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
550 /* adjust time based on time-stamp */
551 if (event->time > CurrentTime[CurrentProc] &&
552 event->evttype != ContinueThread)
553 CurrentTime[CurrentProc] = event->time;
555 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
556 if (!RtsFlags.GranFlags.Light)
559 IF_DEBUG(gran, fprintf(stderr, "GRAN: switch by event-type\n"));
561 /* main event dispatcher in GranSim */
562 switch (event->evttype) {
563 /* Should just be continuing execution */
565 IF_DEBUG(gran, fprintf(stderr, "GRAN: doing ContinueThread\n"));
566 /* ToDo: check assertion
567 ASSERT(run_queue_hd != (StgTSO*)NULL &&
568 run_queue_hd != END_TSO_QUEUE);
570 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
571 if (!RtsFlags.GranFlags.DoAsyncFetch &&
572 procStatus[CurrentProc]==Fetching) {
573 belch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]",
574 CurrentTSO->id, CurrentTSO, CurrentProc);
577 /* Ignore ContinueThreads for completed threads */
578 if (CurrentTSO->what_next == ThreadComplete) {
579 belch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)",
580 CurrentTSO->id, CurrentTSO, CurrentProc);
583 /* Ignore ContinueThreads for threads that are being migrated */
584 if (PROCS(CurrentTSO)==Nowhere) {
585 belch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)",
586 CurrentTSO->id, CurrentTSO, CurrentProc);
589 /* The thread should be at the beginning of the run queue */
590 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
591 belch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread",
592 CurrentTSO->id, CurrentTSO, CurrentProc);
593 break; // run the thread anyway
596 new_event(proc, proc, CurrentTime[proc],
598 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
600 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
601 break; // now actually run the thread; DaH Qu'vam yImuHbej
604 do_the_fetchnode(event);
605 goto next_thread; /* handle next event in event queue */
608 do_the_globalblock(event);
609 goto next_thread; /* handle next event in event queue */
612 do_the_fetchreply(event);
613 goto next_thread; /* handle next event in event queue */
615 case UnblockThread: /* Move from the blocked queue to the tail of */
616 do_the_unblock(event);
617 goto next_thread; /* handle next event in event queue */
619 case ResumeThread: /* Move from the blocked queue to the tail of */
620 /* the runnable queue ( i.e. Qu' SImqa'lu') */
621 event->tso->gran.blocktime +=
622 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
623 do_the_startthread(event);
624 goto next_thread; /* handle next event in event queue */
627 do_the_startthread(event);
628 goto next_thread; /* handle next event in event queue */
631 do_the_movethread(event);
632 goto next_thread; /* handle next event in event queue */
635 do_the_movespark(event);
636 goto next_thread; /* handle next event in event queue */
639 do_the_findwork(event);
640 goto next_thread; /* handle next event in event queue */
643 barf("Illegal event type %u\n", event->evttype);
646 /* This point was scheduler_loop in the old RTS */
648 IF_DEBUG(gran, belch("GRAN: after main switch"));
650 TimeOfLastEvent = CurrentTime[CurrentProc];
651 TimeOfNextEvent = get_time_of_next_event();
652 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
653 // CurrentTSO = ThreadQueueHd;
655 IF_DEBUG(gran, belch("GRAN: time of next event is: %ld",
658 if (RtsFlags.GranFlags.Light)
659 GranSimLight_leave_system(event, &ActiveTSO);
661 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
664 belch("GRAN: end of time-slice is %#lx", EndOfTimeSlice));
666 /* in a GranSim setup the TSO stays on the run queue */
668 /* Take a thread from the run queue. */
669 POP_RUN_QUEUE(t); // take_off_run_queue(t);
672 fprintf(stderr, "GRAN: About to run current thread, which is\n");
675 context_switch = 0; // turned on via GranYield, checking events and time slice
678 DumpGranEvent(GR_SCHEDULE, t));
680 procStatus[CurrentProc] = Busy;
683 if (PendingFetches != END_BF_QUEUE) {
687 /* ToDo: phps merge with spark activation above */
688 /* check whether we have local work and send requests if we have none */
689 if (EMPTY_RUN_QUEUE()) { /* no runnable threads */
690 /* :-[ no local threads => look out for local sparks */
691 /* the spark pool for the current PE */
692 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
693 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
694 pool->hd < pool->tl) {
696 * ToDo: add GC code check that we really have enough heap afterwards!!
698 * If we're here (no runnable threads) and we have pending
699 * sparks, we must have a space problem. Get enough space
700 * to turn one of those pending sparks into a
704 spark = findSpark(rtsFalse); /* get a spark */
705 if (spark != (rtsSpark) NULL) {
706 tso = activateSpark(spark); /* turn the spark into a thread */
707 IF_PAR_DEBUG(schedule,
708 belch("==== schedule: Created TSO %d (%p); %d threads active",
709 tso->id, tso, advisory_thread_count));
711 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
712 belch("==^^ failed to activate spark");
714 } /* otherwise fall through & pick-up new tso */
716 IF_PAR_DEBUG(verbose,
717 belch("==^^ no local sparks (spark pool contains only NFs: %d)",
718 spark_queue_len(pool)));
723 /* If we still have no work we need to send a FISH to get a spark
726 if (EMPTY_RUN_QUEUE()) {
727 /* =8-[ no local sparks => look for work on other PEs */
729 * We really have absolutely no work. Send out a fish
730 * (there may be some out there already), and wait for
731 * something to arrive. We clearly can't run any threads
732 * until a SCHEDULE or RESUME arrives, and so that's what
733 * we're hoping to see. (Of course, we still have to
734 * respond to other types of messages.)
736 TIME now = msTime() /*CURRENT_TIME*/;
737 IF_PAR_DEBUG(verbose,
738 belch("-- now=%ld", now));
739 IF_PAR_DEBUG(verbose,
740 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
741 (last_fish_arrived_at!=0 &&
742 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay > now)) {
743 belch("--$$ delaying FISH until %ld (last fish %ld, delay %ld, now %ld)",
744 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay,
745 last_fish_arrived_at,
746 RtsFlags.ParFlags.fishDelay, now);
749 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
750 (last_fish_arrived_at==0 ||
751 (last_fish_arrived_at+RtsFlags.ParFlags.fishDelay <= now))) {
752 /* outstandingFishes is set in sendFish, processFish;
753 avoid flooding system with fishes via delay */
755 sendFish(pe, mytid, NEW_FISH_AGE, NEW_FISH_HISTORY,
758 // Global statistics: count no. of fishes
759 if (RtsFlags.ParFlags.ParStats.Global &&
760 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
761 globalParStats.tot_fish_mess++;
765 receivedFinish = processMessages();
768 } else if (PacketsWaiting()) { /* Look for incoming messages */
769 receivedFinish = processMessages();
772 /* Now we are sure that we have some work available */
773 ASSERT(run_queue_hd != END_TSO_QUEUE);
775 /* Take a thread from the run queue, if we have work */
776 POP_RUN_QUEUE(t); // take_off_run_queue(END_TSO_QUEUE);
777 IF_DEBUG(sanity,checkTSO(t));
779 /* ToDo: write something to the log-file
780 if (RTSflags.ParFlags.granSimStats && !sameThread)
781 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
785 /* the spark pool for the current PE */
786 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
789 belch("--=^ %d threads, %d sparks on [%#x]",
790 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
793 if (0 && RtsFlags.ParFlags.ParStats.Full &&
794 t && LastTSO && t->id != LastTSO->id &&
795 LastTSO->why_blocked == NotBlocked &&
796 LastTSO->what_next != ThreadComplete) {
797 // if previously scheduled TSO not blocked we have to record the context switch
798 DumpVeryRawGranEvent(TimeOfLastYield, CURRENT_PROC, CURRENT_PROC,
799 GR_DESCHEDULE, LastTSO, (StgClosure *)NULL, 0, 0);
802 if (RtsFlags.ParFlags.ParStats.Full &&
803 (emitSchedule /* forced emit */ ||
804 (t && LastTSO && t->id != LastTSO->id))) {
806 we are running a different TSO, so write a schedule event to log file
807 NB: If we use fair scheduling we also have to write a deschedule
808 event for LastTSO; with unfair scheduling we know that the
809 previous tso has blocked whenever we switch to another tso, so
810 we don't need it in GUM for now
812 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
813 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
814 emitSchedule = rtsFalse;
818 #else /* !GRAN && !PAR */
820 // grab a thread from the run queue
821 ASSERT(run_queue_hd != END_TSO_QUEUE);
824 // Sanity check the thread we're about to run. This can be
825 // expensive if there is lots of thread switching going on...
826 IF_DEBUG(sanity,checkTSO(t));
831 StgMainThread *m = t->main;
838 sched_belch("### Running thread %d in bound thread", t->id));
839 // yes, the Haskell thread is bound to the current native thread
844 sched_belch("### thread %d bound to another OS thread", t->id));
845 // no, bound to a different Haskell thread: pass to that thread
846 PUSH_ON_RUN_QUEUE(t);
847 passCapability(&m->bound_thread_cond);
853 if(mainThread != NULL)
854 // The thread we want to run is bound.
857 sched_belch("### this OS thread cannot run thread %d", t->id));
858 // no, the current native thread is bound to a different
859 // Haskell thread, so pass it to any worker thread
860 PUSH_ON_RUN_QUEUE(t);
861 passCapabilityToWorker();
868 cap->r.rCurrentTSO = t;
870 /* context switches are now initiated by the timer signal, unless
871 * the user specified "context switch as often as possible", with
874 if ((RtsFlags.ConcFlags.ctxtSwitchTicks == 0
875 && (run_queue_hd != END_TSO_QUEUE
876 || blocked_queue_hd != END_TSO_QUEUE
877 || sleeping_queue != END_TSO_QUEUE)))
884 RELEASE_LOCK(&sched_mutex);
886 IF_DEBUG(scheduler, sched_belch("-->> running thread %ld %s ...",
887 t->id, whatNext_strs[t->what_next]));
890 startHeapProfTimer();
893 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
894 /* Run the current thread
896 prev_what_next = t->what_next;
897 switch (prev_what_next) {
900 /* Thread already finished, return to scheduler. */
901 ret = ThreadFinished;
904 errno = t->saved_errno;
905 ret = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
906 t->saved_errno = errno;
908 case ThreadInterpret:
909 ret = interpretBCO(cap);
912 barf("schedule: invalid what_next field");
914 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
916 /* Costs for the scheduler are assigned to CCS_SYSTEM */
922 ACQUIRE_LOCK(&sched_mutex);
924 #ifdef RTS_SUPPORTS_THREADS
925 IF_DEBUG(scheduler,fprintf(stderr,"sched (task %p): ", osThreadId()););
926 #elif !defined(GRAN) && !defined(PAR)
927 IF_DEBUG(scheduler,fprintf(stderr,"sched: "););
929 t = cap->r.rCurrentTSO;
932 /* HACK 675: if the last thread didn't yield, make sure to print a
933 SCHEDULE event to the log file when StgRunning the next thread, even
934 if it is the same one as before */
936 TimeOfLastYield = CURRENT_TIME;
942 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
943 globalGranStats.tot_heapover++;
945 globalParStats.tot_heapover++;
948 // did the task ask for a large block?
949 if (cap->r.rHpAlloc > BLOCK_SIZE_W) {
950 // if so, get one and push it on the front of the nursery.
954 blocks = (nat)BLOCK_ROUND_UP(cap->r.rHpAlloc * sizeof(W_)) / BLOCK_SIZE;
956 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: requesting a large block (size %d)",
957 t->id, whatNext_strs[t->what_next], blocks));
959 // don't do this if it would push us over the
960 // alloc_blocks_lim limit; we'll GC first.
961 if (alloc_blocks + blocks < alloc_blocks_lim) {
963 alloc_blocks += blocks;
964 bd = allocGroup( blocks );
966 // link the new group into the list
967 bd->link = cap->r.rCurrentNursery;
968 bd->u.back = cap->r.rCurrentNursery->u.back;
969 if (cap->r.rCurrentNursery->u.back != NULL) {
970 cap->r.rCurrentNursery->u.back->link = bd;
972 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
973 g0s0->blocks == cap->r.rNursery);
974 cap->r.rNursery = g0s0->blocks = bd;
976 cap->r.rCurrentNursery->u.back = bd;
978 // initialise it as a nursery block. We initialise the
979 // step, gen_no, and flags field of *every* sub-block in
980 // this large block, because this is easier than making
981 // sure that we always find the block head of a large
982 // block whenever we call Bdescr() (eg. evacuate() and
983 // isAlive() in the GC would both have to do this, at
987 for (x = bd; x < bd + blocks; x++) {
994 // don't forget to update the block count in g0s0.
995 g0s0->n_blocks += blocks;
996 // This assert can be a killer if the app is doing lots
997 // of large block allocations.
998 ASSERT(countBlocks(g0s0->blocks) == g0s0->n_blocks);
1000 // now update the nursery to point to the new block
1001 cap->r.rCurrentNursery = bd;
1003 // we might be unlucky and have another thread get on the
1004 // run queue before us and steal the large block, but in that
1005 // case the thread will just end up requesting another large
1007 PUSH_ON_RUN_QUEUE(t);
1012 /* make all the running tasks block on a condition variable,
1013 * maybe set context_switch and wait till they all pile in,
1014 * then have them wait on a GC condition variable.
1016 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: HeapOverflow",
1017 t->id, whatNext_strs[t->what_next]));
1020 ASSERT(!is_on_queue(t,CurrentProc));
1022 /* Currently we emit a DESCHEDULE event before GC in GUM.
1023 ToDo: either add separate event to distinguish SYSTEM time from rest
1024 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1025 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1026 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1027 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1028 emitSchedule = rtsTrue;
1032 ready_to_gc = rtsTrue;
1033 context_switch = 1; /* stop other threads ASAP */
1034 PUSH_ON_RUN_QUEUE(t);
1035 /* actual GC is done at the end of the while loop */
1041 DumpGranEvent(GR_DESCHEDULE, t));
1042 globalGranStats.tot_stackover++;
1045 // DumpGranEvent(GR_DESCHEDULE, t);
1046 globalParStats.tot_stackover++;
1048 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped, StackOverflow",
1049 t->id, whatNext_strs[t->what_next]));
1050 /* just adjust the stack for this thread, then pop it back
1055 /* enlarge the stack */
1056 StgTSO *new_t = threadStackOverflow(t);
1058 /* This TSO has moved, so update any pointers to it from the
1059 * main thread stack. It better not be on any other queues...
1060 * (it shouldn't be).
1062 if (t->main != NULL) {
1063 t->main->tso = new_t;
1065 threadPaused(new_t);
1066 PUSH_ON_RUN_QUEUE(new_t);
1070 case ThreadYielding:
1073 DumpGranEvent(GR_DESCHEDULE, t));
1074 globalGranStats.tot_yields++;
1077 // DumpGranEvent(GR_DESCHEDULE, t);
1078 globalParStats.tot_yields++;
1080 /* put the thread back on the run queue. Then, if we're ready to
1081 * GC, check whether this is the last task to stop. If so, wake
1082 * up the GC thread. getThread will block during a GC until the
1086 if (t->what_next != prev_what_next) {
1087 belch("--<< thread %ld (%s) stopped to switch evaluators",
1088 t->id, whatNext_strs[t->what_next]);
1090 belch("--<< thread %ld (%s) stopped, yielding",
1091 t->id, whatNext_strs[t->what_next]);
1096 //belch("&& Doing sanity check on yielding TSO %ld.", t->id);
1098 ASSERT(t->link == END_TSO_QUEUE);
1100 // Shortcut if we're just switching evaluators: don't bother
1101 // doing stack squeezing (which can be expensive), just run the
1103 if (t->what_next != prev_what_next) {
1110 ASSERT(!is_on_queue(t,CurrentProc));
1113 //belch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1114 checkThreadQsSanity(rtsTrue));
1118 if (RtsFlags.ParFlags.doFairScheduling) {
1119 /* this does round-robin scheduling; good for concurrency */
1120 APPEND_TO_RUN_QUEUE(t);
1122 /* this does unfair scheduling; good for parallelism */
1123 PUSH_ON_RUN_QUEUE(t);
1126 // this does round-robin scheduling; good for concurrency
1127 APPEND_TO_RUN_QUEUE(t);
1131 /* add a ContinueThread event to actually process the thread */
1132 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1134 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1136 belch("GRAN: eventq and runnableq after adding yielded thread to queue again:");
1145 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1146 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)));
1147 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1149 // ??? needed; should emit block before
1151 DumpGranEvent(GR_DESCHEDULE, t));
1152 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1155 ASSERT(procStatus[CurrentProc]==Busy ||
1156 ((procStatus[CurrentProc]==Fetching) &&
1157 (t->block_info.closure!=(StgClosure*)NULL)));
1158 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1159 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1160 procStatus[CurrentProc]==Fetching))
1161 procStatus[CurrentProc] = Idle;
1165 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: ",
1166 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1169 if (t->block_info.closure!=(StgClosure*)NULL)
1170 print_bq(t->block_info.closure));
1172 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1175 /* whatever we schedule next, we must log that schedule */
1176 emitSchedule = rtsTrue;
1179 /* don't need to do anything. Either the thread is blocked on
1180 * I/O, in which case we'll have called addToBlockedQueue
1181 * previously, or it's blocked on an MVar or Blackhole, in which
1182 * case it'll be on the relevant queue already.
1185 fprintf(stderr, "--<< thread %d (%s) stopped: ",
1186 t->id, whatNext_strs[t->what_next]);
1187 printThreadBlockage(t);
1188 fprintf(stderr, "\n"));
1191 /* Only for dumping event to log file
1192 ToDo: do I need this in GranSim, too?
1199 case ThreadFinished:
1200 /* Need to check whether this was a main thread, and if so, signal
1201 * the task that started it with the return value. If we have no
1202 * more main threads, we probably need to stop all the tasks until
1205 /* We also end up here if the thread kills itself with an
1206 * uncaught exception, see Exception.hc.
1208 IF_DEBUG(scheduler,belch("--++ thread %d (%s) finished",
1209 t->id, whatNext_strs[t->what_next]));
1211 endThread(t, CurrentProc); // clean-up the thread
1213 /* For now all are advisory -- HWL */
1214 //if(t->priority==AdvisoryPriority) ??
1215 advisory_thread_count--;
1218 if(t->dist.priority==RevalPriority)
1222 if (RtsFlags.ParFlags.ParStats.Full &&
1223 !RtsFlags.ParFlags.ParStats.Suppressed)
1224 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1228 // Check whether the thread that just completed was a main
1229 // thread, and if so return with the result.
1231 // There is an assumption here that all thread completion goes
1232 // through this point; we need to make sure that if a thread
1233 // ends up in the ThreadKilled state, that it stays on the run
1234 // queue so it can be dealt with here.
1237 #if defined(RTS_SUPPORTS_THREADS)
1240 mainThread->tso == t
1244 // We are a bound thread: this must be our thread that just
1246 ASSERT(mainThread->tso == t);
1248 if (t->what_next == ThreadComplete) {
1249 if (mainThread->ret) {
1250 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1251 *(mainThread->ret) = (StgClosure *)mainThread->tso->sp[1];
1253 mainThread->stat = Success;
1255 if (mainThread->ret) {
1256 *(mainThread->ret) = NULL;
1258 if (was_interrupted) {
1259 mainThread->stat = Interrupted;
1261 mainThread->stat = Killed;
1265 removeThreadLabel((StgWord)mainThread->tso->id);
1267 if (mainThread->prev == NULL) {
1268 main_threads = mainThread->link;
1270 mainThread->prev->link = mainThread->link;
1272 if (mainThread->link != NULL) {
1273 mainThread->link->prev = NULL;
1275 releaseCapability(cap);
1279 #ifdef RTS_SUPPORTS_THREADS
1280 ASSERT(t->main == NULL);
1282 if (t->main != NULL) {
1283 // Must be a main thread that is not the topmost one. Leave
1284 // it on the run queue until the stack has unwound to the
1285 // point where we can deal with this. Leaving it on the run
1286 // queue also ensures that the garbage collector knows about
1287 // this thread and its return value (it gets dropped from the
1288 // all_threads list so there's no other way to find it).
1289 APPEND_TO_RUN_QUEUE(t);
1295 barf("schedule: invalid thread return code %d", (int)ret);
1299 // When we have +RTS -i0 and we're heap profiling, do a census at
1300 // every GC. This lets us get repeatable runs for debugging.
1301 if (performHeapProfile ||
1302 (RtsFlags.ProfFlags.profileInterval==0 &&
1303 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1304 GarbageCollect(GetRoots, rtsTrue);
1306 performHeapProfile = rtsFalse;
1307 ready_to_gc = rtsFalse; // we already GC'd
1312 /* everybody back, start the GC.
1313 * Could do it in this thread, or signal a condition var
1314 * to do it in another thread. Either way, we need to
1315 * broadcast on gc_pending_cond afterward.
1317 #if defined(RTS_SUPPORTS_THREADS)
1318 IF_DEBUG(scheduler,sched_belch("doing GC"));
1320 GarbageCollect(GetRoots,rtsFalse);
1321 ready_to_gc = rtsFalse;
1323 /* add a ContinueThread event to continue execution of current thread */
1324 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1326 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1328 fprintf(stderr, "GRAN: eventq and runnableq after Garbage collection:\n");
1336 IF_GRAN_DEBUG(unused,
1337 print_eventq(EventHd));
1339 event = get_next_event();
1342 /* ToDo: wait for next message to arrive rather than busy wait */
1345 } /* end of while(1) */
1347 IF_PAR_DEBUG(verbose,
1348 belch("== Leaving schedule() after having received Finish"));
1351 /* ---------------------------------------------------------------------------
1352 * rtsSupportsBoundThreads(): is the RTS built to support bound threads?
1353 * used by Control.Concurrent for error checking.
1354 * ------------------------------------------------------------------------- */
1357 rtsSupportsBoundThreads(void)
1366 /* ---------------------------------------------------------------------------
1367 * isThreadBound(tso): check whether tso is bound to an OS thread.
1368 * ------------------------------------------------------------------------- */
1371 isThreadBound(StgTSO* tso USED_IN_THREADED_RTS)
1374 return (tso->main != NULL);
1379 /* ---------------------------------------------------------------------------
1380 * Singleton fork(). Do not copy any running threads.
1381 * ------------------------------------------------------------------------- */
1384 deleteThreadImmediately(StgTSO *tso);
1387 forkProcess(HsStablePtr *entry)
1389 #ifndef mingw32_TARGET_OS
1395 IF_DEBUG(scheduler,sched_belch("forking!"));
1396 rts_lock(); // This not only acquires sched_mutex, it also
1397 // makes sure that no other threads are running
1401 if (pid) { /* parent */
1403 /* just return the pid */
1407 } else { /* child */
1410 // delete all threads
1411 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1413 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1416 // don't allow threads to catch the ThreadKilled exception
1417 deleteThreadImmediately(t);
1420 // wipe the main thread list
1421 while((m = main_threads) != NULL) {
1422 main_threads = m->link;
1424 closeCondition(&m->bound_thread_cond);
1429 #ifdef RTS_SUPPORTS_THREADS
1430 resetTaskManagerAfterFork(); // tell startTask() and friends that
1431 startingWorkerThread = rtsFalse; // we have no worker threads any more
1432 resetWorkerWakeupPipeAfterFork();
1435 rc = rts_evalStableIO(entry, NULL); // run the action
1436 rts_checkSchedStatus("forkProcess",rc);
1440 hs_exit(); // clean up and exit
1444 barf("forkProcess#: primop not implemented for mingw32, sorry!\n");
1446 #endif /* mingw32 */
1449 /* ---------------------------------------------------------------------------
1450 * deleteAllThreads(): kill all the live threads.
1452 * This is used when we catch a user interrupt (^C), before performing
1453 * any necessary cleanups and running finalizers.
1455 * Locks: sched_mutex held.
1456 * ------------------------------------------------------------------------- */
1459 deleteAllThreads ( void )
1462 IF_DEBUG(scheduler,sched_belch("deleting all threads"));
1463 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1464 next = t->global_link;
1467 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1468 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
1469 sleeping_queue = END_TSO_QUEUE;
1472 /* startThread and insertThread are now in GranSim.c -- HWL */
1475 /* ---------------------------------------------------------------------------
1476 * Suspending & resuming Haskell threads.
1478 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1479 * its capability before calling the C function. This allows another
1480 * task to pick up the capability and carry on running Haskell
1481 * threads. It also means that if the C call blocks, it won't lock
1484 * The Haskell thread making the C call is put to sleep for the
1485 * duration of the call, on the susepended_ccalling_threads queue. We
1486 * give out a token to the task, which it can use to resume the thread
1487 * on return from the C function.
1488 * ------------------------------------------------------------------------- */
1491 suspendThread( StgRegTable *reg,
1500 int saved_errno = errno;
1502 /* assume that *reg is a pointer to the StgRegTable part
1505 cap = (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
1507 ACQUIRE_LOCK(&sched_mutex);
1510 sched_belch("thread %d did a _ccall_gc (is_concurrent: %d)", cap->r.rCurrentTSO->id,concCall));
1512 // XXX this might not be necessary --SDM
1513 cap->r.rCurrentTSO->what_next = ThreadRunGHC;
1515 threadPaused(cap->r.rCurrentTSO);
1516 cap->r.rCurrentTSO->link = suspended_ccalling_threads;
1517 suspended_ccalling_threads = cap->r.rCurrentTSO;
1519 if(cap->r.rCurrentTSO->blocked_exceptions == NULL) {
1520 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall;
1521 cap->r.rCurrentTSO->blocked_exceptions = END_TSO_QUEUE;
1523 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall_NoUnblockExc;
1526 /* Use the thread ID as the token; it should be unique */
1527 tok = cap->r.rCurrentTSO->id;
1529 /* Hand back capability */
1530 releaseCapability(cap);
1532 #if defined(RTS_SUPPORTS_THREADS)
1533 /* Preparing to leave the RTS, so ensure there's a native thread/task
1534 waiting to take over.
1536 IF_DEBUG(scheduler, sched_belch("worker (token %d): leaving RTS", tok));
1539 /* Other threads _might_ be available for execution; signal this */
1541 RELEASE_LOCK(&sched_mutex);
1543 errno = saved_errno;
1548 resumeThread( StgInt tok,
1549 rtsBool concCall STG_UNUSED )
1551 StgTSO *tso, **prev;
1553 int saved_errno = errno;
1555 #if defined(RTS_SUPPORTS_THREADS)
1556 /* Wait for permission to re-enter the RTS with the result. */
1557 ACQUIRE_LOCK(&sched_mutex);
1558 waitForReturnCapability(&sched_mutex, &cap);
1560 IF_DEBUG(scheduler, sched_belch("worker (token %d): re-entering RTS", tok));
1562 grabCapability(&cap);
1565 /* Remove the thread off of the suspended list */
1566 prev = &suspended_ccalling_threads;
1567 for (tso = suspended_ccalling_threads;
1568 tso != END_TSO_QUEUE;
1569 prev = &tso->link, tso = tso->link) {
1570 if (tso->id == (StgThreadID)tok) {
1575 if (tso == END_TSO_QUEUE) {
1576 barf("resumeThread: thread not found");
1578 tso->link = END_TSO_QUEUE;
1580 if(tso->why_blocked == BlockedOnCCall) {
1581 awakenBlockedQueueNoLock(tso->blocked_exceptions);
1582 tso->blocked_exceptions = NULL;
1585 /* Reset blocking status */
1586 tso->why_blocked = NotBlocked;
1588 cap->r.rCurrentTSO = tso;
1589 RELEASE_LOCK(&sched_mutex);
1590 errno = saved_errno;
1595 /* ---------------------------------------------------------------------------
1597 * ------------------------------------------------------------------------ */
1598 static void unblockThread(StgTSO *tso);
1600 /* ---------------------------------------------------------------------------
1601 * Comparing Thread ids.
1603 * This is used from STG land in the implementation of the
1604 * instances of Eq/Ord for ThreadIds.
1605 * ------------------------------------------------------------------------ */
1608 cmp_thread(StgPtr tso1, StgPtr tso2)
1610 StgThreadID id1 = ((StgTSO *)tso1)->id;
1611 StgThreadID id2 = ((StgTSO *)tso2)->id;
1613 if (id1 < id2) return (-1);
1614 if (id1 > id2) return 1;
1618 /* ---------------------------------------------------------------------------
1619 * Fetching the ThreadID from an StgTSO.
1621 * This is used in the implementation of Show for ThreadIds.
1622 * ------------------------------------------------------------------------ */
1624 rts_getThreadId(StgPtr tso)
1626 return ((StgTSO *)tso)->id;
1631 labelThread(StgPtr tso, char *label)
1636 /* Caveat: Once set, you can only set the thread name to "" */
1637 len = strlen(label)+1;
1638 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
1639 strncpy(buf,label,len);
1640 /* Update will free the old memory for us */
1641 updateThreadLabel(((StgTSO *)tso)->id,buf);
1645 /* ---------------------------------------------------------------------------
1646 Create a new thread.
1648 The new thread starts with the given stack size. Before the
1649 scheduler can run, however, this thread needs to have a closure
1650 (and possibly some arguments) pushed on its stack. See
1651 pushClosure() in Schedule.h.
1653 createGenThread() and createIOThread() (in SchedAPI.h) are
1654 convenient packaged versions of this function.
1656 currently pri (priority) is only used in a GRAN setup -- HWL
1657 ------------------------------------------------------------------------ */
1659 /* currently pri (priority) is only used in a GRAN setup -- HWL */
1661 createThread(nat size, StgInt pri)
1664 createThread(nat size)
1671 /* First check whether we should create a thread at all */
1673 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
1674 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
1676 belch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1677 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1678 return END_TSO_QUEUE;
1684 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
1687 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
1689 /* catch ridiculously small stack sizes */
1690 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
1691 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
1694 stack_size = size - TSO_STRUCT_SIZEW;
1696 tso = (StgTSO *)allocate(size);
1697 TICK_ALLOC_TSO(stack_size, 0);
1699 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
1701 SET_GRAN_HDR(tso, ThisPE);
1704 // Always start with the compiled code evaluator
1705 tso->what_next = ThreadRunGHC;
1707 tso->id = next_thread_id++;
1708 tso->why_blocked = NotBlocked;
1709 tso->blocked_exceptions = NULL;
1711 tso->saved_errno = 0;
1714 tso->stack_size = stack_size;
1715 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
1717 tso->sp = (P_)&(tso->stack) + stack_size;
1720 tso->prof.CCCS = CCS_MAIN;
1723 /* put a stop frame on the stack */
1724 tso->sp -= sizeofW(StgStopFrame);
1725 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
1728 tso->link = END_TSO_QUEUE;
1729 /* uses more flexible routine in GranSim */
1730 insertThread(tso, CurrentProc);
1732 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
1738 if (RtsFlags.GranFlags.GranSimStats.Full)
1739 DumpGranEvent(GR_START,tso);
1741 if (RtsFlags.ParFlags.ParStats.Full)
1742 DumpGranEvent(GR_STARTQ,tso);
1743 /* HACk to avoid SCHEDULE
1747 /* Link the new thread on the global thread list.
1749 tso->global_link = all_threads;
1753 tso->dist.priority = MandatoryPriority; //by default that is...
1757 tso->gran.pri = pri;
1759 tso->gran.magic = TSO_MAGIC; // debugging only
1761 tso->gran.sparkname = 0;
1762 tso->gran.startedat = CURRENT_TIME;
1763 tso->gran.exported = 0;
1764 tso->gran.basicblocks = 0;
1765 tso->gran.allocs = 0;
1766 tso->gran.exectime = 0;
1767 tso->gran.fetchtime = 0;
1768 tso->gran.fetchcount = 0;
1769 tso->gran.blocktime = 0;
1770 tso->gran.blockcount = 0;
1771 tso->gran.blockedat = 0;
1772 tso->gran.globalsparks = 0;
1773 tso->gran.localsparks = 0;
1774 if (RtsFlags.GranFlags.Light)
1775 tso->gran.clock = Now; /* local clock */
1777 tso->gran.clock = 0;
1779 IF_DEBUG(gran,printTSO(tso));
1782 tso->par.magic = TSO_MAGIC; // debugging only
1784 tso->par.sparkname = 0;
1785 tso->par.startedat = CURRENT_TIME;
1786 tso->par.exported = 0;
1787 tso->par.basicblocks = 0;
1788 tso->par.allocs = 0;
1789 tso->par.exectime = 0;
1790 tso->par.fetchtime = 0;
1791 tso->par.fetchcount = 0;
1792 tso->par.blocktime = 0;
1793 tso->par.blockcount = 0;
1794 tso->par.blockedat = 0;
1795 tso->par.globalsparks = 0;
1796 tso->par.localsparks = 0;
1800 globalGranStats.tot_threads_created++;
1801 globalGranStats.threads_created_on_PE[CurrentProc]++;
1802 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
1803 globalGranStats.tot_sq_probes++;
1805 // collect parallel global statistics (currently done together with GC stats)
1806 if (RtsFlags.ParFlags.ParStats.Global &&
1807 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1808 //fprintf(stderr, "Creating thread %d @ %11.2f\n", tso->id, usertime());
1809 globalParStats.tot_threads_created++;
1815 belch("==__ schedule: Created TSO %d (%p);",
1816 CurrentProc, tso, tso->id));
1818 IF_PAR_DEBUG(verbose,
1819 belch("==__ schedule: Created TSO %d (%p); %d threads active",
1820 tso->id, tso, advisory_thread_count));
1822 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
1823 tso->id, tso->stack_size));
1830 all parallel thread creation calls should fall through the following routine.
1833 createSparkThread(rtsSpark spark)
1835 ASSERT(spark != (rtsSpark)NULL);
1836 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads)
1838 barf("{createSparkThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1839 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1840 return END_TSO_QUEUE;
1844 tso = createThread(RtsFlags.GcFlags.initialStkSize);
1845 if (tso==END_TSO_QUEUE)
1846 barf("createSparkThread: Cannot create TSO");
1848 tso->priority = AdvisoryPriority;
1850 pushClosure(tso,spark);
1851 PUSH_ON_RUN_QUEUE(tso);
1852 advisory_thread_count++;
1859 Turn a spark into a thread.
1860 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
1864 activateSpark (rtsSpark spark)
1868 tso = createSparkThread(spark);
1869 if (RtsFlags.ParFlags.ParStats.Full) {
1870 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
1871 IF_PAR_DEBUG(verbose,
1872 belch("==^^ activateSpark: turning spark of closure %p (%s) into a thread",
1873 (StgClosure *)spark, info_type((StgClosure *)spark)));
1875 // ToDo: fwd info on local/global spark to thread -- HWL
1876 // tso->gran.exported = spark->exported;
1877 // tso->gran.locked = !spark->global;
1878 // tso->gran.sparkname = spark->name;
1884 static SchedulerStatus waitThread_(/*out*/StgMainThread* m,
1885 Capability *initialCapability
1889 /* ---------------------------------------------------------------------------
1892 * scheduleThread puts a thread on the head of the runnable queue.
1893 * This will usually be done immediately after a thread is created.
1894 * The caller of scheduleThread must create the thread using e.g.
1895 * createThread and push an appropriate closure
1896 * on this thread's stack before the scheduler is invoked.
1897 * ------------------------------------------------------------------------ */
1899 static void scheduleThread_ (StgTSO* tso);
1902 scheduleThread_(StgTSO *tso)
1904 // Precondition: sched_mutex must be held.
1905 PUSH_ON_RUN_QUEUE(tso);
1910 scheduleThread(StgTSO* tso)
1912 ACQUIRE_LOCK(&sched_mutex);
1913 scheduleThread_(tso);
1914 RELEASE_LOCK(&sched_mutex);
1917 #if defined(RTS_SUPPORTS_THREADS)
1918 static Condition bound_cond_cache;
1919 static int bound_cond_cache_full = 0;
1924 scheduleWaitThread(StgTSO* tso, /*[out]*/HaskellObj* ret,
1925 Capability *initialCapability)
1927 // Precondition: sched_mutex must be held
1930 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
1935 m->link = main_threads;
1937 if (main_threads != NULL) {
1938 main_threads->prev = m;
1942 #if defined(RTS_SUPPORTS_THREADS)
1943 // Allocating a new condition for each thread is expensive, so we
1944 // cache one. This is a pretty feeble hack, but it helps speed up
1945 // consecutive call-ins quite a bit.
1946 if (bound_cond_cache_full) {
1947 m->bound_thread_cond = bound_cond_cache;
1948 bound_cond_cache_full = 0;
1950 initCondition(&m->bound_thread_cond);
1954 /* Put the thread on the main-threads list prior to scheduling the TSO.
1955 Failure to do so introduces a race condition in the MT case (as
1956 identified by Wolfgang Thaller), whereby the new task/OS thread
1957 created by scheduleThread_() would complete prior to the thread
1958 that spawned it managed to put 'itself' on the main-threads list.
1959 The upshot of it all being that the worker thread wouldn't get to
1960 signal the completion of the its work item for the main thread to
1961 see (==> it got stuck waiting.) -- sof 6/02.
1963 IF_DEBUG(scheduler, sched_belch("waiting for thread (%d)", tso->id));
1965 PUSH_ON_RUN_QUEUE(tso);
1966 // NB. Don't call THREAD_RUNNABLE() here, because the thread is
1967 // bound and only runnable by *this* OS thread, so waking up other
1968 // workers will just slow things down.
1970 return waitThread_(m, initialCapability);
1973 /* ---------------------------------------------------------------------------
1976 * Initialise the scheduler. This resets all the queues - if the
1977 * queues contained any threads, they'll be garbage collected at the
1980 * ------------------------------------------------------------------------ */
1988 for (i=0; i<=MAX_PROC; i++) {
1989 run_queue_hds[i] = END_TSO_QUEUE;
1990 run_queue_tls[i] = END_TSO_QUEUE;
1991 blocked_queue_hds[i] = END_TSO_QUEUE;
1992 blocked_queue_tls[i] = END_TSO_QUEUE;
1993 ccalling_threadss[i] = END_TSO_QUEUE;
1994 sleeping_queue = END_TSO_QUEUE;
1997 run_queue_hd = END_TSO_QUEUE;
1998 run_queue_tl = END_TSO_QUEUE;
1999 blocked_queue_hd = END_TSO_QUEUE;
2000 blocked_queue_tl = END_TSO_QUEUE;
2001 sleeping_queue = END_TSO_QUEUE;
2004 suspended_ccalling_threads = END_TSO_QUEUE;
2006 main_threads = NULL;
2007 all_threads = END_TSO_QUEUE;
2012 RtsFlags.ConcFlags.ctxtSwitchTicks =
2013 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
2015 #if defined(RTS_SUPPORTS_THREADS)
2016 /* Initialise the mutex and condition variables used by
2018 initMutex(&sched_mutex);
2019 initMutex(&term_mutex);
2022 ACQUIRE_LOCK(&sched_mutex);
2024 /* A capability holds the state a native thread needs in
2025 * order to execute STG code. At least one capability is
2026 * floating around (only SMP builds have more than one).
2030 #if defined(RTS_SUPPORTS_THREADS)
2031 /* start our haskell execution tasks */
2032 startTaskManager(0,taskStart);
2035 #if /* defined(SMP) ||*/ defined(PAR)
2039 RELEASE_LOCK(&sched_mutex);
2043 exitScheduler( void )
2045 #if defined(RTS_SUPPORTS_THREADS)
2048 shutting_down_scheduler = rtsTrue;
2051 /* ----------------------------------------------------------------------------
2052 Managing the per-task allocation areas.
2054 Each capability comes with an allocation area. These are
2055 fixed-length block lists into which allocation can be done.
2057 ToDo: no support for two-space collection at the moment???
2058 ------------------------------------------------------------------------- */
2062 waitThread_(StgMainThread* m, Capability *initialCapability)
2064 SchedulerStatus stat;
2066 // Precondition: sched_mutex must be held.
2067 IF_DEBUG(scheduler, sched_belch("new main thread (%d)", m->tso->id));
2070 /* GranSim specific init */
2071 CurrentTSO = m->tso; // the TSO to run
2072 procStatus[MainProc] = Busy; // status of main PE
2073 CurrentProc = MainProc; // PE to run it on
2074 schedule(m,initialCapability);
2076 schedule(m,initialCapability);
2077 ASSERT(m->stat != NoStatus);
2082 #if defined(RTS_SUPPORTS_THREADS)
2083 // Free the condition variable, returning it to the cache if possible.
2084 if (!bound_cond_cache_full) {
2085 bound_cond_cache = m->bound_thread_cond;
2086 bound_cond_cache_full = 1;
2088 closeCondition(&m->bound_thread_cond);
2092 IF_DEBUG(scheduler, sched_belch("main thread (%d) finished", m->tso->id));
2095 // Postcondition: sched_mutex still held
2099 /* ---------------------------------------------------------------------------
2100 Where are the roots that we know about?
2102 - all the threads on the runnable queue
2103 - all the threads on the blocked queue
2104 - all the threads on the sleeping queue
2105 - all the thread currently executing a _ccall_GC
2106 - all the "main threads"
2108 ------------------------------------------------------------------------ */
2110 /* This has to be protected either by the scheduler monitor, or by the
2111 garbage collection monitor (probably the latter).
2116 GetRoots( evac_fn evac )
2121 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2122 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2123 evac((StgClosure **)&run_queue_hds[i]);
2124 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2125 evac((StgClosure **)&run_queue_tls[i]);
2127 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2128 evac((StgClosure **)&blocked_queue_hds[i]);
2129 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2130 evac((StgClosure **)&blocked_queue_tls[i]);
2131 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2132 evac((StgClosure **)&ccalling_threads[i]);
2139 if (run_queue_hd != END_TSO_QUEUE) {
2140 ASSERT(run_queue_tl != END_TSO_QUEUE);
2141 evac((StgClosure **)&run_queue_hd);
2142 evac((StgClosure **)&run_queue_tl);
2145 if (blocked_queue_hd != END_TSO_QUEUE) {
2146 ASSERT(blocked_queue_tl != END_TSO_QUEUE);
2147 evac((StgClosure **)&blocked_queue_hd);
2148 evac((StgClosure **)&blocked_queue_tl);
2151 if (sleeping_queue != END_TSO_QUEUE) {
2152 evac((StgClosure **)&sleeping_queue);
2156 if (suspended_ccalling_threads != END_TSO_QUEUE) {
2157 evac((StgClosure **)&suspended_ccalling_threads);
2160 #if defined(PAR) || defined(GRAN)
2161 markSparkQueue(evac);
2164 #if defined(RTS_USER_SIGNALS)
2165 // mark the signal handlers (signals should be already blocked)
2166 markSignalHandlers(evac);
2170 /* -----------------------------------------------------------------------------
2173 This is the interface to the garbage collector from Haskell land.
2174 We provide this so that external C code can allocate and garbage
2175 collect when called from Haskell via _ccall_GC.
2177 It might be useful to provide an interface whereby the programmer
2178 can specify more roots (ToDo).
2180 This needs to be protected by the GC condition variable above. KH.
2181 -------------------------------------------------------------------------- */
2183 static void (*extra_roots)(evac_fn);
2188 /* Obligated to hold this lock upon entry */
2189 ACQUIRE_LOCK(&sched_mutex);
2190 GarbageCollect(GetRoots,rtsFalse);
2191 RELEASE_LOCK(&sched_mutex);
2195 performMajorGC(void)
2197 ACQUIRE_LOCK(&sched_mutex);
2198 GarbageCollect(GetRoots,rtsTrue);
2199 RELEASE_LOCK(&sched_mutex);
2203 AllRoots(evac_fn evac)
2205 GetRoots(evac); // the scheduler's roots
2206 extra_roots(evac); // the user's roots
2210 performGCWithRoots(void (*get_roots)(evac_fn))
2212 ACQUIRE_LOCK(&sched_mutex);
2213 extra_roots = get_roots;
2214 GarbageCollect(AllRoots,rtsFalse);
2215 RELEASE_LOCK(&sched_mutex);
2218 /* -----------------------------------------------------------------------------
2221 If the thread has reached its maximum stack size, then raise the
2222 StackOverflow exception in the offending thread. Otherwise
2223 relocate the TSO into a larger chunk of memory and adjust its stack
2225 -------------------------------------------------------------------------- */
2228 threadStackOverflow(StgTSO *tso)
2230 nat new_stack_size, new_tso_size, stack_words;
2234 IF_DEBUG(sanity,checkTSO(tso));
2235 if (tso->stack_size >= tso->max_stack_size) {
2238 belch("@@ threadStackOverflow of TSO %d (%p): stack too large (now %ld; max is %ld)",
2239 tso->id, tso, tso->stack_size, tso->max_stack_size);
2240 /* If we're debugging, just print out the top of the stack */
2241 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2244 /* Send this thread the StackOverflow exception */
2245 raiseAsync(tso, (StgClosure *)stackOverflow_closure);
2249 /* Try to double the current stack size. If that takes us over the
2250 * maximum stack size for this thread, then use the maximum instead.
2251 * Finally round up so the TSO ends up as a whole number of blocks.
2253 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2254 new_tso_size = (nat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2255 TSO_STRUCT_SIZE)/sizeof(W_);
2256 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2257 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2259 IF_DEBUG(scheduler, fprintf(stderr,"== sched: increasing stack size from %d words to %d.\n", tso->stack_size, new_stack_size));
2261 dest = (StgTSO *)allocate(new_tso_size);
2262 TICK_ALLOC_TSO(new_stack_size,0);
2264 /* copy the TSO block and the old stack into the new area */
2265 memcpy(dest,tso,TSO_STRUCT_SIZE);
2266 stack_words = tso->stack + tso->stack_size - tso->sp;
2267 new_sp = (P_)dest + new_tso_size - stack_words;
2268 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2270 /* relocate the stack pointers... */
2272 dest->stack_size = new_stack_size;
2274 /* Mark the old TSO as relocated. We have to check for relocated
2275 * TSOs in the garbage collector and any primops that deal with TSOs.
2277 * It's important to set the sp value to just beyond the end
2278 * of the stack, so we don't attempt to scavenge any part of the
2281 tso->what_next = ThreadRelocated;
2283 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2284 tso->why_blocked = NotBlocked;
2285 dest->mut_link = NULL;
2287 IF_PAR_DEBUG(verbose,
2288 belch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld",
2289 tso->id, tso, tso->stack_size);
2290 /* If we're debugging, just print out the top of the stack */
2291 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2294 IF_DEBUG(sanity,checkTSO(tso));
2296 IF_DEBUG(scheduler,printTSO(dest));
2302 /* ---------------------------------------------------------------------------
2303 Wake up a queue that was blocked on some resource.
2304 ------------------------------------------------------------------------ */
2308 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2313 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2315 /* write RESUME events to log file and
2316 update blocked and fetch time (depending on type of the orig closure) */
2317 if (RtsFlags.ParFlags.ParStats.Full) {
2318 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
2319 GR_RESUMEQ, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
2320 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
2321 if (EMPTY_RUN_QUEUE())
2322 emitSchedule = rtsTrue;
2324 switch (get_itbl(node)->type) {
2326 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2331 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2338 barf("{unblockOneLocked}Daq Qagh: unexpected closure in blocking queue");
2345 static StgBlockingQueueElement *
2346 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2349 PEs node_loc, tso_loc;
2351 node_loc = where_is(node); // should be lifted out of loop
2352 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2353 tso_loc = where_is((StgClosure *)tso);
2354 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
2355 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
2356 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
2357 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
2358 // insertThread(tso, node_loc);
2359 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
2361 tso, node, (rtsSpark*)NULL);
2362 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2365 } else { // TSO is remote (actually should be FMBQ)
2366 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
2367 RtsFlags.GranFlags.Costs.gunblocktime +
2368 RtsFlags.GranFlags.Costs.latency;
2369 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
2371 tso, node, (rtsSpark*)NULL);
2372 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2375 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
2377 fprintf(stderr," %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
2378 (node_loc==tso_loc ? "Local" : "Global"),
2379 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
2380 tso->block_info.closure = NULL;
2381 IF_DEBUG(scheduler,belch("-- Waking up thread %ld (%p)",
2385 static StgBlockingQueueElement *
2386 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2388 StgBlockingQueueElement *next;
2390 switch (get_itbl(bqe)->type) {
2392 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
2393 /* if it's a TSO just push it onto the run_queue */
2395 // ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
2396 PUSH_ON_RUN_QUEUE((StgTSO *)bqe);
2398 unblockCount(bqe, node);
2399 /* reset blocking status after dumping event */
2400 ((StgTSO *)bqe)->why_blocked = NotBlocked;
2404 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
2406 bqe->link = (StgBlockingQueueElement *)PendingFetches;
2407 PendingFetches = (StgBlockedFetch *)bqe;
2411 /* can ignore this case in a non-debugging setup;
2412 see comments on RBHSave closures above */
2414 /* check that the closure is an RBHSave closure */
2415 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
2416 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
2417 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
2421 barf("{unblockOneLocked}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
2422 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
2426 IF_PAR_DEBUG(bq, fprintf(stderr, ", %p (%s)", bqe, info_type((StgClosure*)bqe)));
2430 #else /* !GRAN && !PAR */
2432 unblockOneLocked(StgTSO *tso)
2436 ASSERT(get_itbl(tso)->type == TSO);
2437 ASSERT(tso->why_blocked != NotBlocked);
2438 tso->why_blocked = NotBlocked;
2440 PUSH_ON_RUN_QUEUE(tso);
2442 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", tso->id));
2447 #if defined(GRAN) || defined(PAR)
2448 INLINE_ME StgBlockingQueueElement *
2449 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
2451 ACQUIRE_LOCK(&sched_mutex);
2452 bqe = unblockOneLocked(bqe, node);
2453 RELEASE_LOCK(&sched_mutex);
2458 unblockOne(StgTSO *tso)
2460 ACQUIRE_LOCK(&sched_mutex);
2461 tso = unblockOneLocked(tso);
2462 RELEASE_LOCK(&sched_mutex);
2469 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2471 StgBlockingQueueElement *bqe;
2476 belch("##-_ AwBQ for node %p on PE %d @ %ld by TSO %d (%p): ", \
2477 node, CurrentProc, CurrentTime[CurrentProc],
2478 CurrentTSO->id, CurrentTSO));
2480 node_loc = where_is(node);
2482 ASSERT(q == END_BQ_QUEUE ||
2483 get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
2484 get_itbl(q)->type == CONSTR); // closure (type constructor)
2485 ASSERT(is_unique(node));
2487 /* FAKE FETCH: magically copy the node to the tso's proc;
2488 no Fetch necessary because in reality the node should not have been
2489 moved to the other PE in the first place
2491 if (CurrentProc!=node_loc) {
2493 belch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)",
2494 node, node_loc, CurrentProc, CurrentTSO->id,
2495 // CurrentTSO, where_is(CurrentTSO),
2496 node->header.gran.procs));
2497 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
2499 belch("## new bitmask of node %p is %#x",
2500 node, node->header.gran.procs));
2501 if (RtsFlags.GranFlags.GranSimStats.Global) {
2502 globalGranStats.tot_fake_fetches++;
2507 // ToDo: check: ASSERT(CurrentProc==node_loc);
2508 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
2511 bqe points to the current element in the queue
2512 next points to the next element in the queue
2514 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2515 //tso_loc = where_is(tso);
2517 bqe = unblockOneLocked(bqe, node);
2520 /* if this is the BQ of an RBH, we have to put back the info ripped out of
2521 the closure to make room for the anchor of the BQ */
2522 if (bqe!=END_BQ_QUEUE) {
2523 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
2525 ASSERT((info_ptr==&RBH_Save_0_info) ||
2526 (info_ptr==&RBH_Save_1_info) ||
2527 (info_ptr==&RBH_Save_2_info));
2529 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
2530 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
2531 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
2534 belch("## Filled in RBH_Save for %p (%s) at end of AwBQ",
2535 node, info_type(node)));
2538 /* statistics gathering */
2539 if (RtsFlags.GranFlags.GranSimStats.Global) {
2540 // globalGranStats.tot_bq_processing_time += bq_processing_time;
2541 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
2542 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
2543 globalGranStats.tot_awbq++; // total no. of bqs awakened
2546 fprintf(stderr,"## BQ Stats of %p: [%d entries] %s\n",
2547 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
2551 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2553 StgBlockingQueueElement *bqe;
2555 ACQUIRE_LOCK(&sched_mutex);
2557 IF_PAR_DEBUG(verbose,
2558 belch("##-_ AwBQ for node %p on [%x]: ",
2562 if(get_itbl(q)->type == CONSTR || q==END_BQ_QUEUE) {
2563 IF_PAR_DEBUG(verbose, belch("## ... nothing to unblock so lets just return. RFP (BUG?)"));
2568 ASSERT(q == END_BQ_QUEUE ||
2569 get_itbl(q)->type == TSO ||
2570 get_itbl(q)->type == BLOCKED_FETCH ||
2571 get_itbl(q)->type == CONSTR);
2574 while (get_itbl(bqe)->type==TSO ||
2575 get_itbl(bqe)->type==BLOCKED_FETCH) {
2576 bqe = unblockOneLocked(bqe, node);
2578 RELEASE_LOCK(&sched_mutex);
2581 #else /* !GRAN && !PAR */
2584 awakenBlockedQueueNoLock(StgTSO *tso)
2586 while (tso != END_TSO_QUEUE) {
2587 tso = unblockOneLocked(tso);
2592 awakenBlockedQueue(StgTSO *tso)
2594 ACQUIRE_LOCK(&sched_mutex);
2595 while (tso != END_TSO_QUEUE) {
2596 tso = unblockOneLocked(tso);
2598 RELEASE_LOCK(&sched_mutex);
2602 /* ---------------------------------------------------------------------------
2604 - usually called inside a signal handler so it mustn't do anything fancy.
2605 ------------------------------------------------------------------------ */
2608 interruptStgRts(void)
2612 #ifdef RTS_SUPPORTS_THREADS
2613 wakeBlockedWorkerThread();
2617 /* -----------------------------------------------------------------------------
2620 This is for use when we raise an exception in another thread, which
2622 This has nothing to do with the UnblockThread event in GranSim. -- HWL
2623 -------------------------------------------------------------------------- */
2625 #if defined(GRAN) || defined(PAR)
2627 NB: only the type of the blocking queue is different in GranSim and GUM
2628 the operations on the queue-elements are the same
2629 long live polymorphism!
2631 Locks: sched_mutex is held upon entry and exit.
2635 unblockThread(StgTSO *tso)
2637 StgBlockingQueueElement *t, **last;
2639 switch (tso->why_blocked) {
2642 return; /* not blocked */
2645 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
2647 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
2648 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
2650 last = (StgBlockingQueueElement **)&mvar->head;
2651 for (t = (StgBlockingQueueElement *)mvar->head;
2653 last = &t->link, last_tso = t, t = t->link) {
2654 if (t == (StgBlockingQueueElement *)tso) {
2655 *last = (StgBlockingQueueElement *)tso->link;
2656 if (mvar->tail == tso) {
2657 mvar->tail = (StgTSO *)last_tso;
2662 barf("unblockThread (MVAR): TSO not found");
2665 case BlockedOnBlackHole:
2666 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
2668 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
2670 last = &bq->blocking_queue;
2671 for (t = bq->blocking_queue;
2673 last = &t->link, t = t->link) {
2674 if (t == (StgBlockingQueueElement *)tso) {
2675 *last = (StgBlockingQueueElement *)tso->link;
2679 barf("unblockThread (BLACKHOLE): TSO not found");
2682 case BlockedOnException:
2684 StgTSO *target = tso->block_info.tso;
2686 ASSERT(get_itbl(target)->type == TSO);
2688 if (target->what_next == ThreadRelocated) {
2689 target = target->link;
2690 ASSERT(get_itbl(target)->type == TSO);
2693 ASSERT(target->blocked_exceptions != NULL);
2695 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
2696 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
2698 last = &t->link, t = t->link) {
2699 ASSERT(get_itbl(t)->type == TSO);
2700 if (t == (StgBlockingQueueElement *)tso) {
2701 *last = (StgBlockingQueueElement *)tso->link;
2705 barf("unblockThread (Exception): TSO not found");
2709 case BlockedOnWrite:
2710 #if defined(mingw32_TARGET_OS)
2711 case BlockedOnDoProc:
2714 /* take TSO off blocked_queue */
2715 StgBlockingQueueElement *prev = NULL;
2716 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
2717 prev = t, t = t->link) {
2718 if (t == (StgBlockingQueueElement *)tso) {
2720 blocked_queue_hd = (StgTSO *)t->link;
2721 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
2722 blocked_queue_tl = END_TSO_QUEUE;
2725 prev->link = t->link;
2726 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
2727 blocked_queue_tl = (StgTSO *)prev;
2733 barf("unblockThread (I/O): TSO not found");
2736 case BlockedOnDelay:
2738 /* take TSO off sleeping_queue */
2739 StgBlockingQueueElement *prev = NULL;
2740 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
2741 prev = t, t = t->link) {
2742 if (t == (StgBlockingQueueElement *)tso) {
2744 sleeping_queue = (StgTSO *)t->link;
2746 prev->link = t->link;
2751 barf("unblockThread (delay): TSO not found");
2755 barf("unblockThread");
2759 tso->link = END_TSO_QUEUE;
2760 tso->why_blocked = NotBlocked;
2761 tso->block_info.closure = NULL;
2762 PUSH_ON_RUN_QUEUE(tso);
2766 unblockThread(StgTSO *tso)
2770 /* To avoid locking unnecessarily. */
2771 if (tso->why_blocked == NotBlocked) {
2775 switch (tso->why_blocked) {
2778 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
2780 StgTSO *last_tso = END_TSO_QUEUE;
2781 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
2784 for (t = mvar->head; t != END_TSO_QUEUE;
2785 last = &t->link, last_tso = t, t = t->link) {
2788 if (mvar->tail == tso) {
2789 mvar->tail = last_tso;
2794 barf("unblockThread (MVAR): TSO not found");
2797 case BlockedOnBlackHole:
2798 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
2800 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
2802 last = &bq->blocking_queue;
2803 for (t = bq->blocking_queue; t != END_TSO_QUEUE;
2804 last = &t->link, t = t->link) {
2810 barf("unblockThread (BLACKHOLE): TSO not found");
2813 case BlockedOnException:
2815 StgTSO *target = tso->block_info.tso;
2817 ASSERT(get_itbl(target)->type == TSO);
2819 while (target->what_next == ThreadRelocated) {
2820 target = target->link;
2821 ASSERT(get_itbl(target)->type == TSO);
2824 ASSERT(target->blocked_exceptions != NULL);
2826 last = &target->blocked_exceptions;
2827 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
2828 last = &t->link, t = t->link) {
2829 ASSERT(get_itbl(t)->type == TSO);
2835 barf("unblockThread (Exception): TSO not found");
2839 case BlockedOnWrite:
2840 #if defined(mingw32_TARGET_OS)
2841 case BlockedOnDoProc:
2844 StgTSO *prev = NULL;
2845 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
2846 prev = t, t = t->link) {
2849 blocked_queue_hd = t->link;
2850 if (blocked_queue_tl == t) {
2851 blocked_queue_tl = END_TSO_QUEUE;
2854 prev->link = t->link;
2855 if (blocked_queue_tl == t) {
2856 blocked_queue_tl = prev;
2862 barf("unblockThread (I/O): TSO not found");
2865 case BlockedOnDelay:
2867 StgTSO *prev = NULL;
2868 for (t = sleeping_queue; t != END_TSO_QUEUE;
2869 prev = t, t = t->link) {
2872 sleeping_queue = t->link;
2874 prev->link = t->link;
2879 barf("unblockThread (delay): TSO not found");
2883 barf("unblockThread");
2887 tso->link = END_TSO_QUEUE;
2888 tso->why_blocked = NotBlocked;
2889 tso->block_info.closure = NULL;
2890 PUSH_ON_RUN_QUEUE(tso);
2894 /* -----------------------------------------------------------------------------
2897 * The following function implements the magic for raising an
2898 * asynchronous exception in an existing thread.
2900 * We first remove the thread from any queue on which it might be
2901 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
2903 * We strip the stack down to the innermost CATCH_FRAME, building
2904 * thunks in the heap for all the active computations, so they can
2905 * be restarted if necessary. When we reach a CATCH_FRAME, we build
2906 * an application of the handler to the exception, and push it on
2907 * the top of the stack.
2909 * How exactly do we save all the active computations? We create an
2910 * AP_STACK for every UpdateFrame on the stack. Entering one of these
2911 * AP_STACKs pushes everything from the corresponding update frame
2912 * upwards onto the stack. (Actually, it pushes everything up to the
2913 * next update frame plus a pointer to the next AP_STACK object.
2914 * Entering the next AP_STACK object pushes more onto the stack until we
2915 * reach the last AP_STACK object - at which point the stack should look
2916 * exactly as it did when we killed the TSO and we can continue
2917 * execution by entering the closure on top of the stack.
2919 * We can also kill a thread entirely - this happens if either (a) the
2920 * exception passed to raiseAsync is NULL, or (b) there's no
2921 * CATCH_FRAME on the stack. In either case, we strip the entire
2922 * stack and replace the thread with a zombie.
2924 * Locks: sched_mutex held upon entry nor exit.
2926 * -------------------------------------------------------------------------- */
2929 deleteThread(StgTSO *tso)
2931 raiseAsync(tso,NULL);
2935 deleteThreadImmediately(StgTSO *tso)
2936 { // for forkProcess only:
2937 // delete thread without giving it a chance to catch the KillThread exception
2939 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
2943 if (tso->why_blocked != BlockedOnCCall &&
2944 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
2948 tso->what_next = ThreadKilled;
2952 raiseAsyncWithLock(StgTSO *tso, StgClosure *exception)
2954 /* When raising async exs from contexts where sched_mutex isn't held;
2955 use raiseAsyncWithLock(). */
2956 ACQUIRE_LOCK(&sched_mutex);
2957 raiseAsync(tso,exception);
2958 RELEASE_LOCK(&sched_mutex);
2962 raiseAsync(StgTSO *tso, StgClosure *exception)
2964 StgRetInfoTable *info;
2967 // Thread already dead?
2968 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
2973 sched_belch("raising exception in thread %ld.", tso->id));
2975 // Remove it from any blocking queues
2980 // The stack freezing code assumes there's a closure pointer on
2981 // the top of the stack, so we have to arrange that this is the case...
2983 if (sp[0] == (W_)&stg_enter_info) {
2987 sp[0] = (W_)&stg_dummy_ret_closure;
2993 // 1. Let the top of the stack be the "current closure"
2995 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
2998 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
2999 // current closure applied to the chunk of stack up to (but not
3000 // including) the update frame. This closure becomes the "current
3001 // closure". Go back to step 2.
3003 // 4. If it's a CATCH_FRAME, then leave the exception handler on
3004 // top of the stack applied to the exception.
3006 // 5. If it's a STOP_FRAME, then kill the thread.
3011 info = get_ret_itbl((StgClosure *)frame);
3013 while (info->i.type != UPDATE_FRAME
3014 && (info->i.type != CATCH_FRAME || exception == NULL)
3015 && info->i.type != STOP_FRAME) {
3016 frame += stack_frame_sizeW((StgClosure *)frame);
3017 info = get_ret_itbl((StgClosure *)frame);
3020 switch (info->i.type) {
3023 // If we find a CATCH_FRAME, and we've got an exception to raise,
3024 // then build the THUNK raise(exception), and leave it on
3025 // top of the CATCH_FRAME ready to enter.
3029 StgCatchFrame *cf = (StgCatchFrame *)frame;
3033 // we've got an exception to raise, so let's pass it to the
3034 // handler in this frame.
3036 raise = (StgClosure *)allocate(sizeofW(StgClosure)+1);
3037 TICK_ALLOC_SE_THK(1,0);
3038 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
3039 raise->payload[0] = exception;
3041 // throw away the stack from Sp up to the CATCH_FRAME.
3045 /* Ensure that async excpetions are blocked now, so we don't get
3046 * a surprise exception before we get around to executing the
3049 if (tso->blocked_exceptions == NULL) {
3050 tso->blocked_exceptions = END_TSO_QUEUE;
3053 /* Put the newly-built THUNK on top of the stack, ready to execute
3054 * when the thread restarts.
3057 sp[-1] = (W_)&stg_enter_info;
3059 tso->what_next = ThreadRunGHC;
3060 IF_DEBUG(sanity, checkTSO(tso));
3069 // First build an AP_STACK consisting of the stack chunk above the
3070 // current update frame, with the top word on the stack as the
3073 words = frame - sp - 1;
3074 ap = (StgAP_STACK *)allocate(PAP_sizeW(words));
3077 ap->fun = (StgClosure *)sp[0];
3079 for(i=0; i < (nat)words; ++i) {
3080 ap->payload[i] = (StgClosure *)*sp++;
3083 SET_HDR(ap,&stg_AP_STACK_info,
3084 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
3085 TICK_ALLOC_UP_THK(words+1,0);
3088 fprintf(stderr, "sched: Updating ");
3089 printPtr((P_)((StgUpdateFrame *)frame)->updatee);
3090 fprintf(stderr, " with ");
3091 printObj((StgClosure *)ap);
3094 // Replace the updatee with an indirection - happily
3095 // this will also wake up any threads currently
3096 // waiting on the result.
3098 // Warning: if we're in a loop, more than one update frame on
3099 // the stack may point to the same object. Be careful not to
3100 // overwrite an IND_OLDGEN in this case, because we'll screw
3101 // up the mutable lists. To be on the safe side, don't
3102 // overwrite any kind of indirection at all. See also
3103 // threadSqueezeStack in GC.c, where we have to make a similar
3106 if (!closure_IND(((StgUpdateFrame *)frame)->updatee)) {
3107 // revert the black hole
3108 UPD_IND_NOLOCK(((StgUpdateFrame *)frame)->updatee,ap);
3110 sp += sizeofW(StgUpdateFrame) - 1;
3111 sp[0] = (W_)ap; // push onto stack
3116 // We've stripped the entire stack, the thread is now dead.
3117 sp += sizeofW(StgStopFrame);
3118 tso->what_next = ThreadKilled;
3129 /* -----------------------------------------------------------------------------
3130 resurrectThreads is called after garbage collection on the list of
3131 threads found to be garbage. Each of these threads will be woken
3132 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
3133 on an MVar, or NonTermination if the thread was blocked on a Black
3136 Locks: sched_mutex isn't held upon entry nor exit.
3137 -------------------------------------------------------------------------- */
3140 resurrectThreads( StgTSO *threads )
3144 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
3145 next = tso->global_link;
3146 tso->global_link = all_threads;
3148 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
3150 switch (tso->why_blocked) {
3152 case BlockedOnException:
3153 /* Called by GC - sched_mutex lock is currently held. */
3154 raiseAsync(tso,(StgClosure *)BlockedOnDeadMVar_closure);
3156 case BlockedOnBlackHole:
3157 raiseAsync(tso,(StgClosure *)NonTermination_closure);
3160 /* This might happen if the thread was blocked on a black hole
3161 * belonging to a thread that we've just woken up (raiseAsync
3162 * can wake up threads, remember...).
3166 barf("resurrectThreads: thread blocked in a strange way");
3171 /* -----------------------------------------------------------------------------
3172 * Blackhole detection: if we reach a deadlock, test whether any
3173 * threads are blocked on themselves. Any threads which are found to
3174 * be self-blocked get sent a NonTermination exception.
3176 * This is only done in a deadlock situation in order to avoid
3177 * performance overhead in the normal case.
3179 * Locks: sched_mutex is held upon entry and exit.
3180 * -------------------------------------------------------------------------- */
3183 detectBlackHoles( void )
3185 StgTSO *tso = all_threads;
3187 StgClosure *blocked_on;
3188 StgRetInfoTable *info;
3190 for (tso = all_threads; tso != END_TSO_QUEUE; tso = tso->global_link) {
3192 while (tso->what_next == ThreadRelocated) {
3194 ASSERT(get_itbl(tso)->type == TSO);
3197 if (tso->why_blocked != BlockedOnBlackHole) {
3200 blocked_on = tso->block_info.closure;
3202 frame = (StgClosure *)tso->sp;
3205 info = get_ret_itbl(frame);
3206 switch (info->i.type) {
3208 if (((StgUpdateFrame *)frame)->updatee == blocked_on) {
3209 /* We are blocking on one of our own computations, so
3210 * send this thread the NonTermination exception.
3213 sched_belch("thread %d is blocked on itself", tso->id));
3214 raiseAsync(tso, (StgClosure *)NonTermination_closure);
3218 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3224 // normal stack frames; do nothing except advance the pointer
3226 (StgPtr)frame += stack_frame_sizeW(frame);
3233 /* ----------------------------------------------------------------------------
3234 * Debugging: why is a thread blocked
3235 * [Also provides useful information when debugging threaded programs
3236 * at the Haskell source code level, so enable outside of DEBUG. --sof 7/02]
3237 ------------------------------------------------------------------------- */
3241 printThreadBlockage(StgTSO *tso)
3243 switch (tso->why_blocked) {
3245 fprintf(stderr,"is blocked on read from fd %d", tso->block_info.fd);
3247 case BlockedOnWrite:
3248 fprintf(stderr,"is blocked on write to fd %d", tso->block_info.fd);
3250 #if defined(mingw32_TARGET_OS)
3251 case BlockedOnDoProc:
3252 fprintf(stderr,"is blocked on proc (request: %d)", tso->block_info.async_result->reqID);
3255 case BlockedOnDelay:
3256 fprintf(stderr,"is blocked until %d", tso->block_info.target);
3259 fprintf(stderr,"is blocked on an MVar");
3261 case BlockedOnException:
3262 fprintf(stderr,"is blocked on delivering an exception to thread %d",
3263 tso->block_info.tso->id);
3265 case BlockedOnBlackHole:
3266 fprintf(stderr,"is blocked on a black hole");
3269 fprintf(stderr,"is not blocked");
3273 fprintf(stderr,"is blocked on global address; local FM_BQ is %p (%s)",
3274 tso->block_info.closure, info_type(tso->block_info.closure));
3276 case BlockedOnGA_NoSend:
3277 fprintf(stderr,"is blocked on global address (no send); local FM_BQ is %p (%s)",
3278 tso->block_info.closure, info_type(tso->block_info.closure));
3281 case BlockedOnCCall:
3282 fprintf(stderr,"is blocked on an external call");
3284 case BlockedOnCCall_NoUnblockExc:
3285 fprintf(stderr,"is blocked on an external call (exceptions were already blocked)");
3288 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
3289 tso->why_blocked, tso->id, tso);
3295 printThreadStatus(StgTSO *tso)
3297 switch (tso->what_next) {
3299 fprintf(stderr,"has been killed");
3301 case ThreadComplete:
3302 fprintf(stderr,"has completed");
3305 printThreadBlockage(tso);
3310 printAllThreads(void)
3316 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3317 ullong_format_string(TIME_ON_PROC(CurrentProc),
3318 time_string, rtsFalse/*no commas!*/);
3320 fprintf(stderr, "all threads at [%s]:\n", time_string);
3322 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3323 ullong_format_string(CURRENT_TIME,
3324 time_string, rtsFalse/*no commas!*/);
3326 fprintf(stderr,"all threads at [%s]:\n", time_string);
3328 fprintf(stderr,"all threads:\n");
3331 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
3332 fprintf(stderr, "\tthread %d @ %p ", t->id, (void *)t);
3333 label = lookupThreadLabel(t->id);
3334 if (label) fprintf(stderr,"[\"%s\"] ",(char *)label);
3335 printThreadStatus(t);
3336 fprintf(stderr,"\n");
3343 Print a whole blocking queue attached to node (debugging only).
3347 print_bq (StgClosure *node)
3349 StgBlockingQueueElement *bqe;
3353 fprintf(stderr,"## BQ of closure %p (%s): ",
3354 node, info_type(node));
3356 /* should cover all closures that may have a blocking queue */
3357 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3358 get_itbl(node)->type == FETCH_ME_BQ ||
3359 get_itbl(node)->type == RBH ||
3360 get_itbl(node)->type == MVAR);
3362 ASSERT(node!=(StgClosure*)NULL); // sanity check
3364 print_bqe(((StgBlockingQueue*)node)->blocking_queue);
3368 Print a whole blocking queue starting with the element bqe.
3371 print_bqe (StgBlockingQueueElement *bqe)
3376 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3378 for (end = (bqe==END_BQ_QUEUE);
3379 !end; // iterate until bqe points to a CONSTR
3380 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE),
3381 bqe = end ? END_BQ_QUEUE : bqe->link) {
3382 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3383 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3384 /* types of closures that may appear in a blocking queue */
3385 ASSERT(get_itbl(bqe)->type == TSO ||
3386 get_itbl(bqe)->type == BLOCKED_FETCH ||
3387 get_itbl(bqe)->type == CONSTR);
3388 /* only BQs of an RBH end with an RBH_Save closure */
3389 //ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3391 switch (get_itbl(bqe)->type) {
3393 fprintf(stderr," TSO %u (%x),",
3394 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
3397 fprintf(stderr," BF (node=%p, ga=((%x, %d, %x)),",
3398 ((StgBlockedFetch *)bqe)->node,
3399 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
3400 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
3401 ((StgBlockedFetch *)bqe)->ga.weight);
3404 fprintf(stderr," %s (IP %p),",
3405 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3406 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3407 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3408 "RBH_Save_?"), get_itbl(bqe));
3411 barf("Unexpected closure type %s in blocking queue", // of %p (%s)",
3412 info_type((StgClosure *)bqe)); // , node, info_type(node));
3416 fputc('\n', stderr);
3418 # elif defined(GRAN)
3420 print_bq (StgClosure *node)
3422 StgBlockingQueueElement *bqe;
3423 PEs node_loc, tso_loc;
3426 /* should cover all closures that may have a blocking queue */
3427 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3428 get_itbl(node)->type == FETCH_ME_BQ ||
3429 get_itbl(node)->type == RBH);
3431 ASSERT(node!=(StgClosure*)NULL); // sanity check
3432 node_loc = where_is(node);
3434 fprintf(stderr,"## BQ of closure %p (%s) on [PE %d]: ",
3435 node, info_type(node), node_loc);
3438 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3440 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
3441 !end; // iterate until bqe points to a CONSTR
3442 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
3443 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3444 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3445 /* types of closures that may appear in a blocking queue */
3446 ASSERT(get_itbl(bqe)->type == TSO ||
3447 get_itbl(bqe)->type == CONSTR);
3448 /* only BQs of an RBH end with an RBH_Save closure */
3449 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3451 tso_loc = where_is((StgClosure *)bqe);
3452 switch (get_itbl(bqe)->type) {
3454 fprintf(stderr," TSO %d (%p) on [PE %d],",
3455 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
3458 fprintf(stderr," %s (IP %p),",
3459 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3460 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3461 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3462 "RBH_Save_?"), get_itbl(bqe));
3465 barf("Unexpected closure type %s in blocking queue of %p (%s)",
3466 info_type((StgClosure *)bqe), node, info_type(node));
3470 fputc('\n', stderr);
3474 Nice and easy: only TSOs on the blocking queue
3477 print_bq (StgClosure *node)
3481 ASSERT(node!=(StgClosure*)NULL); // sanity check
3482 for (tso = ((StgBlockingQueue*)node)->blocking_queue;
3483 tso != END_TSO_QUEUE;
3485 ASSERT(tso!=NULL && tso!=END_TSO_QUEUE); // sanity check
3486 ASSERT(get_itbl(tso)->type == TSO); // guess what, sanity check
3487 fprintf(stderr," TSO %d (%p),", tso->id, tso);
3489 fputc('\n', stderr);
3500 for (i=0, tso=run_queue_hd;
3501 tso != END_TSO_QUEUE;
3510 sched_belch(char *s, ...)
3514 #ifdef RTS_SUPPORTS_THREADS
3515 fprintf(stderr, "sched (task %p): ", osThreadId());
3517 fprintf(stderr, "== ");
3519 fprintf(stderr, "sched: ");
3521 vfprintf(stderr, s, ap);
3522 fprintf(stderr, "\n");