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 sched_state = SCHED_RUNNING;
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 Capability *scheduleDoGC(Capability *cap, Task *task,
232 void (*get_roots)(evac_fn));
234 static void unblockThread(Capability *cap, StgTSO *tso);
235 static rtsBool checkBlackHoles(Capability *cap);
236 static void AllRoots(evac_fn evac);
238 static StgTSO *threadStackOverflow(Capability *cap, StgTSO *tso);
240 static void raiseAsync_(Capability *cap, StgTSO *tso, StgClosure *exception,
241 rtsBool stop_at_atomically, StgPtr stop_here);
243 static void deleteThread (Capability *cap, StgTSO *tso);
244 static void deleteAllThreads (Capability *cap);
247 static void printThreadBlockage(StgTSO *tso);
248 static void printThreadStatus(StgTSO *tso);
249 void printThreadQueue(StgTSO *tso);
252 #if defined(PARALLEL_HASKELL)
253 StgTSO * createSparkThread(rtsSpark spark);
254 StgTSO * activateSpark (rtsSpark spark);
258 static char *whatNext_strs[] = {
268 /* -----------------------------------------------------------------------------
269 * Putting a thread on the run queue: different scheduling policies
270 * -------------------------------------------------------------------------- */
273 addToRunQueue( Capability *cap, StgTSO *t )
275 #if defined(PARALLEL_HASKELL)
276 if (RtsFlags.ParFlags.doFairScheduling) {
277 // this does round-robin scheduling; good for concurrency
278 appendToRunQueue(cap,t);
280 // this does unfair scheduling; good for parallelism
281 pushOnRunQueue(cap,t);
284 // this does round-robin scheduling; good for concurrency
285 appendToRunQueue(cap,t);
289 /* ---------------------------------------------------------------------------
290 Main scheduling loop.
292 We use round-robin scheduling, each thread returning to the
293 scheduler loop when one of these conditions is detected:
296 * timer expires (thread yields)
302 In a GranSim setup this loop iterates over the global event queue.
303 This revolves around the global event queue, which determines what
304 to do next. Therefore, it's more complicated than either the
305 concurrent or the parallel (GUM) setup.
308 GUM iterates over incoming messages.
309 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
310 and sends out a fish whenever it has nothing to do; in-between
311 doing the actual reductions (shared code below) it processes the
312 incoming messages and deals with delayed operations
313 (see PendingFetches).
314 This is not the ugliest code you could imagine, but it's bloody close.
316 ------------------------------------------------------------------------ */
319 schedule (Capability *initialCapability, Task *task)
323 StgThreadReturnCode ret;
326 #elif defined(PARALLEL_HASKELL)
329 rtsBool receivedFinish = rtsFalse;
331 nat tp_size, sp_size; // stats only
336 #if defined(THREADED_RTS)
337 rtsBool first = rtsTrue;
340 cap = initialCapability;
342 // Pre-condition: this task owns initialCapability.
343 // The sched_mutex is *NOT* held
344 // NB. on return, we still hold a capability.
347 sched_belch("### NEW SCHEDULER LOOP (task: %p, cap: %p)",
348 task, initialCapability);
353 // -----------------------------------------------------------
354 // Scheduler loop starts here:
356 #if defined(PARALLEL_HASKELL)
357 #define TERMINATION_CONDITION (!receivedFinish)
359 #define TERMINATION_CONDITION ((event = get_next_event()) != (rtsEvent*)NULL)
361 #define TERMINATION_CONDITION rtsTrue
364 while (TERMINATION_CONDITION) {
367 /* Choose the processor with the next event */
368 CurrentProc = event->proc;
369 CurrentTSO = event->tso;
372 #if defined(THREADED_RTS)
374 // don't yield the first time, we want a chance to run this
375 // thread for a bit, even if there are others banging at the
378 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
380 // Yield the capability to higher-priority tasks if necessary.
381 yieldCapability(&cap, task);
385 #if defined(THREADED_RTS)
386 schedulePushWork(cap,task);
389 // Check whether we have re-entered the RTS from Haskell without
390 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
392 if (cap->in_haskell) {
393 errorBelch("schedule: re-entered unsafely.\n"
394 " Perhaps a 'foreign import unsafe' should be 'safe'?");
395 stg_exit(EXIT_FAILURE);
398 // The interruption / shutdown sequence.
400 // In order to cleanly shut down the runtime, we want to:
401 // * make sure that all main threads return to their callers
402 // with the state 'Interrupted'.
403 // * clean up all OS threads assocated with the runtime
404 // * free all memory etc.
406 // So the sequence for ^C goes like this:
408 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
409 // arranges for some Capability to wake up
411 // * all threads in the system are halted, and the zombies are
412 // placed on the run queue for cleaning up. We acquire all
413 // the capabilities in order to delete the threads, this is
414 // done by scheduleDoGC() for convenience (because GC already
415 // needs to acquire all the capabilities). We can't kill
416 // threads involved in foreign calls.
418 // * sched_state := SCHED_INTERRUPTED
420 // * somebody calls shutdownHaskell(), which calls exitScheduler()
422 // * sched_state := SCHED_SHUTTING_DOWN
424 // * all workers exit when the run queue on their capability
425 // drains. All main threads will also exit when their TSO
426 // reaches the head of the run queue and they can return.
428 // * eventually all Capabilities will shut down, and the RTS can
431 // * We might be left with threads blocked in foreign calls,
432 // we should really attempt to kill these somehow (TODO);
434 switch (sched_state) {
437 case SCHED_INTERRUPTING:
438 IF_DEBUG(scheduler, sched_belch("SCHED_INTERRUPTING"));
439 #if defined(THREADED_RTS)
440 discardSparksCap(cap);
442 /* scheduleDoGC() deletes all the threads */
443 cap = scheduleDoGC(cap,task,rtsFalse,GetRoots);
445 case SCHED_INTERRUPTED:
446 IF_DEBUG(scheduler, sched_belch("SCHED_INTERRUPTED"));
448 case SCHED_SHUTTING_DOWN:
449 IF_DEBUG(scheduler, sched_belch("SCHED_SHUTTING_DOWN"));
450 // If we are a worker, just exit. If we're a bound thread
451 // then we will exit below when we've removed our TSO from
453 if (task->tso == NULL && emptyRunQueue(cap)) {
458 barf("sched_state: %d", sched_state);
461 #if defined(THREADED_RTS)
462 // If the run queue is empty, take a spark and turn it into a thread.
464 if (emptyRunQueue(cap)) {
466 spark = findSpark(cap);
469 sched_belch("turning spark of closure %p into a thread",
470 (StgClosure *)spark));
471 createSparkThread(cap,spark);
475 #endif // THREADED_RTS
477 scheduleStartSignalHandlers(cap);
479 // Only check the black holes here if we've nothing else to do.
480 // During normal execution, the black hole list only gets checked
481 // at GC time, to avoid repeatedly traversing this possibly long
482 // list each time around the scheduler.
483 if (emptyRunQueue(cap)) { scheduleCheckBlackHoles(cap); }
485 scheduleCheckBlockedThreads(cap);
487 scheduleDetectDeadlock(cap,task);
488 #if defined(THREADED_RTS)
489 cap = task->cap; // reload cap, it might have changed
492 // Normally, the only way we can get here with no threads to
493 // run is if a keyboard interrupt received during
494 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
495 // Additionally, it is not fatal for the
496 // threaded RTS to reach here with no threads to run.
498 // win32: might be here due to awaitEvent() being abandoned
499 // as a result of a console event having been delivered.
500 if ( emptyRunQueue(cap) ) {
501 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
502 ASSERT(sched_state >= SCHED_INTERRUPTING);
504 continue; // nothing to do
507 #if defined(PARALLEL_HASKELL)
508 scheduleSendPendingMessages();
509 if (emptyRunQueue(cap) && scheduleActivateSpark())
513 ASSERT(next_fish_to_send_at==0); // i.e. no delayed fishes left!
516 /* If we still have no work we need to send a FISH to get a spark
518 if (emptyRunQueue(cap)) {
519 if (!scheduleGetRemoteWork(&receivedFinish)) continue;
520 ASSERT(rtsFalse); // should not happen at the moment
522 // from here: non-empty run queue.
523 // TODO: merge above case with this, only one call processMessages() !
524 if (PacketsWaiting()) { /* process incoming messages, if
525 any pending... only in else
526 because getRemoteWork waits for
528 receivedFinish = processMessages();
533 scheduleProcessEvent(event);
537 // Get a thread to run
539 t = popRunQueue(cap);
541 #if defined(GRAN) || defined(PAR)
542 scheduleGranParReport(); // some kind of debuging output
544 // Sanity check the thread we're about to run. This can be
545 // expensive if there is lots of thread switching going on...
546 IF_DEBUG(sanity,checkTSO(t));
549 #if defined(THREADED_RTS)
550 // Check whether we can run this thread in the current task.
551 // If not, we have to pass our capability to the right task.
553 Task *bound = t->bound;
558 sched_belch("### Running thread %d in bound thread",
560 // yes, the Haskell thread is bound to the current native thread
563 sched_belch("### thread %d bound to another OS thread",
565 // no, bound to a different Haskell thread: pass to that thread
566 pushOnRunQueue(cap,t);
570 // The thread we want to run is unbound.
573 sched_belch("### this OS thread cannot run thread %d", t->id));
574 // no, the current native thread is bound to a different
575 // Haskell thread, so pass it to any worker thread
576 pushOnRunQueue(cap,t);
583 cap->r.rCurrentTSO = t;
585 /* context switches are initiated by the timer signal, unless
586 * the user specified "context switch as often as possible", with
589 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
590 && !emptyThreadQueues(cap)) {
596 IF_DEBUG(scheduler, sched_belch("-->> running thread %ld %s ...",
597 (long)t->id, whatNext_strs[t->what_next]));
599 #if defined(PROFILING)
600 startHeapProfTimer();
603 // ----------------------------------------------------------------------
604 // Run the current thread
606 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
608 prev_what_next = t->what_next;
610 errno = t->saved_errno;
611 cap->in_haskell = rtsTrue;
615 recent_activity = ACTIVITY_YES;
617 switch (prev_what_next) {
621 /* Thread already finished, return to scheduler. */
622 ret = ThreadFinished;
628 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
629 cap = regTableToCapability(r);
634 case ThreadInterpret:
635 cap = interpretBCO(cap);
640 barf("schedule: invalid what_next field");
643 cap->in_haskell = rtsFalse;
645 // The TSO might have moved, eg. if it re-entered the RTS and a GC
646 // happened. So find the new location:
647 t = cap->r.rCurrentTSO;
649 // We have run some Haskell code: there might be blackhole-blocked
650 // threads to wake up now.
651 // Lock-free test here should be ok, we're just setting a flag.
652 if ( blackhole_queue != END_TSO_QUEUE ) {
653 blackholes_need_checking = rtsTrue;
656 // And save the current errno in this thread.
657 // XXX: possibly bogus for SMP because this thread might already
658 // be running again, see code below.
659 t->saved_errno = errno;
661 #if defined(THREADED_RTS)
662 // If ret is ThreadBlocked, and this Task is bound to the TSO that
663 // blocked, we are in limbo - the TSO is now owned by whatever it
664 // is blocked on, and may in fact already have been woken up,
665 // perhaps even on a different Capability. It may be the case
666 // that task->cap != cap. We better yield this Capability
667 // immediately and return to normaility.
668 if (ret == ThreadBlocked) {
670 sched_belch("--<< thread %d (%s) stopped: blocked\n",
671 t->id, whatNext_strs[t->what_next]));
676 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
678 // ----------------------------------------------------------------------
680 // Costs for the scheduler are assigned to CCS_SYSTEM
681 #if defined(PROFILING)
686 #if defined(THREADED_RTS)
687 IF_DEBUG(scheduler,debugBelch("sched (task %p): ", (void *)(unsigned long)(unsigned int)osThreadId()););
688 #elif !defined(GRAN) && !defined(PARALLEL_HASKELL)
689 IF_DEBUG(scheduler,debugBelch("sched: "););
692 schedulePostRunThread();
694 ready_to_gc = rtsFalse;
698 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
702 scheduleHandleStackOverflow(cap,task,t);
706 if (scheduleHandleYield(cap, t, prev_what_next)) {
707 // shortcut for switching between compiler/interpreter:
713 scheduleHandleThreadBlocked(t);
717 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
718 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
722 barf("schedule: invalid thread return code %d", (int)ret);
725 if (scheduleDoHeapProfile(ready_to_gc)) { ready_to_gc = rtsFalse; }
727 cap = scheduleDoGC(cap,task,rtsFalse,GetRoots);
729 } /* end of while() */
731 IF_PAR_DEBUG(verbose,
732 debugBelch("== Leaving schedule() after having received Finish\n"));
735 /* ----------------------------------------------------------------------------
736 * Setting up the scheduler loop
737 * ------------------------------------------------------------------------- */
740 schedulePreLoop(void)
743 /* set up first event to get things going */
744 /* ToDo: assign costs for system setup and init MainTSO ! */
745 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
747 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
750 debugBelch("GRAN: Init CurrentTSO (in schedule) = %p\n",
752 G_TSO(CurrentTSO, 5));
754 if (RtsFlags.GranFlags.Light) {
755 /* Save current time; GranSim Light only */
756 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
761 /* -----------------------------------------------------------------------------
764 * Push work to other Capabilities if we have some.
765 * -------------------------------------------------------------------------- */
767 #if defined(THREADED_RTS)
769 schedulePushWork(Capability *cap USED_IF_THREADS,
770 Task *task USED_IF_THREADS)
772 Capability *free_caps[n_capabilities], *cap0;
775 // Check whether we have more threads on our run queue, or sparks
776 // in our pool, that we could hand to another Capability.
777 if ((emptyRunQueue(cap) || cap->run_queue_hd->link == END_TSO_QUEUE)
778 && sparkPoolSizeCap(cap) < 2) {
782 // First grab as many free Capabilities as we can.
783 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
784 cap0 = &capabilities[i];
785 if (cap != cap0 && tryGrabCapability(cap0,task)) {
786 if (!emptyRunQueue(cap0) || cap->returning_tasks_hd != NULL) {
787 // it already has some work, we just grabbed it at
788 // the wrong moment. Or maybe it's deadlocked!
789 releaseCapability(cap0);
791 free_caps[n_free_caps++] = cap0;
796 // we now have n_free_caps free capabilities stashed in
797 // free_caps[]. Share our run queue equally with them. This is
798 // probably the simplest thing we could do; improvements we might
799 // want to do include:
801 // - giving high priority to moving relatively new threads, on
802 // the gournds that they haven't had time to build up a
803 // working set in the cache on this CPU/Capability.
805 // - giving low priority to moving long-lived threads
807 if (n_free_caps > 0) {
808 StgTSO *prev, *t, *next;
809 rtsBool pushed_to_all;
811 IF_DEBUG(scheduler, sched_belch("excess threads on run queue and %d free capabilities, sharing...", n_free_caps));
814 pushed_to_all = rtsFalse;
816 if (cap->run_queue_hd != END_TSO_QUEUE) {
817 prev = cap->run_queue_hd;
819 prev->link = END_TSO_QUEUE;
820 for (; t != END_TSO_QUEUE; t = next) {
822 t->link = END_TSO_QUEUE;
823 if (t->what_next == ThreadRelocated
824 || t->bound == task) { // don't move my bound thread
827 } else if (i == n_free_caps) {
828 pushed_to_all = rtsTrue;
834 IF_DEBUG(scheduler, sched_belch("pushing thread %d to capability %d", t->id, free_caps[i]->no));
835 appendToRunQueue(free_caps[i],t);
836 if (t->bound) { t->bound->cap = free_caps[i]; }
840 cap->run_queue_tl = prev;
843 // If there are some free capabilities that we didn't push any
844 // threads to, then try to push a spark to each one.
845 if (!pushed_to_all) {
847 // i is the next free capability to push to
848 for (; i < n_free_caps; i++) {
849 if (emptySparkPoolCap(free_caps[i])) {
850 spark = findSpark(cap);
852 IF_DEBUG(scheduler, sched_belch("pushing spark %p to capability %d", spark, free_caps[i]->no));
853 newSpark(&(free_caps[i]->r), spark);
859 // release the capabilities
860 for (i = 0; i < n_free_caps; i++) {
861 task->cap = free_caps[i];
862 releaseCapability(free_caps[i]);
865 task->cap = cap; // reset to point to our Capability.
869 /* ----------------------------------------------------------------------------
870 * Start any pending signal handlers
871 * ------------------------------------------------------------------------- */
873 #if defined(RTS_USER_SIGNALS) && (!defined(THREADED_RTS) || defined(mingw32_HOST_OS))
875 scheduleStartSignalHandlers(Capability *cap)
877 if (signals_pending()) { // safe outside the lock
878 startSignalHandlers(cap);
883 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
888 /* ----------------------------------------------------------------------------
889 * Check for blocked threads that can be woken up.
890 * ------------------------------------------------------------------------- */
893 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
895 #if !defined(THREADED_RTS)
897 // Check whether any waiting threads need to be woken up. If the
898 // run queue is empty, and there are no other tasks running, we
899 // can wait indefinitely for something to happen.
901 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
903 awaitEvent( emptyRunQueue(cap) && !blackholes_need_checking );
909 /* ----------------------------------------------------------------------------
910 * Check for threads blocked on BLACKHOLEs that can be woken up
911 * ------------------------------------------------------------------------- */
913 scheduleCheckBlackHoles (Capability *cap)
915 if ( blackholes_need_checking ) // check without the lock first
917 ACQUIRE_LOCK(&sched_mutex);
918 if ( blackholes_need_checking ) {
919 checkBlackHoles(cap);
920 blackholes_need_checking = rtsFalse;
922 RELEASE_LOCK(&sched_mutex);
926 /* ----------------------------------------------------------------------------
927 * Detect deadlock conditions and attempt to resolve them.
928 * ------------------------------------------------------------------------- */
931 scheduleDetectDeadlock (Capability *cap, Task *task)
934 #if defined(PARALLEL_HASKELL)
935 // ToDo: add deadlock detection in GUM (similar to THREADED_RTS) -- HWL
940 * Detect deadlock: when we have no threads to run, there are no
941 * threads blocked, waiting for I/O, or sleeping, and all the
942 * other tasks are waiting for work, we must have a deadlock of
945 if ( emptyThreadQueues(cap) )
947 #if defined(THREADED_RTS)
949 * In the threaded RTS, we only check for deadlock if there
950 * has been no activity in a complete timeslice. This means
951 * we won't eagerly start a full GC just because we don't have
952 * any threads to run currently.
954 if (recent_activity != ACTIVITY_INACTIVE) return;
957 IF_DEBUG(scheduler, sched_belch("deadlocked, forcing major GC..."));
959 // Garbage collection can release some new threads due to
960 // either (a) finalizers or (b) threads resurrected because
961 // they are unreachable and will therefore be sent an
962 // exception. Any threads thus released will be immediately
964 cap = scheduleDoGC (cap, task, rtsTrue/*force major GC*/, GetRoots);
966 recent_activity = ACTIVITY_DONE_GC;
968 if ( !emptyRunQueue(cap) ) return;
970 #if defined(RTS_USER_SIGNALS) && (!defined(THREADED_RTS) || defined(mingw32_HOST_OS))
971 /* If we have user-installed signal handlers, then wait
972 * for signals to arrive rather then bombing out with a
975 if ( anyUserHandlers() ) {
977 sched_belch("still deadlocked, waiting for signals..."));
981 if (signals_pending()) {
982 startSignalHandlers(cap);
985 // either we have threads to run, or we were interrupted:
986 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
990 #if !defined(THREADED_RTS)
991 /* Probably a real deadlock. Send the current main thread the
992 * Deadlock exception.
995 switch (task->tso->why_blocked) {
997 case BlockedOnBlackHole:
998 case BlockedOnException:
1000 raiseAsync(cap, task->tso, (StgClosure *)NonTermination_closure);
1003 barf("deadlock: main thread blocked in a strange way");
1011 /* ----------------------------------------------------------------------------
1012 * Process an event (GRAN only)
1013 * ------------------------------------------------------------------------- */
1017 scheduleProcessEvent(rtsEvent *event)
1021 if (RtsFlags.GranFlags.Light)
1022 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
1024 /* adjust time based on time-stamp */
1025 if (event->time > CurrentTime[CurrentProc] &&
1026 event->evttype != ContinueThread)
1027 CurrentTime[CurrentProc] = event->time;
1029 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
1030 if (!RtsFlags.GranFlags.Light)
1033 IF_DEBUG(gran, debugBelch("GRAN: switch by event-type\n"));
1035 /* main event dispatcher in GranSim */
1036 switch (event->evttype) {
1037 /* Should just be continuing execution */
1038 case ContinueThread:
1039 IF_DEBUG(gran, debugBelch("GRAN: doing ContinueThread\n"));
1040 /* ToDo: check assertion
1041 ASSERT(run_queue_hd != (StgTSO*)NULL &&
1042 run_queue_hd != END_TSO_QUEUE);
1044 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
1045 if (!RtsFlags.GranFlags.DoAsyncFetch &&
1046 procStatus[CurrentProc]==Fetching) {
1047 debugBelch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]\n",
1048 CurrentTSO->id, CurrentTSO, CurrentProc);
1051 /* Ignore ContinueThreads for completed threads */
1052 if (CurrentTSO->what_next == ThreadComplete) {
1053 debugBelch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)\n",
1054 CurrentTSO->id, CurrentTSO, CurrentProc);
1057 /* Ignore ContinueThreads for threads that are being migrated */
1058 if (PROCS(CurrentTSO)==Nowhere) {
1059 debugBelch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)\n",
1060 CurrentTSO->id, CurrentTSO, CurrentProc);
1063 /* The thread should be at the beginning of the run queue */
1064 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
1065 debugBelch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread\n",
1066 CurrentTSO->id, CurrentTSO, CurrentProc);
1067 break; // run the thread anyway
1070 new_event(proc, proc, CurrentTime[proc],
1072 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
1074 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
1075 break; // now actually run the thread; DaH Qu'vam yImuHbej
1078 do_the_fetchnode(event);
1079 goto next_thread; /* handle next event in event queue */
1082 do_the_globalblock(event);
1083 goto next_thread; /* handle next event in event queue */
1086 do_the_fetchreply(event);
1087 goto next_thread; /* handle next event in event queue */
1089 case UnblockThread: /* Move from the blocked queue to the tail of */
1090 do_the_unblock(event);
1091 goto next_thread; /* handle next event in event queue */
1093 case ResumeThread: /* Move from the blocked queue to the tail of */
1094 /* the runnable queue ( i.e. Qu' SImqa'lu') */
1095 event->tso->gran.blocktime +=
1096 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
1097 do_the_startthread(event);
1098 goto next_thread; /* handle next event in event queue */
1101 do_the_startthread(event);
1102 goto next_thread; /* handle next event in event queue */
1105 do_the_movethread(event);
1106 goto next_thread; /* handle next event in event queue */
1109 do_the_movespark(event);
1110 goto next_thread; /* handle next event in event queue */
1113 do_the_findwork(event);
1114 goto next_thread; /* handle next event in event queue */
1117 barf("Illegal event type %u\n", event->evttype);
1120 /* This point was scheduler_loop in the old RTS */
1122 IF_DEBUG(gran, debugBelch("GRAN: after main switch\n"));
1124 TimeOfLastEvent = CurrentTime[CurrentProc];
1125 TimeOfNextEvent = get_time_of_next_event();
1126 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
1127 // CurrentTSO = ThreadQueueHd;
1129 IF_DEBUG(gran, debugBelch("GRAN: time of next event is: %ld\n",
1132 if (RtsFlags.GranFlags.Light)
1133 GranSimLight_leave_system(event, &ActiveTSO);
1135 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
1138 debugBelch("GRAN: end of time-slice is %#lx\n", EndOfTimeSlice));
1140 /* in a GranSim setup the TSO stays on the run queue */
1142 /* Take a thread from the run queue. */
1143 POP_RUN_QUEUE(t); // take_off_run_queue(t);
1146 debugBelch("GRAN: About to run current thread, which is\n");
1149 context_switch = 0; // turned on via GranYield, checking events and time slice
1152 DumpGranEvent(GR_SCHEDULE, t));
1154 procStatus[CurrentProc] = Busy;
1158 /* ----------------------------------------------------------------------------
1159 * Send pending messages (PARALLEL_HASKELL only)
1160 * ------------------------------------------------------------------------- */
1162 #if defined(PARALLEL_HASKELL)
1164 scheduleSendPendingMessages(void)
1170 # if defined(PAR) // global Mem.Mgmt., omit for now
1171 if (PendingFetches != END_BF_QUEUE) {
1176 if (RtsFlags.ParFlags.BufferTime) {
1177 // if we use message buffering, we must send away all message
1178 // packets which have become too old...
1184 /* ----------------------------------------------------------------------------
1185 * Activate spark threads (PARALLEL_HASKELL only)
1186 * ------------------------------------------------------------------------- */
1188 #if defined(PARALLEL_HASKELL)
1190 scheduleActivateSpark(void)
1193 ASSERT(emptyRunQueue());
1194 /* We get here if the run queue is empty and want some work.
1195 We try to turn a spark into a thread, and add it to the run queue,
1196 from where it will be picked up in the next iteration of the scheduler
1200 /* :-[ no local threads => look out for local sparks */
1201 /* the spark pool for the current PE */
1202 pool = &(cap.r.rSparks); // JB: cap = (old) MainCap
1203 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
1204 pool->hd < pool->tl) {
1206 * ToDo: add GC code check that we really have enough heap afterwards!!
1208 * If we're here (no runnable threads) and we have pending
1209 * sparks, we must have a space problem. Get enough space
1210 * to turn one of those pending sparks into a
1214 spark = findSpark(rtsFalse); /* get a spark */
1215 if (spark != (rtsSpark) NULL) {
1216 tso = createThreadFromSpark(spark); /* turn the spark into a thread */
1217 IF_PAR_DEBUG(fish, // schedule,
1218 debugBelch("==== schedule: Created TSO %d (%p); %d threads active\n",
1219 tso->id, tso, advisory_thread_count));
1221 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
1222 IF_PAR_DEBUG(fish, // schedule,
1223 debugBelch("==^^ failed to create thread from spark @ %lx\n",
1225 return rtsFalse; /* failed to generate a thread */
1226 } /* otherwise fall through & pick-up new tso */
1228 IF_PAR_DEBUG(fish, // schedule,
1229 debugBelch("==^^ no local sparks (spark pool contains only NFs: %d)\n",
1230 spark_queue_len(pool)));
1231 return rtsFalse; /* failed to generate a thread */
1233 return rtsTrue; /* success in generating a thread */
1234 } else { /* no more threads permitted or pool empty */
1235 return rtsFalse; /* failed to generateThread */
1238 tso = NULL; // avoid compiler warning only
1239 return rtsFalse; /* dummy in non-PAR setup */
1242 #endif // PARALLEL_HASKELL
1244 /* ----------------------------------------------------------------------------
1245 * Get work from a remote node (PARALLEL_HASKELL only)
1246 * ------------------------------------------------------------------------- */
1248 #if defined(PARALLEL_HASKELL)
1250 scheduleGetRemoteWork(rtsBool *receivedFinish)
1252 ASSERT(emptyRunQueue());
1254 if (RtsFlags.ParFlags.BufferTime) {
1255 IF_PAR_DEBUG(verbose,
1256 debugBelch("...send all pending data,"));
1259 for (i=1; i<=nPEs; i++)
1260 sendImmediately(i); // send all messages away immediately
1264 //++EDEN++ idle() , i.e. send all buffers, wait for work
1265 // suppress fishing in EDEN... just look for incoming messages
1266 // (blocking receive)
1267 IF_PAR_DEBUG(verbose,
1268 debugBelch("...wait for incoming messages...\n"));
1269 *receivedFinish = processMessages(); // blocking receive...
1271 // and reenter scheduling loop after having received something
1272 // (return rtsFalse below)
1274 # else /* activate SPARKS machinery */
1275 /* We get here, if we have no work, tried to activate a local spark, but still
1276 have no work. We try to get a remote spark, by sending a FISH message.
1277 Thread migration should be added here, and triggered when a sequence of
1278 fishes returns without work. */
1279 delay = (RtsFlags.ParFlags.fishDelay!=0ll ? RtsFlags.ParFlags.fishDelay : 0ll);
1281 /* =8-[ no local sparks => look for work on other PEs */
1283 * We really have absolutely no work. Send out a fish
1284 * (there may be some out there already), and wait for
1285 * something to arrive. We clearly can't run any threads
1286 * until a SCHEDULE or RESUME arrives, and so that's what
1287 * we're hoping to see. (Of course, we still have to
1288 * respond to other types of messages.)
1290 rtsTime now = msTime() /*CURRENT_TIME*/;
1291 IF_PAR_DEBUG(verbose,
1292 debugBelch("-- now=%ld\n", now));
1293 IF_PAR_DEBUG(fish, // verbose,
1294 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1295 (last_fish_arrived_at!=0 &&
1296 last_fish_arrived_at+delay > now)) {
1297 debugBelch("--$$ <%llu> delaying FISH until %llu (last fish %llu, delay %llu)\n",
1298 now, last_fish_arrived_at+delay,
1299 last_fish_arrived_at,
1303 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1304 advisory_thread_count < RtsFlags.ParFlags.maxThreads) { // send a FISH, but when?
1305 if (last_fish_arrived_at==0 ||
1306 (last_fish_arrived_at+delay <= now)) { // send FISH now!
1307 /* outstandingFishes is set in sendFish, processFish;
1308 avoid flooding system with fishes via delay */
1309 next_fish_to_send_at = 0;
1311 /* ToDo: this should be done in the main scheduling loop to avoid the
1312 busy wait here; not so bad if fish delay is very small */
1313 int iq = 0; // DEBUGGING -- HWL
1314 next_fish_to_send_at = last_fish_arrived_at+delay; // remember when to send
1315 /* send a fish when ready, but process messages that arrive in the meantime */
1317 if (PacketsWaiting()) {
1319 *receivedFinish = processMessages();
1322 } while (!*receivedFinish || now<next_fish_to_send_at);
1323 // JB: This means the fish could become obsolete, if we receive
1324 // work. Better check for work again?
1325 // last line: while (!receivedFinish || !haveWork || now<...)
1326 // next line: if (receivedFinish || haveWork )
1328 if (*receivedFinish) // no need to send a FISH if we are finishing anyway
1329 return rtsFalse; // NB: this will leave scheduler loop
1330 // immediately after return!
1332 IF_PAR_DEBUG(fish, // verbose,
1333 debugBelch("--$$ <%llu> sent delayed fish (%d processMessages); active/total threads=%d/%d\n",now,iq,run_queue_len(),advisory_thread_count));
1337 // JB: IMHO, this should all be hidden inside sendFish(...)
1339 sendFish(pe, thisPE, NEW_FISH_AGE, NEW_FISH_HISTORY,
1342 // Global statistics: count no. of fishes
1343 if (RtsFlags.ParFlags.ParStats.Global &&
1344 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1345 globalParStats.tot_fish_mess++;
1349 /* delayed fishes must have been sent by now! */
1350 next_fish_to_send_at = 0;
1353 *receivedFinish = processMessages();
1354 # endif /* SPARKS */
1357 /* NB: this function always returns rtsFalse, meaning the scheduler
1358 loop continues with the next iteration;
1360 return code means success in finding work; we enter this function
1361 if there is no local work, thus have to send a fish which takes
1362 time until it arrives with work; in the meantime we should process
1363 messages in the main loop;
1366 #endif // PARALLEL_HASKELL
1368 /* ----------------------------------------------------------------------------
1369 * PAR/GRAN: Report stats & debugging info(?)
1370 * ------------------------------------------------------------------------- */
1372 #if defined(PAR) || defined(GRAN)
1374 scheduleGranParReport(void)
1376 ASSERT(run_queue_hd != END_TSO_QUEUE);
1378 /* Take a thread from the run queue, if we have work */
1379 POP_RUN_QUEUE(t); // take_off_run_queue(END_TSO_QUEUE);
1381 /* If this TSO has got its outport closed in the meantime,
1382 * it mustn't be run. Instead, we have to clean it up as if it was finished.
1383 * It has to be marked as TH_DEAD for this purpose.
1384 * If it is TH_TERM instead, it is supposed to have finished in the normal way.
1386 JB: TODO: investigate wether state change field could be nuked
1387 entirely and replaced by the normal tso state (whatnext
1388 field). All we want to do is to kill tsos from outside.
1391 /* ToDo: write something to the log-file
1392 if (RTSflags.ParFlags.granSimStats && !sameThread)
1393 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
1397 /* the spark pool for the current PE */
1398 pool = &(cap.r.rSparks); // cap = (old) MainCap
1401 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1402 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1405 debugBelch("--=^ %d threads, %d sparks on [%#x]\n",
1406 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1408 if (RtsFlags.ParFlags.ParStats.Full &&
1409 (t->par.sparkname != (StgInt)0) && // only log spark generated threads
1410 (emitSchedule || // forced emit
1411 (t && LastTSO && t->id != LastTSO->id))) {
1413 we are running a different TSO, so write a schedule event to log file
1414 NB: If we use fair scheduling we also have to write a deschedule
1415 event for LastTSO; with unfair scheduling we know that the
1416 previous tso has blocked whenever we switch to another tso, so
1417 we don't need it in GUM for now
1419 IF_PAR_DEBUG(fish, // schedule,
1420 debugBelch("____ scheduling spark generated thread %d (%lx) (%lx) via a forced emit\n",t->id,t,t->par.sparkname));
1422 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1423 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
1424 emitSchedule = rtsFalse;
1429 /* ----------------------------------------------------------------------------
1430 * After running a thread...
1431 * ------------------------------------------------------------------------- */
1434 schedulePostRunThread(void)
1437 /* HACK 675: if the last thread didn't yield, make sure to print a
1438 SCHEDULE event to the log file when StgRunning the next thread, even
1439 if it is the same one as before */
1441 TimeOfLastYield = CURRENT_TIME;
1444 /* some statistics gathering in the parallel case */
1446 #if defined(GRAN) || defined(PAR) || defined(EDEN)
1450 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
1451 globalGranStats.tot_heapover++;
1453 globalParStats.tot_heapover++;
1460 DumpGranEvent(GR_DESCHEDULE, t));
1461 globalGranStats.tot_stackover++;
1464 // DumpGranEvent(GR_DESCHEDULE, t);
1465 globalParStats.tot_stackover++;
1469 case ThreadYielding:
1472 DumpGranEvent(GR_DESCHEDULE, t));
1473 globalGranStats.tot_yields++;
1476 // DumpGranEvent(GR_DESCHEDULE, t);
1477 globalParStats.tot_yields++;
1484 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1485 t->id, t, whatNext_strs[t->what_next], t->block_info.closure,
1486 (t->block_info.closure==(StgClosure*)NULL ? 99 : where_is(t->block_info.closure)));
1487 if (t->block_info.closure!=(StgClosure*)NULL)
1488 print_bq(t->block_info.closure);
1491 // ??? needed; should emit block before
1493 DumpGranEvent(GR_DESCHEDULE, t));
1494 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1497 ASSERT(procStatus[CurrentProc]==Busy ||
1498 ((procStatus[CurrentProc]==Fetching) &&
1499 (t->block_info.closure!=(StgClosure*)NULL)));
1500 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1501 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1502 procStatus[CurrentProc]==Fetching))
1503 procStatus[CurrentProc] = Idle;
1506 //++PAR++ blockThread() writes the event (change?)
1510 case ThreadFinished:
1514 barf("parGlobalStats: unknown return code");
1520 /* -----------------------------------------------------------------------------
1521 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1522 * -------------------------------------------------------------------------- */
1525 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1527 // did the task ask for a large block?
1528 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1529 // if so, get one and push it on the front of the nursery.
1533 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1536 debugBelch("--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1537 (long)t->id, whatNext_strs[t->what_next], blocks));
1539 // don't do this if the nursery is (nearly) full, we'll GC first.
1540 if (cap->r.rCurrentNursery->link != NULL ||
1541 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1542 // if the nursery has only one block.
1545 bd = allocGroup( blocks );
1547 cap->r.rNursery->n_blocks += blocks;
1549 // link the new group into the list
1550 bd->link = cap->r.rCurrentNursery;
1551 bd->u.back = cap->r.rCurrentNursery->u.back;
1552 if (cap->r.rCurrentNursery->u.back != NULL) {
1553 cap->r.rCurrentNursery->u.back->link = bd;
1555 #if !defined(THREADED_RTS)
1556 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
1557 g0s0 == cap->r.rNursery);
1559 cap->r.rNursery->blocks = bd;
1561 cap->r.rCurrentNursery->u.back = bd;
1563 // initialise it as a nursery block. We initialise the
1564 // step, gen_no, and flags field of *every* sub-block in
1565 // this large block, because this is easier than making
1566 // sure that we always find the block head of a large
1567 // block whenever we call Bdescr() (eg. evacuate() and
1568 // isAlive() in the GC would both have to do this, at
1572 for (x = bd; x < bd + blocks; x++) {
1573 x->step = cap->r.rNursery;
1579 // This assert can be a killer if the app is doing lots
1580 // of large block allocations.
1581 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1583 // now update the nursery to point to the new block
1584 cap->r.rCurrentNursery = bd;
1586 // we might be unlucky and have another thread get on the
1587 // run queue before us and steal the large block, but in that
1588 // case the thread will just end up requesting another large
1590 pushOnRunQueue(cap,t);
1591 return rtsFalse; /* not actually GC'ing */
1596 debugBelch("--<< thread %ld (%s) stopped: HeapOverflow\n",
1597 (long)t->id, whatNext_strs[t->what_next]));
1599 ASSERT(!is_on_queue(t,CurrentProc));
1600 #elif defined(PARALLEL_HASKELL)
1601 /* Currently we emit a DESCHEDULE event before GC in GUM.
1602 ToDo: either add separate event to distinguish SYSTEM time from rest
1603 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1604 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1605 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1606 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1607 emitSchedule = rtsTrue;
1611 pushOnRunQueue(cap,t);
1613 /* actual GC is done at the end of the while loop in schedule() */
1616 /* -----------------------------------------------------------------------------
1617 * Handle a thread that returned to the scheduler with ThreadStackOverflow
1618 * -------------------------------------------------------------------------- */
1621 scheduleHandleStackOverflow (Capability *cap, Task *task, StgTSO *t)
1623 IF_DEBUG(scheduler,debugBelch("--<< thread %ld (%s) stopped, StackOverflow\n",
1624 (long)t->id, whatNext_strs[t->what_next]));
1625 /* just adjust the stack for this thread, then pop it back
1629 /* enlarge the stack */
1630 StgTSO *new_t = threadStackOverflow(cap, t);
1632 /* The TSO attached to this Task may have moved, so update the
1635 if (task->tso == t) {
1638 pushOnRunQueue(cap,new_t);
1642 /* -----------------------------------------------------------------------------
1643 * Handle a thread that returned to the scheduler with ThreadYielding
1644 * -------------------------------------------------------------------------- */
1647 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1649 // Reset the context switch flag. We don't do this just before
1650 // running the thread, because that would mean we would lose ticks
1651 // during GC, which can lead to unfair scheduling (a thread hogs
1652 // the CPU because the tick always arrives during GC). This way
1653 // penalises threads that do a lot of allocation, but that seems
1654 // better than the alternative.
1657 /* put the thread back on the run queue. Then, if we're ready to
1658 * GC, check whether this is the last task to stop. If so, wake
1659 * up the GC thread. getThread will block during a GC until the
1663 if (t->what_next != prev_what_next) {
1664 debugBelch("--<< thread %ld (%s) stopped to switch evaluators\n",
1665 (long)t->id, whatNext_strs[t->what_next]);
1667 debugBelch("--<< thread %ld (%s) stopped, yielding\n",
1668 (long)t->id, whatNext_strs[t->what_next]);
1673 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1675 ASSERT(t->link == END_TSO_QUEUE);
1677 // Shortcut if we're just switching evaluators: don't bother
1678 // doing stack squeezing (which can be expensive), just run the
1680 if (t->what_next != prev_what_next) {
1685 ASSERT(!is_on_queue(t,CurrentProc));
1688 //debugBelch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1689 checkThreadQsSanity(rtsTrue));
1693 addToRunQueue(cap,t);
1696 /* add a ContinueThread event to actually process the thread */
1697 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1699 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1701 debugBelch("GRAN: eventq and runnableq after adding yielded thread to queue again:\n");
1708 /* -----------------------------------------------------------------------------
1709 * Handle a thread that returned to the scheduler with ThreadBlocked
1710 * -------------------------------------------------------------------------- */
1713 scheduleHandleThreadBlocked( StgTSO *t
1714 #if !defined(GRAN) && !defined(DEBUG)
1721 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: \n",
1722 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)));
1723 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1725 // ??? needed; should emit block before
1727 DumpGranEvent(GR_DESCHEDULE, t));
1728 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1731 ASSERT(procStatus[CurrentProc]==Busy ||
1732 ((procStatus[CurrentProc]==Fetching) &&
1733 (t->block_info.closure!=(StgClosure*)NULL)));
1734 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1735 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1736 procStatus[CurrentProc]==Fetching))
1737 procStatus[CurrentProc] = Idle;
1741 debugBelch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: \n",
1742 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1745 if (t->block_info.closure!=(StgClosure*)NULL)
1746 print_bq(t->block_info.closure));
1748 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1751 /* whatever we schedule next, we must log that schedule */
1752 emitSchedule = rtsTrue;
1756 // We don't need to do anything. The thread is blocked, and it
1757 // has tidied up its stack and placed itself on whatever queue
1758 // it needs to be on.
1760 #if !defined(THREADED_RTS)
1761 ASSERT(t->why_blocked != NotBlocked);
1762 // This might not be true under THREADED_RTS: we don't have
1763 // exclusive access to this TSO, so someone might have
1764 // woken it up by now. This actually happens: try
1765 // conc023 +RTS -N2.
1769 debugBelch("--<< thread %d (%s) stopped: ",
1770 t->id, whatNext_strs[t->what_next]);
1771 printThreadBlockage(t);
1774 /* Only for dumping event to log file
1775 ToDo: do I need this in GranSim, too?
1781 /* -----------------------------------------------------------------------------
1782 * Handle a thread that returned to the scheduler with ThreadFinished
1783 * -------------------------------------------------------------------------- */
1786 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1788 /* Need to check whether this was a main thread, and if so,
1789 * return with the return value.
1791 * We also end up here if the thread kills itself with an
1792 * uncaught exception, see Exception.cmm.
1794 IF_DEBUG(scheduler,debugBelch("--++ thread %d (%s) finished\n",
1795 t->id, whatNext_strs[t->what_next]));
1798 endThread(t, CurrentProc); // clean-up the thread
1799 #elif defined(PARALLEL_HASKELL)
1800 /* For now all are advisory -- HWL */
1801 //if(t->priority==AdvisoryPriority) ??
1802 advisory_thread_count--; // JB: Caution with this counter, buggy!
1805 if(t->dist.priority==RevalPriority)
1809 # if defined(EDENOLD)
1810 // the thread could still have an outport... (BUG)
1811 if (t->eden.outport != -1) {
1812 // delete the outport for the tso which has finished...
1813 IF_PAR_DEBUG(eden_ports,
1814 debugBelch("WARNING: Scheduler removes outport %d for TSO %d.\n",
1815 t->eden.outport, t->id));
1818 // thread still in the process (HEAVY BUG! since outport has just been closed...)
1819 if (t->eden.epid != -1) {
1820 IF_PAR_DEBUG(eden_ports,
1821 debugBelch("WARNING: Scheduler removes TSO %d from process %d .\n",
1822 t->id, t->eden.epid));
1823 removeTSOfromProcess(t);
1828 if (RtsFlags.ParFlags.ParStats.Full &&
1829 !RtsFlags.ParFlags.ParStats.Suppressed)
1830 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1832 // t->par only contains statistics: left out for now...
1834 debugBelch("**** end thread: ended sparked thread %d (%lx); sparkname: %lx\n",
1835 t->id,t,t->par.sparkname));
1837 #endif // PARALLEL_HASKELL
1840 // Check whether the thread that just completed was a bound
1841 // thread, and if so return with the result.
1843 // There is an assumption here that all thread completion goes
1844 // through this point; we need to make sure that if a thread
1845 // ends up in the ThreadKilled state, that it stays on the run
1846 // queue so it can be dealt with here.
1851 if (t->bound != task) {
1852 #if !defined(THREADED_RTS)
1853 // Must be a bound thread that is not the topmost one. Leave
1854 // it on the run queue until the stack has unwound to the
1855 // point where we can deal with this. Leaving it on the run
1856 // queue also ensures that the garbage collector knows about
1857 // this thread and its return value (it gets dropped from the
1858 // all_threads list so there's no other way to find it).
1859 appendToRunQueue(cap,t);
1862 // this cannot happen in the threaded RTS, because a
1863 // bound thread can only be run by the appropriate Task.
1864 barf("finished bound thread that isn't mine");
1868 ASSERT(task->tso == t);
1870 if (t->what_next == ThreadComplete) {
1872 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
1873 *(task->ret) = (StgClosure *)task->tso->sp[1];
1875 task->stat = Success;
1878 *(task->ret) = NULL;
1880 if (sched_state >= SCHED_INTERRUPTING) {
1881 task->stat = Interrupted;
1883 task->stat = Killed;
1887 removeThreadLabel((StgWord)task->tso->id);
1889 return rtsTrue; // tells schedule() to return
1895 /* -----------------------------------------------------------------------------
1896 * Perform a heap census, if PROFILING
1897 * -------------------------------------------------------------------------- */
1900 scheduleDoHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1902 #if defined(PROFILING)
1903 // When we have +RTS -i0 and we're heap profiling, do a census at
1904 // every GC. This lets us get repeatable runs for debugging.
1905 if (performHeapProfile ||
1906 (RtsFlags.ProfFlags.profileInterval==0 &&
1907 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1909 // checking black holes is necessary before GC, otherwise
1910 // there may be threads that are unreachable except by the
1911 // blackhole queue, which the GC will consider to be
1913 scheduleCheckBlackHoles(&MainCapability);
1915 IF_DEBUG(scheduler, sched_belch("garbage collecting before heap census"));
1916 GarbageCollect(GetRoots, rtsTrue);
1918 IF_DEBUG(scheduler, sched_belch("performing heap census"));
1921 performHeapProfile = rtsFalse;
1922 return rtsTrue; // true <=> we already GC'd
1928 /* -----------------------------------------------------------------------------
1929 * Perform a garbage collection if necessary
1930 * -------------------------------------------------------------------------- */
1933 scheduleDoGC (Capability *cap, Task *task USED_IF_THREADS,
1934 rtsBool force_major, void (*get_roots)(evac_fn))
1938 static volatile StgWord waiting_for_gc;
1939 rtsBool was_waiting;
1944 // In order to GC, there must be no threads running Haskell code.
1945 // Therefore, the GC thread needs to hold *all* the capabilities,
1946 // and release them after the GC has completed.
1948 // This seems to be the simplest way: previous attempts involved
1949 // making all the threads with capabilities give up their
1950 // capabilities and sleep except for the *last* one, which
1951 // actually did the GC. But it's quite hard to arrange for all
1952 // the other tasks to sleep and stay asleep.
1955 was_waiting = cas(&waiting_for_gc, 0, 1);
1958 IF_DEBUG(scheduler, sched_belch("someone else is trying to GC..."));
1959 if (cap) yieldCapability(&cap,task);
1960 } while (waiting_for_gc);
1961 return cap; // NOTE: task->cap might have changed here
1964 for (i=0; i < n_capabilities; i++) {
1965 IF_DEBUG(scheduler, sched_belch("ready_to_gc, grabbing all the capabilies (%d/%d)", i, n_capabilities));
1966 if (cap != &capabilities[i]) {
1967 Capability *pcap = &capabilities[i];
1968 // we better hope this task doesn't get migrated to
1969 // another Capability while we're waiting for this one.
1970 // It won't, because load balancing happens while we have
1971 // all the Capabilities, but even so it's a slightly
1972 // unsavoury invariant.
1975 waitForReturnCapability(&pcap, task);
1976 if (pcap != &capabilities[i]) {
1977 barf("scheduleDoGC: got the wrong capability");
1982 waiting_for_gc = rtsFalse;
1985 /* Kick any transactions which are invalid back to their
1986 * atomically frames. When next scheduled they will try to
1987 * commit, this commit will fail and they will retry.
1992 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1993 if (t->what_next == ThreadRelocated) {
1996 next = t->global_link;
1997 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
1998 if (!stmValidateNestOfTransactions (t -> trec)) {
1999 IF_DEBUG(stm, sched_belch("trec %p found wasting its time", t));
2001 // strip the stack back to the
2002 // ATOMICALLY_FRAME, aborting the (nested)
2003 // transaction, and saving the stack of any
2004 // partially-evaluated thunks on the heap.
2005 raiseAsync_(&capabilities[0], t, NULL, rtsTrue, NULL);
2008 ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
2016 // so this happens periodically:
2017 if (cap) scheduleCheckBlackHoles(cap);
2019 IF_DEBUG(scheduler, printAllThreads());
2022 * We now have all the capabilities; if we're in an interrupting
2023 * state, then we should take the opportunity to delete all the
2024 * threads in the system.
2026 if (sched_state >= SCHED_INTERRUPTING) {
2027 deleteAllThreads(&capabilities[0]);
2028 sched_state = SCHED_INTERRUPTED;
2031 /* everybody back, start the GC.
2032 * Could do it in this thread, or signal a condition var
2033 * to do it in another thread. Either way, we need to
2034 * broadcast on gc_pending_cond afterward.
2036 #if defined(THREADED_RTS)
2037 IF_DEBUG(scheduler,sched_belch("doing GC"));
2039 GarbageCollect(get_roots, force_major);
2041 #if defined(THREADED_RTS)
2042 // release our stash of capabilities.
2043 for (i = 0; i < n_capabilities; i++) {
2044 if (cap != &capabilities[i]) {
2045 task->cap = &capabilities[i];
2046 releaseCapability(&capabilities[i]);
2057 /* add a ContinueThread event to continue execution of current thread */
2058 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
2060 t, (StgClosure*)NULL, (rtsSpark*)NULL);
2062 debugBelch("GRAN: eventq and runnableq after Garbage collection:\n\n");
2070 /* ---------------------------------------------------------------------------
2071 * rtsSupportsBoundThreads(): is the RTS built to support bound threads?
2072 * used by Control.Concurrent for error checking.
2073 * ------------------------------------------------------------------------- */
2076 rtsSupportsBoundThreads(void)
2078 #if defined(THREADED_RTS)
2085 /* ---------------------------------------------------------------------------
2086 * isThreadBound(tso): check whether tso is bound to an OS thread.
2087 * ------------------------------------------------------------------------- */
2090 isThreadBound(StgTSO* tso USED_IF_THREADS)
2092 #if defined(THREADED_RTS)
2093 return (tso->bound != NULL);
2098 /* ---------------------------------------------------------------------------
2099 * Singleton fork(). Do not copy any running threads.
2100 * ------------------------------------------------------------------------- */
2102 #if !defined(mingw32_HOST_OS)
2103 #define FORKPROCESS_PRIMOP_SUPPORTED
2106 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2108 deleteThread_(Capability *cap, StgTSO *tso);
2111 forkProcess(HsStablePtr *entry
2112 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
2117 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2123 #if defined(THREADED_RTS)
2124 if (RtsFlags.ParFlags.nNodes > 1) {
2125 errorBelch("forking not supported with +RTS -N<n> greater than 1");
2126 stg_exit(EXIT_FAILURE);
2130 IF_DEBUG(scheduler,sched_belch("forking!"));
2132 // ToDo: for SMP, we should probably acquire *all* the capabilities
2137 if (pid) { // parent
2139 // just return the pid
2145 // Now, all OS threads except the thread that forked are
2146 // stopped. We need to stop all Haskell threads, including
2147 // those involved in foreign calls. Also we need to delete
2148 // all Tasks, because they correspond to OS threads that are
2151 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
2152 if (t->what_next == ThreadRelocated) {
2155 next = t->global_link;
2156 // don't allow threads to catch the ThreadKilled
2157 // exception, but we do want to raiseAsync() because these
2158 // threads may be evaluating thunks that we need later.
2159 deleteThread_(cap,t);
2163 // Empty the run queue. It seems tempting to let all the
2164 // killed threads stay on the run queue as zombies to be
2165 // cleaned up later, but some of them correspond to bound
2166 // threads for which the corresponding Task does not exist.
2167 cap->run_queue_hd = END_TSO_QUEUE;
2168 cap->run_queue_tl = END_TSO_QUEUE;
2170 // Any suspended C-calling Tasks are no more, their OS threads
2172 cap->suspended_ccalling_tasks = NULL;
2174 // Empty the all_threads list. Otherwise, the garbage
2175 // collector may attempt to resurrect some of these threads.
2176 all_threads = END_TSO_QUEUE;
2178 // Wipe the task list, except the current Task.
2179 ACQUIRE_LOCK(&sched_mutex);
2180 for (task = all_tasks; task != NULL; task=task->all_link) {
2181 if (task != cap->running_task) {
2185 RELEASE_LOCK(&sched_mutex);
2187 #if defined(THREADED_RTS)
2188 // Wipe our spare workers list, they no longer exist. New
2189 // workers will be created if necessary.
2190 cap->spare_workers = NULL;
2191 cap->returning_tasks_hd = NULL;
2192 cap->returning_tasks_tl = NULL;
2195 cap = rts_evalStableIO(cap, entry, NULL); // run the action
2196 rts_checkSchedStatus("forkProcess",cap);
2199 hs_exit(); // clean up and exit
2200 stg_exit(EXIT_SUCCESS);
2202 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
2203 barf("forkProcess#: primop not supported on this platform, sorry!\n");
2208 /* ---------------------------------------------------------------------------
2209 * Delete all the threads in the system
2210 * ------------------------------------------------------------------------- */
2213 deleteAllThreads ( Capability *cap )
2216 IF_DEBUG(scheduler,sched_belch("deleting all threads"));
2217 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
2218 if (t->what_next == ThreadRelocated) {
2221 next = t->global_link;
2222 deleteThread(cap,t);
2226 // The run queue now contains a bunch of ThreadKilled threads. We
2227 // must not throw these away: the main thread(s) will be in there
2228 // somewhere, and the main scheduler loop has to deal with it.
2229 // Also, the run queue is the only thing keeping these threads from
2230 // being GC'd, and we don't want the "main thread has been GC'd" panic.
2232 #if !defined(THREADED_RTS)
2233 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
2234 ASSERT(sleeping_queue == END_TSO_QUEUE);
2238 /* -----------------------------------------------------------------------------
2239 Managing the suspended_ccalling_tasks list.
2240 Locks required: sched_mutex
2241 -------------------------------------------------------------------------- */
2244 suspendTask (Capability *cap, Task *task)
2246 ASSERT(task->next == NULL && task->prev == NULL);
2247 task->next = cap->suspended_ccalling_tasks;
2249 if (cap->suspended_ccalling_tasks) {
2250 cap->suspended_ccalling_tasks->prev = task;
2252 cap->suspended_ccalling_tasks = task;
2256 recoverSuspendedTask (Capability *cap, Task *task)
2259 task->prev->next = task->next;
2261 ASSERT(cap->suspended_ccalling_tasks == task);
2262 cap->suspended_ccalling_tasks = task->next;
2265 task->next->prev = task->prev;
2267 task->next = task->prev = NULL;
2270 /* ---------------------------------------------------------------------------
2271 * Suspending & resuming Haskell threads.
2273 * When making a "safe" call to C (aka _ccall_GC), the task gives back
2274 * its capability before calling the C function. This allows another
2275 * task to pick up the capability and carry on running Haskell
2276 * threads. It also means that if the C call blocks, it won't lock
2279 * The Haskell thread making the C call is put to sleep for the
2280 * duration of the call, on the susepended_ccalling_threads queue. We
2281 * give out a token to the task, which it can use to resume the thread
2282 * on return from the C function.
2283 * ------------------------------------------------------------------------- */
2286 suspendThread (StgRegTable *reg)
2289 int saved_errno = errno;
2293 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
2295 cap = regTableToCapability(reg);
2297 task = cap->running_task;
2298 tso = cap->r.rCurrentTSO;
2301 sched_belch("thread %d did a safe foreign call", cap->r.rCurrentTSO->id));
2303 // XXX this might not be necessary --SDM
2304 tso->what_next = ThreadRunGHC;
2306 threadPaused(cap,tso);
2308 if(tso->blocked_exceptions == NULL) {
2309 tso->why_blocked = BlockedOnCCall;
2310 tso->blocked_exceptions = END_TSO_QUEUE;
2312 tso->why_blocked = BlockedOnCCall_NoUnblockExc;
2315 // Hand back capability
2316 task->suspended_tso = tso;
2318 ACQUIRE_LOCK(&cap->lock);
2320 suspendTask(cap,task);
2321 cap->in_haskell = rtsFalse;
2322 releaseCapability_(cap);
2324 RELEASE_LOCK(&cap->lock);
2326 #if defined(THREADED_RTS)
2327 /* Preparing to leave the RTS, so ensure there's a native thread/task
2328 waiting to take over.
2330 IF_DEBUG(scheduler, sched_belch("thread %d: leaving RTS", tso->id));
2333 errno = saved_errno;
2338 resumeThread (void *task_)
2342 int saved_errno = errno;
2346 // Wait for permission to re-enter the RTS with the result.
2347 waitForReturnCapability(&cap,task);
2348 // we might be on a different capability now... but if so, our
2349 // entry on the suspended_ccalling_tasks list will also have been
2352 // Remove the thread from the suspended list
2353 recoverSuspendedTask(cap,task);
2355 tso = task->suspended_tso;
2356 task->suspended_tso = NULL;
2357 tso->link = END_TSO_QUEUE;
2358 IF_DEBUG(scheduler, sched_belch("thread %d: re-entering RTS", tso->id));
2360 if (tso->why_blocked == BlockedOnCCall) {
2361 awakenBlockedQueue(cap,tso->blocked_exceptions);
2362 tso->blocked_exceptions = NULL;
2365 /* Reset blocking status */
2366 tso->why_blocked = NotBlocked;
2368 cap->r.rCurrentTSO = tso;
2369 cap->in_haskell = rtsTrue;
2370 errno = saved_errno;
2372 /* We might have GC'd, mark the TSO dirty again */
2375 IF_DEBUG(sanity, checkTSO(tso));
2380 /* ---------------------------------------------------------------------------
2381 * Comparing Thread ids.
2383 * This is used from STG land in the implementation of the
2384 * instances of Eq/Ord for ThreadIds.
2385 * ------------------------------------------------------------------------ */
2388 cmp_thread(StgPtr tso1, StgPtr tso2)
2390 StgThreadID id1 = ((StgTSO *)tso1)->id;
2391 StgThreadID id2 = ((StgTSO *)tso2)->id;
2393 if (id1 < id2) return (-1);
2394 if (id1 > id2) return 1;
2398 /* ---------------------------------------------------------------------------
2399 * Fetching the ThreadID from an StgTSO.
2401 * This is used in the implementation of Show for ThreadIds.
2402 * ------------------------------------------------------------------------ */
2404 rts_getThreadId(StgPtr tso)
2406 return ((StgTSO *)tso)->id;
2411 labelThread(StgPtr tso, char *label)
2416 /* Caveat: Once set, you can only set the thread name to "" */
2417 len = strlen(label)+1;
2418 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
2419 strncpy(buf,label,len);
2420 /* Update will free the old memory for us */
2421 updateThreadLabel(((StgTSO *)tso)->id,buf);
2425 /* ---------------------------------------------------------------------------
2426 Create a new thread.
2428 The new thread starts with the given stack size. Before the
2429 scheduler can run, however, this thread needs to have a closure
2430 (and possibly some arguments) pushed on its stack. See
2431 pushClosure() in Schedule.h.
2433 createGenThread() and createIOThread() (in SchedAPI.h) are
2434 convenient packaged versions of this function.
2436 currently pri (priority) is only used in a GRAN setup -- HWL
2437 ------------------------------------------------------------------------ */
2439 /* currently pri (priority) is only used in a GRAN setup -- HWL */
2441 createThread(nat size, StgInt pri)
2444 createThread(Capability *cap, nat size)
2450 /* sched_mutex is *not* required */
2452 /* First check whether we should create a thread at all */
2453 #if defined(PARALLEL_HASKELL)
2454 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
2455 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
2457 debugBelch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)\n",
2458 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
2459 return END_TSO_QUEUE;
2465 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
2468 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
2470 /* catch ridiculously small stack sizes */
2471 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
2472 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
2475 stack_size = size - TSO_STRUCT_SIZEW;
2477 tso = (StgTSO *)allocateLocal(cap, size);
2478 TICK_ALLOC_TSO(stack_size, 0);
2480 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
2482 SET_GRAN_HDR(tso, ThisPE);
2485 // Always start with the compiled code evaluator
2486 tso->what_next = ThreadRunGHC;
2488 tso->why_blocked = NotBlocked;
2489 tso->blocked_exceptions = NULL;
2490 tso->flags = TSO_DIRTY;
2492 tso->saved_errno = 0;
2495 tso->stack_size = stack_size;
2496 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
2498 tso->sp = (P_)&(tso->stack) + stack_size;
2500 tso->trec = NO_TREC;
2503 tso->prof.CCCS = CCS_MAIN;
2506 /* put a stop frame on the stack */
2507 tso->sp -= sizeofW(StgStopFrame);
2508 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
2509 tso->link = END_TSO_QUEUE;
2513 /* uses more flexible routine in GranSim */
2514 insertThread(tso, CurrentProc);
2516 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
2522 if (RtsFlags.GranFlags.GranSimStats.Full)
2523 DumpGranEvent(GR_START,tso);
2524 #elif defined(PARALLEL_HASKELL)
2525 if (RtsFlags.ParFlags.ParStats.Full)
2526 DumpGranEvent(GR_STARTQ,tso);
2527 /* HACk to avoid SCHEDULE
2531 /* Link the new thread on the global thread list.
2533 ACQUIRE_LOCK(&sched_mutex);
2534 tso->id = next_thread_id++; // while we have the mutex
2535 tso->global_link = all_threads;
2537 RELEASE_LOCK(&sched_mutex);
2540 tso->dist.priority = MandatoryPriority; //by default that is...
2544 tso->gran.pri = pri;
2546 tso->gran.magic = TSO_MAGIC; // debugging only
2548 tso->gran.sparkname = 0;
2549 tso->gran.startedat = CURRENT_TIME;
2550 tso->gran.exported = 0;
2551 tso->gran.basicblocks = 0;
2552 tso->gran.allocs = 0;
2553 tso->gran.exectime = 0;
2554 tso->gran.fetchtime = 0;
2555 tso->gran.fetchcount = 0;
2556 tso->gran.blocktime = 0;
2557 tso->gran.blockcount = 0;
2558 tso->gran.blockedat = 0;
2559 tso->gran.globalsparks = 0;
2560 tso->gran.localsparks = 0;
2561 if (RtsFlags.GranFlags.Light)
2562 tso->gran.clock = Now; /* local clock */
2564 tso->gran.clock = 0;
2566 IF_DEBUG(gran,printTSO(tso));
2567 #elif defined(PARALLEL_HASKELL)
2569 tso->par.magic = TSO_MAGIC; // debugging only
2571 tso->par.sparkname = 0;
2572 tso->par.startedat = CURRENT_TIME;
2573 tso->par.exported = 0;
2574 tso->par.basicblocks = 0;
2575 tso->par.allocs = 0;
2576 tso->par.exectime = 0;
2577 tso->par.fetchtime = 0;
2578 tso->par.fetchcount = 0;
2579 tso->par.blocktime = 0;
2580 tso->par.blockcount = 0;
2581 tso->par.blockedat = 0;
2582 tso->par.globalsparks = 0;
2583 tso->par.localsparks = 0;
2587 globalGranStats.tot_threads_created++;
2588 globalGranStats.threads_created_on_PE[CurrentProc]++;
2589 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
2590 globalGranStats.tot_sq_probes++;
2591 #elif defined(PARALLEL_HASKELL)
2592 // collect parallel global statistics (currently done together with GC stats)
2593 if (RtsFlags.ParFlags.ParStats.Global &&
2594 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
2595 //debugBelch("Creating thread %d @ %11.2f\n", tso->id, usertime());
2596 globalParStats.tot_threads_created++;
2602 sched_belch("==__ schedule: Created TSO %d (%p);",
2603 CurrentProc, tso, tso->id));
2604 #elif defined(PARALLEL_HASKELL)
2605 IF_PAR_DEBUG(verbose,
2606 sched_belch("==__ schedule: Created TSO %d (%p); %d threads active",
2607 (long)tso->id, tso, advisory_thread_count));
2609 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
2610 (long)tso->id, (long)tso->stack_size));
2617 all parallel thread creation calls should fall through the following routine.
2620 createThreadFromSpark(rtsSpark spark)
2622 ASSERT(spark != (rtsSpark)NULL);
2623 // JB: TAKE CARE OF THIS COUNTER! BUGGY
2624 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads)
2626 barf("{createSparkThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
2627 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
2628 return END_TSO_QUEUE;
2632 tso = createThread(RtsFlags.GcFlags.initialStkSize);
2633 if (tso==END_TSO_QUEUE)
2634 barf("createSparkThread: Cannot create TSO");
2636 tso->priority = AdvisoryPriority;
2638 pushClosure(tso,spark);
2640 advisory_thread_count++; // JB: TAKE CARE OF THIS COUNTER! BUGGY
2647 Turn a spark into a thread.
2648 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
2652 activateSpark (rtsSpark spark)
2656 tso = createSparkThread(spark);
2657 if (RtsFlags.ParFlags.ParStats.Full) {
2658 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
2659 IF_PAR_DEBUG(verbose,
2660 debugBelch("==^^ activateSpark: turning spark of closure %p (%s) into a thread\n",
2661 (StgClosure *)spark, info_type((StgClosure *)spark)));
2663 // ToDo: fwd info on local/global spark to thread -- HWL
2664 // tso->gran.exported = spark->exported;
2665 // tso->gran.locked = !spark->global;
2666 // tso->gran.sparkname = spark->name;
2672 /* ---------------------------------------------------------------------------
2675 * scheduleThread puts a thread on the end of the runnable queue.
2676 * This will usually be done immediately after a thread is created.
2677 * The caller of scheduleThread must create the thread using e.g.
2678 * createThread and push an appropriate closure
2679 * on this thread's stack before the scheduler is invoked.
2680 * ------------------------------------------------------------------------ */
2683 scheduleThread(Capability *cap, StgTSO *tso)
2685 // The thread goes at the *end* of the run-queue, to avoid possible
2686 // starvation of any threads already on the queue.
2687 appendToRunQueue(cap,tso);
2691 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *cap)
2695 // We already created/initialised the Task
2696 task = cap->running_task;
2698 // This TSO is now a bound thread; make the Task and TSO
2699 // point to each other.
2704 task->stat = NoStatus;
2706 appendToRunQueue(cap,tso);
2708 IF_DEBUG(scheduler, sched_belch("new bound thread (%d)", tso->id));
2711 /* GranSim specific init */
2712 CurrentTSO = m->tso; // the TSO to run
2713 procStatus[MainProc] = Busy; // status of main PE
2714 CurrentProc = MainProc; // PE to run it on
2717 cap = schedule(cap,task);
2719 ASSERT(task->stat != NoStatus);
2720 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
2722 IF_DEBUG(scheduler, sched_belch("bound thread (%d) finished", task->tso->id));
2726 /* ----------------------------------------------------------------------------
2728 * ------------------------------------------------------------------------- */
2730 #if defined(THREADED_RTS)
2732 workerStart(Task *task)
2736 // See startWorkerTask().
2737 ACQUIRE_LOCK(&task->lock);
2739 RELEASE_LOCK(&task->lock);
2741 // set the thread-local pointer to the Task:
2744 // schedule() runs without a lock.
2745 cap = schedule(cap,task);
2747 // On exit from schedule(), we have a Capability.
2748 releaseCapability(cap);
2753 /* ---------------------------------------------------------------------------
2756 * Initialise the scheduler. This resets all the queues - if the
2757 * queues contained any threads, they'll be garbage collected at the
2760 * ------------------------------------------------------------------------ */
2767 for (i=0; i<=MAX_PROC; i++) {
2768 run_queue_hds[i] = END_TSO_QUEUE;
2769 run_queue_tls[i] = END_TSO_QUEUE;
2770 blocked_queue_hds[i] = END_TSO_QUEUE;
2771 blocked_queue_tls[i] = END_TSO_QUEUE;
2772 ccalling_threadss[i] = END_TSO_QUEUE;
2773 blackhole_queue[i] = END_TSO_QUEUE;
2774 sleeping_queue = END_TSO_QUEUE;
2776 #elif !defined(THREADED_RTS)
2777 blocked_queue_hd = END_TSO_QUEUE;
2778 blocked_queue_tl = END_TSO_QUEUE;
2779 sleeping_queue = END_TSO_QUEUE;
2782 blackhole_queue = END_TSO_QUEUE;
2783 all_threads = END_TSO_QUEUE;
2786 sched_state = SCHED_RUNNING;
2788 RtsFlags.ConcFlags.ctxtSwitchTicks =
2789 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
2791 #if defined(THREADED_RTS)
2792 /* Initialise the mutex and condition variables used by
2794 initMutex(&sched_mutex);
2797 ACQUIRE_LOCK(&sched_mutex);
2799 /* A capability holds the state a native thread needs in
2800 * order to execute STG code. At least one capability is
2801 * floating around (only THREADED_RTS builds have more than one).
2807 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
2811 #if defined(THREADED_RTS)
2813 * Eagerly start one worker to run each Capability, except for
2814 * Capability 0. The idea is that we're probably going to start a
2815 * bound thread on Capability 0 pretty soon, so we don't want a
2816 * worker task hogging it.
2821 for (i = 1; i < n_capabilities; i++) {
2822 cap = &capabilities[i];
2823 ACQUIRE_LOCK(&cap->lock);
2824 startWorkerTask(cap, workerStart);
2825 RELEASE_LOCK(&cap->lock);
2830 RELEASE_LOCK(&sched_mutex);
2834 exitScheduler( void )
2838 #if defined(THREADED_RTS)
2839 ACQUIRE_LOCK(&sched_mutex);
2840 task = newBoundTask();
2841 RELEASE_LOCK(&sched_mutex);
2844 // If we haven't killed all the threads yet, do it now.
2845 if (sched_state < SCHED_INTERRUPTED) {
2846 sched_state = SCHED_INTERRUPTING;
2847 scheduleDoGC(NULL,task,rtsFalse,GetRoots);
2849 sched_state = SCHED_SHUTTING_DOWN;
2851 #if defined(THREADED_RTS)
2855 for (i = 0; i < n_capabilities; i++) {
2856 shutdownCapability(&capabilities[i], task);
2858 boundTaskExiting(task);
2864 /* ---------------------------------------------------------------------------
2865 Where are the roots that we know about?
2867 - all the threads on the runnable queue
2868 - all the threads on the blocked queue
2869 - all the threads on the sleeping queue
2870 - all the thread currently executing a _ccall_GC
2871 - all the "main threads"
2873 ------------------------------------------------------------------------ */
2875 /* This has to be protected either by the scheduler monitor, or by the
2876 garbage collection monitor (probably the latter).
2881 GetRoots( evac_fn evac )
2888 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2889 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2890 evac((StgClosure **)&run_queue_hds[i]);
2891 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2892 evac((StgClosure **)&run_queue_tls[i]);
2894 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2895 evac((StgClosure **)&blocked_queue_hds[i]);
2896 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2897 evac((StgClosure **)&blocked_queue_tls[i]);
2898 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2899 evac((StgClosure **)&ccalling_threads[i]);
2906 for (i = 0; i < n_capabilities; i++) {
2907 cap = &capabilities[i];
2908 evac((StgClosure **)&cap->run_queue_hd);
2909 evac((StgClosure **)&cap->run_queue_tl);
2911 for (task = cap->suspended_ccalling_tasks; task != NULL;
2913 IF_DEBUG(scheduler,sched_belch("evac'ing suspended TSO %d", task->suspended_tso->id));
2914 evac((StgClosure **)&task->suspended_tso);
2918 #if !defined(THREADED_RTS)
2919 evac((StgClosure **)(void *)&blocked_queue_hd);
2920 evac((StgClosure **)(void *)&blocked_queue_tl);
2921 evac((StgClosure **)(void *)&sleeping_queue);
2925 // evac((StgClosure **)&blackhole_queue);
2927 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL) || defined(GRAN)
2928 markSparkQueue(evac);
2931 #if defined(RTS_USER_SIGNALS)
2932 // mark the signal handlers (signals should be already blocked)
2933 markSignalHandlers(evac);
2937 /* -----------------------------------------------------------------------------
2940 This is the interface to the garbage collector from Haskell land.
2941 We provide this so that external C code can allocate and garbage
2942 collect when called from Haskell via _ccall_GC.
2944 It might be useful to provide an interface whereby the programmer
2945 can specify more roots (ToDo).
2947 This needs to be protected by the GC condition variable above. KH.
2948 -------------------------------------------------------------------------- */
2950 static void (*extra_roots)(evac_fn);
2953 performGC_(rtsBool force_major, void (*get_roots)(evac_fn))
2955 Task *task = myTask();
2958 ACQUIRE_LOCK(&sched_mutex);
2959 task = newBoundTask();
2960 RELEASE_LOCK(&sched_mutex);
2961 scheduleDoGC(NULL,task,force_major, get_roots);
2962 boundTaskExiting(task);
2964 scheduleDoGC(NULL,task,force_major, get_roots);
2971 performGC_(rtsFalse, GetRoots);
2975 performMajorGC(void)
2977 performGC_(rtsTrue, GetRoots);
2981 AllRoots(evac_fn evac)
2983 GetRoots(evac); // the scheduler's roots
2984 extra_roots(evac); // the user's roots
2988 performGCWithRoots(void (*get_roots)(evac_fn))
2990 extra_roots = get_roots;
2991 performGC_(rtsFalse, AllRoots);
2994 /* -----------------------------------------------------------------------------
2997 If the thread has reached its maximum stack size, then raise the
2998 StackOverflow exception in the offending thread. Otherwise
2999 relocate the TSO into a larger chunk of memory and adjust its stack
3001 -------------------------------------------------------------------------- */
3004 threadStackOverflow(Capability *cap, StgTSO *tso)
3006 nat new_stack_size, stack_words;
3011 IF_DEBUG(sanity,checkTSO(tso));
3012 if (tso->stack_size >= tso->max_stack_size) {
3015 debugBelch("@@ threadStackOverflow of TSO %ld (%p): stack too large (now %ld; max is %ld)\n",
3016 (long)tso->id, tso, (long)tso->stack_size, (long)tso->max_stack_size);
3017 /* If we're debugging, just print out the top of the stack */
3018 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
3021 /* Send this thread the StackOverflow exception */
3022 raiseAsync(cap, tso, (StgClosure *)stackOverflow_closure);
3026 /* Try to double the current stack size. If that takes us over the
3027 * maximum stack size for this thread, then use the maximum instead.
3028 * Finally round up so the TSO ends up as a whole number of blocks.
3030 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
3031 new_tso_size = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
3032 TSO_STRUCT_SIZE)/sizeof(W_);
3033 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
3034 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
3036 IF_DEBUG(scheduler, sched_belch("increasing stack size from %ld words to %d.\n", (long)tso->stack_size, new_stack_size));
3038 dest = (StgTSO *)allocate(new_tso_size);
3039 TICK_ALLOC_TSO(new_stack_size,0);
3041 /* copy the TSO block and the old stack into the new area */
3042 memcpy(dest,tso,TSO_STRUCT_SIZE);
3043 stack_words = tso->stack + tso->stack_size - tso->sp;
3044 new_sp = (P_)dest + new_tso_size - stack_words;
3045 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
3047 /* relocate the stack pointers... */
3049 dest->stack_size = new_stack_size;
3051 /* Mark the old TSO as relocated. We have to check for relocated
3052 * TSOs in the garbage collector and any primops that deal with TSOs.
3054 * It's important to set the sp value to just beyond the end
3055 * of the stack, so we don't attempt to scavenge any part of the
3058 tso->what_next = ThreadRelocated;
3060 tso->sp = (P_)&(tso->stack[tso->stack_size]);
3061 tso->why_blocked = NotBlocked;
3063 IF_PAR_DEBUG(verbose,
3064 debugBelch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld\n",
3065 tso->id, tso, tso->stack_size);
3066 /* If we're debugging, just print out the top of the stack */
3067 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
3070 IF_DEBUG(sanity,checkTSO(tso));
3072 IF_DEBUG(scheduler,printTSO(dest));
3078 /* ---------------------------------------------------------------------------
3079 Wake up a queue that was blocked on some resource.
3080 ------------------------------------------------------------------------ */
3084 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
3087 #elif defined(PARALLEL_HASKELL)
3089 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
3091 /* write RESUME events to log file and
3092 update blocked and fetch time (depending on type of the orig closure) */
3093 if (RtsFlags.ParFlags.ParStats.Full) {
3094 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
3095 GR_RESUMEQ, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
3096 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
3097 if (emptyRunQueue())
3098 emitSchedule = rtsTrue;
3100 switch (get_itbl(node)->type) {
3102 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
3107 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
3114 barf("{unblockOne}Daq Qagh: unexpected closure in blocking queue");
3121 StgBlockingQueueElement *
3122 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
3125 PEs node_loc, tso_loc;
3127 node_loc = where_is(node); // should be lifted out of loop
3128 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
3129 tso_loc = where_is((StgClosure *)tso);
3130 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
3131 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
3132 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
3133 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
3134 // insertThread(tso, node_loc);
3135 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
3137 tso, node, (rtsSpark*)NULL);
3138 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
3141 } else { // TSO is remote (actually should be FMBQ)
3142 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
3143 RtsFlags.GranFlags.Costs.gunblocktime +
3144 RtsFlags.GranFlags.Costs.latency;
3145 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
3147 tso, node, (rtsSpark*)NULL);
3148 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
3151 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
3153 debugBelch(" %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
3154 (node_loc==tso_loc ? "Local" : "Global"),
3155 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
3156 tso->block_info.closure = NULL;
3157 IF_DEBUG(scheduler,debugBelch("-- Waking up thread %ld (%p)\n",
3160 #elif defined(PARALLEL_HASKELL)
3161 StgBlockingQueueElement *
3162 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
3164 StgBlockingQueueElement *next;
3166 switch (get_itbl(bqe)->type) {
3168 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
3169 /* if it's a TSO just push it onto the run_queue */
3171 ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
3172 APPEND_TO_RUN_QUEUE((StgTSO *)bqe);
3174 unblockCount(bqe, node);
3175 /* reset blocking status after dumping event */
3176 ((StgTSO *)bqe)->why_blocked = NotBlocked;
3180 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
3182 bqe->link = (StgBlockingQueueElement *)PendingFetches;
3183 PendingFetches = (StgBlockedFetch *)bqe;
3187 /* can ignore this case in a non-debugging setup;
3188 see comments on RBHSave closures above */
3190 /* check that the closure is an RBHSave closure */
3191 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
3192 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
3193 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
3197 barf("{unblockOne}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
3198 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
3202 IF_PAR_DEBUG(bq, debugBelch(", %p (%s)\n", bqe, info_type((StgClosure*)bqe)));
3208 unblockOne(Capability *cap, StgTSO *tso)
3212 ASSERT(get_itbl(tso)->type == TSO);
3213 ASSERT(tso->why_blocked != NotBlocked);
3214 tso->why_blocked = NotBlocked;
3216 tso->link = END_TSO_QUEUE;
3218 // We might have just migrated this TSO to our Capability:
3220 tso->bound->cap = cap;
3223 appendToRunQueue(cap,tso);
3225 // we're holding a newly woken thread, make sure we context switch
3226 // quickly so we can migrate it if necessary.
3228 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", (long)tso->id));
3235 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
3237 StgBlockingQueueElement *bqe;
3242 debugBelch("##-_ AwBQ for node %p on PE %d @ %ld by TSO %d (%p): \n", \
3243 node, CurrentProc, CurrentTime[CurrentProc],
3244 CurrentTSO->id, CurrentTSO));
3246 node_loc = where_is(node);
3248 ASSERT(q == END_BQ_QUEUE ||
3249 get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
3250 get_itbl(q)->type == CONSTR); // closure (type constructor)
3251 ASSERT(is_unique(node));
3253 /* FAKE FETCH: magically copy the node to the tso's proc;
3254 no Fetch necessary because in reality the node should not have been
3255 moved to the other PE in the first place
3257 if (CurrentProc!=node_loc) {
3259 debugBelch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)\n",
3260 node, node_loc, CurrentProc, CurrentTSO->id,
3261 // CurrentTSO, where_is(CurrentTSO),
3262 node->header.gran.procs));
3263 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
3265 debugBelch("## new bitmask of node %p is %#x\n",
3266 node, node->header.gran.procs));
3267 if (RtsFlags.GranFlags.GranSimStats.Global) {
3268 globalGranStats.tot_fake_fetches++;
3273 // ToDo: check: ASSERT(CurrentProc==node_loc);
3274 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
3277 bqe points to the current element in the queue
3278 next points to the next element in the queue
3280 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
3281 //tso_loc = where_is(tso);
3283 bqe = unblockOne(bqe, node);
3286 /* if this is the BQ of an RBH, we have to put back the info ripped out of
3287 the closure to make room for the anchor of the BQ */
3288 if (bqe!=END_BQ_QUEUE) {
3289 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
3291 ASSERT((info_ptr==&RBH_Save_0_info) ||
3292 (info_ptr==&RBH_Save_1_info) ||
3293 (info_ptr==&RBH_Save_2_info));
3295 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
3296 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
3297 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
3300 debugBelch("## Filled in RBH_Save for %p (%s) at end of AwBQ\n",
3301 node, info_type(node)));
3304 /* statistics gathering */
3305 if (RtsFlags.GranFlags.GranSimStats.Global) {
3306 // globalGranStats.tot_bq_processing_time += bq_processing_time;
3307 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
3308 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
3309 globalGranStats.tot_awbq++; // total no. of bqs awakened
3312 debugBelch("## BQ Stats of %p: [%d entries] %s\n",
3313 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
3315 #elif defined(PARALLEL_HASKELL)
3317 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
3319 StgBlockingQueueElement *bqe;
3321 IF_PAR_DEBUG(verbose,
3322 debugBelch("##-_ AwBQ for node %p on [%x]: \n",
3326 if(get_itbl(q)->type == CONSTR || q==END_BQ_QUEUE) {
3327 IF_PAR_DEBUG(verbose, debugBelch("## ... nothing to unblock so lets just return. RFP (BUG?)\n"));
3332 ASSERT(q == END_BQ_QUEUE ||
3333 get_itbl(q)->type == TSO ||
3334 get_itbl(q)->type == BLOCKED_FETCH ||
3335 get_itbl(q)->type == CONSTR);
3338 while (get_itbl(bqe)->type==TSO ||
3339 get_itbl(bqe)->type==BLOCKED_FETCH) {
3340 bqe = unblockOne(bqe, node);
3344 #else /* !GRAN && !PARALLEL_HASKELL */
3347 awakenBlockedQueue(Capability *cap, StgTSO *tso)
3349 if (tso == NULL) return; // hack; see bug #1235728, and comments in
3351 while (tso != END_TSO_QUEUE) {
3352 tso = unblockOne(cap,tso);
3357 /* ---------------------------------------------------------------------------
3359 - usually called inside a signal handler so it mustn't do anything fancy.
3360 ------------------------------------------------------------------------ */
3363 interruptStgRts(void)
3365 sched_state = SCHED_INTERRUPTING;
3367 #if defined(THREADED_RTS)
3368 prodAllCapabilities();
3372 /* -----------------------------------------------------------------------------
3375 This is for use when we raise an exception in another thread, which
3377 This has nothing to do with the UnblockThread event in GranSim. -- HWL
3378 -------------------------------------------------------------------------- */
3380 #if defined(GRAN) || defined(PARALLEL_HASKELL)
3382 NB: only the type of the blocking queue is different in GranSim and GUM
3383 the operations on the queue-elements are the same
3384 long live polymorphism!
3386 Locks: sched_mutex is held upon entry and exit.
3390 unblockThread(Capability *cap, StgTSO *tso)
3392 StgBlockingQueueElement *t, **last;
3394 switch (tso->why_blocked) {
3397 return; /* not blocked */
3400 // Be careful: nothing to do here! We tell the scheduler that the thread
3401 // is runnable and we leave it to the stack-walking code to abort the
3402 // transaction while unwinding the stack. We should perhaps have a debugging
3403 // test to make sure that this really happens and that the 'zombie' transaction
3404 // does not get committed.
3408 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3410 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
3411 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3413 last = (StgBlockingQueueElement **)&mvar->head;
3414 for (t = (StgBlockingQueueElement *)mvar->head;
3416 last = &t->link, last_tso = t, t = t->link) {
3417 if (t == (StgBlockingQueueElement *)tso) {
3418 *last = (StgBlockingQueueElement *)tso->link;
3419 if (mvar->tail == tso) {
3420 mvar->tail = (StgTSO *)last_tso;
3425 barf("unblockThread (MVAR): TSO not found");
3428 case BlockedOnBlackHole:
3429 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3431 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3433 last = &bq->blocking_queue;
3434 for (t = bq->blocking_queue;
3436 last = &t->link, t = t->link) {
3437 if (t == (StgBlockingQueueElement *)tso) {
3438 *last = (StgBlockingQueueElement *)tso->link;
3442 barf("unblockThread (BLACKHOLE): TSO not found");
3445 case BlockedOnException:
3447 StgTSO *target = tso->block_info.tso;
3449 ASSERT(get_itbl(target)->type == TSO);
3451 if (target->what_next == ThreadRelocated) {
3452 target = target->link;
3453 ASSERT(get_itbl(target)->type == TSO);
3456 ASSERT(target->blocked_exceptions != NULL);
3458 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
3459 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
3461 last = &t->link, t = t->link) {
3462 ASSERT(get_itbl(t)->type == TSO);
3463 if (t == (StgBlockingQueueElement *)tso) {
3464 *last = (StgBlockingQueueElement *)tso->link;
3468 barf("unblockThread (Exception): TSO not found");
3472 case BlockedOnWrite:
3473 #if defined(mingw32_HOST_OS)
3474 case BlockedOnDoProc:
3477 /* take TSO off blocked_queue */
3478 StgBlockingQueueElement *prev = NULL;
3479 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
3480 prev = t, t = t->link) {
3481 if (t == (StgBlockingQueueElement *)tso) {
3483 blocked_queue_hd = (StgTSO *)t->link;
3484 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3485 blocked_queue_tl = END_TSO_QUEUE;
3488 prev->link = t->link;
3489 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3490 blocked_queue_tl = (StgTSO *)prev;
3493 #if defined(mingw32_HOST_OS)
3494 /* (Cooperatively) signal that the worker thread should abort
3497 abandonWorkRequest(tso->block_info.async_result->reqID);
3502 barf("unblockThread (I/O): TSO not found");
3505 case BlockedOnDelay:
3507 /* take TSO off sleeping_queue */
3508 StgBlockingQueueElement *prev = NULL;
3509 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
3510 prev = t, t = t->link) {
3511 if (t == (StgBlockingQueueElement *)tso) {
3513 sleeping_queue = (StgTSO *)t->link;
3515 prev->link = t->link;
3520 barf("unblockThread (delay): TSO not found");
3524 barf("unblockThread");
3528 tso->link = END_TSO_QUEUE;
3529 tso->why_blocked = NotBlocked;
3530 tso->block_info.closure = NULL;
3531 pushOnRunQueue(cap,tso);
3535 unblockThread(Capability *cap, StgTSO *tso)
3539 /* To avoid locking unnecessarily. */
3540 if (tso->why_blocked == NotBlocked) {
3544 switch (tso->why_blocked) {
3547 // Be careful: nothing to do here! We tell the scheduler that the thread
3548 // is runnable and we leave it to the stack-walking code to abort the
3549 // transaction while unwinding the stack. We should perhaps have a debugging
3550 // test to make sure that this really happens and that the 'zombie' transaction
3551 // does not get committed.
3555 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3557 StgTSO *last_tso = END_TSO_QUEUE;
3558 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3561 for (t = mvar->head; t != END_TSO_QUEUE;
3562 last = &t->link, last_tso = t, t = t->link) {
3565 if (mvar->tail == tso) {
3566 mvar->tail = last_tso;
3571 barf("unblockThread (MVAR): TSO not found");
3574 case BlockedOnBlackHole:
3576 last = &blackhole_queue;
3577 for (t = blackhole_queue; t != END_TSO_QUEUE;
3578 last = &t->link, t = t->link) {
3584 barf("unblockThread (BLACKHOLE): TSO not found");
3587 case BlockedOnException:
3589 StgTSO *target = tso->block_info.tso;
3591 ASSERT(get_itbl(target)->type == TSO);
3593 while (target->what_next == ThreadRelocated) {
3594 target = target->link;
3595 ASSERT(get_itbl(target)->type == TSO);
3598 ASSERT(target->blocked_exceptions != NULL);
3600 last = &target->blocked_exceptions;
3601 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
3602 last = &t->link, t = t->link) {
3603 ASSERT(get_itbl(t)->type == TSO);
3609 barf("unblockThread (Exception): TSO not found");
3612 #if !defined(THREADED_RTS)
3614 case BlockedOnWrite:
3615 #if defined(mingw32_HOST_OS)
3616 case BlockedOnDoProc:
3619 StgTSO *prev = NULL;
3620 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
3621 prev = t, t = t->link) {
3624 blocked_queue_hd = t->link;
3625 if (blocked_queue_tl == t) {
3626 blocked_queue_tl = END_TSO_QUEUE;
3629 prev->link = t->link;
3630 if (blocked_queue_tl == t) {
3631 blocked_queue_tl = prev;
3634 #if defined(mingw32_HOST_OS)
3635 /* (Cooperatively) signal that the worker thread should abort
3638 abandonWorkRequest(tso->block_info.async_result->reqID);
3643 barf("unblockThread (I/O): TSO not found");
3646 case BlockedOnDelay:
3648 StgTSO *prev = NULL;
3649 for (t = sleeping_queue; t != END_TSO_QUEUE;
3650 prev = t, t = t->link) {
3653 sleeping_queue = t->link;
3655 prev->link = t->link;
3660 barf("unblockThread (delay): TSO not found");
3665 barf("unblockThread");
3669 tso->link = END_TSO_QUEUE;
3670 tso->why_blocked = NotBlocked;
3671 tso->block_info.closure = NULL;
3672 appendToRunQueue(cap,tso);
3674 // We might have just migrated this TSO to our Capability:
3676 tso->bound->cap = cap;
3681 /* -----------------------------------------------------------------------------
3684 * Check the blackhole_queue for threads that can be woken up. We do
3685 * this periodically: before every GC, and whenever the run queue is
3688 * An elegant solution might be to just wake up all the blocked
3689 * threads with awakenBlockedQueue occasionally: they'll go back to
3690 * sleep again if the object is still a BLACKHOLE. Unfortunately this
3691 * doesn't give us a way to tell whether we've actually managed to
3692 * wake up any threads, so we would be busy-waiting.
3694 * -------------------------------------------------------------------------- */
3697 checkBlackHoles (Capability *cap)
3700 rtsBool any_woke_up = rtsFalse;
3703 // blackhole_queue is global:
3704 ASSERT_LOCK_HELD(&sched_mutex);
3706 IF_DEBUG(scheduler, sched_belch("checking threads blocked on black holes"));
3708 // ASSUMES: sched_mutex
3709 prev = &blackhole_queue;
3710 t = blackhole_queue;
3711 while (t != END_TSO_QUEUE) {
3712 ASSERT(t->why_blocked == BlockedOnBlackHole);
3713 type = get_itbl(t->block_info.closure)->type;
3714 if (type != BLACKHOLE && type != CAF_BLACKHOLE) {
3715 IF_DEBUG(sanity,checkTSO(t));
3716 t = unblockOne(cap, t);
3717 // urk, the threads migrate to the current capability
3718 // here, but we'd like to keep them on the original one.
3720 any_woke_up = rtsTrue;
3730 /* -----------------------------------------------------------------------------
3733 * The following function implements the magic for raising an
3734 * asynchronous exception in an existing thread.
3736 * We first remove the thread from any queue on which it might be
3737 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
3739 * We strip the stack down to the innermost CATCH_FRAME, building
3740 * thunks in the heap for all the active computations, so they can
3741 * be restarted if necessary. When we reach a CATCH_FRAME, we build
3742 * an application of the handler to the exception, and push it on
3743 * the top of the stack.
3745 * How exactly do we save all the active computations? We create an
3746 * AP_STACK for every UpdateFrame on the stack. Entering one of these
3747 * AP_STACKs pushes everything from the corresponding update frame
3748 * upwards onto the stack. (Actually, it pushes everything up to the
3749 * next update frame plus a pointer to the next AP_STACK object.
3750 * Entering the next AP_STACK object pushes more onto the stack until we
3751 * reach the last AP_STACK object - at which point the stack should look
3752 * exactly as it did when we killed the TSO and we can continue
3753 * execution by entering the closure on top of the stack.
3755 * We can also kill a thread entirely - this happens if either (a) the
3756 * exception passed to raiseAsync is NULL, or (b) there's no
3757 * CATCH_FRAME on the stack. In either case, we strip the entire
3758 * stack and replace the thread with a zombie.
3760 * ToDo: in THREADED_RTS mode, this function is only safe if either
3761 * (a) we hold all the Capabilities (eg. in GC, or if there is only
3762 * one Capability), or (b) we own the Capability that the TSO is
3763 * currently blocked on or on the run queue of.
3765 * -------------------------------------------------------------------------- */
3768 raiseAsync(Capability *cap, StgTSO *tso, StgClosure *exception)
3770 raiseAsync_(cap, tso, exception, rtsFalse, NULL);
3774 suspendComputation(Capability *cap, StgTSO *tso, StgPtr stop_here)
3776 raiseAsync_(cap, tso, NULL, rtsFalse, stop_here);
3780 raiseAsync_(Capability *cap, StgTSO *tso, StgClosure *exception,
3781 rtsBool stop_at_atomically, StgPtr stop_here)
3783 StgRetInfoTable *info;
3787 // Thread already dead?
3788 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3793 sched_belch("raising exception in thread %ld.", (long)tso->id));
3795 // Remove it from any blocking queues
3796 unblockThread(cap,tso);
3798 // mark it dirty; we're about to change its stack.
3803 // The stack freezing code assumes there's a closure pointer on
3804 // the top of the stack, so we have to arrange that this is the case...
3806 if (sp[0] == (W_)&stg_enter_info) {
3810 sp[0] = (W_)&stg_dummy_ret_closure;
3814 while (stop_here == NULL || frame < stop_here) {
3816 // 1. Let the top of the stack be the "current closure"
3818 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
3821 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
3822 // current closure applied to the chunk of stack up to (but not
3823 // including) the update frame. This closure becomes the "current
3824 // closure". Go back to step 2.
3826 // 4. If it's a CATCH_FRAME, then leave the exception handler on
3827 // top of the stack applied to the exception.
3829 // 5. If it's a STOP_FRAME, then kill the thread.
3831 // NB: if we pass an ATOMICALLY_FRAME then abort the associated
3834 info = get_ret_itbl((StgClosure *)frame);
3836 switch (info->i.type) {
3843 // First build an AP_STACK consisting of the stack chunk above the
3844 // current update frame, with the top word on the stack as the
3847 words = frame - sp - 1;
3848 ap = (StgAP_STACK *)allocateLocal(cap,AP_STACK_sizeW(words));
3851 ap->fun = (StgClosure *)sp[0];
3853 for(i=0; i < (nat)words; ++i) {
3854 ap->payload[i] = (StgClosure *)*sp++;
3857 SET_HDR(ap,&stg_AP_STACK_info,
3858 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
3859 TICK_ALLOC_UP_THK(words+1,0);
3862 debugBelch("sched: Updating ");
3863 printPtr((P_)((StgUpdateFrame *)frame)->updatee);
3864 debugBelch(" with ");
3865 printObj((StgClosure *)ap);
3868 // Replace the updatee with an indirection
3870 // Warning: if we're in a loop, more than one update frame on
3871 // the stack may point to the same object. Be careful not to
3872 // overwrite an IND_OLDGEN in this case, because we'll screw
3873 // up the mutable lists. To be on the safe side, don't
3874 // overwrite any kind of indirection at all. See also
3875 // threadSqueezeStack in GC.c, where we have to make a similar
3878 if (!closure_IND(((StgUpdateFrame *)frame)->updatee)) {
3879 // revert the black hole
3880 UPD_IND_NOLOCK(((StgUpdateFrame *)frame)->updatee,
3883 sp += sizeofW(StgUpdateFrame) - 1;
3884 sp[0] = (W_)ap; // push onto stack
3886 continue; //no need to bump frame
3890 // We've stripped the entire stack, the thread is now dead.
3891 tso->what_next = ThreadKilled;
3892 tso->sp = frame + sizeofW(StgStopFrame);
3896 // If we find a CATCH_FRAME, and we've got an exception to raise,
3897 // then build the THUNK raise(exception), and leave it on
3898 // top of the CATCH_FRAME ready to enter.
3902 StgCatchFrame *cf = (StgCatchFrame *)frame;
3906 if (exception == NULL) break;
3908 // we've got an exception to raise, so let's pass it to the
3909 // handler in this frame.
3911 raise = (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
3912 TICK_ALLOC_SE_THK(1,0);
3913 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
3914 raise->payload[0] = exception;
3916 // throw away the stack from Sp up to the CATCH_FRAME.
3920 /* Ensure that async excpetions are blocked now, so we don't get
3921 * a surprise exception before we get around to executing the
3924 if (tso->blocked_exceptions == NULL) {
3925 tso->blocked_exceptions = END_TSO_QUEUE;
3928 /* Put the newly-built THUNK on top of the stack, ready to execute
3929 * when the thread restarts.
3932 sp[-1] = (W_)&stg_enter_info;
3934 tso->what_next = ThreadRunGHC;
3935 IF_DEBUG(sanity, checkTSO(tso));
3939 case ATOMICALLY_FRAME:
3940 if (stop_at_atomically) {
3941 ASSERT(stmGetEnclosingTRec(tso->trec) == NO_TREC);
3942 stmCondemnTransaction(cap, tso -> trec);
3946 // R1 is not a register: the return convention for IO in
3947 // this case puts the return value on the stack, so we
3948 // need to set up the stack to return to the atomically
3949 // frame properly...
3950 tso->sp = frame - 2;
3951 tso->sp[1] = (StgWord) &stg_NO_FINALIZER_closure; // why not?
3952 tso->sp[0] = (StgWord) &stg_ut_1_0_unreg_info;
3954 tso->what_next = ThreadRunGHC;
3957 // Not stop_at_atomically... fall through and abort the
3960 case CATCH_RETRY_FRAME:
3961 // IF we find an ATOMICALLY_FRAME then we abort the
3962 // current transaction and propagate the exception. In
3963 // this case (unlike ordinary exceptions) we do not care
3964 // whether the transaction is valid or not because its
3965 // possible validity cannot have caused the exception
3966 // and will not be visible after the abort.
3968 debugBelch("Found atomically block delivering async exception\n"));
3969 StgTRecHeader *trec = tso -> trec;
3970 StgTRecHeader *outer = stmGetEnclosingTRec(trec);
3971 stmAbortTransaction(cap, trec);
3972 tso -> trec = outer;
3979 // move on to the next stack frame
3980 frame += stack_frame_sizeW((StgClosure *)frame);
3983 // if we got here, then we stopped at stop_here
3984 ASSERT(stop_here != NULL);
3987 /* -----------------------------------------------------------------------------
3990 This is used for interruption (^C) and forking, and corresponds to
3991 raising an exception but without letting the thread catch the
3993 -------------------------------------------------------------------------- */
3996 deleteThread (Capability *cap, StgTSO *tso)
3998 if (tso->why_blocked != BlockedOnCCall &&
3999 tso->why_blocked != BlockedOnCCall_NoUnblockExc) {
4000 raiseAsync(cap,tso,NULL);
4004 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
4006 deleteThread_(Capability *cap, StgTSO *tso)
4007 { // for forkProcess only:
4008 // like deleteThread(), but we delete threads in foreign calls, too.
4010 if (tso->why_blocked == BlockedOnCCall ||
4011 tso->why_blocked == BlockedOnCCall_NoUnblockExc) {
4012 unblockOne(cap,tso);
4013 tso->what_next = ThreadKilled;
4015 deleteThread(cap,tso);
4020 /* -----------------------------------------------------------------------------
4021 raiseExceptionHelper
4023 This function is called by the raise# primitve, just so that we can
4024 move some of the tricky bits of raising an exception from C-- into
4025 C. Who knows, it might be a useful re-useable thing here too.
4026 -------------------------------------------------------------------------- */
4029 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
4031 Capability *cap = regTableToCapability(reg);
4032 StgThunk *raise_closure = NULL;
4034 StgRetInfoTable *info;
4036 // This closure represents the expression 'raise# E' where E
4037 // is the exception raise. It is used to overwrite all the
4038 // thunks which are currently under evaluataion.
4041 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
4042 // LDV profiling: stg_raise_info has THUNK as its closure
4043 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
4044 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
4045 // 1 does not cause any problem unless profiling is performed.
4046 // However, when LDV profiling goes on, we need to linearly scan
4047 // small object pool, where raise_closure is stored, so we should
4048 // use MIN_UPD_SIZE.
4050 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
4051 // sizeofW(StgClosure)+1);
4055 // Walk up the stack, looking for the catch frame. On the way,
4056 // we update any closures pointed to from update frames with the
4057 // raise closure that we just built.
4061 info = get_ret_itbl((StgClosure *)p);
4062 next = p + stack_frame_sizeW((StgClosure *)p);
4063 switch (info->i.type) {
4066 // Only create raise_closure if we need to.
4067 if (raise_closure == NULL) {
4069 (StgThunk *)allocateLocal(cap,sizeofW(StgThunk)+1);
4070 SET_HDR(raise_closure, &stg_raise_info, CCCS);
4071 raise_closure->payload[0] = exception;
4073 UPD_IND(((StgUpdateFrame *)p)->updatee,(StgClosure *)raise_closure);
4077 case ATOMICALLY_FRAME:
4078 IF_DEBUG(stm, debugBelch("Found ATOMICALLY_FRAME at %p\n", p));
4080 return ATOMICALLY_FRAME;
4086 case CATCH_STM_FRAME:
4087 IF_DEBUG(stm, debugBelch("Found CATCH_STM_FRAME at %p\n", p));
4089 return CATCH_STM_FRAME;
4095 case CATCH_RETRY_FRAME:
4104 /* -----------------------------------------------------------------------------
4105 findRetryFrameHelper
4107 This function is called by the retry# primitive. It traverses the stack
4108 leaving tso->sp referring to the frame which should handle the retry.
4110 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
4111 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
4113 We skip CATCH_STM_FRAMEs because retries are not considered to be exceptions,
4114 despite the similar implementation.
4116 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
4117 not be created within memory transactions.
4118 -------------------------------------------------------------------------- */
4121 findRetryFrameHelper (StgTSO *tso)
4124 StgRetInfoTable *info;
4128 info = get_ret_itbl((StgClosure *)p);
4129 next = p + stack_frame_sizeW((StgClosure *)p);
4130 switch (info->i.type) {
4132 case ATOMICALLY_FRAME:
4133 IF_DEBUG(stm, debugBelch("Found ATOMICALLY_FRAME at %p during retrry\n", p));
4135 return ATOMICALLY_FRAME;
4137 case CATCH_RETRY_FRAME:
4138 IF_DEBUG(stm, debugBelch("Found CATCH_RETRY_FRAME at %p during retrry\n", p));
4140 return CATCH_RETRY_FRAME;
4142 case CATCH_STM_FRAME:
4144 ASSERT(info->i.type != CATCH_FRAME);
4145 ASSERT(info->i.type != STOP_FRAME);
4152 /* -----------------------------------------------------------------------------
4153 resurrectThreads is called after garbage collection on the list of
4154 threads found to be garbage. Each of these threads will be woken
4155 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
4156 on an MVar, or NonTermination if the thread was blocked on a Black
4159 Locks: assumes we hold *all* the capabilities.
4160 -------------------------------------------------------------------------- */
4163 resurrectThreads (StgTSO *threads)
4168 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
4169 next = tso->global_link;
4170 tso->global_link = all_threads;
4172 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
4174 // Wake up the thread on the Capability it was last on for a
4175 // bound thread, or last_free_capability otherwise.
4177 cap = tso->bound->cap;
4179 cap = last_free_capability;
4182 switch (tso->why_blocked) {
4184 case BlockedOnException:
4185 /* Called by GC - sched_mutex lock is currently held. */
4186 raiseAsync(cap, tso,(StgClosure *)BlockedOnDeadMVar_closure);
4188 case BlockedOnBlackHole:
4189 raiseAsync(cap, tso,(StgClosure *)NonTermination_closure);
4192 raiseAsync(cap, tso,(StgClosure *)BlockedIndefinitely_closure);
4195 /* This might happen if the thread was blocked on a black hole
4196 * belonging to a thread that we've just woken up (raiseAsync
4197 * can wake up threads, remember...).
4201 barf("resurrectThreads: thread blocked in a strange way");
4206 /* ----------------------------------------------------------------------------
4207 * Debugging: why is a thread blocked
4208 * [Also provides useful information when debugging threaded programs
4209 * at the Haskell source code level, so enable outside of DEBUG. --sof 7/02]
4210 ------------------------------------------------------------------------- */
4214 printThreadBlockage(StgTSO *tso)
4216 switch (tso->why_blocked) {
4218 debugBelch("is blocked on read from fd %d", (int)(tso->block_info.fd));
4220 case BlockedOnWrite:
4221 debugBelch("is blocked on write to fd %d", (int)(tso->block_info.fd));
4223 #if defined(mingw32_HOST_OS)
4224 case BlockedOnDoProc:
4225 debugBelch("is blocked on proc (request: %ld)", tso->block_info.async_result->reqID);
4228 case BlockedOnDelay:
4229 debugBelch("is blocked until %ld", (long)(tso->block_info.target));
4232 debugBelch("is blocked on an MVar @ %p", tso->block_info.closure);
4234 case BlockedOnException:
4235 debugBelch("is blocked on delivering an exception to thread %d",
4236 tso->block_info.tso->id);
4238 case BlockedOnBlackHole:
4239 debugBelch("is blocked on a black hole");
4242 debugBelch("is not blocked");
4244 #if defined(PARALLEL_HASKELL)
4246 debugBelch("is blocked on global address; local FM_BQ is %p (%s)",
4247 tso->block_info.closure, info_type(tso->block_info.closure));
4249 case BlockedOnGA_NoSend:
4250 debugBelch("is blocked on global address (no send); local FM_BQ is %p (%s)",
4251 tso->block_info.closure, info_type(tso->block_info.closure));
4254 case BlockedOnCCall:
4255 debugBelch("is blocked on an external call");
4257 case BlockedOnCCall_NoUnblockExc:
4258 debugBelch("is blocked on an external call (exceptions were already blocked)");
4261 debugBelch("is blocked on an STM operation");
4264 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
4265 tso->why_blocked, tso->id, tso);
4270 printThreadStatus(StgTSO *t)
4272 debugBelch("\tthread %4d @ %p ", t->id, (void *)t);
4274 void *label = lookupThreadLabel(t->id);
4275 if (label) debugBelch("[\"%s\"] ",(char *)label);
4277 if (t->what_next == ThreadRelocated) {
4278 debugBelch("has been relocated...\n");
4280 switch (t->what_next) {
4282 debugBelch("has been killed");
4284 case ThreadComplete:
4285 debugBelch("has completed");
4288 printThreadBlockage(t);
4295 printAllThreads(void)
4302 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
4303 ullong_format_string(TIME_ON_PROC(CurrentProc),
4304 time_string, rtsFalse/*no commas!*/);
4306 debugBelch("all threads at [%s]:\n", time_string);
4307 # elif defined(PARALLEL_HASKELL)
4308 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
4309 ullong_format_string(CURRENT_TIME,
4310 time_string, rtsFalse/*no commas!*/);
4312 debugBelch("all threads at [%s]:\n", time_string);
4314 debugBelch("all threads:\n");
4317 for (i = 0; i < n_capabilities; i++) {
4318 cap = &capabilities[i];
4319 debugBelch("threads on capability %d:\n", cap->no);
4320 for (t = cap->run_queue_hd; t != END_TSO_QUEUE; t = t->link) {
4321 printThreadStatus(t);
4325 debugBelch("other threads:\n");
4326 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
4327 if (t->why_blocked != NotBlocked) {
4328 printThreadStatus(t);
4330 if (t->what_next == ThreadRelocated) {
4333 next = t->global_link;
4340 printThreadQueue(StgTSO *t)
4343 for (; t != END_TSO_QUEUE; t = t->link) {
4344 printThreadStatus(t);
4347 debugBelch("%d threads on queue\n", i);
4351 Print a whole blocking queue attached to node (debugging only).
4353 # if defined(PARALLEL_HASKELL)
4355 print_bq (StgClosure *node)
4357 StgBlockingQueueElement *bqe;
4361 debugBelch("## BQ of closure %p (%s): ",
4362 node, info_type(node));
4364 /* should cover all closures that may have a blocking queue */
4365 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
4366 get_itbl(node)->type == FETCH_ME_BQ ||
4367 get_itbl(node)->type == RBH ||
4368 get_itbl(node)->type == MVAR);
4370 ASSERT(node!=(StgClosure*)NULL); // sanity check
4372 print_bqe(((StgBlockingQueue*)node)->blocking_queue);
4376 Print a whole blocking queue starting with the element bqe.
4379 print_bqe (StgBlockingQueueElement *bqe)
4384 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
4386 for (end = (bqe==END_BQ_QUEUE);
4387 !end; // iterate until bqe points to a CONSTR
4388 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE),
4389 bqe = end ? END_BQ_QUEUE : bqe->link) {
4390 ASSERT(bqe != END_BQ_QUEUE); // sanity check
4391 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
4392 /* types of closures that may appear in a blocking queue */
4393 ASSERT(get_itbl(bqe)->type == TSO ||
4394 get_itbl(bqe)->type == BLOCKED_FETCH ||
4395 get_itbl(bqe)->type == CONSTR);
4396 /* only BQs of an RBH end with an RBH_Save closure */
4397 //ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
4399 switch (get_itbl(bqe)->type) {
4401 debugBelch(" TSO %u (%x),",
4402 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
4405 debugBelch(" BF (node=%p, ga=((%x, %d, %x)),",
4406 ((StgBlockedFetch *)bqe)->node,
4407 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
4408 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
4409 ((StgBlockedFetch *)bqe)->ga.weight);
4412 debugBelch(" %s (IP %p),",
4413 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
4414 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
4415 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
4416 "RBH_Save_?"), get_itbl(bqe));
4419 barf("Unexpected closure type %s in blocking queue", // of %p (%s)",
4420 info_type((StgClosure *)bqe)); // , node, info_type(node));
4426 # elif defined(GRAN)
4428 print_bq (StgClosure *node)
4430 StgBlockingQueueElement *bqe;
4431 PEs node_loc, tso_loc;
4434 /* should cover all closures that may have a blocking queue */
4435 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
4436 get_itbl(node)->type == FETCH_ME_BQ ||
4437 get_itbl(node)->type == RBH);
4439 ASSERT(node!=(StgClosure*)NULL); // sanity check
4440 node_loc = where_is(node);
4442 debugBelch("## BQ of closure %p (%s) on [PE %d]: ",
4443 node, info_type(node), node_loc);
4446 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
4448 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
4449 !end; // iterate until bqe points to a CONSTR
4450 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
4451 ASSERT(bqe != END_BQ_QUEUE); // sanity check
4452 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
4453 /* types of closures that may appear in a blocking queue */
4454 ASSERT(get_itbl(bqe)->type == TSO ||
4455 get_itbl(bqe)->type == CONSTR);
4456 /* only BQs of an RBH end with an RBH_Save closure */
4457 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
4459 tso_loc = where_is((StgClosure *)bqe);
4460 switch (get_itbl(bqe)->type) {
4462 debugBelch(" TSO %d (%p) on [PE %d],",
4463 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
4466 debugBelch(" %s (IP %p),",
4467 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
4468 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
4469 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
4470 "RBH_Save_?"), get_itbl(bqe));
4473 barf("Unexpected closure type %s in blocking queue of %p (%s)",
4474 info_type((StgClosure *)bqe), node, info_type(node));
4482 #if defined(PARALLEL_HASKELL)
4489 for (i=0, tso=run_queue_hd;
4490 tso != END_TSO_QUEUE;
4491 i++, tso=tso->link) {
4500 sched_belch(char *s, ...)
4505 debugBelch("sched (task %p): ", (void *)(unsigned long)(unsigned int)osThreadId());
4506 #elif defined(PARALLEL_HASKELL)
4509 debugBelch("sched: ");