1 /* ---------------------------------------------------------------------------
2 * $Id: Schedule.c,v 1.82 2000/11/13 14:42:16 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
8 * The main scheduling code in GranSim is quite different from that in std
9 * (concurrent) Haskell: while concurrent Haskell just iterates over the
10 * threads in the runnable queue, GranSim is event driven, i.e. it iterates
11 * over the events in the global event queue. -- HWL
12 * --------------------------------------------------------------------------*/
14 //@node Main scheduling code, , ,
15 //@section Main scheduling code
17 /* Version with scheduler monitor support for SMPs.
19 This design provides a high-level API to create and schedule threads etc.
20 as documented in the SMP design document.
22 It uses a monitor design controlled by a single mutex to exercise control
23 over accesses to shared data structures, and builds on the Posix threads
26 The majority of state is shared. In order to keep essential per-task state,
27 there is a Capability structure, which contains all the information
28 needed to run a thread: its STG registers, a pointer to its TSO, a
29 nursery etc. During STG execution, a pointer to the capability is
30 kept in a register (BaseReg).
32 In a non-SMP build, there is one global capability, namely MainRegTable.
39 //* Variables and Data structures::
40 //* Main scheduling loop::
41 //* Suspend and Resume::
43 //* Garbage Collextion Routines::
44 //* Blocking Queue Routines::
45 //* Exception Handling Routines::
46 //* Debugging Routines::
50 //@node Includes, Variables and Data structures, Main scheduling code, Main scheduling code
51 //@subsection Includes
59 #include "StgStartup.h"
63 #include "StgMiscClosures.h"
65 #include "Evaluator.h"
66 #include "Exception.h"
74 #if defined(GRAN) || defined(PAR)
75 # include "GranSimRts.h"
77 # include "ParallelRts.h"
78 # include "Parallel.h"
79 # include "ParallelDebug.h"
87 //@node Variables and Data structures, Prototypes, Includes, Main scheduling code
88 //@subsection Variables and Data structures
92 * These are the threads which clients have requested that we run.
94 * In an SMP build, we might have several concurrent clients all
95 * waiting for results, and each one will wait on a condition variable
96 * until the result is available.
98 * In non-SMP, clients are strictly nested: the first client calls
99 * into the RTS, which might call out again to C with a _ccall_GC, and
100 * eventually re-enter the RTS.
102 * Main threads information is kept in a linked list:
104 //@cindex StgMainThread
105 typedef struct StgMainThread_ {
107 SchedulerStatus stat;
110 pthread_cond_t wakeup;
112 struct StgMainThread_ *link;
115 /* Main thread queue.
116 * Locks required: sched_mutex.
118 static StgMainThread *main_threads;
121 * Locks required: sched_mutex.
125 StgTSO* ActiveTSO = NULL; /* for assigning system costs; GranSim-Light only */
126 /* rtsTime TimeOfNextEvent, EndOfTimeSlice; now in GranSim.c */
129 In GranSim we have a runable and a blocked queue for each processor.
130 In order to minimise code changes new arrays run_queue_hds/tls
131 are created. run_queue_hd is then a short cut (macro) for
132 run_queue_hds[CurrentProc] (see GranSim.h).
135 StgTSO *run_queue_hds[MAX_PROC], *run_queue_tls[MAX_PROC];
136 StgTSO *blocked_queue_hds[MAX_PROC], *blocked_queue_tls[MAX_PROC];
137 StgTSO *ccalling_threadss[MAX_PROC];
138 /* We use the same global list of threads (all_threads) in GranSim as in
139 the std RTS (i.e. we are cheating). However, we don't use this list in
140 the GranSim specific code at the moment (so we are only potentially
145 StgTSO *run_queue_hd, *run_queue_tl;
146 StgTSO *blocked_queue_hd, *blocked_queue_tl;
147 StgTSO *sleeping_queue; /* perhaps replace with a hash table? */
151 /* Linked list of all threads.
152 * Used for detecting garbage collected threads.
156 /* Threads suspended in _ccall_GC.
158 static StgTSO *suspended_ccalling_threads;
160 static void GetRoots(void);
161 static StgTSO *threadStackOverflow(StgTSO *tso);
163 /* KH: The following two flags are shared memory locations. There is no need
164 to lock them, since they are only unset at the end of a scheduler
168 /* flag set by signal handler to precipitate a context switch */
169 //@cindex context_switch
172 /* if this flag is set as well, give up execution */
173 //@cindex interrupted
176 /* Next thread ID to allocate.
177 * Locks required: sched_mutex
179 //@cindex next_thread_id
180 StgThreadID next_thread_id = 1;
183 * Pointers to the state of the current thread.
184 * Rule of thumb: if CurrentTSO != NULL, then we're running a Haskell
185 * thread. If CurrentTSO == NULL, then we're at the scheduler level.
188 /* The smallest stack size that makes any sense is:
189 * RESERVED_STACK_WORDS (so we can get back from the stack overflow)
190 * + sizeofW(StgStopFrame) (the stg_stop_thread_info frame)
191 * + 1 (the realworld token for an IO thread)
192 * + 1 (the closure to enter)
194 * A thread with this stack will bomb immediately with a stack
195 * overflow, which will increase its stack size.
198 #define MIN_STACK_WORDS (RESERVED_STACK_WORDS + sizeofW(StgStopFrame) + 2)
200 /* Free capability list.
201 * Locks required: sched_mutex.
204 //@cindex free_capabilities
205 //@cindex n_free_capabilities
206 Capability *free_capabilities; /* Available capabilities for running threads */
207 nat n_free_capabilities; /* total number of available capabilities */
209 //@cindex MainRegTable
210 Capability MainRegTable; /* for non-SMP, we have one global capability */
217 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
218 * exists - earlier gccs apparently didn't.
225 /* All our current task ids, saved in case we need to kill them later.
232 void addToBlockedQueue ( StgTSO *tso );
234 static void schedule ( void );
235 void interruptStgRts ( void );
237 static StgTSO * createThread_ ( nat size, rtsBool have_lock, StgInt pri );
239 static StgTSO * createThread_ ( nat size, rtsBool have_lock );
242 static void detectBlackHoles ( void );
245 static void sched_belch(char *s, ...);
249 //@cindex sched_mutex
251 //@cindex thread_ready_cond
252 //@cindex gc_pending_cond
253 pthread_mutex_t sched_mutex = PTHREAD_MUTEX_INITIALIZER;
254 pthread_mutex_t term_mutex = PTHREAD_MUTEX_INITIALIZER;
255 pthread_cond_t thread_ready_cond = PTHREAD_COND_INITIALIZER;
256 pthread_cond_t gc_pending_cond = PTHREAD_COND_INITIALIZER;
263 rtsTime TimeOfLastYield;
267 char *whatNext_strs[] = {
275 char *threadReturnCode_strs[] = {
276 "HeapOverflow", /* might also be StackOverflow */
285 * The thread state for the main thread.
286 // ToDo: check whether not needed any more
290 //@node Main scheduling loop, Suspend and Resume, Prototypes, Main scheduling code
291 //@subsection Main scheduling loop
293 /* ---------------------------------------------------------------------------
294 Main scheduling loop.
296 We use round-robin scheduling, each thread returning to the
297 scheduler loop when one of these conditions is detected:
300 * timer expires (thread yields)
305 Locking notes: we acquire the scheduler lock once at the beginning
306 of the scheduler loop, and release it when
308 * running a thread, or
309 * waiting for work, or
310 * waiting for a GC to complete.
313 In a GranSim setup this loop iterates over the global event queue.
314 This revolves around the global event queue, which determines what
315 to do next. Therefore, it's more complicated than either the
316 concurrent or the parallel (GUM) setup.
319 GUM iterates over incoming messages.
320 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
321 and sends out a fish whenever it has nothing to do; in-between
322 doing the actual reductions (shared code below) it processes the
323 incoming messages and deals with delayed operations
324 (see PendingFetches).
325 This is not the ugliest code you could imagine, but it's bloody close.
327 ------------------------------------------------------------------------ */
334 StgThreadReturnCode ret;
343 rtsBool was_interrupted = rtsFalse;
345 ACQUIRE_LOCK(&sched_mutex);
349 /* set up first event to get things going */
350 /* ToDo: assign costs for system setup and init MainTSO ! */
351 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
353 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
356 fprintf(stderr, "GRAN: Init CurrentTSO (in schedule) = %p\n", CurrentTSO);
357 G_TSO(CurrentTSO, 5));
359 if (RtsFlags.GranFlags.Light) {
360 /* Save current time; GranSim Light only */
361 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
364 event = get_next_event();
366 while (event!=(rtsEvent*)NULL) {
367 /* Choose the processor with the next event */
368 CurrentProc = event->proc;
369 CurrentTSO = event->tso;
373 while (!GlobalStopPending) { /* GlobalStopPending set in par_exit */
381 IF_DEBUG(scheduler, printAllThreads());
383 /* If we're interrupted (the user pressed ^C, or some other
384 * termination condition occurred), kill all the currently running
388 IF_DEBUG(scheduler, sched_belch("interrupted"));
390 interrupted = rtsFalse;
391 was_interrupted = rtsTrue;
394 /* Go through the list of main threads and wake up any
395 * clients whose computations have finished. ToDo: this
396 * should be done more efficiently without a linear scan
397 * of the main threads list, somehow...
401 StgMainThread *m, **prev;
402 prev = &main_threads;
403 for (m = main_threads; m != NULL; m = m->link) {
404 switch (m->tso->what_next) {
407 *(m->ret) = (StgClosure *)m->tso->sp[0];
411 pthread_cond_broadcast(&m->wakeup);
415 if (was_interrupted) {
416 m->stat = Interrupted;
420 pthread_cond_broadcast(&m->wakeup);
430 /* in GUM do this only on the Main PE */
433 /* If our main thread has finished or been killed, return.
436 StgMainThread *m = main_threads;
437 if (m->tso->what_next == ThreadComplete
438 || m->tso->what_next == ThreadKilled) {
439 main_threads = main_threads->link;
440 if (m->tso->what_next == ThreadComplete) {
441 /* we finished successfully, fill in the return value */
442 if (m->ret) { *(m->ret) = (StgClosure *)m->tso->sp[0]; };
446 if (was_interrupted) {
447 m->stat = Interrupted;
457 /* Top up the run queue from our spark pool. We try to make the
458 * number of threads in the run queue equal to the number of
463 nat n = n_free_capabilities;
464 StgTSO *tso = run_queue_hd;
466 /* Count the run queue */
467 while (n > 0 && tso != END_TSO_QUEUE) {
476 break; /* no more sparks in the pool */
478 /* I'd prefer this to be done in activateSpark -- HWL */
479 /* tricky - it needs to hold the scheduler lock and
480 * not try to re-acquire it -- SDM */
482 tso = createThread_(RtsFlags.GcFlags.initialStkSize, rtsTrue);
483 pushClosure(tso,spark);
484 PUSH_ON_RUN_QUEUE(tso);
486 advisory_thread_count++;
490 sched_belch("turning spark of closure %p into a thread",
491 (StgClosure *)spark));
494 /* We need to wake up the other tasks if we just created some
497 if (n_free_capabilities - n > 1) {
498 pthread_cond_signal(&thread_ready_cond);
503 /* Check whether any waiting threads need to be woken up. If the
504 * run queue is empty, and there are no other tasks running, we
505 * can wait indefinitely for something to happen.
506 * ToDo: what if another client comes along & requests another
509 if (blocked_queue_hd != END_TSO_QUEUE || sleeping_queue != END_TSO_QUEUE) {
511 (run_queue_hd == END_TSO_QUEUE)
513 && (n_free_capabilities == RtsFlags.ParFlags.nNodes)
517 /* we can be interrupted while waiting for I/O... */
518 if (interrupted) continue;
520 /* check for signals each time around the scheduler */
521 #ifndef mingw32_TARGET_OS
522 if (signals_pending()) {
523 start_signal_handlers();
528 * Detect deadlock: when we have no threads to run, there are no
529 * threads waiting on I/O or sleeping, and all the other tasks are
530 * waiting for work, we must have a deadlock of some description.
532 * We first try to find threads blocked on themselves (ie. black
533 * holes), and generate NonTermination exceptions where necessary.
535 * If no threads are black holed, we have a deadlock situation, so
536 * inform all the main threads.
539 if (blocked_queue_hd == END_TSO_QUEUE
540 && run_queue_hd == END_TSO_QUEUE
541 && sleeping_queue == END_TSO_QUEUE
542 && (n_free_capabilities == RtsFlags.ParFlags.nNodes))
544 IF_DEBUG(scheduler, sched_belch("deadlocked, checking for black holes..."));
546 if (run_queue_hd == END_TSO_QUEUE) {
548 for (m = main_threads; m != NULL; m = m->link) {
551 pthread_cond_broadcast(&m->wakeup);
557 if (blocked_queue_hd == END_TSO_QUEUE
558 && run_queue_hd == END_TSO_QUEUE
559 && sleeping_queue == END_TSO_QUEUE)
561 IF_DEBUG(scheduler, sched_belch("deadlocked, checking for black holes..."));
563 if (run_queue_hd == END_TSO_QUEUE) {
564 StgMainThread *m = main_threads;
567 main_threads = m->link;
574 /* If there's a GC pending, don't do anything until it has
578 IF_DEBUG(scheduler,sched_belch("waiting for GC"));
579 pthread_cond_wait(&gc_pending_cond, &sched_mutex);
582 /* block until we've got a thread on the run queue and a free
585 while (run_queue_hd == END_TSO_QUEUE || free_capabilities == NULL) {
586 IF_DEBUG(scheduler, sched_belch("waiting for work"));
587 pthread_cond_wait(&thread_ready_cond, &sched_mutex);
588 IF_DEBUG(scheduler, sched_belch("work now available"));
594 if (RtsFlags.GranFlags.Light)
595 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
597 /* adjust time based on time-stamp */
598 if (event->time > CurrentTime[CurrentProc] &&
599 event->evttype != ContinueThread)
600 CurrentTime[CurrentProc] = event->time;
602 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
603 if (!RtsFlags.GranFlags.Light)
606 IF_DEBUG(gran, fprintf(stderr, "GRAN: switch by event-type\n"))
608 /* main event dispatcher in GranSim */
609 switch (event->evttype) {
610 /* Should just be continuing execution */
612 IF_DEBUG(gran, fprintf(stderr, "GRAN: doing ContinueThread\n"));
613 /* ToDo: check assertion
614 ASSERT(run_queue_hd != (StgTSO*)NULL &&
615 run_queue_hd != END_TSO_QUEUE);
617 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
618 if (!RtsFlags.GranFlags.DoAsyncFetch &&
619 procStatus[CurrentProc]==Fetching) {
620 belch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]",
621 CurrentTSO->id, CurrentTSO, CurrentProc);
624 /* Ignore ContinueThreads for completed threads */
625 if (CurrentTSO->what_next == ThreadComplete) {
626 belch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)",
627 CurrentTSO->id, CurrentTSO, CurrentProc);
630 /* Ignore ContinueThreads for threads that are being migrated */
631 if (PROCS(CurrentTSO)==Nowhere) {
632 belch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)",
633 CurrentTSO->id, CurrentTSO, CurrentProc);
636 /* The thread should be at the beginning of the run queue */
637 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
638 belch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread",
639 CurrentTSO->id, CurrentTSO, CurrentProc);
640 break; // run the thread anyway
643 new_event(proc, proc, CurrentTime[proc],
645 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
647 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
648 break; // now actually run the thread; DaH Qu'vam yImuHbej
651 do_the_fetchnode(event);
652 goto next_thread; /* handle next event in event queue */
655 do_the_globalblock(event);
656 goto next_thread; /* handle next event in event queue */
659 do_the_fetchreply(event);
660 goto next_thread; /* handle next event in event queue */
662 case UnblockThread: /* Move from the blocked queue to the tail of */
663 do_the_unblock(event);
664 goto next_thread; /* handle next event in event queue */
666 case ResumeThread: /* Move from the blocked queue to the tail of */
667 /* the runnable queue ( i.e. Qu' SImqa'lu') */
668 event->tso->gran.blocktime +=
669 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
670 do_the_startthread(event);
671 goto next_thread; /* handle next event in event queue */
674 do_the_startthread(event);
675 goto next_thread; /* handle next event in event queue */
678 do_the_movethread(event);
679 goto next_thread; /* handle next event in event queue */
682 do_the_movespark(event);
683 goto next_thread; /* handle next event in event queue */
686 do_the_findwork(event);
687 goto next_thread; /* handle next event in event queue */
690 barf("Illegal event type %u\n", event->evttype);
693 /* This point was scheduler_loop in the old RTS */
695 IF_DEBUG(gran, belch("GRAN: after main switch"));
697 TimeOfLastEvent = CurrentTime[CurrentProc];
698 TimeOfNextEvent = get_time_of_next_event();
699 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
700 // CurrentTSO = ThreadQueueHd;
702 IF_DEBUG(gran, belch("GRAN: time of next event is: %ld",
705 if (RtsFlags.GranFlags.Light)
706 GranSimLight_leave_system(event, &ActiveTSO);
708 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
711 belch("GRAN: end of time-slice is %#lx", EndOfTimeSlice));
713 /* in a GranSim setup the TSO stays on the run queue */
715 /* Take a thread from the run queue. */
716 t = POP_RUN_QUEUE(); // take_off_run_queue(t);
719 fprintf(stderr, "GRAN: About to run current thread, which is\n");
722 context_switch = 0; // turned on via GranYield, checking events and time slice
725 DumpGranEvent(GR_SCHEDULE, t));
727 procStatus[CurrentProc] = Busy;
731 if (PendingFetches != END_BF_QUEUE) {
735 /* ToDo: phps merge with spark activation above */
736 /* check whether we have local work and send requests if we have none */
737 if (run_queue_hd == END_TSO_QUEUE) { /* no runnable threads */
738 /* :-[ no local threads => look out for local sparks */
739 /* the spark pool for the current PE */
740 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
741 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
742 pool->hd < pool->tl) {
744 * ToDo: add GC code check that we really have enough heap afterwards!!
746 * If we're here (no runnable threads) and we have pending
747 * sparks, we must have a space problem. Get enough space
748 * to turn one of those pending sparks into a
752 spark = findSpark(); /* get a spark */
753 if (spark != (rtsSpark) NULL) {
754 tso = activateSpark(spark); /* turn the spark into a thread */
755 IF_PAR_DEBUG(schedule,
756 belch("==== schedule: Created TSO %d (%p); %d threads active",
757 tso->id, tso, advisory_thread_count));
759 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
760 belch("==^^ failed to activate spark");
762 } /* otherwise fall through & pick-up new tso */
764 IF_PAR_DEBUG(verbose,
765 belch("==^^ no local sparks (spark pool contains only NFs: %d)",
766 spark_queue_len(pool)));
770 /* =8-[ no local sparks => look for work on other PEs */
773 * We really have absolutely no work. Send out a fish
774 * (there may be some out there already), and wait for
775 * something to arrive. We clearly can't run any threads
776 * until a SCHEDULE or RESUME arrives, and so that's what
777 * we're hoping to see. (Of course, we still have to
778 * respond to other types of messages.)
781 outstandingFishes < RtsFlags.ParFlags.maxFishes ) { // &&
782 // (last_fish_arrived_at+FISH_DELAY < CURRENT_TIME)) {
783 /* fishing set in sendFish, processFish;
784 avoid flooding system with fishes via delay */
786 sendFish(pe, mytid, NEW_FISH_AGE, NEW_FISH_HISTORY,
794 } else if (PacketsWaiting()) { /* Look for incoming messages */
798 /* Now we are sure that we have some work available */
799 ASSERT(run_queue_hd != END_TSO_QUEUE);
800 /* Take a thread from the run queue, if we have work */
801 t = POP_RUN_QUEUE(); // take_off_run_queue(END_TSO_QUEUE);
803 /* ToDo: write something to the log-file
804 if (RTSflags.ParFlags.granSimStats && !sameThread)
805 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
809 /* the spark pool for the current PE */
810 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
812 IF_DEBUG(scheduler, belch("--^^ %d sparks on [%#x] (hd=%x; tl=%x; base=%x, lim=%x)",
813 spark_queue_len(pool),
815 pool->hd, pool->tl, pool->base, pool->lim));
817 IF_DEBUG(scheduler, belch("--== %d threads on [%#x] (hd=%x; tl=%x)",
818 run_queue_len(), CURRENT_PROC,
819 run_queue_hd, run_queue_tl));
824 we are running a different TSO, so write a schedule event to log file
825 NB: If we use fair scheduling we also have to write a deschedule
826 event for LastTSO; with unfair scheduling we know that the
827 previous tso has blocked whenever we switch to another tso, so
828 we don't need it in GUM for now
830 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
831 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
835 #else /* !GRAN && !PAR */
837 /* grab a thread from the run queue
839 ASSERT(run_queue_hd != END_TSO_QUEUE);
841 IF_DEBUG(sanity,checkTSO(t));
848 cap = free_capabilities;
849 free_capabilities = cap->link;
850 n_free_capabilities--;
855 cap->rCurrentTSO = t;
857 /* context switches are now initiated by the timer signal, unless
858 * the user specified "context switch as often as possible", with
861 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
862 && (run_queue_hd != END_TSO_QUEUE
863 || blocked_queue_hd != END_TSO_QUEUE
864 || sleeping_queue != END_TSO_QUEUE))
869 RELEASE_LOCK(&sched_mutex);
871 IF_DEBUG(scheduler, sched_belch("-->> Running TSO %ld (%p) %s ...",
872 t->id, t, whatNext_strs[t->what_next]));
874 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
875 /* Run the current thread
877 switch (cap->rCurrentTSO->what_next) {
880 /* Thread already finished, return to scheduler. */
881 ret = ThreadFinished;
884 ret = StgRun((StgFunPtr) stg_enterStackTop, cap);
887 ret = StgRun((StgFunPtr) stg_returnToStackTop, cap);
889 case ThreadEnterHugs:
893 IF_DEBUG(scheduler,sched_belch("entering Hugs"));
894 c = (StgClosure *)(cap->rCurrentTSO->sp[0]);
895 cap->rCurrentTSO->sp += 1;
900 barf("Panic: entered a BCO but no bytecode interpreter in this build");
903 barf("schedule: invalid what_next field");
905 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
907 /* Costs for the scheduler are assigned to CCS_SYSTEM */
912 ACQUIRE_LOCK(&sched_mutex);
915 IF_DEBUG(scheduler,fprintf(stderr,"scheduler (task %ld): ", pthread_self()););
916 #elif !defined(GRAN) && !defined(PAR)
917 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: "););
919 t = cap->rCurrentTSO;
922 /* HACK 675: if the last thread didn't yield, make sure to print a
923 SCHEDULE event to the log file when StgRunning the next thread, even
924 if it is the same one as before */
925 LastTSO = t; //(ret == ThreadBlocked) ? END_TSO_QUEUE : t;
926 TimeOfLastYield = CURRENT_TIME;
931 /* make all the running tasks block on a condition variable,
932 * maybe set context_switch and wait till they all pile in,
933 * then have them wait on a GC condition variable.
935 IF_DEBUG(scheduler,belch("--<< thread %ld (%p; %s) stopped: HeapOverflow",
936 t->id, t, whatNext_strs[t->what_next]));
939 ASSERT(!is_on_queue(t,CurrentProc));
942 ready_to_gc = rtsTrue;
943 context_switch = 1; /* stop other threads ASAP */
944 PUSH_ON_RUN_QUEUE(t);
945 /* actual GC is done at the end of the while loop */
949 IF_DEBUG(scheduler,belch("--<< thread %ld (%p; %s) stopped, StackOverflow",
950 t->id, t, whatNext_strs[t->what_next]));
951 /* just adjust the stack for this thread, then pop it back
957 /* enlarge the stack */
958 StgTSO *new_t = threadStackOverflow(t);
960 /* This TSO has moved, so update any pointers to it from the
961 * main thread stack. It better not be on any other queues...
964 for (m = main_threads; m != NULL; m = m->link) {
970 PUSH_ON_RUN_QUEUE(new_t);
977 DumpGranEvent(GR_DESCHEDULE, t));
978 globalGranStats.tot_yields++;
981 DumpGranEvent(GR_DESCHEDULE, t));
983 /* put the thread back on the run queue. Then, if we're ready to
984 * GC, check whether this is the last task to stop. If so, wake
985 * up the GC thread. getThread will block during a GC until the
989 if (t->what_next == ThreadEnterHugs) {
990 /* ToDo: or maybe a timer expired when we were in Hugs?
991 * or maybe someone hit ctrl-C
993 belch("--<< thread %ld (%p; %s) stopped to switch to Hugs",
994 t->id, t, whatNext_strs[t->what_next]);
996 belch("--<< thread %ld (%p; %s) stopped, yielding",
997 t->id, t, whatNext_strs[t->what_next]);
1004 //belch("&& Doing sanity check on yielding TSO %ld.", t->id);
1006 ASSERT(t->link == END_TSO_QUEUE);
1008 ASSERT(!is_on_queue(t,CurrentProc));
1011 //belch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1012 checkThreadQsSanity(rtsTrue));
1014 APPEND_TO_RUN_QUEUE(t);
1016 /* add a ContinueThread event to actually process the thread */
1017 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1019 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1021 belch("GRAN: eventq and runnableq after adding yielded thread to queue again:");
1030 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1031 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)));
1032 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1034 // ??? needed; should emit block before
1036 DumpGranEvent(GR_DESCHEDULE, t));
1037 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1040 ASSERT(procStatus[CurrentProc]==Busy ||
1041 ((procStatus[CurrentProc]==Fetching) &&
1042 (t->block_info.closure!=(StgClosure*)NULL)));
1043 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1044 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1045 procStatus[CurrentProc]==Fetching))
1046 procStatus[CurrentProc] = Idle;
1050 DumpGranEvent(GR_DESCHEDULE, t));
1052 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1056 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: ",
1057 t->id, t, whatNext_strs[t->what_next], t->block_info.closure);
1058 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1061 /* don't need to do anything. Either the thread is blocked on
1062 * I/O, in which case we'll have called addToBlockedQueue
1063 * previously, or it's blocked on an MVar or Blackhole, in which
1064 * case it'll be on the relevant queue already.
1067 fprintf(stderr, "--<< thread %d (%p) stopped: ", t->id, t);
1068 printThreadBlockage(t);
1069 fprintf(stderr, "\n"));
1071 /* Only for dumping event to log file
1072 ToDo: do I need this in GranSim, too?
1079 case ThreadFinished:
1080 /* Need to check whether this was a main thread, and if so, signal
1081 * the task that started it with the return value. If we have no
1082 * more main threads, we probably need to stop all the tasks until
1085 /* We also end up here if the thread kills itself with an
1086 * uncaught exception, see Exception.hc.
1088 IF_DEBUG(scheduler,belch("--++ thread %d (%p) finished", t->id, t));
1090 endThread(t, CurrentProc); // clean-up the thread
1092 advisory_thread_count--;
1093 if (RtsFlags.ParFlags.ParStats.Full)
1094 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1099 barf("schedule: invalid thread return code %d", (int)ret);
1103 cap->link = free_capabilities;
1104 free_capabilities = cap;
1105 n_free_capabilities++;
1109 if (ready_to_gc && n_free_capabilities == RtsFlags.ParFlags.nNodes)
1114 /* everybody back, start the GC.
1115 * Could do it in this thread, or signal a condition var
1116 * to do it in another thread. Either way, we need to
1117 * broadcast on gc_pending_cond afterward.
1120 IF_DEBUG(scheduler,sched_belch("doing GC"));
1122 GarbageCollect(GetRoots,rtsFalse);
1123 ready_to_gc = rtsFalse;
1125 pthread_cond_broadcast(&gc_pending_cond);
1128 /* add a ContinueThread event to continue execution of current thread */
1129 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1131 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1133 fprintf(stderr, "GRAN: eventq and runnableq after Garbage collection:\n");
1140 IF_GRAN_DEBUG(unused,
1141 print_eventq(EventHd));
1143 event = get_next_event();
1147 /* ToDo: wait for next message to arrive rather than busy wait */
1152 t = take_off_run_queue(END_TSO_QUEUE);
1155 } /* end of while(1) */
1158 /* ---------------------------------------------------------------------------
1159 * deleteAllThreads(): kill all the live threads.
1161 * This is used when we catch a user interrupt (^C), before performing
1162 * any necessary cleanups and running finalizers.
1163 * ------------------------------------------------------------------------- */
1165 void deleteAllThreads ( void )
1168 IF_DEBUG(scheduler,sched_belch("deleting all threads"));
1169 for (t = run_queue_hd; t != END_TSO_QUEUE; t = t->link) {
1172 for (t = blocked_queue_hd; t != END_TSO_QUEUE; t = t->link) {
1175 for (t = sleeping_queue; t != END_TSO_QUEUE; t = t->link) {
1178 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1179 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
1180 sleeping_queue = END_TSO_QUEUE;
1183 /* startThread and insertThread are now in GranSim.c -- HWL */
1185 //@node Suspend and Resume, Run queue code, Main scheduling loop, Main scheduling code
1186 //@subsection Suspend and Resume
1188 /* ---------------------------------------------------------------------------
1189 * Suspending & resuming Haskell threads.
1191 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1192 * its capability before calling the C function. This allows another
1193 * task to pick up the capability and carry on running Haskell
1194 * threads. It also means that if the C call blocks, it won't lock
1197 * The Haskell thread making the C call is put to sleep for the
1198 * duration of the call, on the susepended_ccalling_threads queue. We
1199 * give out a token to the task, which it can use to resume the thread
1200 * on return from the C function.
1201 * ------------------------------------------------------------------------- */
1204 suspendThread( Capability *cap )
1208 ACQUIRE_LOCK(&sched_mutex);
1211 sched_belch("thread %d did a _ccall_gc", cap->rCurrentTSO->id));
1213 threadPaused(cap->rCurrentTSO);
1214 cap->rCurrentTSO->link = suspended_ccalling_threads;
1215 suspended_ccalling_threads = cap->rCurrentTSO;
1217 /* Use the thread ID as the token; it should be unique */
1218 tok = cap->rCurrentTSO->id;
1221 cap->link = free_capabilities;
1222 free_capabilities = cap;
1223 n_free_capabilities++;
1226 RELEASE_LOCK(&sched_mutex);
1231 resumeThread( StgInt tok )
1233 StgTSO *tso, **prev;
1236 ACQUIRE_LOCK(&sched_mutex);
1238 prev = &suspended_ccalling_threads;
1239 for (tso = suspended_ccalling_threads;
1240 tso != END_TSO_QUEUE;
1241 prev = &tso->link, tso = tso->link) {
1242 if (tso->id == (StgThreadID)tok) {
1247 if (tso == END_TSO_QUEUE) {
1248 barf("resumeThread: thread not found");
1250 tso->link = END_TSO_QUEUE;
1253 while (free_capabilities == NULL) {
1254 IF_DEBUG(scheduler, sched_belch("waiting to resume"));
1255 pthread_cond_wait(&thread_ready_cond, &sched_mutex);
1256 IF_DEBUG(scheduler, sched_belch("resuming thread %d", tso->id));
1258 cap = free_capabilities;
1259 free_capabilities = cap->link;
1260 n_free_capabilities--;
1262 cap = &MainRegTable;
1265 cap->rCurrentTSO = tso;
1267 RELEASE_LOCK(&sched_mutex);
1272 /* ---------------------------------------------------------------------------
1274 * ------------------------------------------------------------------------ */
1275 static void unblockThread(StgTSO *tso);
1277 /* ---------------------------------------------------------------------------
1278 * Comparing Thread ids.
1280 * This is used from STG land in the implementation of the
1281 * instances of Eq/Ord for ThreadIds.
1282 * ------------------------------------------------------------------------ */
1284 int cmp_thread(const StgTSO *tso1, const StgTSO *tso2)
1286 StgThreadID id1 = tso1->id;
1287 StgThreadID id2 = tso2->id;
1289 if (id1 < id2) return (-1);
1290 if (id1 > id2) return 1;
1294 /* ---------------------------------------------------------------------------
1295 Create a new thread.
1297 The new thread starts with the given stack size. Before the
1298 scheduler can run, however, this thread needs to have a closure
1299 (and possibly some arguments) pushed on its stack. See
1300 pushClosure() in Schedule.h.
1302 createGenThread() and createIOThread() (in SchedAPI.h) are
1303 convenient packaged versions of this function.
1305 currently pri (priority) is only used in a GRAN setup -- HWL
1306 ------------------------------------------------------------------------ */
1307 //@cindex createThread
1309 /* currently pri (priority) is only used in a GRAN setup -- HWL */
1311 createThread(nat stack_size, StgInt pri)
1313 return createThread_(stack_size, rtsFalse, pri);
1317 createThread_(nat size, rtsBool have_lock, StgInt pri)
1321 createThread(nat stack_size)
1323 return createThread_(stack_size, rtsFalse);
1327 createThread_(nat size, rtsBool have_lock)
1334 /* First check whether we should create a thread at all */
1336 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
1337 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
1339 belch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1340 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1341 return END_TSO_QUEUE;
1347 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
1350 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
1352 /* catch ridiculously small stack sizes */
1353 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
1354 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
1357 stack_size = size - TSO_STRUCT_SIZEW;
1359 tso = (StgTSO *)allocate(size);
1360 TICK_ALLOC_TSO(size-TSO_STRUCT_SIZEW, 0);
1362 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
1364 SET_GRAN_HDR(tso, ThisPE);
1366 tso->what_next = ThreadEnterGHC;
1368 /* tso->id needs to be unique. For now we use a heavyweight mutex to
1369 * protect the increment operation on next_thread_id.
1370 * In future, we could use an atomic increment instead.
1372 if (!have_lock) { ACQUIRE_LOCK(&sched_mutex); }
1373 tso->id = next_thread_id++;
1374 if (!have_lock) { RELEASE_LOCK(&sched_mutex); }
1376 tso->why_blocked = NotBlocked;
1377 tso->blocked_exceptions = NULL;
1379 tso->stack_size = stack_size;
1380 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
1382 tso->sp = (P_)&(tso->stack) + stack_size;
1385 tso->prof.CCCS = CCS_MAIN;
1388 /* put a stop frame on the stack */
1389 tso->sp -= sizeofW(StgStopFrame);
1390 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
1391 tso->su = (StgUpdateFrame*)tso->sp;
1395 tso->link = END_TSO_QUEUE;
1396 /* uses more flexible routine in GranSim */
1397 insertThread(tso, CurrentProc);
1399 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
1404 #if defined(GRAN) || defined(PAR)
1405 DumpGranEvent(GR_START,tso);
1408 /* Link the new thread on the global thread list.
1410 tso->global_link = all_threads;
1414 tso->gran.pri = pri;
1416 tso->gran.magic = TSO_MAGIC; // debugging only
1418 tso->gran.sparkname = 0;
1419 tso->gran.startedat = CURRENT_TIME;
1420 tso->gran.exported = 0;
1421 tso->gran.basicblocks = 0;
1422 tso->gran.allocs = 0;
1423 tso->gran.exectime = 0;
1424 tso->gran.fetchtime = 0;
1425 tso->gran.fetchcount = 0;
1426 tso->gran.blocktime = 0;
1427 tso->gran.blockcount = 0;
1428 tso->gran.blockedat = 0;
1429 tso->gran.globalsparks = 0;
1430 tso->gran.localsparks = 0;
1431 if (RtsFlags.GranFlags.Light)
1432 tso->gran.clock = Now; /* local clock */
1434 tso->gran.clock = 0;
1436 IF_DEBUG(gran,printTSO(tso));
1439 tso->par.magic = TSO_MAGIC; // debugging only
1441 tso->par.sparkname = 0;
1442 tso->par.startedat = CURRENT_TIME;
1443 tso->par.exported = 0;
1444 tso->par.basicblocks = 0;
1445 tso->par.allocs = 0;
1446 tso->par.exectime = 0;
1447 tso->par.fetchtime = 0;
1448 tso->par.fetchcount = 0;
1449 tso->par.blocktime = 0;
1450 tso->par.blockcount = 0;
1451 tso->par.blockedat = 0;
1452 tso->par.globalsparks = 0;
1453 tso->par.localsparks = 0;
1457 globalGranStats.tot_threads_created++;
1458 globalGranStats.threads_created_on_PE[CurrentProc]++;
1459 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
1460 globalGranStats.tot_sq_probes++;
1465 belch("==__ schedule: Created TSO %d (%p);",
1466 CurrentProc, tso, tso->id));
1468 IF_PAR_DEBUG(verbose,
1469 belch("==__ schedule: Created TSO %d (%p); %d threads active",
1470 tso->id, tso, advisory_thread_count));
1472 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
1473 tso->id, tso->stack_size));
1479 Turn a spark into a thread.
1480 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
1483 //@cindex activateSpark
1485 activateSpark (rtsSpark spark)
1489 ASSERT(spark != (rtsSpark)NULL);
1490 tso = createThread_(RtsFlags.GcFlags.initialStkSize, rtsTrue);
1491 if (tso!=END_TSO_QUEUE) {
1492 pushClosure(tso,spark);
1493 PUSH_ON_RUN_QUEUE(tso);
1494 advisory_thread_count++;
1496 if (RtsFlags.ParFlags.ParStats.Full) {
1497 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
1498 IF_PAR_DEBUG(verbose,
1499 belch("==^^ activateSpark: turning spark of closure %p (%s) into a thread",
1500 (StgClosure *)spark, info_type((StgClosure *)spark)));
1503 barf("activateSpark: Cannot create TSO");
1505 // ToDo: fwd info on local/global spark to thread -- HWL
1506 // tso->gran.exported = spark->exported;
1507 // tso->gran.locked = !spark->global;
1508 // tso->gran.sparkname = spark->name;
1514 /* ---------------------------------------------------------------------------
1517 * scheduleThread puts a thread on the head of the runnable queue.
1518 * This will usually be done immediately after a thread is created.
1519 * The caller of scheduleThread must create the thread using e.g.
1520 * createThread and push an appropriate closure
1521 * on this thread's stack before the scheduler is invoked.
1522 * ------------------------------------------------------------------------ */
1525 scheduleThread(StgTSO *tso)
1527 if (tso==END_TSO_QUEUE){
1532 ACQUIRE_LOCK(&sched_mutex);
1534 /* Put the new thread on the head of the runnable queue. The caller
1535 * better push an appropriate closure on this thread's stack
1536 * beforehand. In the SMP case, the thread may start running as
1537 * soon as we release the scheduler lock below.
1539 PUSH_ON_RUN_QUEUE(tso);
1543 IF_DEBUG(scheduler,printTSO(tso));
1545 RELEASE_LOCK(&sched_mutex);
1548 /* ---------------------------------------------------------------------------
1551 * Start up Posix threads to run each of the scheduler tasks.
1552 * I believe the task ids are not needed in the system as defined.
1554 * ------------------------------------------------------------------------ */
1556 #if defined(PAR) || defined(SMP)
1558 taskStart( void *arg STG_UNUSED )
1560 rts_evalNothing(NULL);
1564 /* ---------------------------------------------------------------------------
1567 * Initialise the scheduler. This resets all the queues - if the
1568 * queues contained any threads, they'll be garbage collected at the
1571 * This now calls startTasks(), so should only be called once! KH @ 25/10/99
1572 * ------------------------------------------------------------------------ */
1576 term_handler(int sig STG_UNUSED)
1579 ACQUIRE_LOCK(&term_mutex);
1581 RELEASE_LOCK(&term_mutex);
1586 //@cindex initScheduler
1593 for (i=0; i<=MAX_PROC; i++) {
1594 run_queue_hds[i] = END_TSO_QUEUE;
1595 run_queue_tls[i] = END_TSO_QUEUE;
1596 blocked_queue_hds[i] = END_TSO_QUEUE;
1597 blocked_queue_tls[i] = END_TSO_QUEUE;
1598 ccalling_threadss[i] = END_TSO_QUEUE;
1599 sleeping_queue = END_TSO_QUEUE;
1602 run_queue_hd = END_TSO_QUEUE;
1603 run_queue_tl = END_TSO_QUEUE;
1604 blocked_queue_hd = END_TSO_QUEUE;
1605 blocked_queue_tl = END_TSO_QUEUE;
1606 sleeping_queue = END_TSO_QUEUE;
1609 suspended_ccalling_threads = END_TSO_QUEUE;
1611 main_threads = NULL;
1612 all_threads = END_TSO_QUEUE;
1617 RtsFlags.ConcFlags.ctxtSwitchTicks =
1618 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
1621 ecafList = END_ECAF_LIST;
1625 /* Install the SIGHUP handler */
1628 struct sigaction action,oact;
1630 action.sa_handler = term_handler;
1631 sigemptyset(&action.sa_mask);
1632 action.sa_flags = 0;
1633 if (sigaction(SIGTERM, &action, &oact) != 0) {
1634 barf("can't install TERM handler");
1640 /* Allocate N Capabilities */
1643 Capability *cap, *prev;
1646 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
1647 cap = stgMallocBytes(sizeof(Capability), "initScheduler:capabilities");
1651 free_capabilities = cap;
1652 n_free_capabilities = RtsFlags.ParFlags.nNodes;
1654 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: Allocated %d capabilities\n",
1655 n_free_capabilities););
1658 #if defined(SMP) || defined(PAR)
1671 /* make some space for saving all the thread ids */
1672 task_ids = stgMallocBytes(RtsFlags.ParFlags.nNodes * sizeof(task_info),
1673 "initScheduler:task_ids");
1675 /* and create all the threads */
1676 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
1677 r = pthread_create(&tid,NULL,taskStart,NULL);
1679 barf("startTasks: Can't create new Posix thread");
1681 task_ids[i].id = tid;
1682 task_ids[i].mut_time = 0.0;
1683 task_ids[i].mut_etime = 0.0;
1684 task_ids[i].gc_time = 0.0;
1685 task_ids[i].gc_etime = 0.0;
1686 task_ids[i].elapsedtimestart = elapsedtime();
1687 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: Started task: %ld\n",tid););
1693 exitScheduler( void )
1698 /* Don't want to use pthread_cancel, since we'd have to install
1699 * these silly exception handlers (pthread_cleanup_{push,pop}) around
1703 /* Cancel all our tasks */
1704 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
1705 pthread_cancel(task_ids[i].id);
1708 /* Wait for all the tasks to terminate */
1709 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
1710 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: waiting for task %ld\n",
1712 pthread_join(task_ids[i].id, NULL);
1716 /* Send 'em all a SIGHUP. That should shut 'em up.
1718 await_death = RtsFlags.ParFlags.nNodes;
1719 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
1720 pthread_kill(task_ids[i].id,SIGTERM);
1722 while (await_death > 0) {
1728 /* -----------------------------------------------------------------------------
1729 Managing the per-task allocation areas.
1731 Each capability comes with an allocation area. These are
1732 fixed-length block lists into which allocation can be done.
1734 ToDo: no support for two-space collection at the moment???
1735 -------------------------------------------------------------------------- */
1737 /* -----------------------------------------------------------------------------
1738 * waitThread is the external interface for running a new computation
1739 * and waiting for the result.
1741 * In the non-SMP case, we create a new main thread, push it on the
1742 * main-thread stack, and invoke the scheduler to run it. The
1743 * scheduler will return when the top main thread on the stack has
1744 * completed or died, and fill in the necessary fields of the
1745 * main_thread structure.
1747 * In the SMP case, we create a main thread as before, but we then
1748 * create a new condition variable and sleep on it. When our new
1749 * main thread has completed, we'll be woken up and the status/result
1750 * will be in the main_thread struct.
1751 * -------------------------------------------------------------------------- */
1754 howManyThreadsAvail ( void )
1758 for (q = run_queue_hd; q != END_TSO_QUEUE; q = q->link)
1760 for (q = blocked_queue_hd; q != END_TSO_QUEUE; q = q->link)
1762 for (q = sleeping_queue; q != END_TSO_QUEUE; q = q->link)
1768 finishAllThreads ( void )
1771 while (run_queue_hd != END_TSO_QUEUE) {
1772 waitThread ( run_queue_hd, NULL );
1774 while (blocked_queue_hd != END_TSO_QUEUE) {
1775 waitThread ( blocked_queue_hd, NULL );
1777 while (sleeping_queue != END_TSO_QUEUE) {
1778 waitThread ( blocked_queue_hd, NULL );
1781 (blocked_queue_hd != END_TSO_QUEUE ||
1782 run_queue_hd != END_TSO_QUEUE ||
1783 sleeping_queue != END_TSO_QUEUE);
1787 waitThread(StgTSO *tso, /*out*/StgClosure **ret)
1790 SchedulerStatus stat;
1792 ACQUIRE_LOCK(&sched_mutex);
1794 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
1800 pthread_cond_init(&m->wakeup, NULL);
1803 m->link = main_threads;
1806 IF_DEBUG(scheduler, fprintf(stderr, "scheduler: new main thread (%d)\n",
1811 pthread_cond_wait(&m->wakeup, &sched_mutex);
1812 } while (m->stat == NoStatus);
1814 /* GranSim specific init */
1815 CurrentTSO = m->tso; // the TSO to run
1816 procStatus[MainProc] = Busy; // status of main PE
1817 CurrentProc = MainProc; // PE to run it on
1822 ASSERT(m->stat != NoStatus);
1828 pthread_cond_destroy(&m->wakeup);
1831 IF_DEBUG(scheduler, fprintf(stderr, "scheduler: main thread (%d) finished\n",
1835 RELEASE_LOCK(&sched_mutex);
1840 //@node Run queue code, Garbage Collextion Routines, Suspend and Resume, Main scheduling code
1841 //@subsection Run queue code
1845 NB: In GranSim we have many run queues; run_queue_hd is actually a macro
1846 unfolding to run_queue_hds[CurrentProc], thus CurrentProc is an
1847 implicit global variable that has to be correct when calling these
1851 /* Put the new thread on the head of the runnable queue.
1852 * The caller of createThread better push an appropriate closure
1853 * on this thread's stack before the scheduler is invoked.
1855 static /* inline */ void
1856 add_to_run_queue(tso)
1859 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
1860 tso->link = run_queue_hd;
1862 if (run_queue_tl == END_TSO_QUEUE) {
1867 /* Put the new thread at the end of the runnable queue. */
1868 static /* inline */ void
1869 push_on_run_queue(tso)
1872 ASSERT(get_itbl((StgClosure *)tso)->type == TSO);
1873 ASSERT(run_queue_hd!=NULL && run_queue_tl!=NULL);
1874 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
1875 if (run_queue_hd == END_TSO_QUEUE) {
1878 run_queue_tl->link = tso;
1884 Should be inlined because it's used very often in schedule. The tso
1885 argument is actually only needed in GranSim, where we want to have the
1886 possibility to schedule *any* TSO on the run queue, irrespective of the
1887 actual ordering. Therefore, if tso is not the nil TSO then we traverse
1888 the run queue and dequeue the tso, adjusting the links in the queue.
1890 //@cindex take_off_run_queue
1891 static /* inline */ StgTSO*
1892 take_off_run_queue(StgTSO *tso) {
1896 qetlaHbogh Qu' ngaSbogh ghomDaQ {tso} yIteq!
1898 if tso is specified, unlink that tso from the run_queue (doesn't have
1899 to be at the beginning of the queue); GranSim only
1901 if (tso!=END_TSO_QUEUE) {
1902 /* find tso in queue */
1903 for (t=run_queue_hd, prev=END_TSO_QUEUE;
1904 t!=END_TSO_QUEUE && t!=tso;
1908 /* now actually dequeue the tso */
1909 if (prev!=END_TSO_QUEUE) {
1910 ASSERT(run_queue_hd!=t);
1911 prev->link = t->link;
1913 /* t is at beginning of thread queue */
1914 ASSERT(run_queue_hd==t);
1915 run_queue_hd = t->link;
1917 /* t is at end of thread queue */
1918 if (t->link==END_TSO_QUEUE) {
1919 ASSERT(t==run_queue_tl);
1920 run_queue_tl = prev;
1922 ASSERT(run_queue_tl!=t);
1924 t->link = END_TSO_QUEUE;
1926 /* take tso from the beginning of the queue; std concurrent code */
1928 if (t != END_TSO_QUEUE) {
1929 run_queue_hd = t->link;
1930 t->link = END_TSO_QUEUE;
1931 if (run_queue_hd == END_TSO_QUEUE) {
1932 run_queue_tl = END_TSO_QUEUE;
1941 //@node Garbage Collextion Routines, Blocking Queue Routines, Run queue code, Main scheduling code
1942 //@subsection Garbage Collextion Routines
1944 /* ---------------------------------------------------------------------------
1945 Where are the roots that we know about?
1947 - all the threads on the runnable queue
1948 - all the threads on the blocked queue
1949 - all the threads on the sleeping queue
1950 - all the thread currently executing a _ccall_GC
1951 - all the "main threads"
1953 ------------------------------------------------------------------------ */
1955 /* This has to be protected either by the scheduler monitor, or by the
1956 garbage collection monitor (probably the latter).
1960 static void GetRoots(void)
1967 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
1968 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
1969 run_queue_hds[i] = (StgTSO *)MarkRoot((StgClosure *)run_queue_hds[i]);
1970 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
1971 run_queue_tls[i] = (StgTSO *)MarkRoot((StgClosure *)run_queue_tls[i]);
1973 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
1974 blocked_queue_hds[i] = (StgTSO *)MarkRoot((StgClosure *)blocked_queue_hds[i]);
1975 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
1976 blocked_queue_tls[i] = (StgTSO *)MarkRoot((StgClosure *)blocked_queue_tls[i]);
1977 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
1978 ccalling_threadss[i] = (StgTSO *)MarkRoot((StgClosure *)ccalling_threadss[i]);
1985 if (run_queue_hd != END_TSO_QUEUE) {
1986 ASSERT(run_queue_tl != END_TSO_QUEUE);
1987 run_queue_hd = (StgTSO *)MarkRoot((StgClosure *)run_queue_hd);
1988 run_queue_tl = (StgTSO *)MarkRoot((StgClosure *)run_queue_tl);
1991 if (blocked_queue_hd != END_TSO_QUEUE) {
1992 ASSERT(blocked_queue_tl != END_TSO_QUEUE);
1993 blocked_queue_hd = (StgTSO *)MarkRoot((StgClosure *)blocked_queue_hd);
1994 blocked_queue_tl = (StgTSO *)MarkRoot((StgClosure *)blocked_queue_tl);
1997 if (sleeping_queue != END_TSO_QUEUE) {
1998 sleeping_queue = (StgTSO *)MarkRoot((StgClosure *)sleeping_queue);
2002 for (m = main_threads; m != NULL; m = m->link) {
2003 m->tso = (StgTSO *)MarkRoot((StgClosure *)m->tso);
2005 if (suspended_ccalling_threads != END_TSO_QUEUE)
2006 suspended_ccalling_threads =
2007 (StgTSO *)MarkRoot((StgClosure *)suspended_ccalling_threads);
2009 #if defined(SMP) || defined(PAR) || defined(GRAN)
2014 /* -----------------------------------------------------------------------------
2017 This is the interface to the garbage collector from Haskell land.
2018 We provide this so that external C code can allocate and garbage
2019 collect when called from Haskell via _ccall_GC.
2021 It might be useful to provide an interface whereby the programmer
2022 can specify more roots (ToDo).
2024 This needs to be protected by the GC condition variable above. KH.
2025 -------------------------------------------------------------------------- */
2027 void (*extra_roots)(void);
2032 GarbageCollect(GetRoots,rtsFalse);
2036 performMajorGC(void)
2038 GarbageCollect(GetRoots,rtsTrue);
2044 GetRoots(); /* the scheduler's roots */
2045 extra_roots(); /* the user's roots */
2049 performGCWithRoots(void (*get_roots)(void))
2051 extra_roots = get_roots;
2053 GarbageCollect(AllRoots,rtsFalse);
2056 /* -----------------------------------------------------------------------------
2059 If the thread has reached its maximum stack size, then raise the
2060 StackOverflow exception in the offending thread. Otherwise
2061 relocate the TSO into a larger chunk of memory and adjust its stack
2063 -------------------------------------------------------------------------- */
2066 threadStackOverflow(StgTSO *tso)
2068 nat new_stack_size, new_tso_size, diff, stack_words;
2072 IF_DEBUG(sanity,checkTSO(tso));
2073 if (tso->stack_size >= tso->max_stack_size) {
2076 belch("@@ threadStackOverflow of TSO %d (%p): stack too large (now %ld; max is %ld",
2077 tso->id, tso, tso->stack_size, tso->max_stack_size);
2078 /* If we're debugging, just print out the top of the stack */
2079 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2083 fprintf(stderr, "fatal: stack overflow in Hugs; aborting\n" );
2086 /* Send this thread the StackOverflow exception */
2087 raiseAsync(tso, (StgClosure *)stackOverflow_closure);
2092 /* Try to double the current stack size. If that takes us over the
2093 * maximum stack size for this thread, then use the maximum instead.
2094 * Finally round up so the TSO ends up as a whole number of blocks.
2096 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2097 new_tso_size = (nat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2098 TSO_STRUCT_SIZE)/sizeof(W_);
2099 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2100 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2102 IF_DEBUG(scheduler, fprintf(stderr,"scheduler: increasing stack size from %d words to %d.\n", tso->stack_size, new_stack_size));
2104 dest = (StgTSO *)allocate(new_tso_size);
2105 TICK_ALLOC_TSO(new_tso_size-sizeofW(StgTSO),0);
2107 /* copy the TSO block and the old stack into the new area */
2108 memcpy(dest,tso,TSO_STRUCT_SIZE);
2109 stack_words = tso->stack + tso->stack_size - tso->sp;
2110 new_sp = (P_)dest + new_tso_size - stack_words;
2111 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2113 /* relocate the stack pointers... */
2114 diff = (P_)new_sp - (P_)tso->sp; /* In *words* */
2115 dest->su = (StgUpdateFrame *) ((P_)dest->su + diff);
2117 dest->stack_size = new_stack_size;
2119 /* and relocate the update frame list */
2120 relocate_TSO(tso, dest);
2122 /* Mark the old TSO as relocated. We have to check for relocated
2123 * TSOs in the garbage collector and any primops that deal with TSOs.
2125 * It's important to set the sp and su values to just beyond the end
2126 * of the stack, so we don't attempt to scavenge any part of the
2129 tso->what_next = ThreadRelocated;
2131 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2132 tso->su = (StgUpdateFrame *)tso->sp;
2133 tso->why_blocked = NotBlocked;
2134 dest->mut_link = NULL;
2136 IF_PAR_DEBUG(verbose,
2137 belch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld",
2138 tso->id, tso, tso->stack_size);
2139 /* If we're debugging, just print out the top of the stack */
2140 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2143 IF_DEBUG(sanity,checkTSO(tso));
2145 IF_DEBUG(scheduler,printTSO(dest));
2151 //@node Blocking Queue Routines, Exception Handling Routines, Garbage Collextion Routines, Main scheduling code
2152 //@subsection Blocking Queue Routines
2154 /* ---------------------------------------------------------------------------
2155 Wake up a queue that was blocked on some resource.
2156 ------------------------------------------------------------------------ */
2160 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2165 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2167 /* write RESUME events to log file and
2168 update blocked and fetch time (depending on type of the orig closure) */
2169 if (RtsFlags.ParFlags.ParStats.Full) {
2170 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
2171 GR_RESUME, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
2172 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
2174 switch (get_itbl(node)->type) {
2176 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2181 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2184 barf("{unblockOneLocked}Daq Qagh: unexpected closure in blocking queue");
2191 static StgBlockingQueueElement *
2192 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2195 PEs node_loc, tso_loc;
2197 node_loc = where_is(node); // should be lifted out of loop
2198 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2199 tso_loc = where_is((StgClosure *)tso);
2200 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
2201 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
2202 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
2203 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
2204 // insertThread(tso, node_loc);
2205 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
2207 tso, node, (rtsSpark*)NULL);
2208 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2211 } else { // TSO is remote (actually should be FMBQ)
2212 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
2213 RtsFlags.GranFlags.Costs.gunblocktime +
2214 RtsFlags.GranFlags.Costs.latency;
2215 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
2217 tso, node, (rtsSpark*)NULL);
2218 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2221 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
2223 fprintf(stderr," %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
2224 (node_loc==tso_loc ? "Local" : "Global"),
2225 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
2226 tso->block_info.closure = NULL;
2227 IF_DEBUG(scheduler,belch("-- Waking up thread %ld (%p)",
2231 static StgBlockingQueueElement *
2232 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2234 StgBlockingQueueElement *next;
2236 switch (get_itbl(bqe)->type) {
2238 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
2239 /* if it's a TSO just push it onto the run_queue */
2241 // ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
2242 PUSH_ON_RUN_QUEUE((StgTSO *)bqe);
2244 unblockCount(bqe, node);
2245 /* reset blocking status after dumping event */
2246 ((StgTSO *)bqe)->why_blocked = NotBlocked;
2250 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
2252 bqe->link = PendingFetches;
2253 PendingFetches = bqe;
2257 /* can ignore this case in a non-debugging setup;
2258 see comments on RBHSave closures above */
2260 /* check that the closure is an RBHSave closure */
2261 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
2262 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
2263 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
2267 barf("{unblockOneLocked}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
2268 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
2272 // IF_DEBUG(scheduler,sched_belch("waking up thread %ld", tso->id));
2276 #else /* !GRAN && !PAR */
2278 unblockOneLocked(StgTSO *tso)
2282 ASSERT(get_itbl(tso)->type == TSO);
2283 ASSERT(tso->why_blocked != NotBlocked);
2284 tso->why_blocked = NotBlocked;
2286 PUSH_ON_RUN_QUEUE(tso);
2288 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", tso->id));
2293 #if defined(GRAN) || defined(PAR)
2294 inline StgBlockingQueueElement *
2295 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
2297 ACQUIRE_LOCK(&sched_mutex);
2298 bqe = unblockOneLocked(bqe, node);
2299 RELEASE_LOCK(&sched_mutex);
2304 unblockOne(StgTSO *tso)
2306 ACQUIRE_LOCK(&sched_mutex);
2307 tso = unblockOneLocked(tso);
2308 RELEASE_LOCK(&sched_mutex);
2315 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2317 StgBlockingQueueElement *bqe;
2322 belch("## AwBQ for node %p on PE %d @ %ld by TSO %d (%p): ", \
2323 node, CurrentProc, CurrentTime[CurrentProc],
2324 CurrentTSO->id, CurrentTSO));
2326 node_loc = where_is(node);
2328 ASSERT(get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
2329 get_itbl(q)->type == CONSTR); // closure (type constructor)
2330 ASSERT(is_unique(node));
2332 /* FAKE FETCH: magically copy the node to the tso's proc;
2333 no Fetch necessary because in reality the node should not have been
2334 moved to the other PE in the first place
2336 if (CurrentProc!=node_loc) {
2338 belch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)",
2339 node, node_loc, CurrentProc, CurrentTSO->id,
2340 // CurrentTSO, where_is(CurrentTSO),
2341 node->header.gran.procs));
2342 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
2344 belch("## new bitmask of node %p is %#x",
2345 node, node->header.gran.procs));
2346 if (RtsFlags.GranFlags.GranSimStats.Global) {
2347 globalGranStats.tot_fake_fetches++;
2352 // ToDo: check: ASSERT(CurrentProc==node_loc);
2353 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
2356 bqe points to the current element in the queue
2357 next points to the next element in the queue
2359 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2360 //tso_loc = where_is(tso);
2362 bqe = unblockOneLocked(bqe, node);
2365 /* if this is the BQ of an RBH, we have to put back the info ripped out of
2366 the closure to make room for the anchor of the BQ */
2367 if (bqe!=END_BQ_QUEUE) {
2368 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
2370 ASSERT((info_ptr==&RBH_Save_0_info) ||
2371 (info_ptr==&RBH_Save_1_info) ||
2372 (info_ptr==&RBH_Save_2_info));
2374 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
2375 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
2376 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
2379 belch("## Filled in RBH_Save for %p (%s) at end of AwBQ",
2380 node, info_type(node)));
2383 /* statistics gathering */
2384 if (RtsFlags.GranFlags.GranSimStats.Global) {
2385 // globalGranStats.tot_bq_processing_time += bq_processing_time;
2386 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
2387 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
2388 globalGranStats.tot_awbq++; // total no. of bqs awakened
2391 fprintf(stderr,"## BQ Stats of %p: [%d entries] %s\n",
2392 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
2396 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2398 StgBlockingQueueElement *bqe, *next;
2400 ACQUIRE_LOCK(&sched_mutex);
2402 IF_PAR_DEBUG(verbose,
2403 belch("## AwBQ for node %p on [%x]: ",
2406 ASSERT(get_itbl(q)->type == TSO ||
2407 get_itbl(q)->type == BLOCKED_FETCH ||
2408 get_itbl(q)->type == CONSTR);
2411 while (get_itbl(bqe)->type==TSO ||
2412 get_itbl(bqe)->type==BLOCKED_FETCH) {
2413 bqe = unblockOneLocked(bqe, node);
2415 RELEASE_LOCK(&sched_mutex);
2418 #else /* !GRAN && !PAR */
2420 awakenBlockedQueue(StgTSO *tso)
2422 ACQUIRE_LOCK(&sched_mutex);
2423 while (tso != END_TSO_QUEUE) {
2424 tso = unblockOneLocked(tso);
2426 RELEASE_LOCK(&sched_mutex);
2430 //@node Exception Handling Routines, Debugging Routines, Blocking Queue Routines, Main scheduling code
2431 //@subsection Exception Handling Routines
2433 /* ---------------------------------------------------------------------------
2435 - usually called inside a signal handler so it mustn't do anything fancy.
2436 ------------------------------------------------------------------------ */
2439 interruptStgRts(void)
2445 /* -----------------------------------------------------------------------------
2448 This is for use when we raise an exception in another thread, which
2450 This has nothing to do with the UnblockThread event in GranSim. -- HWL
2451 -------------------------------------------------------------------------- */
2453 #if defined(GRAN) || defined(PAR)
2455 NB: only the type of the blocking queue is different in GranSim and GUM
2456 the operations on the queue-elements are the same
2457 long live polymorphism!
2460 unblockThread(StgTSO *tso)
2462 StgBlockingQueueElement *t, **last;
2464 ACQUIRE_LOCK(&sched_mutex);
2465 switch (tso->why_blocked) {
2468 return; /* not blocked */
2471 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
2473 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
2474 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
2476 last = (StgBlockingQueueElement **)&mvar->head;
2477 for (t = (StgBlockingQueueElement *)mvar->head;
2479 last = &t->link, last_tso = t, t = t->link) {
2480 if (t == (StgBlockingQueueElement *)tso) {
2481 *last = (StgBlockingQueueElement *)tso->link;
2482 if (mvar->tail == tso) {
2483 mvar->tail = (StgTSO *)last_tso;
2488 barf("unblockThread (MVAR): TSO not found");
2491 case BlockedOnBlackHole:
2492 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
2494 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
2496 last = &bq->blocking_queue;
2497 for (t = bq->blocking_queue;
2499 last = &t->link, t = t->link) {
2500 if (t == (StgBlockingQueueElement *)tso) {
2501 *last = (StgBlockingQueueElement *)tso->link;
2505 barf("unblockThread (BLACKHOLE): TSO not found");
2508 case BlockedOnException:
2510 StgTSO *target = tso->block_info.tso;
2512 ASSERT(get_itbl(target)->type == TSO);
2513 ASSERT(target->blocked_exceptions != NULL);
2515 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
2516 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
2518 last = &t->link, t = t->link) {
2519 ASSERT(get_itbl(t)->type == TSO);
2520 if (t == (StgBlockingQueueElement *)tso) {
2521 *last = (StgBlockingQueueElement *)tso->link;
2525 barf("unblockThread (Exception): TSO not found");
2529 case BlockedOnWrite:
2531 StgBlockingQueueElement *prev = NULL;
2532 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
2533 prev = t, t = t->link) {
2534 if (t == (StgBlockingQueueElement *)tso) {
2536 blocked_queue_hd = (StgTSO *)t->link;
2537 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
2538 blocked_queue_tl = END_TSO_QUEUE;
2541 prev->link = t->link;
2542 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
2543 blocked_queue_tl = (StgTSO *)prev;
2549 barf("unblockThread (I/O): TSO not found");
2552 case BlockedOnDelay:
2554 StgBlockingQueueElement *prev = NULL;
2555 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
2556 prev = t, t = t->link) {
2557 if (t == (StgBlockingQueueElement *)tso) {
2559 sleeping_queue = (StgTSO *)t->link;
2561 prev->link = t->link;
2566 barf("unblockThread (I/O): TSO not found");
2570 barf("unblockThread");
2574 tso->link = END_TSO_QUEUE;
2575 tso->why_blocked = NotBlocked;
2576 tso->block_info.closure = NULL;
2577 PUSH_ON_RUN_QUEUE(tso);
2578 RELEASE_LOCK(&sched_mutex);
2582 unblockThread(StgTSO *tso)
2586 ACQUIRE_LOCK(&sched_mutex);
2587 switch (tso->why_blocked) {
2590 return; /* not blocked */
2593 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
2595 StgTSO *last_tso = END_TSO_QUEUE;
2596 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
2599 for (t = mvar->head; t != END_TSO_QUEUE;
2600 last = &t->link, last_tso = t, t = t->link) {
2603 if (mvar->tail == tso) {
2604 mvar->tail = last_tso;
2609 barf("unblockThread (MVAR): TSO not found");
2612 case BlockedOnBlackHole:
2613 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
2615 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
2617 last = &bq->blocking_queue;
2618 for (t = bq->blocking_queue; t != END_TSO_QUEUE;
2619 last = &t->link, t = t->link) {
2625 barf("unblockThread (BLACKHOLE): TSO not found");
2628 case BlockedOnException:
2630 StgTSO *target = tso->block_info.tso;
2632 ASSERT(get_itbl(target)->type == TSO);
2633 ASSERT(target->blocked_exceptions != NULL);
2635 last = &target->blocked_exceptions;
2636 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
2637 last = &t->link, t = t->link) {
2638 ASSERT(get_itbl(t)->type == TSO);
2644 barf("unblockThread (Exception): TSO not found");
2648 case BlockedOnWrite:
2650 StgTSO *prev = NULL;
2651 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
2652 prev = t, t = t->link) {
2655 blocked_queue_hd = t->link;
2656 if (blocked_queue_tl == t) {
2657 blocked_queue_tl = END_TSO_QUEUE;
2660 prev->link = t->link;
2661 if (blocked_queue_tl == t) {
2662 blocked_queue_tl = prev;
2668 barf("unblockThread (I/O): TSO not found");
2671 case BlockedOnDelay:
2673 StgTSO *prev = NULL;
2674 for (t = sleeping_queue; t != END_TSO_QUEUE;
2675 prev = t, t = t->link) {
2678 sleeping_queue = t->link;
2680 prev->link = t->link;
2685 barf("unblockThread (I/O): TSO not found");
2689 barf("unblockThread");
2693 tso->link = END_TSO_QUEUE;
2694 tso->why_blocked = NotBlocked;
2695 tso->block_info.closure = NULL;
2696 PUSH_ON_RUN_QUEUE(tso);
2697 RELEASE_LOCK(&sched_mutex);
2701 /* -----------------------------------------------------------------------------
2704 * The following function implements the magic for raising an
2705 * asynchronous exception in an existing thread.
2707 * We first remove the thread from any queue on which it might be
2708 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
2710 * We strip the stack down to the innermost CATCH_FRAME, building
2711 * thunks in the heap for all the active computations, so they can
2712 * be restarted if necessary. When we reach a CATCH_FRAME, we build
2713 * an application of the handler to the exception, and push it on
2714 * the top of the stack.
2716 * How exactly do we save all the active computations? We create an
2717 * AP_UPD for every UpdateFrame on the stack. Entering one of these
2718 * AP_UPDs pushes everything from the corresponding update frame
2719 * upwards onto the stack. (Actually, it pushes everything up to the
2720 * next update frame plus a pointer to the next AP_UPD object.
2721 * Entering the next AP_UPD object pushes more onto the stack until we
2722 * reach the last AP_UPD object - at which point the stack should look
2723 * exactly as it did when we killed the TSO and we can continue
2724 * execution by entering the closure on top of the stack.
2726 * We can also kill a thread entirely - this happens if either (a) the
2727 * exception passed to raiseAsync is NULL, or (b) there's no
2728 * CATCH_FRAME on the stack. In either case, we strip the entire
2729 * stack and replace the thread with a zombie.
2731 * -------------------------------------------------------------------------- */
2734 deleteThread(StgTSO *tso)
2736 raiseAsync(tso,NULL);
2740 raiseAsync(StgTSO *tso, StgClosure *exception)
2742 StgUpdateFrame* su = tso->su;
2743 StgPtr sp = tso->sp;
2745 /* Thread already dead? */
2746 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
2750 IF_DEBUG(scheduler, sched_belch("raising exception in thread %ld.", tso->id));
2752 /* Remove it from any blocking queues */
2755 /* The stack freezing code assumes there's a closure pointer on
2756 * the top of the stack. This isn't always the case with compiled
2757 * code, so we have to push a dummy closure on the top which just
2758 * returns to the next return address on the stack.
2760 if ( LOOKS_LIKE_GHC_INFO((void*)*sp) ) {
2761 *(--sp) = (W_)&stg_dummy_ret_closure;
2765 int words = ((P_)su - (P_)sp) - 1;
2769 /* If we find a CATCH_FRAME, and we've got an exception to raise,
2770 * then build PAP(handler,exception,realworld#), and leave it on
2771 * top of the stack ready to enter.
2773 if (get_itbl(su)->type == CATCH_FRAME && exception != NULL) {
2774 StgCatchFrame *cf = (StgCatchFrame *)su;
2775 /* we've got an exception to raise, so let's pass it to the
2776 * handler in this frame.
2778 ap = (StgAP_UPD *)allocate(sizeofW(StgPAP) + 2);
2779 TICK_ALLOC_UPD_PAP(3,0);
2780 SET_HDR(ap,&stg_PAP_info,cf->header.prof.ccs);
2783 ap->fun = cf->handler; /* :: Exception -> IO a */
2784 ap->payload[0] = exception;
2785 ap->payload[1] = ARG_TAG(0); /* realworld token */
2787 /* throw away the stack from Sp up to and including the
2790 sp = (P_)su + sizeofW(StgCatchFrame) - 1;
2793 /* Restore the blocked/unblocked state for asynchronous exceptions
2794 * at the CATCH_FRAME.
2796 * If exceptions were unblocked at the catch, arrange that they
2797 * are unblocked again after executing the handler by pushing an
2798 * unblockAsyncExceptions_ret stack frame.
2800 if (!cf->exceptions_blocked) {
2801 *(sp--) = (W_)&stg_unblockAsyncExceptionszh_ret_info;
2804 /* Ensure that async exceptions are blocked when running the handler.
2806 if (tso->blocked_exceptions == NULL) {
2807 tso->blocked_exceptions = END_TSO_QUEUE;
2810 /* Put the newly-built PAP on top of the stack, ready to execute
2811 * when the thread restarts.
2815 tso->what_next = ThreadEnterGHC;
2816 IF_DEBUG(sanity, checkTSO(tso));
2820 /* First build an AP_UPD consisting of the stack chunk above the
2821 * current update frame, with the top word on the stack as the
2824 ap = (StgAP_UPD *)allocate(AP_sizeW(words));
2829 ap->fun = (StgClosure *)sp[0];
2831 for(i=0; i < (nat)words; ++i) {
2832 ap->payload[i] = (StgClosure *)*sp++;
2835 switch (get_itbl(su)->type) {
2839 SET_HDR(ap,&stg_AP_UPD_info,su->header.prof.ccs /* ToDo */);
2840 TICK_ALLOC_UP_THK(words+1,0);
2843 fprintf(stderr, "scheduler: Updating ");
2844 printPtr((P_)su->updatee);
2845 fprintf(stderr, " with ");
2846 printObj((StgClosure *)ap);
2849 /* Replace the updatee with an indirection - happily
2850 * this will also wake up any threads currently
2851 * waiting on the result.
2853 UPD_IND_NOLOCK(su->updatee,ap); /* revert the black hole */
2855 sp += sizeofW(StgUpdateFrame) -1;
2856 sp[0] = (W_)ap; /* push onto stack */
2862 StgCatchFrame *cf = (StgCatchFrame *)su;
2865 /* We want a PAP, not an AP_UPD. Fortunately, the
2866 * layout's the same.
2868 SET_HDR(ap,&stg_PAP_info,su->header.prof.ccs /* ToDo */);
2869 TICK_ALLOC_UPD_PAP(words+1,0);
2871 /* now build o = FUN(catch,ap,handler) */
2872 o = (StgClosure *)allocate(sizeofW(StgClosure)+2);
2873 TICK_ALLOC_FUN(2,0);
2874 SET_HDR(o,&stg_catch_info,su->header.prof.ccs /* ToDo */);
2875 o->payload[0] = (StgClosure *)ap;
2876 o->payload[1] = cf->handler;
2879 fprintf(stderr, "scheduler: Built ");
2880 printObj((StgClosure *)o);
2883 /* pop the old handler and put o on the stack */
2885 sp += sizeofW(StgCatchFrame) - 1;
2892 StgSeqFrame *sf = (StgSeqFrame *)su;
2895 SET_HDR(ap,&stg_PAP_info,su->header.prof.ccs /* ToDo */);
2896 TICK_ALLOC_UPD_PAP(words+1,0);
2898 /* now build o = FUN(seq,ap) */
2899 o = (StgClosure *)allocate(sizeofW(StgClosure)+1);
2900 TICK_ALLOC_SE_THK(1,0);
2901 SET_HDR(o,&stg_seq_info,su->header.prof.ccs /* ToDo */);
2902 o->payload[0] = (StgClosure *)ap;
2905 fprintf(stderr, "scheduler: Built ");
2906 printObj((StgClosure *)o);
2909 /* pop the old handler and put o on the stack */
2911 sp += sizeofW(StgSeqFrame) - 1;
2917 /* We've stripped the entire stack, the thread is now dead. */
2918 sp += sizeofW(StgStopFrame) - 1;
2919 sp[0] = (W_)exception; /* save the exception */
2920 tso->what_next = ThreadKilled;
2921 tso->su = (StgUpdateFrame *)(sp+1);
2932 /* -----------------------------------------------------------------------------
2933 resurrectThreads is called after garbage collection on the list of
2934 threads found to be garbage. Each of these threads will be woken
2935 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2936 on an MVar, or NonTermination if the thread was blocked on a Black
2938 -------------------------------------------------------------------------- */
2941 resurrectThreads( StgTSO *threads )
2945 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2946 next = tso->global_link;
2947 tso->global_link = all_threads;
2949 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
2951 switch (tso->why_blocked) {
2953 case BlockedOnException:
2954 raiseAsync(tso,(StgClosure *)BlockedOnDeadMVar_closure);
2956 case BlockedOnBlackHole:
2957 raiseAsync(tso,(StgClosure *)NonTermination_closure);
2960 /* This might happen if the thread was blocked on a black hole
2961 * belonging to a thread that we've just woken up (raiseAsync
2962 * can wake up threads, remember...).
2966 barf("resurrectThreads: thread blocked in a strange way");
2971 /* -----------------------------------------------------------------------------
2972 * Blackhole detection: if we reach a deadlock, test whether any
2973 * threads are blocked on themselves. Any threads which are found to
2974 * be self-blocked get sent a NonTermination exception.
2976 * This is only done in a deadlock situation in order to avoid
2977 * performance overhead in the normal case.
2978 * -------------------------------------------------------------------------- */
2981 detectBlackHoles( void )
2983 StgTSO *t = all_threads;
2984 StgUpdateFrame *frame;
2985 StgClosure *blocked_on;
2987 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
2989 if (t->why_blocked != BlockedOnBlackHole) {
2993 blocked_on = t->block_info.closure;
2995 for (frame = t->su; ; frame = frame->link) {
2996 switch (get_itbl(frame)->type) {
2999 if (frame->updatee == blocked_on) {
3000 /* We are blocking on one of our own computations, so
3001 * send this thread the NonTermination exception.
3004 sched_belch("thread %d is blocked on itself", t->id));
3005 raiseAsync(t, (StgClosure *)NonTermination_closure);
3026 //@node Debugging Routines, Index, Exception Handling Routines, Main scheduling code
3027 //@subsection Debugging Routines
3029 /* -----------------------------------------------------------------------------
3030 Debugging: why is a thread blocked
3031 -------------------------------------------------------------------------- */
3036 printThreadBlockage(StgTSO *tso)
3038 switch (tso->why_blocked) {
3040 fprintf(stderr,"is blocked on read from fd %d", tso->block_info.fd);
3042 case BlockedOnWrite:
3043 fprintf(stderr,"is blocked on write to fd %d", tso->block_info.fd);
3045 case BlockedOnDelay:
3046 fprintf(stderr,"is blocked until %d", tso->block_info.target);
3049 fprintf(stderr,"is blocked on an MVar");
3051 case BlockedOnException:
3052 fprintf(stderr,"is blocked on delivering an exception to thread %d",
3053 tso->block_info.tso->id);
3055 case BlockedOnBlackHole:
3056 fprintf(stderr,"is blocked on a black hole");
3059 fprintf(stderr,"is not blocked");
3063 fprintf(stderr,"is blocked on global address; local FM_BQ is %p (%s)",
3064 tso->block_info.closure, info_type(tso->block_info.closure));
3066 case BlockedOnGA_NoSend:
3067 fprintf(stderr,"is blocked on global address (no send); local FM_BQ is %p (%s)",
3068 tso->block_info.closure, info_type(tso->block_info.closure));
3072 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
3073 tso->why_blocked, tso->id, tso);
3078 printThreadStatus(StgTSO *tso)
3080 switch (tso->what_next) {
3082 fprintf(stderr,"has been killed");
3084 case ThreadComplete:
3085 fprintf(stderr,"has completed");
3088 printThreadBlockage(tso);
3093 printAllThreads(void)
3097 sched_belch("all threads:");
3098 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
3099 fprintf(stderr, "\tthread %d ", t->id);
3100 printThreadStatus(t);
3101 fprintf(stderr,"\n");
3106 Print a whole blocking queue attached to node (debugging only).
3111 print_bq (StgClosure *node)
3113 StgBlockingQueueElement *bqe;
3117 fprintf(stderr,"## BQ of closure %p (%s): ",
3118 node, info_type(node));
3120 /* should cover all closures that may have a blocking queue */
3121 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3122 get_itbl(node)->type == FETCH_ME_BQ ||
3123 get_itbl(node)->type == RBH);
3125 ASSERT(node!=(StgClosure*)NULL); // sanity check
3127 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3129 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
3130 !end; // iterate until bqe points to a CONSTR
3131 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
3132 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3133 ASSERT(bqe != (StgTSO*)NULL); // sanity check
3134 /* types of closures that may appear in a blocking queue */
3135 ASSERT(get_itbl(bqe)->type == TSO ||
3136 get_itbl(bqe)->type == BLOCKED_FETCH ||
3137 get_itbl(bqe)->type == CONSTR);
3138 /* only BQs of an RBH end with an RBH_Save closure */
3139 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3141 switch (get_itbl(bqe)->type) {
3143 fprintf(stderr," TSO %d (%x),",
3144 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
3147 fprintf(stderr," BF (node=%p, ga=((%x, %d, %x)),",
3148 ((StgBlockedFetch *)bqe)->node,
3149 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
3150 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
3151 ((StgBlockedFetch *)bqe)->ga.weight);
3154 fprintf(stderr," %s (IP %p),",
3155 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3156 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3157 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3158 "RBH_Save_?"), get_itbl(bqe));
3161 barf("Unexpected closure type %s in blocking queue of %p (%s)",
3162 info_type(bqe), node, info_type(node));
3166 fputc('\n', stderr);
3168 # elif defined(GRAN)
3170 print_bq (StgClosure *node)
3172 StgBlockingQueueElement *bqe;
3173 PEs node_loc, tso_loc;
3176 /* should cover all closures that may have a blocking queue */
3177 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3178 get_itbl(node)->type == FETCH_ME_BQ ||
3179 get_itbl(node)->type == RBH);
3181 ASSERT(node!=(StgClosure*)NULL); // sanity check
3182 node_loc = where_is(node);
3184 fprintf(stderr,"## BQ of closure %p (%s) on [PE %d]: ",
3185 node, info_type(node), node_loc);
3188 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3190 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
3191 !end; // iterate until bqe points to a CONSTR
3192 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
3193 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3194 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3195 /* types of closures that may appear in a blocking queue */
3196 ASSERT(get_itbl(bqe)->type == TSO ||
3197 get_itbl(bqe)->type == CONSTR);
3198 /* only BQs of an RBH end with an RBH_Save closure */
3199 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3201 tso_loc = where_is((StgClosure *)bqe);
3202 switch (get_itbl(bqe)->type) {
3204 fprintf(stderr," TSO %d (%p) on [PE %d],",
3205 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
3208 fprintf(stderr," %s (IP %p),",
3209 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3210 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3211 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3212 "RBH_Save_?"), get_itbl(bqe));
3215 barf("Unexpected closure type %s in blocking queue of %p (%s)",
3216 info_type((StgClosure *)bqe), node, info_type(node));
3220 fputc('\n', stderr);
3224 Nice and easy: only TSOs on the blocking queue
3227 print_bq (StgClosure *node)
3231 ASSERT(node!=(StgClosure*)NULL); // sanity check
3232 for (tso = ((StgBlockingQueue*)node)->blocking_queue;
3233 tso != END_TSO_QUEUE;
3235 ASSERT(tso!=NULL && tso!=END_TSO_QUEUE); // sanity check
3236 ASSERT(get_itbl(tso)->type == TSO); // guess what, sanity check
3237 fprintf(stderr," TSO %d (%p),", tso->id, tso);
3239 fputc('\n', stderr);
3250 for (i=0, tso=run_queue_hd;
3251 tso != END_TSO_QUEUE;
3260 sched_belch(char *s, ...)
3265 fprintf(stderr, "scheduler (task %ld): ", pthread_self());
3267 fprintf(stderr, "scheduler: ");
3269 vfprintf(stderr, s, ap);
3270 fprintf(stderr, "\n");
3276 //@node Index, , Debugging Routines, Main scheduling code
3280 //* MainRegTable:: @cindex\s-+MainRegTable
3281 //* StgMainThread:: @cindex\s-+StgMainThread
3282 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
3283 //* blocked_queue_hd:: @cindex\s-+blocked_queue_hd
3284 //* blocked_queue_tl:: @cindex\s-+blocked_queue_tl
3285 //* context_switch:: @cindex\s-+context_switch
3286 //* createThread:: @cindex\s-+createThread
3287 //* free_capabilities:: @cindex\s-+free_capabilities
3288 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
3289 //* initScheduler:: @cindex\s-+initScheduler
3290 //* interrupted:: @cindex\s-+interrupted
3291 //* n_free_capabilities:: @cindex\s-+n_free_capabilities
3292 //* next_thread_id:: @cindex\s-+next_thread_id
3293 //* print_bq:: @cindex\s-+print_bq
3294 //* run_queue_hd:: @cindex\s-+run_queue_hd
3295 //* run_queue_tl:: @cindex\s-+run_queue_tl
3296 //* sched_mutex:: @cindex\s-+sched_mutex
3297 //* schedule:: @cindex\s-+schedule
3298 //* take_off_run_queue:: @cindex\s-+take_off_run_queue
3299 //* task_ids:: @cindex\s-+task_ids
3300 //* term_mutex:: @cindex\s-+term_mutex
3301 //* thread_ready_cond:: @cindex\s-+thread_ready_cond