1 /* ---------------------------------------------------------------------------
2 * $Id: Schedule.c,v 1.163 2003/03/19 18:41:19 sof Exp $
4 * (c) The GHC Team, 1998-2000
8 * Different GHC ways use this scheduler quite differently (see comments below)
9 * Here is the global picture:
11 * WAY Name CPP flag What's it for
12 * --------------------------------------
13 * mp GUM PAR Parallel execution on a distributed memory machine
14 * s SMP SMP Parallel execution on a shared memory machine
15 * mg GranSim GRAN Simulation of parallel execution
16 * md GUM/GdH DIST Distributed execution (based on GUM)
18 * --------------------------------------------------------------------------*/
20 //@node Main scheduling code, , ,
21 //@section Main scheduling code
24 * Version with scheduler monitor support for SMPs (WAY=s):
26 This design provides a high-level API to create and schedule threads etc.
27 as documented in the SMP design document.
29 It uses a monitor design controlled by a single mutex to exercise control
30 over accesses to shared data structures, and builds on the Posix threads
33 The majority of state is shared. In order to keep essential per-task state,
34 there is a Capability structure, which contains all the information
35 needed to run a thread: its STG registers, a pointer to its TSO, a
36 nursery etc. During STG execution, a pointer to the capability is
37 kept in a register (BaseReg).
39 In a non-SMP build, there is one global capability, namely MainRegTable.
43 * Version with support for distributed memory parallelism aka GUM (WAY=mp):
45 The main scheduling loop in GUM iterates until a finish message is received.
46 In that case a global flag @receivedFinish@ is set and this instance of
47 the RTS shuts down. See ghc/rts/parallel/HLComms.c:processMessages()
48 for the handling of incoming messages, such as PP_FINISH.
49 Note that in the parallel case we have a system manager that coordinates
50 different PEs, each of which are running one instance of the RTS.
51 See ghc/rts/parallel/SysMan.c for the main routine of the parallel program.
52 From this routine processes executing ghc/rts/Main.c are spawned. -- HWL
54 * Version with support for simulating parallel execution aka GranSim (WAY=mg):
56 The main scheduling code in GranSim is quite different from that in std
57 (concurrent) Haskell: while concurrent Haskell just iterates over the
58 threads in the runnable queue, GranSim is event driven, i.e. it iterates
59 over the events in the global event queue. -- HWL
64 //* Variables and Data structures::
65 //* Main scheduling loop::
66 //* Suspend and Resume::
68 //* Garbage Collextion Routines::
69 //* Blocking Queue Routines::
70 //* Exception Handling Routines::
71 //* Debugging Routines::
75 //@node Includes, Variables and Data structures, Main scheduling code, Main scheduling code
76 //@subsection Includes
78 #include "PosixSource.h"
85 #include "StgStartup.h"
87 #define COMPILING_SCHEDULER
89 #include "StgMiscClosures.h"
91 #include "Interpreter.h"
92 #include "Exception.h"
99 #include "ThreadLabels.h"
101 #include "Proftimer.h"
102 #include "ProfHeap.h"
104 #if defined(GRAN) || defined(PAR)
105 # include "GranSimRts.h"
106 # include "GranSim.h"
107 # include "ParallelRts.h"
108 # include "Parallel.h"
109 # include "ParallelDebug.h"
110 # include "FetchMe.h"
114 #include "Capability.h"
115 #include "OSThreads.h"
118 #ifdef HAVE_SYS_TYPES_H
119 #include <sys/types.h>
129 //@node Variables and Data structures, Prototypes, Includes, Main scheduling code
130 //@subsection Variables and Data structures
132 /* Main thread queue.
133 * Locks required: sched_mutex.
135 StgMainThread *main_threads = NULL;
138 // Pointer to the thread that executes main
139 // When this thread is finished, the program terminates
140 // by calling shutdownHaskellAndExit.
141 // It would be better to add a call to shutdownHaskellAndExit
142 // to the Main.main wrapper and to remove this hack.
143 StgMainThread *main_main_thread = NULL;
147 * Locks required: sched_mutex.
151 StgTSO* ActiveTSO = NULL; /* for assigning system costs; GranSim-Light only */
152 /* rtsTime TimeOfNextEvent, EndOfTimeSlice; now in GranSim.c */
155 In GranSim we have a runnable and a blocked queue for each processor.
156 In order to minimise code changes new arrays run_queue_hds/tls
157 are created. run_queue_hd is then a short cut (macro) for
158 run_queue_hds[CurrentProc] (see GranSim.h).
161 StgTSO *run_queue_hds[MAX_PROC], *run_queue_tls[MAX_PROC];
162 StgTSO *blocked_queue_hds[MAX_PROC], *blocked_queue_tls[MAX_PROC];
163 StgTSO *ccalling_threadss[MAX_PROC];
164 /* We use the same global list of threads (all_threads) in GranSim as in
165 the std RTS (i.e. we are cheating). However, we don't use this list in
166 the GranSim specific code at the moment (so we are only potentially
171 StgTSO *run_queue_hd = NULL;
172 StgTSO *run_queue_tl = NULL;
173 StgTSO *blocked_queue_hd = NULL;
174 StgTSO *blocked_queue_tl = NULL;
175 StgTSO *sleeping_queue = NULL; /* perhaps replace with a hash table? */
179 /* Linked list of all threads.
180 * Used for detecting garbage collected threads.
182 StgTSO *all_threads = NULL;
184 /* When a thread performs a safe C call (_ccall_GC, using old
185 * terminology), it gets put on the suspended_ccalling_threads
186 * list. Used by the garbage collector.
188 static StgTSO *suspended_ccalling_threads;
190 static StgTSO *threadStackOverflow(StgTSO *tso);
192 /* KH: The following two flags are shared memory locations. There is no need
193 to lock them, since they are only unset at the end of a scheduler
197 /* flag set by signal handler to precipitate a context switch */
198 //@cindex context_switch
199 nat context_switch = 0;
201 /* if this flag is set as well, give up execution */
202 //@cindex interrupted
203 rtsBool interrupted = rtsFalse;
205 /* Next thread ID to allocate.
206 * Locks required: thread_id_mutex
208 //@cindex next_thread_id
209 static StgThreadID next_thread_id = 1;
212 * Pointers to the state of the current thread.
213 * Rule of thumb: if CurrentTSO != NULL, then we're running a Haskell
214 * thread. If CurrentTSO == NULL, then we're at the scheduler level.
217 /* The smallest stack size that makes any sense is:
218 * RESERVED_STACK_WORDS (so we can get back from the stack overflow)
219 * + sizeofW(StgStopFrame) (the stg_stop_thread_info frame)
220 * + 1 (the realworld token for an IO thread)
221 * + 1 (the closure to enter)
223 * A thread with this stack will bomb immediately with a stack
224 * overflow, which will increase its stack size.
227 #define MIN_STACK_WORDS (RESERVED_STACK_WORDS + sizeofW(StgStopFrame) + 2)
234 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
235 * exists - earlier gccs apparently didn't.
240 static rtsBool ready_to_gc;
243 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
244 * in an MT setting, needed to signal that a worker thread shouldn't hang around
245 * in the scheduler when it is out of work.
247 static rtsBool shutting_down_scheduler = rtsFalse;
249 void addToBlockedQueue ( StgTSO *tso );
251 static void schedule ( void );
252 void interruptStgRts ( void );
254 static void detectBlackHoles ( void );
257 static void sched_belch(char *s, ...);
260 #if defined(RTS_SUPPORTS_THREADS)
261 /* ToDo: carefully document the invariants that go together
262 * with these synchronisation objects.
264 Mutex sched_mutex = INIT_MUTEX_VAR;
265 Mutex term_mutex = INIT_MUTEX_VAR;
268 * A heavyweight solution to the problem of protecting
269 * the thread_id from concurrent update.
271 Mutex thread_id_mutex = INIT_MUTEX_VAR;
275 static Condition gc_pending_cond = INIT_COND_VAR;
279 #endif /* RTS_SUPPORTS_THREADS */
283 rtsTime TimeOfLastYield;
284 rtsBool emitSchedule = rtsTrue;
288 static char *whatNext_strs[] = {
298 StgTSO * createSparkThread(rtsSpark spark);
299 StgTSO * activateSpark (rtsSpark spark);
303 * The thread state for the main thread.
304 // ToDo: check whether not needed any more
308 #if defined(PAR) || defined(RTS_SUPPORTS_THREADS)
309 static void taskStart(void);
317 #if defined(RTS_SUPPORTS_THREADS)
319 startSchedulerTask(void)
321 startTask(taskStart);
325 //@node Main scheduling loop, Suspend and Resume, Prototypes, Main scheduling code
326 //@subsection Main scheduling loop
328 /* ---------------------------------------------------------------------------
329 Main scheduling loop.
331 We use round-robin scheduling, each thread returning to the
332 scheduler loop when one of these conditions is detected:
335 * timer expires (thread yields)
340 Locking notes: we acquire the scheduler lock once at the beginning
341 of the scheduler loop, and release it when
343 * running a thread, or
344 * waiting for work, or
345 * waiting for a GC to complete.
348 In a GranSim setup this loop iterates over the global event queue.
349 This revolves around the global event queue, which determines what
350 to do next. Therefore, it's more complicated than either the
351 concurrent or the parallel (GUM) setup.
354 GUM iterates over incoming messages.
355 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
356 and sends out a fish whenever it has nothing to do; in-between
357 doing the actual reductions (shared code below) it processes the
358 incoming messages and deals with delayed operations
359 (see PendingFetches).
360 This is not the ugliest code you could imagine, but it's bloody close.
362 ------------------------------------------------------------------------ */
369 StgThreadReturnCode ret;
377 rtsBool receivedFinish = rtsFalse;
379 nat tp_size, sp_size; // stats only
382 rtsBool was_interrupted = rtsFalse;
383 StgTSOWhatNext prev_what_next;
385 ACQUIRE_LOCK(&sched_mutex);
387 #if defined(RTS_SUPPORTS_THREADS)
388 waitForWorkCapability(&sched_mutex, &cap, rtsFalse);
389 IF_DEBUG(scheduler, sched_belch("worker thread (osthread %p): entering RTS", osThreadId()));
391 /* simply initialise it in the non-threaded case */
392 grabCapability(&cap);
396 /* set up first event to get things going */
397 /* ToDo: assign costs for system setup and init MainTSO ! */
398 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
400 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
403 fprintf(stderr, "GRAN: Init CurrentTSO (in schedule) = %p\n", CurrentTSO);
404 G_TSO(CurrentTSO, 5));
406 if (RtsFlags.GranFlags.Light) {
407 /* Save current time; GranSim Light only */
408 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
411 event = get_next_event();
413 while (event!=(rtsEvent*)NULL) {
414 /* Choose the processor with the next event */
415 CurrentProc = event->proc;
416 CurrentTSO = event->tso;
420 while (!receivedFinish) { /* set by processMessages */
421 /* when receiving PP_FINISH message */
428 IF_DEBUG(scheduler, printAllThreads());
430 #if defined(RTS_SUPPORTS_THREADS)
431 /* Check to see whether there are any worker threads
432 waiting to deposit external call results. If so,
433 yield our capability */
434 yieldToReturningWorker(&sched_mutex, &cap);
437 /* If we're interrupted (the user pressed ^C, or some other
438 * termination condition occurred), kill all the currently running
442 IF_DEBUG(scheduler, sched_belch("interrupted"));
443 interrupted = rtsFalse;
444 was_interrupted = rtsTrue;
445 #if defined(RTS_SUPPORTS_THREADS)
446 // In the threaded RTS, deadlock detection doesn't work,
447 // so just exit right away.
448 prog_belch("interrupted");
449 releaseCapability(cap);
450 startTask(taskStart); // thread-safe-call to shutdownHaskellAndExit
451 RELEASE_LOCK(&sched_mutex);
452 shutdownHaskellAndExit(EXIT_SUCCESS);
458 /* Go through the list of main threads and wake up any
459 * clients whose computations have finished. ToDo: this
460 * should be done more efficiently without a linear scan
461 * of the main threads list, somehow...
463 #if defined(RTS_SUPPORTS_THREADS)
465 StgMainThread *m, **prev;
466 prev = &main_threads;
467 for (m = main_threads; m != NULL; prev = &m->link, m = m->link) {
468 switch (m->tso->what_next) {
471 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
472 *(m->ret) = (StgClosure *)m->tso->sp[1];
476 broadcastCondition(&m->wakeup);
478 removeThreadLabel((StgWord)m->tso);
480 if(m == main_main_thread)
482 releaseCapability(cap);
483 startTask(taskStart); // thread-safe-call to shutdownHaskellAndExit
484 RELEASE_LOCK(&sched_mutex);
485 shutdownHaskellAndExit(EXIT_SUCCESS);
489 if (m->ret) *(m->ret) = NULL;
491 if (was_interrupted) {
492 m->stat = Interrupted;
496 broadcastCondition(&m->wakeup);
498 removeThreadLabel((StgWord)m->tso);
500 if(m == main_main_thread)
502 releaseCapability(cap);
503 startTask(taskStart); // thread-safe-call to shutdownHaskellAndExit
504 RELEASE_LOCK(&sched_mutex);
505 shutdownHaskellAndExit(EXIT_SUCCESS);
514 #else /* not threaded */
517 /* in GUM do this only on the Main PE */
520 /* If our main thread has finished or been killed, return.
523 StgMainThread *m = main_threads;
524 if (m->tso->what_next == ThreadComplete
525 || m->tso->what_next == ThreadKilled) {
527 removeThreadLabel((StgWord)m->tso);
529 main_threads = main_threads->link;
530 if (m->tso->what_next == ThreadComplete) {
531 // We finished successfully, fill in the return value
532 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
533 if (m->ret) { *(m->ret) = (StgClosure *)m->tso->sp[1]; };
537 if (m->ret) { *(m->ret) = NULL; };
538 if (was_interrupted) {
539 m->stat = Interrupted;
549 /* Top up the run queue from our spark pool. We try to make the
550 * number of threads in the run queue equal to the number of
553 * Disable spark support in SMP for now, non-essential & requires
554 * a little bit of work to make it compile cleanly. -- sof 1/02.
556 #if 0 /* defined(SMP) */
558 nat n = getFreeCapabilities();
559 StgTSO *tso = run_queue_hd;
561 /* Count the run queue */
562 while (n > 0 && tso != END_TSO_QUEUE) {
569 spark = findSpark(rtsFalse);
571 break; /* no more sparks in the pool */
573 /* I'd prefer this to be done in activateSpark -- HWL */
574 /* tricky - it needs to hold the scheduler lock and
575 * not try to re-acquire it -- SDM */
576 createSparkThread(spark);
578 sched_belch("==^^ turning spark of closure %p into a thread",
579 (StgClosure *)spark));
582 /* We need to wake up the other tasks if we just created some
585 if (getFreeCapabilities() - n > 1) {
586 signalCondition( &thread_ready_cond );
591 /* check for signals each time around the scheduler */
592 #ifndef mingw32_TARGET_OS
593 if (signals_pending()) {
594 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
595 startSignalHandlers();
596 ACQUIRE_LOCK(&sched_mutex);
600 /* Check whether any waiting threads need to be woken up. If the
601 * run queue is empty, and there are no other tasks running, we
602 * can wait indefinitely for something to happen.
604 if ( !EMPTY_QUEUE(blocked_queue_hd) || !EMPTY_QUEUE(sleeping_queue)
605 #if defined(RTS_SUPPORTS_THREADS) && !defined(SMP)
610 awaitEvent( EMPTY_RUN_QUEUE()
612 && allFreeCapabilities()
616 /* we can be interrupted while waiting for I/O... */
617 if (interrupted) continue;
620 * Detect deadlock: when we have no threads to run, there are no
621 * threads waiting on I/O or sleeping, and all the other tasks are
622 * waiting for work, we must have a deadlock of some description.
624 * We first try to find threads blocked on themselves (ie. black
625 * holes), and generate NonTermination exceptions where necessary.
627 * If no threads are black holed, we have a deadlock situation, so
628 * inform all the main threads.
630 #if !defined(PAR) && !defined(RTS_SUPPORTS_THREADS)
631 if ( EMPTY_THREAD_QUEUES()
632 #if defined(RTS_SUPPORTS_THREADS)
633 && EMPTY_QUEUE(suspended_ccalling_threads)
636 && allFreeCapabilities()
640 IF_DEBUG(scheduler, sched_belch("deadlocked, forcing major GC..."));
641 #if defined(THREADED_RTS)
642 /* and SMP mode ..? */
643 releaseCapability(cap);
645 // Garbage collection can release some new threads due to
646 // either (a) finalizers or (b) threads resurrected because
647 // they are about to be send BlockedOnDeadMVar. Any threads
648 // thus released will be immediately runnable.
649 GarbageCollect(GetRoots,rtsTrue);
651 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
654 sched_belch("still deadlocked, checking for black holes..."));
657 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
659 #ifndef mingw32_TARGET_OS
660 /* If we have user-installed signal handlers, then wait
661 * for signals to arrive rather then bombing out with a
664 #if defined(RTS_SUPPORTS_THREADS)
665 if ( 0 ) { /* hmm..what to do? Simply stop waiting for
666 a signal with no runnable threads (or I/O
667 suspended ones) leads nowhere quick.
668 For now, simply shut down when we reach this
671 ToDo: define precisely under what conditions
672 the Scheduler should shut down in an MT setting.
675 if ( anyUserHandlers() ) {
678 sched_belch("still deadlocked, waiting for signals..."));
682 // we might be interrupted...
683 if (interrupted) { continue; }
685 if (signals_pending()) {
686 RELEASE_LOCK(&sched_mutex);
687 startSignalHandlers();
688 ACQUIRE_LOCK(&sched_mutex);
690 ASSERT(!EMPTY_RUN_QUEUE());
695 /* Probably a real deadlock. Send the current main thread the
696 * Deadlock exception (or in the SMP build, send *all* main
697 * threads the deadlock exception, since none of them can make
702 #if defined(RTS_SUPPORTS_THREADS)
703 for (m = main_threads; m != NULL; m = m->link) {
704 switch (m->tso->why_blocked) {
705 case BlockedOnBlackHole:
706 raiseAsync(m->tso, (StgClosure *)NonTermination_closure);
708 case BlockedOnException:
710 raiseAsync(m->tso, (StgClosure *)Deadlock_closure);
713 barf("deadlock: main thread blocked in a strange way");
718 switch (m->tso->why_blocked) {
719 case BlockedOnBlackHole:
720 raiseAsync(m->tso, (StgClosure *)NonTermination_closure);
722 case BlockedOnException:
724 raiseAsync(m->tso, (StgClosure *)Deadlock_closure);
727 barf("deadlock: main thread blocked in a strange way");
732 #if defined(RTS_SUPPORTS_THREADS)
733 /* ToDo: revisit conditions (and mechanism) for shutting
734 down a multi-threaded world */
735 IF_DEBUG(scheduler, sched_belch("all done, i think...shutting down."));
736 RELEASE_LOCK(&sched_mutex);
743 #elif defined(RTS_SUPPORTS_THREADS)
744 /* ToDo: add deadlock detection in threaded RTS */
746 /* ToDo: add deadlock detection in GUM (similar to SMP) -- HWL */
750 /* If there's a GC pending, don't do anything until it has
754 IF_DEBUG(scheduler,sched_belch("waiting for GC"));
755 waitCondition( &gc_pending_cond, &sched_mutex );
759 #if defined(RTS_SUPPORTS_THREADS)
761 /* block until we've got a thread on the run queue and a free
765 if ( EMPTY_RUN_QUEUE() ) {
766 /* Give up our capability */
767 releaseCapability(cap);
769 /* If we're in the process of shutting down (& running the
770 * a batch of finalisers), don't wait around.
772 if ( shutting_down_scheduler ) {
773 RELEASE_LOCK(&sched_mutex);
776 IF_DEBUG(scheduler, sched_belch("thread %d: waiting for work", osThreadId()));
777 waitForWorkCapability(&sched_mutex, &cap, rtsTrue);
778 IF_DEBUG(scheduler, sched_belch("thread %d: work now available", osThreadId()));
781 if ( EMPTY_RUN_QUEUE() ) {
782 continue; // nothing to do
788 if (RtsFlags.GranFlags.Light)
789 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
791 /* adjust time based on time-stamp */
792 if (event->time > CurrentTime[CurrentProc] &&
793 event->evttype != ContinueThread)
794 CurrentTime[CurrentProc] = event->time;
796 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
797 if (!RtsFlags.GranFlags.Light)
800 IF_DEBUG(gran, fprintf(stderr, "GRAN: switch by event-type\n"));
802 /* main event dispatcher in GranSim */
803 switch (event->evttype) {
804 /* Should just be continuing execution */
806 IF_DEBUG(gran, fprintf(stderr, "GRAN: doing ContinueThread\n"));
807 /* ToDo: check assertion
808 ASSERT(run_queue_hd != (StgTSO*)NULL &&
809 run_queue_hd != END_TSO_QUEUE);
811 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
812 if (!RtsFlags.GranFlags.DoAsyncFetch &&
813 procStatus[CurrentProc]==Fetching) {
814 belch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]",
815 CurrentTSO->id, CurrentTSO, CurrentProc);
818 /* Ignore ContinueThreads for completed threads */
819 if (CurrentTSO->what_next == ThreadComplete) {
820 belch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)",
821 CurrentTSO->id, CurrentTSO, CurrentProc);
824 /* Ignore ContinueThreads for threads that are being migrated */
825 if (PROCS(CurrentTSO)==Nowhere) {
826 belch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)",
827 CurrentTSO->id, CurrentTSO, CurrentProc);
830 /* The thread should be at the beginning of the run queue */
831 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
832 belch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread",
833 CurrentTSO->id, CurrentTSO, CurrentProc);
834 break; // run the thread anyway
837 new_event(proc, proc, CurrentTime[proc],
839 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
841 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
842 break; // now actually run the thread; DaH Qu'vam yImuHbej
845 do_the_fetchnode(event);
846 goto next_thread; /* handle next event in event queue */
849 do_the_globalblock(event);
850 goto next_thread; /* handle next event in event queue */
853 do_the_fetchreply(event);
854 goto next_thread; /* handle next event in event queue */
856 case UnblockThread: /* Move from the blocked queue to the tail of */
857 do_the_unblock(event);
858 goto next_thread; /* handle next event in event queue */
860 case ResumeThread: /* Move from the blocked queue to the tail of */
861 /* the runnable queue ( i.e. Qu' SImqa'lu') */
862 event->tso->gran.blocktime +=
863 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
864 do_the_startthread(event);
865 goto next_thread; /* handle next event in event queue */
868 do_the_startthread(event);
869 goto next_thread; /* handle next event in event queue */
872 do_the_movethread(event);
873 goto next_thread; /* handle next event in event queue */
876 do_the_movespark(event);
877 goto next_thread; /* handle next event in event queue */
880 do_the_findwork(event);
881 goto next_thread; /* handle next event in event queue */
884 barf("Illegal event type %u\n", event->evttype);
887 /* This point was scheduler_loop in the old RTS */
889 IF_DEBUG(gran, belch("GRAN: after main switch"));
891 TimeOfLastEvent = CurrentTime[CurrentProc];
892 TimeOfNextEvent = get_time_of_next_event();
893 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
894 // CurrentTSO = ThreadQueueHd;
896 IF_DEBUG(gran, belch("GRAN: time of next event is: %ld",
899 if (RtsFlags.GranFlags.Light)
900 GranSimLight_leave_system(event, &ActiveTSO);
902 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
905 belch("GRAN: end of time-slice is %#lx", EndOfTimeSlice));
907 /* in a GranSim setup the TSO stays on the run queue */
909 /* Take a thread from the run queue. */
910 t = POP_RUN_QUEUE(); // take_off_run_queue(t);
913 fprintf(stderr, "GRAN: About to run current thread, which is\n");
916 context_switch = 0; // turned on via GranYield, checking events and time slice
919 DumpGranEvent(GR_SCHEDULE, t));
921 procStatus[CurrentProc] = Busy;
924 if (PendingFetches != END_BF_QUEUE) {
928 /* ToDo: phps merge with spark activation above */
929 /* check whether we have local work and send requests if we have none */
930 if (EMPTY_RUN_QUEUE()) { /* no runnable threads */
931 /* :-[ no local threads => look out for local sparks */
932 /* the spark pool for the current PE */
933 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
934 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
935 pool->hd < pool->tl) {
937 * ToDo: add GC code check that we really have enough heap afterwards!!
939 * If we're here (no runnable threads) and we have pending
940 * sparks, we must have a space problem. Get enough space
941 * to turn one of those pending sparks into a
945 spark = findSpark(rtsFalse); /* get a spark */
946 if (spark != (rtsSpark) NULL) {
947 tso = activateSpark(spark); /* turn the spark into a thread */
948 IF_PAR_DEBUG(schedule,
949 belch("==== schedule: Created TSO %d (%p); %d threads active",
950 tso->id, tso, advisory_thread_count));
952 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
953 belch("==^^ failed to activate spark");
955 } /* otherwise fall through & pick-up new tso */
957 IF_PAR_DEBUG(verbose,
958 belch("==^^ no local sparks (spark pool contains only NFs: %d)",
959 spark_queue_len(pool)));
964 /* If we still have no work we need to send a FISH to get a spark
967 if (EMPTY_RUN_QUEUE()) {
968 /* =8-[ no local sparks => look for work on other PEs */
970 * We really have absolutely no work. Send out a fish
971 * (there may be some out there already), and wait for
972 * something to arrive. We clearly can't run any threads
973 * until a SCHEDULE or RESUME arrives, and so that's what
974 * we're hoping to see. (Of course, we still have to
975 * respond to other types of messages.)
977 TIME now = msTime() /*CURRENT_TIME*/;
978 IF_PAR_DEBUG(verbose,
979 belch("-- now=%ld", now));
980 IF_PAR_DEBUG(verbose,
981 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
982 (last_fish_arrived_at!=0 &&
983 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay > now)) {
984 belch("--$$ delaying FISH until %ld (last fish %ld, delay %ld, now %ld)",
985 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay,
986 last_fish_arrived_at,
987 RtsFlags.ParFlags.fishDelay, now);
990 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
991 (last_fish_arrived_at==0 ||
992 (last_fish_arrived_at+RtsFlags.ParFlags.fishDelay <= now))) {
993 /* outstandingFishes is set in sendFish, processFish;
994 avoid flooding system with fishes via delay */
996 sendFish(pe, mytid, NEW_FISH_AGE, NEW_FISH_HISTORY,
999 // Global statistics: count no. of fishes
1000 if (RtsFlags.ParFlags.ParStats.Global &&
1001 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1002 globalParStats.tot_fish_mess++;
1006 receivedFinish = processMessages();
1009 } else if (PacketsWaiting()) { /* Look for incoming messages */
1010 receivedFinish = processMessages();
1013 /* Now we are sure that we have some work available */
1014 ASSERT(run_queue_hd != END_TSO_QUEUE);
1016 /* Take a thread from the run queue, if we have work */
1017 t = POP_RUN_QUEUE(); // take_off_run_queue(END_TSO_QUEUE);
1018 IF_DEBUG(sanity,checkTSO(t));
1020 /* ToDo: write something to the log-file
1021 if (RTSflags.ParFlags.granSimStats && !sameThread)
1022 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
1026 /* the spark pool for the current PE */
1027 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
1030 belch("--=^ %d threads, %d sparks on [%#x]",
1031 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1034 if (0 && RtsFlags.ParFlags.ParStats.Full &&
1035 t && LastTSO && t->id != LastTSO->id &&
1036 LastTSO->why_blocked == NotBlocked &&
1037 LastTSO->what_next != ThreadComplete) {
1038 // if previously scheduled TSO not blocked we have to record the context switch
1039 DumpVeryRawGranEvent(TimeOfLastYield, CURRENT_PROC, CURRENT_PROC,
1040 GR_DESCHEDULE, LastTSO, (StgClosure *)NULL, 0, 0);
1043 if (RtsFlags.ParFlags.ParStats.Full &&
1044 (emitSchedule /* forced emit */ ||
1045 (t && LastTSO && t->id != LastTSO->id))) {
1047 we are running a different TSO, so write a schedule event to log file
1048 NB: If we use fair scheduling we also have to write a deschedule
1049 event for LastTSO; with unfair scheduling we know that the
1050 previous tso has blocked whenever we switch to another tso, so
1051 we don't need it in GUM for now
1053 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1054 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
1055 emitSchedule = rtsFalse;
1059 #else /* !GRAN && !PAR */
1061 /* grab a thread from the run queue */
1062 ASSERT(run_queue_hd != END_TSO_QUEUE);
1063 t = POP_RUN_QUEUE();
1064 // Sanity check the thread we're about to run. This can be
1065 // expensive if there is lots of thread switching going on...
1066 IF_DEBUG(sanity,checkTSO(t));
1069 cap->r.rCurrentTSO = t;
1071 /* context switches are now initiated by the timer signal, unless
1072 * the user specified "context switch as often as possible", with
1075 if ((RtsFlags.ConcFlags.ctxtSwitchTicks == 0
1076 && (run_queue_hd != END_TSO_QUEUE
1077 || blocked_queue_hd != END_TSO_QUEUE
1078 || sleeping_queue != END_TSO_QUEUE)))
1085 RELEASE_LOCK(&sched_mutex);
1087 IF_DEBUG(scheduler, sched_belch("-->> running thread %ld %s ...",
1088 t->id, whatNext_strs[t->what_next]));
1091 startHeapProfTimer();
1094 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1095 /* Run the current thread
1097 prev_what_next = t->what_next;
1098 switch (prev_what_next) {
1100 case ThreadComplete:
1101 /* Thread already finished, return to scheduler. */
1102 ret = ThreadFinished;
1105 ret = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
1107 case ThreadInterpret:
1108 ret = interpretBCO(cap);
1111 barf("schedule: invalid what_next field");
1113 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1115 /* Costs for the scheduler are assigned to CCS_SYSTEM */
1117 stopHeapProfTimer();
1121 ACQUIRE_LOCK(&sched_mutex);
1123 #ifdef RTS_SUPPORTS_THREADS
1124 IF_DEBUG(scheduler,fprintf(stderr,"scheduler (task %ld): ", osThreadId()););
1125 #elif !defined(GRAN) && !defined(PAR)
1126 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: "););
1128 t = cap->r.rCurrentTSO;
1131 /* HACK 675: if the last thread didn't yield, make sure to print a
1132 SCHEDULE event to the log file when StgRunning the next thread, even
1133 if it is the same one as before */
1135 TimeOfLastYield = CURRENT_TIME;
1141 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
1142 globalGranStats.tot_heapover++;
1144 globalParStats.tot_heapover++;
1147 // did the task ask for a large block?
1148 if (cap->r.rHpAlloc > BLOCK_SIZE_W) {
1149 // if so, get one and push it on the front of the nursery.
1153 blocks = (nat)BLOCK_ROUND_UP(cap->r.rHpAlloc * sizeof(W_)) / BLOCK_SIZE;
1155 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: requesting a large block (size %d)",
1156 t->id, whatNext_strs[t->what_next], blocks));
1158 // don't do this if it would push us over the
1159 // alloc_blocks_lim limit; we'll GC first.
1160 if (alloc_blocks + blocks < alloc_blocks_lim) {
1162 alloc_blocks += blocks;
1163 bd = allocGroup( blocks );
1165 // link the new group into the list
1166 bd->link = cap->r.rCurrentNursery;
1167 bd->u.back = cap->r.rCurrentNursery->u.back;
1168 if (cap->r.rCurrentNursery->u.back != NULL) {
1169 cap->r.rCurrentNursery->u.back->link = bd;
1171 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
1172 g0s0->blocks == cap->r.rNursery);
1173 cap->r.rNursery = g0s0->blocks = bd;
1175 cap->r.rCurrentNursery->u.back = bd;
1177 // initialise it as a nursery block. We initialise the
1178 // step, gen_no, and flags field of *every* sub-block in
1179 // this large block, because this is easier than making
1180 // sure that we always find the block head of a large
1181 // block whenever we call Bdescr() (eg. evacuate() and
1182 // isAlive() in the GC would both have to do this, at
1186 for (x = bd; x < bd + blocks; x++) {
1193 // don't forget to update the block count in g0s0.
1194 g0s0->n_blocks += blocks;
1195 // This assert can be a killer if the app is doing lots
1196 // of large block allocations.
1197 ASSERT(countBlocks(g0s0->blocks) == g0s0->n_blocks);
1199 // now update the nursery to point to the new block
1200 cap->r.rCurrentNursery = bd;
1202 // we might be unlucky and have another thread get on the
1203 // run queue before us and steal the large block, but in that
1204 // case the thread will just end up requesting another large
1206 PUSH_ON_RUN_QUEUE(t);
1211 /* make all the running tasks block on a condition variable,
1212 * maybe set context_switch and wait till they all pile in,
1213 * then have them wait on a GC condition variable.
1215 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: HeapOverflow",
1216 t->id, whatNext_strs[t->what_next]));
1219 ASSERT(!is_on_queue(t,CurrentProc));
1221 /* Currently we emit a DESCHEDULE event before GC in GUM.
1222 ToDo: either add separate event to distinguish SYSTEM time from rest
1223 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1224 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1225 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1226 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1227 emitSchedule = rtsTrue;
1231 ready_to_gc = rtsTrue;
1232 context_switch = 1; /* stop other threads ASAP */
1233 PUSH_ON_RUN_QUEUE(t);
1234 /* actual GC is done at the end of the while loop */
1240 DumpGranEvent(GR_DESCHEDULE, t));
1241 globalGranStats.tot_stackover++;
1244 // DumpGranEvent(GR_DESCHEDULE, t);
1245 globalParStats.tot_stackover++;
1247 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped, StackOverflow",
1248 t->id, whatNext_strs[t->what_next]));
1249 /* just adjust the stack for this thread, then pop it back
1255 /* enlarge the stack */
1256 StgTSO *new_t = threadStackOverflow(t);
1258 /* This TSO has moved, so update any pointers to it from the
1259 * main thread stack. It better not be on any other queues...
1260 * (it shouldn't be).
1262 for (m = main_threads; m != NULL; m = m->link) {
1267 threadPaused(new_t);
1268 PUSH_ON_RUN_QUEUE(new_t);
1272 case ThreadYielding:
1275 DumpGranEvent(GR_DESCHEDULE, t));
1276 globalGranStats.tot_yields++;
1279 // DumpGranEvent(GR_DESCHEDULE, t);
1280 globalParStats.tot_yields++;
1282 /* put the thread back on the run queue. Then, if we're ready to
1283 * GC, check whether this is the last task to stop. If so, wake
1284 * up the GC thread. getThread will block during a GC until the
1288 if (t->what_next != prev_what_next) {
1289 belch("--<< thread %ld (%s) stopped to switch evaluators",
1290 t->id, whatNext_strs[t->what_next]);
1292 belch("--<< thread %ld (%s) stopped, yielding",
1293 t->id, whatNext_strs[t->what_next]);
1298 //belch("&& Doing sanity check on yielding TSO %ld.", t->id);
1300 ASSERT(t->link == END_TSO_QUEUE);
1302 // Shortcut if we're just switching evaluators: don't bother
1303 // doing stack squeezing (which can be expensive), just run the
1305 if (t->what_next != prev_what_next) {
1312 ASSERT(!is_on_queue(t,CurrentProc));
1315 //belch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1316 checkThreadQsSanity(rtsTrue));
1320 if (RtsFlags.ParFlags.doFairScheduling) {
1321 /* this does round-robin scheduling; good for concurrency */
1322 APPEND_TO_RUN_QUEUE(t);
1324 /* this does unfair scheduling; good for parallelism */
1325 PUSH_ON_RUN_QUEUE(t);
1328 // this does round-robin scheduling; good for concurrency
1329 APPEND_TO_RUN_QUEUE(t);
1333 /* add a ContinueThread event to actually process the thread */
1334 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1336 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1338 belch("GRAN: eventq and runnableq after adding yielded thread to queue again:");
1347 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1348 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)));
1349 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1351 // ??? needed; should emit block before
1353 DumpGranEvent(GR_DESCHEDULE, t));
1354 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1357 ASSERT(procStatus[CurrentProc]==Busy ||
1358 ((procStatus[CurrentProc]==Fetching) &&
1359 (t->block_info.closure!=(StgClosure*)NULL)));
1360 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1361 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1362 procStatus[CurrentProc]==Fetching))
1363 procStatus[CurrentProc] = Idle;
1367 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: ",
1368 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1371 if (t->block_info.closure!=(StgClosure*)NULL)
1372 print_bq(t->block_info.closure));
1374 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1377 /* whatever we schedule next, we must log that schedule */
1378 emitSchedule = rtsTrue;
1381 /* don't need to do anything. Either the thread is blocked on
1382 * I/O, in which case we'll have called addToBlockedQueue
1383 * previously, or it's blocked on an MVar or Blackhole, in which
1384 * case it'll be on the relevant queue already.
1387 fprintf(stderr, "--<< thread %d (%s) stopped: ",
1388 t->id, whatNext_strs[t->what_next]);
1389 printThreadBlockage(t);
1390 fprintf(stderr, "\n"));
1392 /* Only for dumping event to log file
1393 ToDo: do I need this in GranSim, too?
1400 case ThreadFinished:
1401 /* Need to check whether this was a main thread, and if so, signal
1402 * the task that started it with the return value. If we have no
1403 * more main threads, we probably need to stop all the tasks until
1406 /* We also end up here if the thread kills itself with an
1407 * uncaught exception, see Exception.hc.
1409 IF_DEBUG(scheduler,belch("--++ thread %d (%s) finished",
1410 t->id, whatNext_strs[t->what_next]));
1412 endThread(t, CurrentProc); // clean-up the thread
1414 /* For now all are advisory -- HWL */
1415 //if(t->priority==AdvisoryPriority) ??
1416 advisory_thread_count--;
1419 if(t->dist.priority==RevalPriority)
1423 if (RtsFlags.ParFlags.ParStats.Full &&
1424 !RtsFlags.ParFlags.ParStats.Suppressed)
1425 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1430 barf("schedule: invalid thread return code %d", (int)ret);
1434 // When we have +RTS -i0 and we're heap profiling, do a census at
1435 // every GC. This lets us get repeatable runs for debugging.
1436 if (performHeapProfile ||
1437 (RtsFlags.ProfFlags.profileInterval==0 &&
1438 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1439 GarbageCollect(GetRoots, rtsTrue);
1441 performHeapProfile = rtsFalse;
1442 ready_to_gc = rtsFalse; // we already GC'd
1448 && allFreeCapabilities()
1451 /* everybody back, start the GC.
1452 * Could do it in this thread, or signal a condition var
1453 * to do it in another thread. Either way, we need to
1454 * broadcast on gc_pending_cond afterward.
1456 #if defined(RTS_SUPPORTS_THREADS)
1457 IF_DEBUG(scheduler,sched_belch("doing GC"));
1459 GarbageCollect(GetRoots,rtsFalse);
1460 ready_to_gc = rtsFalse;
1462 broadcastCondition(&gc_pending_cond);
1465 /* add a ContinueThread event to continue execution of current thread */
1466 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1468 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1470 fprintf(stderr, "GRAN: eventq and runnableq after Garbage collection:\n");
1478 IF_GRAN_DEBUG(unused,
1479 print_eventq(EventHd));
1481 event = get_next_event();
1484 /* ToDo: wait for next message to arrive rather than busy wait */
1487 } /* end of while(1) */
1489 IF_PAR_DEBUG(verbose,
1490 belch("== Leaving schedule() after having received Finish"));
1493 /* ---------------------------------------------------------------------------
1494 * Singleton fork(). Do not copy any running threads.
1495 * ------------------------------------------------------------------------- */
1497 StgInt forkProcess(StgTSO* tso) {
1499 #ifndef mingw32_TARGET_OS
1505 IF_DEBUG(scheduler,sched_belch("forking!"));
1508 if (pid) { /* parent */
1510 /* just return the pid */
1512 } else { /* child */
1513 /* wipe all other threads */
1514 run_queue_hd = run_queue_tl = tso;
1515 tso->link = END_TSO_QUEUE;
1517 /* When clearing out the threads, we need to ensure
1518 that a 'main thread' is left behind; if there isn't,
1519 the Scheduler will shutdown next time it is entered.
1521 ==> we don't kill a thread that's on the main_threads
1522 list (nor the current thread.)
1524 [ Attempts at implementing the more ambitious scheme of
1525 killing the main_threads also, and then adding the
1526 current thread onto the main_threads list if it wasn't
1527 there already, failed -- waitThread() (for one) wasn't
1528 up to it. If it proves to be desirable to also kill
1529 the main threads, then this scheme will have to be
1530 revisited (and fully debugged!)
1535 /* DO NOT TOUCH THE QUEUES directly because most of the code around
1536 us is picky about finding the thread still in its queue when
1537 handling the deleteThread() */
1539 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1542 /* Don't kill the current thread.. */
1543 if (t->id == tso->id) continue;
1545 /* ..or a main thread */
1546 for (m = main_threads; m != NULL; m = m->link) {
1547 if (m->tso->id == t->id) {
1559 barf("forkProcess#: primop not implemented for mingw32, sorry! (%u)\n", tso->id);
1560 /* pointlessly printing out the TSOs 'id' to avoid CC unused warning. */
1562 #endif /* mingw32 */
1565 /* ---------------------------------------------------------------------------
1566 * deleteAllThreads(): kill all the live threads.
1568 * This is used when we catch a user interrupt (^C), before performing
1569 * any necessary cleanups and running finalizers.
1571 * Locks: sched_mutex held.
1572 * ------------------------------------------------------------------------- */
1574 void deleteAllThreads ( void )
1577 IF_DEBUG(scheduler,sched_belch("deleting all threads"));
1578 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1579 next = t->global_link;
1582 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1583 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
1584 sleeping_queue = END_TSO_QUEUE;
1587 /* startThread and insertThread are now in GranSim.c -- HWL */
1590 //@node Suspend and Resume, Run queue code, Main scheduling loop, Main scheduling code
1591 //@subsection Suspend and Resume
1593 /* ---------------------------------------------------------------------------
1594 * Suspending & resuming Haskell threads.
1596 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1597 * its capability before calling the C function. This allows another
1598 * task to pick up the capability and carry on running Haskell
1599 * threads. It also means that if the C call blocks, it won't lock
1602 * The Haskell thread making the C call is put to sleep for the
1603 * duration of the call, on the susepended_ccalling_threads queue. We
1604 * give out a token to the task, which it can use to resume the thread
1605 * on return from the C function.
1606 * ------------------------------------------------------------------------- */
1609 suspendThread( StgRegTable *reg,
1611 #if !defined(RTS_SUPPORTS_THREADS) && !defined(DEBUG)
1619 /* assume that *reg is a pointer to the StgRegTable part
1622 cap = (Capability *)((void *)reg - sizeof(StgFunTable));
1624 ACQUIRE_LOCK(&sched_mutex);
1627 sched_belch("thread %d did a _ccall_gc (is_concurrent: %d)", cap->r.rCurrentTSO->id,concCall));
1629 // XXX this might not be necessary --SDM
1630 cap->r.rCurrentTSO->what_next = ThreadRunGHC;
1632 threadPaused(cap->r.rCurrentTSO);
1633 cap->r.rCurrentTSO->link = suspended_ccalling_threads;
1634 suspended_ccalling_threads = cap->r.rCurrentTSO;
1636 #if defined(RTS_SUPPORTS_THREADS)
1637 if(cap->r.rCurrentTSO->blocked_exceptions == NULL)
1639 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall;
1640 cap->r.rCurrentTSO->blocked_exceptions = END_TSO_QUEUE;
1644 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall_NoUnblockExc;
1648 /* Use the thread ID as the token; it should be unique */
1649 tok = cap->r.rCurrentTSO->id;
1651 /* Hand back capability */
1652 releaseCapability(cap);
1654 #if defined(RTS_SUPPORTS_THREADS)
1655 /* Preparing to leave the RTS, so ensure there's a native thread/task
1656 waiting to take over.
1658 IF_DEBUG(scheduler, sched_belch("worker thread (%d, osthread %p): leaving RTS", tok, osThreadId()));
1659 //if (concCall) { // implementing "safe" as opposed to "threadsafe" is more difficult
1660 startTask(taskStart);
1664 /* Other threads _might_ be available for execution; signal this */
1666 RELEASE_LOCK(&sched_mutex);
1671 resumeThread( StgInt tok,
1673 #if !defined(RTS_SUPPORTS_THREADS)
1678 StgTSO *tso, **prev;
1681 #if defined(RTS_SUPPORTS_THREADS)
1682 /* Wait for permission to re-enter the RTS with the result. */
1683 ACQUIRE_LOCK(&sched_mutex);
1684 grabReturnCapability(&sched_mutex, &cap);
1686 IF_DEBUG(scheduler, sched_belch("worker thread (%d, osthread %p): re-entering RTS", tok, osThreadId()));
1688 grabCapability(&cap);
1691 /* Remove the thread off of the suspended list */
1692 prev = &suspended_ccalling_threads;
1693 for (tso = suspended_ccalling_threads;
1694 tso != END_TSO_QUEUE;
1695 prev = &tso->link, tso = tso->link) {
1696 if (tso->id == (StgThreadID)tok) {
1701 if (tso == END_TSO_QUEUE) {
1702 barf("resumeThread: thread not found");
1704 tso->link = END_TSO_QUEUE;
1706 #if defined(RTS_SUPPORTS_THREADS)
1707 if(tso->why_blocked == BlockedOnCCall)
1709 awakenBlockedQueueNoLock(tso->blocked_exceptions);
1710 tso->blocked_exceptions = NULL;
1714 /* Reset blocking status */
1715 tso->why_blocked = NotBlocked;
1717 cap->r.rCurrentTSO = tso;
1718 #if defined(RTS_SUPPORTS_THREADS)
1719 RELEASE_LOCK(&sched_mutex);
1725 /* ---------------------------------------------------------------------------
1727 * ------------------------------------------------------------------------ */
1728 static void unblockThread(StgTSO *tso);
1730 /* ---------------------------------------------------------------------------
1731 * Comparing Thread ids.
1733 * This is used from STG land in the implementation of the
1734 * instances of Eq/Ord for ThreadIds.
1735 * ------------------------------------------------------------------------ */
1738 cmp_thread(StgPtr tso1, StgPtr tso2)
1740 StgThreadID id1 = ((StgTSO *)tso1)->id;
1741 StgThreadID id2 = ((StgTSO *)tso2)->id;
1743 if (id1 < id2) return (-1);
1744 if (id1 > id2) return 1;
1748 /* ---------------------------------------------------------------------------
1749 * Fetching the ThreadID from an StgTSO.
1751 * This is used in the implementation of Show for ThreadIds.
1752 * ------------------------------------------------------------------------ */
1754 rts_getThreadId(StgPtr tso)
1756 return ((StgTSO *)tso)->id;
1761 labelThread(StgPtr tso, char *label)
1766 /* Caveat: Once set, you can only set the thread name to "" */
1767 len = strlen(label)+1;
1770 fprintf(stderr,"insufficient memory for labelThread!\n");
1772 strncpy(buf,label,len);
1773 /* Update will free the old memory for us */
1774 updateThreadLabel((StgWord)tso,buf);
1778 /* ---------------------------------------------------------------------------
1779 Create a new thread.
1781 The new thread starts with the given stack size. Before the
1782 scheduler can run, however, this thread needs to have a closure
1783 (and possibly some arguments) pushed on its stack. See
1784 pushClosure() in Schedule.h.
1786 createGenThread() and createIOThread() (in SchedAPI.h) are
1787 convenient packaged versions of this function.
1789 currently pri (priority) is only used in a GRAN setup -- HWL
1790 ------------------------------------------------------------------------ */
1791 //@cindex createThread
1793 /* currently pri (priority) is only used in a GRAN setup -- HWL */
1795 createThread(nat size, StgInt pri)
1798 createThread(nat size)
1805 /* First check whether we should create a thread at all */
1807 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
1808 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
1810 belch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1811 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1812 return END_TSO_QUEUE;
1818 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
1821 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
1823 /* catch ridiculously small stack sizes */
1824 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
1825 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
1828 stack_size = size - TSO_STRUCT_SIZEW;
1830 tso = (StgTSO *)allocate(size);
1831 TICK_ALLOC_TSO(stack_size, 0);
1833 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
1835 SET_GRAN_HDR(tso, ThisPE);
1838 // Always start with the compiled code evaluator
1839 tso->what_next = ThreadRunGHC;
1841 /* tso->id needs to be unique. For now we use a heavyweight mutex to
1842 * protect the increment operation on next_thread_id.
1843 * In future, we could use an atomic increment instead.
1845 ACQUIRE_LOCK(&thread_id_mutex);
1846 tso->id = next_thread_id++;
1847 RELEASE_LOCK(&thread_id_mutex);
1849 tso->why_blocked = NotBlocked;
1850 tso->blocked_exceptions = NULL;
1852 tso->stack_size = stack_size;
1853 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
1855 tso->sp = (P_)&(tso->stack) + stack_size;
1858 tso->prof.CCCS = CCS_MAIN;
1861 /* put a stop frame on the stack */
1862 tso->sp -= sizeofW(StgStopFrame);
1863 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
1866 tso->link = END_TSO_QUEUE;
1867 /* uses more flexible routine in GranSim */
1868 insertThread(tso, CurrentProc);
1870 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
1876 if (RtsFlags.GranFlags.GranSimStats.Full)
1877 DumpGranEvent(GR_START,tso);
1879 if (RtsFlags.ParFlags.ParStats.Full)
1880 DumpGranEvent(GR_STARTQ,tso);
1881 /* HACk to avoid SCHEDULE
1885 /* Link the new thread on the global thread list.
1887 tso->global_link = all_threads;
1891 tso->dist.priority = MandatoryPriority; //by default that is...
1895 tso->gran.pri = pri;
1897 tso->gran.magic = TSO_MAGIC; // debugging only
1899 tso->gran.sparkname = 0;
1900 tso->gran.startedat = CURRENT_TIME;
1901 tso->gran.exported = 0;
1902 tso->gran.basicblocks = 0;
1903 tso->gran.allocs = 0;
1904 tso->gran.exectime = 0;
1905 tso->gran.fetchtime = 0;
1906 tso->gran.fetchcount = 0;
1907 tso->gran.blocktime = 0;
1908 tso->gran.blockcount = 0;
1909 tso->gran.blockedat = 0;
1910 tso->gran.globalsparks = 0;
1911 tso->gran.localsparks = 0;
1912 if (RtsFlags.GranFlags.Light)
1913 tso->gran.clock = Now; /* local clock */
1915 tso->gran.clock = 0;
1917 IF_DEBUG(gran,printTSO(tso));
1920 tso->par.magic = TSO_MAGIC; // debugging only
1922 tso->par.sparkname = 0;
1923 tso->par.startedat = CURRENT_TIME;
1924 tso->par.exported = 0;
1925 tso->par.basicblocks = 0;
1926 tso->par.allocs = 0;
1927 tso->par.exectime = 0;
1928 tso->par.fetchtime = 0;
1929 tso->par.fetchcount = 0;
1930 tso->par.blocktime = 0;
1931 tso->par.blockcount = 0;
1932 tso->par.blockedat = 0;
1933 tso->par.globalsparks = 0;
1934 tso->par.localsparks = 0;
1938 globalGranStats.tot_threads_created++;
1939 globalGranStats.threads_created_on_PE[CurrentProc]++;
1940 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
1941 globalGranStats.tot_sq_probes++;
1943 // collect parallel global statistics (currently done together with GC stats)
1944 if (RtsFlags.ParFlags.ParStats.Global &&
1945 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1946 //fprintf(stderr, "Creating thread %d @ %11.2f\n", tso->id, usertime());
1947 globalParStats.tot_threads_created++;
1953 belch("==__ schedule: Created TSO %d (%p);",
1954 CurrentProc, tso, tso->id));
1956 IF_PAR_DEBUG(verbose,
1957 belch("==__ schedule: Created TSO %d (%p); %d threads active",
1958 tso->id, tso, advisory_thread_count));
1960 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
1961 tso->id, tso->stack_size));
1968 all parallel thread creation calls should fall through the following routine.
1971 createSparkThread(rtsSpark spark)
1973 ASSERT(spark != (rtsSpark)NULL);
1974 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads)
1976 barf("{createSparkThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1977 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1978 return END_TSO_QUEUE;
1982 tso = createThread(RtsFlags.GcFlags.initialStkSize);
1983 if (tso==END_TSO_QUEUE)
1984 barf("createSparkThread: Cannot create TSO");
1986 tso->priority = AdvisoryPriority;
1988 pushClosure(tso,spark);
1989 PUSH_ON_RUN_QUEUE(tso);
1990 advisory_thread_count++;
1997 Turn a spark into a thread.
1998 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
2001 //@cindex activateSpark
2003 activateSpark (rtsSpark spark)
2007 tso = createSparkThread(spark);
2008 if (RtsFlags.ParFlags.ParStats.Full) {
2009 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
2010 IF_PAR_DEBUG(verbose,
2011 belch("==^^ activateSpark: turning spark of closure %p (%s) into a thread",
2012 (StgClosure *)spark, info_type((StgClosure *)spark)));
2014 // ToDo: fwd info on local/global spark to thread -- HWL
2015 // tso->gran.exported = spark->exported;
2016 // tso->gran.locked = !spark->global;
2017 // tso->gran.sparkname = spark->name;
2023 static SchedulerStatus waitThread_(/*out*/StgMainThread* m
2024 #if defined(THREADED_RTS)
2025 , rtsBool blockWaiting
2030 /* ---------------------------------------------------------------------------
2033 * scheduleThread puts a thread on the head of the runnable queue.
2034 * This will usually be done immediately after a thread is created.
2035 * The caller of scheduleThread must create the thread using e.g.
2036 * createThread and push an appropriate closure
2037 * on this thread's stack before the scheduler is invoked.
2038 * ------------------------------------------------------------------------ */
2040 static void scheduleThread_ (StgTSO* tso);
2043 scheduleThread_(StgTSO *tso)
2045 // Precondition: sched_mutex must be held.
2047 /* Put the new thread on the head of the runnable queue. The caller
2048 * better push an appropriate closure on this thread's stack
2049 * beforehand. In the SMP case, the thread may start running as
2050 * soon as we release the scheduler lock below.
2052 PUSH_ON_RUN_QUEUE(tso);
2056 IF_DEBUG(scheduler,printTSO(tso));
2060 void scheduleThread(StgTSO* tso)
2062 ACQUIRE_LOCK(&sched_mutex);
2063 scheduleThread_(tso);
2064 RELEASE_LOCK(&sched_mutex);
2068 scheduleWaitThread(StgTSO* tso, /*[out]*/HaskellObj* ret)
2069 { // Precondition: sched_mutex must be held
2072 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
2076 #if defined(RTS_SUPPORTS_THREADS)
2077 initCondition(&m->wakeup);
2080 /* Put the thread on the main-threads list prior to scheduling the TSO.
2081 Failure to do so introduces a race condition in the MT case (as
2082 identified by Wolfgang Thaller), whereby the new task/OS thread
2083 created by scheduleThread_() would complete prior to the thread
2084 that spawned it managed to put 'itself' on the main-threads list.
2085 The upshot of it all being that the worker thread wouldn't get to
2086 signal the completion of the its work item for the main thread to
2087 see (==> it got stuck waiting.) -- sof 6/02.
2089 IF_DEBUG(scheduler, sched_belch("waiting for thread (%d)\n", tso->id));
2091 m->link = main_threads;
2094 scheduleThread_(tso);
2095 #if defined(THREADED_RTS)
2096 return waitThread_(m, rtsTrue);
2098 return waitThread_(m);
2102 /* ---------------------------------------------------------------------------
2105 * Initialise the scheduler. This resets all the queues - if the
2106 * queues contained any threads, they'll be garbage collected at the
2109 * ------------------------------------------------------------------------ */
2113 term_handler(int sig STG_UNUSED)
2116 ACQUIRE_LOCK(&term_mutex);
2118 RELEASE_LOCK(&term_mutex);
2129 for (i=0; i<=MAX_PROC; i++) {
2130 run_queue_hds[i] = END_TSO_QUEUE;
2131 run_queue_tls[i] = END_TSO_QUEUE;
2132 blocked_queue_hds[i] = END_TSO_QUEUE;
2133 blocked_queue_tls[i] = END_TSO_QUEUE;
2134 ccalling_threadss[i] = END_TSO_QUEUE;
2135 sleeping_queue = END_TSO_QUEUE;
2138 run_queue_hd = END_TSO_QUEUE;
2139 run_queue_tl = END_TSO_QUEUE;
2140 blocked_queue_hd = END_TSO_QUEUE;
2141 blocked_queue_tl = END_TSO_QUEUE;
2142 sleeping_queue = END_TSO_QUEUE;
2145 suspended_ccalling_threads = END_TSO_QUEUE;
2147 main_threads = NULL;
2148 all_threads = END_TSO_QUEUE;
2153 RtsFlags.ConcFlags.ctxtSwitchTicks =
2154 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
2156 #if defined(RTS_SUPPORTS_THREADS)
2157 /* Initialise the mutex and condition variables used by
2159 initMutex(&sched_mutex);
2160 initMutex(&term_mutex);
2161 initMutex(&thread_id_mutex);
2163 initCondition(&thread_ready_cond);
2167 initCondition(&gc_pending_cond);
2170 #if defined(RTS_SUPPORTS_THREADS)
2171 ACQUIRE_LOCK(&sched_mutex);
2174 /* Install the SIGHUP handler */
2177 struct sigaction action,oact;
2179 action.sa_handler = term_handler;
2180 sigemptyset(&action.sa_mask);
2181 action.sa_flags = 0;
2182 if (sigaction(SIGTERM, &action, &oact) != 0) {
2183 barf("can't install TERM handler");
2188 /* A capability holds the state a native thread needs in
2189 * order to execute STG code. At least one capability is
2190 * floating around (only SMP builds have more than one).
2194 #if defined(RTS_SUPPORTS_THREADS)
2195 /* start our haskell execution tasks */
2197 startTaskManager(RtsFlags.ParFlags.nNodes, taskStart);
2199 startTaskManager(0,taskStart);
2203 #if /* defined(SMP) ||*/ defined(PAR)
2207 #if defined(RTS_SUPPORTS_THREADS)
2208 RELEASE_LOCK(&sched_mutex);
2214 exitScheduler( void )
2216 #if defined(RTS_SUPPORTS_THREADS)
2219 shutting_down_scheduler = rtsTrue;
2222 /* -----------------------------------------------------------------------------
2223 Managing the per-task allocation areas.
2225 Each capability comes with an allocation area. These are
2226 fixed-length block lists into which allocation can be done.
2228 ToDo: no support for two-space collection at the moment???
2229 -------------------------------------------------------------------------- */
2231 /* -----------------------------------------------------------------------------
2232 * waitThread is the external interface for running a new computation
2233 * and waiting for the result.
2235 * In the non-SMP case, we create a new main thread, push it on the
2236 * main-thread stack, and invoke the scheduler to run it. The
2237 * scheduler will return when the top main thread on the stack has
2238 * completed or died, and fill in the necessary fields of the
2239 * main_thread structure.
2241 * In the SMP case, we create a main thread as before, but we then
2242 * create a new condition variable and sleep on it. When our new
2243 * main thread has completed, we'll be woken up and the status/result
2244 * will be in the main_thread struct.
2245 * -------------------------------------------------------------------------- */
2248 howManyThreadsAvail ( void )
2252 for (q = run_queue_hd; q != END_TSO_QUEUE; q = q->link)
2254 for (q = blocked_queue_hd; q != END_TSO_QUEUE; q = q->link)
2256 for (q = sleeping_queue; q != END_TSO_QUEUE; q = q->link)
2262 finishAllThreads ( void )
2265 while (run_queue_hd != END_TSO_QUEUE) {
2266 waitThread ( run_queue_hd, NULL);
2268 while (blocked_queue_hd != END_TSO_QUEUE) {
2269 waitThread ( blocked_queue_hd, NULL);
2271 while (sleeping_queue != END_TSO_QUEUE) {
2272 waitThread ( blocked_queue_hd, NULL);
2275 (blocked_queue_hd != END_TSO_QUEUE ||
2276 run_queue_hd != END_TSO_QUEUE ||
2277 sleeping_queue != END_TSO_QUEUE);
2281 waitThread(StgTSO *tso, /*out*/StgClosure **ret)
2284 SchedulerStatus stat;
2286 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
2290 #if defined(RTS_SUPPORTS_THREADS)
2291 initCondition(&m->wakeup);
2294 /* see scheduleWaitThread() comment */
2295 ACQUIRE_LOCK(&sched_mutex);
2296 m->link = main_threads;
2299 IF_DEBUG(scheduler, sched_belch("waiting for thread %d", tso->id));
2300 #if defined(THREADED_RTS)
2301 stat = waitThread_(m, rtsFalse);
2303 stat = waitThread_(m);
2305 RELEASE_LOCK(&sched_mutex);
2311 waitThread_(StgMainThread* m
2312 #if defined(THREADED_RTS)
2313 , rtsBool blockWaiting
2317 SchedulerStatus stat;
2319 // Precondition: sched_mutex must be held.
2320 IF_DEBUG(scheduler, sched_belch("== scheduler: new main thread (%d)\n", m->tso->id));
2322 #if defined(RTS_SUPPORTS_THREADS)
2324 # if defined(THREADED_RTS)
2325 if (!blockWaiting) {
2326 /* In the threaded case, the OS thread that called main()
2327 * gets to enter the RTS directly without going via another
2330 main_main_thread = m;
2331 RELEASE_LOCK(&sched_mutex);
2333 ACQUIRE_LOCK(&sched_mutex);
2334 main_main_thread = NULL;
2335 ASSERT(m->stat != NoStatus);
2340 waitCondition(&m->wakeup, &sched_mutex);
2341 } while (m->stat == NoStatus);
2344 /* GranSim specific init */
2345 CurrentTSO = m->tso; // the TSO to run
2346 procStatus[MainProc] = Busy; // status of main PE
2347 CurrentProc = MainProc; // PE to run it on
2349 RELEASE_LOCK(&sched_mutex);
2352 RELEASE_LOCK(&sched_mutex);
2354 ASSERT(m->stat != NoStatus);
2359 #if defined(RTS_SUPPORTS_THREADS)
2360 closeCondition(&m->wakeup);
2363 IF_DEBUG(scheduler, fprintf(stderr, "== scheduler: main thread (%d) finished\n",
2367 // Postcondition: sched_mutex still held
2371 //@node Run queue code, Garbage Collextion Routines, Suspend and Resume, Main scheduling code
2372 //@subsection Run queue code
2376 NB: In GranSim we have many run queues; run_queue_hd is actually a macro
2377 unfolding to run_queue_hds[CurrentProc], thus CurrentProc is an
2378 implicit global variable that has to be correct when calling these
2382 /* Put the new thread on the head of the runnable queue.
2383 * The caller of createThread better push an appropriate closure
2384 * on this thread's stack before the scheduler is invoked.
2386 static /* inline */ void
2387 add_to_run_queue(tso)
2390 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
2391 tso->link = run_queue_hd;
2393 if (run_queue_tl == END_TSO_QUEUE) {
2398 /* Put the new thread at the end of the runnable queue. */
2399 static /* inline */ void
2400 push_on_run_queue(tso)
2403 ASSERT(get_itbl((StgClosure *)tso)->type == TSO);
2404 ASSERT(run_queue_hd!=NULL && run_queue_tl!=NULL);
2405 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
2406 if (run_queue_hd == END_TSO_QUEUE) {
2409 run_queue_tl->link = tso;
2415 Should be inlined because it's used very often in schedule. The tso
2416 argument is actually only needed in GranSim, where we want to have the
2417 possibility to schedule *any* TSO on the run queue, irrespective of the
2418 actual ordering. Therefore, if tso is not the nil TSO then we traverse
2419 the run queue and dequeue the tso, adjusting the links in the queue.
2421 //@cindex take_off_run_queue
2422 static /* inline */ StgTSO*
2423 take_off_run_queue(StgTSO *tso) {
2427 qetlaHbogh Qu' ngaSbogh ghomDaQ {tso} yIteq!
2429 if tso is specified, unlink that tso from the run_queue (doesn't have
2430 to be at the beginning of the queue); GranSim only
2432 if (tso!=END_TSO_QUEUE) {
2433 /* find tso in queue */
2434 for (t=run_queue_hd, prev=END_TSO_QUEUE;
2435 t!=END_TSO_QUEUE && t!=tso;
2439 /* now actually dequeue the tso */
2440 if (prev!=END_TSO_QUEUE) {
2441 ASSERT(run_queue_hd!=t);
2442 prev->link = t->link;
2444 /* t is at beginning of thread queue */
2445 ASSERT(run_queue_hd==t);
2446 run_queue_hd = t->link;
2448 /* t is at end of thread queue */
2449 if (t->link==END_TSO_QUEUE) {
2450 ASSERT(t==run_queue_tl);
2451 run_queue_tl = prev;
2453 ASSERT(run_queue_tl!=t);
2455 t->link = END_TSO_QUEUE;
2457 /* take tso from the beginning of the queue; std concurrent code */
2459 if (t != END_TSO_QUEUE) {
2460 run_queue_hd = t->link;
2461 t->link = END_TSO_QUEUE;
2462 if (run_queue_hd == END_TSO_QUEUE) {
2463 run_queue_tl = END_TSO_QUEUE;
2472 //@node Garbage Collextion Routines, Blocking Queue Routines, Run queue code, Main scheduling code
2473 //@subsection Garbage Collextion Routines
2475 /* ---------------------------------------------------------------------------
2476 Where are the roots that we know about?
2478 - all the threads on the runnable queue
2479 - all the threads on the blocked queue
2480 - all the threads on the sleeping queue
2481 - all the thread currently executing a _ccall_GC
2482 - all the "main threads"
2484 ------------------------------------------------------------------------ */
2486 /* This has to be protected either by the scheduler monitor, or by the
2487 garbage collection monitor (probably the latter).
2492 GetRoots(evac_fn evac)
2497 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2498 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2499 evac((StgClosure **)&run_queue_hds[i]);
2500 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2501 evac((StgClosure **)&run_queue_tls[i]);
2503 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2504 evac((StgClosure **)&blocked_queue_hds[i]);
2505 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2506 evac((StgClosure **)&blocked_queue_tls[i]);
2507 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2508 evac((StgClosure **)&ccalling_threads[i]);
2515 if (run_queue_hd != END_TSO_QUEUE) {
2516 ASSERT(run_queue_tl != END_TSO_QUEUE);
2517 evac((StgClosure **)&run_queue_hd);
2518 evac((StgClosure **)&run_queue_tl);
2521 if (blocked_queue_hd != END_TSO_QUEUE) {
2522 ASSERT(blocked_queue_tl != END_TSO_QUEUE);
2523 evac((StgClosure **)&blocked_queue_hd);
2524 evac((StgClosure **)&blocked_queue_tl);
2527 if (sleeping_queue != END_TSO_QUEUE) {
2528 evac((StgClosure **)&sleeping_queue);
2532 if (suspended_ccalling_threads != END_TSO_QUEUE) {
2533 evac((StgClosure **)&suspended_ccalling_threads);
2536 #if defined(PAR) || defined(GRAN)
2537 markSparkQueue(evac);
2540 #ifndef mingw32_TARGET_OS
2541 // mark the signal handlers (signals should be already blocked)
2542 markSignalHandlers(evac);
2545 // main threads which have completed need to be retained until they
2546 // are dealt with in the main scheduler loop. They won't be
2547 // retained any other way: the GC will drop them from the
2548 // all_threads list, so we have to be careful to treat them as roots
2552 for (m = main_threads; m != NULL; m = m->link) {
2553 switch (m->tso->what_next) {
2554 case ThreadComplete:
2556 evac((StgClosure **)&m->tso);
2565 /* -----------------------------------------------------------------------------
2568 This is the interface to the garbage collector from Haskell land.
2569 We provide this so that external C code can allocate and garbage
2570 collect when called from Haskell via _ccall_GC.
2572 It might be useful to provide an interface whereby the programmer
2573 can specify more roots (ToDo).
2575 This needs to be protected by the GC condition variable above. KH.
2576 -------------------------------------------------------------------------- */
2578 static void (*extra_roots)(evac_fn);
2583 /* Obligated to hold this lock upon entry */
2584 ACQUIRE_LOCK(&sched_mutex);
2585 GarbageCollect(GetRoots,rtsFalse);
2586 RELEASE_LOCK(&sched_mutex);
2590 performMajorGC(void)
2592 ACQUIRE_LOCK(&sched_mutex);
2593 GarbageCollect(GetRoots,rtsTrue);
2594 RELEASE_LOCK(&sched_mutex);
2598 AllRoots(evac_fn evac)
2600 GetRoots(evac); // the scheduler's roots
2601 extra_roots(evac); // the user's roots
2605 performGCWithRoots(void (*get_roots)(evac_fn))
2607 ACQUIRE_LOCK(&sched_mutex);
2608 extra_roots = get_roots;
2609 GarbageCollect(AllRoots,rtsFalse);
2610 RELEASE_LOCK(&sched_mutex);
2613 /* -----------------------------------------------------------------------------
2616 If the thread has reached its maximum stack size, then raise the
2617 StackOverflow exception in the offending thread. Otherwise
2618 relocate the TSO into a larger chunk of memory and adjust its stack
2620 -------------------------------------------------------------------------- */
2623 threadStackOverflow(StgTSO *tso)
2625 nat new_stack_size, new_tso_size, diff, stack_words;
2629 IF_DEBUG(sanity,checkTSO(tso));
2630 if (tso->stack_size >= tso->max_stack_size) {
2633 belch("@@ threadStackOverflow of TSO %d (%p): stack too large (now %ld; max is %ld",
2634 tso->id, tso, tso->stack_size, tso->max_stack_size);
2635 /* If we're debugging, just print out the top of the stack */
2636 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2639 /* Send this thread the StackOverflow exception */
2640 raiseAsync(tso, (StgClosure *)stackOverflow_closure);
2644 /* Try to double the current stack size. If that takes us over the
2645 * maximum stack size for this thread, then use the maximum instead.
2646 * Finally round up so the TSO ends up as a whole number of blocks.
2648 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2649 new_tso_size = (nat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2650 TSO_STRUCT_SIZE)/sizeof(W_);
2651 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2652 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2654 IF_DEBUG(scheduler, fprintf(stderr,"== scheduler: increasing stack size from %d words to %d.\n", tso->stack_size, new_stack_size));
2656 dest = (StgTSO *)allocate(new_tso_size);
2657 TICK_ALLOC_TSO(new_stack_size,0);
2659 /* copy the TSO block and the old stack into the new area */
2660 memcpy(dest,tso,TSO_STRUCT_SIZE);
2661 stack_words = tso->stack + tso->stack_size - tso->sp;
2662 new_sp = (P_)dest + new_tso_size - stack_words;
2663 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2665 /* relocate the stack pointers... */
2666 diff = (P_)new_sp - (P_)tso->sp; /* In *words* */
2668 dest->stack_size = new_stack_size;
2670 /* Mark the old TSO as relocated. We have to check for relocated
2671 * TSOs in the garbage collector and any primops that deal with TSOs.
2673 * It's important to set the sp value to just beyond the end
2674 * of the stack, so we don't attempt to scavenge any part of the
2677 tso->what_next = ThreadRelocated;
2679 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2680 tso->why_blocked = NotBlocked;
2681 dest->mut_link = NULL;
2683 IF_PAR_DEBUG(verbose,
2684 belch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld",
2685 tso->id, tso, tso->stack_size);
2686 /* If we're debugging, just print out the top of the stack */
2687 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2690 IF_DEBUG(sanity,checkTSO(tso));
2692 IF_DEBUG(scheduler,printTSO(dest));
2698 //@node Blocking Queue Routines, Exception Handling Routines, Garbage Collextion Routines, Main scheduling code
2699 //@subsection Blocking Queue Routines
2701 /* ---------------------------------------------------------------------------
2702 Wake up a queue that was blocked on some resource.
2703 ------------------------------------------------------------------------ */
2707 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2712 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2714 /* write RESUME events to log file and
2715 update blocked and fetch time (depending on type of the orig closure) */
2716 if (RtsFlags.ParFlags.ParStats.Full) {
2717 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
2718 GR_RESUMEQ, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
2719 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
2720 if (EMPTY_RUN_QUEUE())
2721 emitSchedule = rtsTrue;
2723 switch (get_itbl(node)->type) {
2725 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2730 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2737 barf("{unblockOneLocked}Daq Qagh: unexpected closure in blocking queue");
2744 static StgBlockingQueueElement *
2745 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2748 PEs node_loc, tso_loc;
2750 node_loc = where_is(node); // should be lifted out of loop
2751 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2752 tso_loc = where_is((StgClosure *)tso);
2753 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
2754 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
2755 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
2756 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
2757 // insertThread(tso, node_loc);
2758 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
2760 tso, node, (rtsSpark*)NULL);
2761 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2764 } else { // TSO is remote (actually should be FMBQ)
2765 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
2766 RtsFlags.GranFlags.Costs.gunblocktime +
2767 RtsFlags.GranFlags.Costs.latency;
2768 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
2770 tso, node, (rtsSpark*)NULL);
2771 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2774 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
2776 fprintf(stderr," %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
2777 (node_loc==tso_loc ? "Local" : "Global"),
2778 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
2779 tso->block_info.closure = NULL;
2780 IF_DEBUG(scheduler,belch("-- Waking up thread %ld (%p)",
2784 static StgBlockingQueueElement *
2785 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2787 StgBlockingQueueElement *next;
2789 switch (get_itbl(bqe)->type) {
2791 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
2792 /* if it's a TSO just push it onto the run_queue */
2794 // ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
2795 PUSH_ON_RUN_QUEUE((StgTSO *)bqe);
2797 unblockCount(bqe, node);
2798 /* reset blocking status after dumping event */
2799 ((StgTSO *)bqe)->why_blocked = NotBlocked;
2803 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
2805 bqe->link = (StgBlockingQueueElement *)PendingFetches;
2806 PendingFetches = (StgBlockedFetch *)bqe;
2810 /* can ignore this case in a non-debugging setup;
2811 see comments on RBHSave closures above */
2813 /* check that the closure is an RBHSave closure */
2814 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
2815 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
2816 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
2820 barf("{unblockOneLocked}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
2821 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
2825 IF_PAR_DEBUG(bq, fprintf(stderr, ", %p (%s)", bqe, info_type((StgClosure*)bqe)));
2829 #else /* !GRAN && !PAR */
2831 unblockOneLocked(StgTSO *tso)
2835 ASSERT(get_itbl(tso)->type == TSO);
2836 ASSERT(tso->why_blocked != NotBlocked);
2837 tso->why_blocked = NotBlocked;
2839 PUSH_ON_RUN_QUEUE(tso);
2841 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", tso->id));
2846 #if defined(GRAN) || defined(PAR)
2847 inline StgBlockingQueueElement *
2848 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
2850 ACQUIRE_LOCK(&sched_mutex);
2851 bqe = unblockOneLocked(bqe, node);
2852 RELEASE_LOCK(&sched_mutex);
2857 unblockOne(StgTSO *tso)
2859 ACQUIRE_LOCK(&sched_mutex);
2860 tso = unblockOneLocked(tso);
2861 RELEASE_LOCK(&sched_mutex);
2868 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2870 StgBlockingQueueElement *bqe;
2875 belch("##-_ AwBQ for node %p on PE %d @ %ld by TSO %d (%p): ", \
2876 node, CurrentProc, CurrentTime[CurrentProc],
2877 CurrentTSO->id, CurrentTSO));
2879 node_loc = where_is(node);
2881 ASSERT(q == END_BQ_QUEUE ||
2882 get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
2883 get_itbl(q)->type == CONSTR); // closure (type constructor)
2884 ASSERT(is_unique(node));
2886 /* FAKE FETCH: magically copy the node to the tso's proc;
2887 no Fetch necessary because in reality the node should not have been
2888 moved to the other PE in the first place
2890 if (CurrentProc!=node_loc) {
2892 belch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)",
2893 node, node_loc, CurrentProc, CurrentTSO->id,
2894 // CurrentTSO, where_is(CurrentTSO),
2895 node->header.gran.procs));
2896 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
2898 belch("## new bitmask of node %p is %#x",
2899 node, node->header.gran.procs));
2900 if (RtsFlags.GranFlags.GranSimStats.Global) {
2901 globalGranStats.tot_fake_fetches++;
2906 // ToDo: check: ASSERT(CurrentProc==node_loc);
2907 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
2910 bqe points to the current element in the queue
2911 next points to the next element in the queue
2913 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2914 //tso_loc = where_is(tso);
2916 bqe = unblockOneLocked(bqe, node);
2919 /* if this is the BQ of an RBH, we have to put back the info ripped out of
2920 the closure to make room for the anchor of the BQ */
2921 if (bqe!=END_BQ_QUEUE) {
2922 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
2924 ASSERT((info_ptr==&RBH_Save_0_info) ||
2925 (info_ptr==&RBH_Save_1_info) ||
2926 (info_ptr==&RBH_Save_2_info));
2928 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
2929 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
2930 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
2933 belch("## Filled in RBH_Save for %p (%s) at end of AwBQ",
2934 node, info_type(node)));
2937 /* statistics gathering */
2938 if (RtsFlags.GranFlags.GranSimStats.Global) {
2939 // globalGranStats.tot_bq_processing_time += bq_processing_time;
2940 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
2941 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
2942 globalGranStats.tot_awbq++; // total no. of bqs awakened
2945 fprintf(stderr,"## BQ Stats of %p: [%d entries] %s\n",
2946 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
2950 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2952 StgBlockingQueueElement *bqe;
2954 ACQUIRE_LOCK(&sched_mutex);
2956 IF_PAR_DEBUG(verbose,
2957 belch("##-_ AwBQ for node %p on [%x]: ",
2961 if(get_itbl(q)->type == CONSTR || q==END_BQ_QUEUE) {
2962 IF_PAR_DEBUG(verbose, belch("## ... nothing to unblock so lets just return. RFP (BUG?)"));
2967 ASSERT(q == END_BQ_QUEUE ||
2968 get_itbl(q)->type == TSO ||
2969 get_itbl(q)->type == BLOCKED_FETCH ||
2970 get_itbl(q)->type == CONSTR);
2973 while (get_itbl(bqe)->type==TSO ||
2974 get_itbl(bqe)->type==BLOCKED_FETCH) {
2975 bqe = unblockOneLocked(bqe, node);
2977 RELEASE_LOCK(&sched_mutex);
2980 #else /* !GRAN && !PAR */
2982 #ifdef RTS_SUPPORTS_THREADS
2984 awakenBlockedQueueNoLock(StgTSO *tso)
2986 while (tso != END_TSO_QUEUE) {
2987 tso = unblockOneLocked(tso);
2993 awakenBlockedQueue(StgTSO *tso)
2995 ACQUIRE_LOCK(&sched_mutex);
2996 while (tso != END_TSO_QUEUE) {
2997 tso = unblockOneLocked(tso);
2999 RELEASE_LOCK(&sched_mutex);
3003 //@node Exception Handling Routines, Debugging Routines, Blocking Queue Routines, Main scheduling code
3004 //@subsection Exception Handling Routines
3006 /* ---------------------------------------------------------------------------
3008 - usually called inside a signal handler so it mustn't do anything fancy.
3009 ------------------------------------------------------------------------ */
3012 interruptStgRts(void)
3018 /* -----------------------------------------------------------------------------
3021 This is for use when we raise an exception in another thread, which
3023 This has nothing to do with the UnblockThread event in GranSim. -- HWL
3024 -------------------------------------------------------------------------- */
3026 #if defined(GRAN) || defined(PAR)
3028 NB: only the type of the blocking queue is different in GranSim and GUM
3029 the operations on the queue-elements are the same
3030 long live polymorphism!
3032 Locks: sched_mutex is held upon entry and exit.
3036 unblockThread(StgTSO *tso)
3038 StgBlockingQueueElement *t, **last;
3040 switch (tso->why_blocked) {
3043 return; /* not blocked */
3046 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3048 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
3049 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3051 last = (StgBlockingQueueElement **)&mvar->head;
3052 for (t = (StgBlockingQueueElement *)mvar->head;
3054 last = &t->link, last_tso = t, t = t->link) {
3055 if (t == (StgBlockingQueueElement *)tso) {
3056 *last = (StgBlockingQueueElement *)tso->link;
3057 if (mvar->tail == tso) {
3058 mvar->tail = (StgTSO *)last_tso;
3063 barf("unblockThread (MVAR): TSO not found");
3066 case BlockedOnBlackHole:
3067 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3069 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3071 last = &bq->blocking_queue;
3072 for (t = bq->blocking_queue;
3074 last = &t->link, t = t->link) {
3075 if (t == (StgBlockingQueueElement *)tso) {
3076 *last = (StgBlockingQueueElement *)tso->link;
3080 barf("unblockThread (BLACKHOLE): TSO not found");
3083 case BlockedOnException:
3085 StgTSO *target = tso->block_info.tso;
3087 ASSERT(get_itbl(target)->type == TSO);
3089 if (target->what_next == ThreadRelocated) {
3090 target = target->link;
3091 ASSERT(get_itbl(target)->type == TSO);
3094 ASSERT(target->blocked_exceptions != NULL);
3096 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
3097 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
3099 last = &t->link, t = t->link) {
3100 ASSERT(get_itbl(t)->type == TSO);
3101 if (t == (StgBlockingQueueElement *)tso) {
3102 *last = (StgBlockingQueueElement *)tso->link;
3106 barf("unblockThread (Exception): TSO not found");
3110 case BlockedOnWrite:
3112 /* take TSO off blocked_queue */
3113 StgBlockingQueueElement *prev = NULL;
3114 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
3115 prev = t, t = t->link) {
3116 if (t == (StgBlockingQueueElement *)tso) {
3118 blocked_queue_hd = (StgTSO *)t->link;
3119 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3120 blocked_queue_tl = END_TSO_QUEUE;
3123 prev->link = t->link;
3124 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3125 blocked_queue_tl = (StgTSO *)prev;
3131 barf("unblockThread (I/O): TSO not found");
3134 case BlockedOnDelay:
3136 /* take TSO off sleeping_queue */
3137 StgBlockingQueueElement *prev = NULL;
3138 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
3139 prev = t, t = t->link) {
3140 if (t == (StgBlockingQueueElement *)tso) {
3142 sleeping_queue = (StgTSO *)t->link;
3144 prev->link = t->link;
3149 barf("unblockThread (I/O): TSO not found");
3153 barf("unblockThread");
3157 tso->link = END_TSO_QUEUE;
3158 tso->why_blocked = NotBlocked;
3159 tso->block_info.closure = NULL;
3160 PUSH_ON_RUN_QUEUE(tso);
3164 unblockThread(StgTSO *tso)
3168 /* To avoid locking unnecessarily. */
3169 if (tso->why_blocked == NotBlocked) {
3173 switch (tso->why_blocked) {
3176 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3178 StgTSO *last_tso = END_TSO_QUEUE;
3179 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3182 for (t = mvar->head; t != END_TSO_QUEUE;
3183 last = &t->link, last_tso = t, t = t->link) {
3186 if (mvar->tail == tso) {
3187 mvar->tail = last_tso;
3192 barf("unblockThread (MVAR): TSO not found");
3195 case BlockedOnBlackHole:
3196 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3198 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3200 last = &bq->blocking_queue;
3201 for (t = bq->blocking_queue; t != END_TSO_QUEUE;
3202 last = &t->link, t = t->link) {
3208 barf("unblockThread (BLACKHOLE): TSO not found");
3211 case BlockedOnException:
3213 StgTSO *target = tso->block_info.tso;
3215 ASSERT(get_itbl(target)->type == TSO);
3217 while (target->what_next == ThreadRelocated) {
3218 target = target->link;
3219 ASSERT(get_itbl(target)->type == TSO);
3222 ASSERT(target->blocked_exceptions != NULL);
3224 last = &target->blocked_exceptions;
3225 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
3226 last = &t->link, t = t->link) {
3227 ASSERT(get_itbl(t)->type == TSO);
3233 barf("unblockThread (Exception): TSO not found");
3237 case BlockedOnWrite:
3239 StgTSO *prev = NULL;
3240 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
3241 prev = t, t = t->link) {
3244 blocked_queue_hd = t->link;
3245 if (blocked_queue_tl == t) {
3246 blocked_queue_tl = END_TSO_QUEUE;
3249 prev->link = t->link;
3250 if (blocked_queue_tl == t) {
3251 blocked_queue_tl = prev;
3257 barf("unblockThread (I/O): TSO not found");
3260 case BlockedOnDelay:
3262 StgTSO *prev = NULL;
3263 for (t = sleeping_queue; t != END_TSO_QUEUE;
3264 prev = t, t = t->link) {
3267 sleeping_queue = t->link;
3269 prev->link = t->link;
3274 barf("unblockThread (I/O): TSO not found");
3278 barf("unblockThread");
3282 tso->link = END_TSO_QUEUE;
3283 tso->why_blocked = NotBlocked;
3284 tso->block_info.closure = NULL;
3285 PUSH_ON_RUN_QUEUE(tso);
3289 /* -----------------------------------------------------------------------------
3292 * The following function implements the magic for raising an
3293 * asynchronous exception in an existing thread.
3295 * We first remove the thread from any queue on which it might be
3296 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
3298 * We strip the stack down to the innermost CATCH_FRAME, building
3299 * thunks in the heap for all the active computations, so they can
3300 * be restarted if necessary. When we reach a CATCH_FRAME, we build
3301 * an application of the handler to the exception, and push it on
3302 * the top of the stack.
3304 * How exactly do we save all the active computations? We create an
3305 * AP_STACK for every UpdateFrame on the stack. Entering one of these
3306 * AP_STACKs pushes everything from the corresponding update frame
3307 * upwards onto the stack. (Actually, it pushes everything up to the
3308 * next update frame plus a pointer to the next AP_STACK object.
3309 * Entering the next AP_STACK object pushes more onto the stack until we
3310 * reach the last AP_STACK object - at which point the stack should look
3311 * exactly as it did when we killed the TSO and we can continue
3312 * execution by entering the closure on top of the stack.
3314 * We can also kill a thread entirely - this happens if either (a) the
3315 * exception passed to raiseAsync is NULL, or (b) there's no
3316 * CATCH_FRAME on the stack. In either case, we strip the entire
3317 * stack and replace the thread with a zombie.
3319 * Locks: sched_mutex held upon entry nor exit.
3321 * -------------------------------------------------------------------------- */
3324 deleteThread(StgTSO *tso)
3326 raiseAsync(tso,NULL);
3330 raiseAsyncWithLock(StgTSO *tso, StgClosure *exception)
3332 /* When raising async exs from contexts where sched_mutex isn't held;
3333 use raiseAsyncWithLock(). */
3334 ACQUIRE_LOCK(&sched_mutex);
3335 raiseAsync(tso,exception);
3336 RELEASE_LOCK(&sched_mutex);
3340 raiseAsync(StgTSO *tso, StgClosure *exception)
3342 StgRetInfoTable *info;
3345 // Thread already dead?
3346 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3351 sched_belch("raising exception in thread %ld.", tso->id));
3353 // Remove it from any blocking queues
3358 // The stack freezing code assumes there's a closure pointer on
3359 // the top of the stack, so we have to arrange that this is the case...
3361 if (sp[0] == (W_)&stg_enter_info) {
3365 sp[0] = (W_)&stg_dummy_ret_closure;
3371 // 1. Let the top of the stack be the "current closure"
3373 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
3376 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
3377 // current closure applied to the chunk of stack up to (but not
3378 // including) the update frame. This closure becomes the "current
3379 // closure". Go back to step 2.
3381 // 4. If it's a CATCH_FRAME, then leave the exception handler on
3382 // top of the stack applied to the exception.
3384 // 5. If it's a STOP_FRAME, then kill the thread.
3389 info = get_ret_itbl((StgClosure *)frame);
3391 while (info->i.type != UPDATE_FRAME
3392 && (info->i.type != CATCH_FRAME || exception == NULL)
3393 && info->i.type != STOP_FRAME) {
3394 frame += stack_frame_sizeW((StgClosure *)frame);
3395 info = get_ret_itbl((StgClosure *)frame);
3398 switch (info->i.type) {
3401 // If we find a CATCH_FRAME, and we've got an exception to raise,
3402 // then build the THUNK raise(exception), and leave it on
3403 // top of the CATCH_FRAME ready to enter.
3407 StgCatchFrame *cf = (StgCatchFrame *)frame;
3411 // we've got an exception to raise, so let's pass it to the
3412 // handler in this frame.
3414 raise = (StgClosure *)allocate(sizeofW(StgClosure)+1);
3415 TICK_ALLOC_SE_THK(1,0);
3416 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
3417 raise->payload[0] = exception;
3419 // throw away the stack from Sp up to the CATCH_FRAME.
3423 /* Ensure that async excpetions are blocked now, so we don't get
3424 * a surprise exception before we get around to executing the
3427 if (tso->blocked_exceptions == NULL) {
3428 tso->blocked_exceptions = END_TSO_QUEUE;
3431 /* Put the newly-built THUNK on top of the stack, ready to execute
3432 * when the thread restarts.
3435 sp[-1] = (W_)&stg_enter_info;
3437 tso->what_next = ThreadRunGHC;
3438 IF_DEBUG(sanity, checkTSO(tso));
3447 // First build an AP_STACK consisting of the stack chunk above the
3448 // current update frame, with the top word on the stack as the
3451 words = frame - sp - 1;
3452 ap = (StgAP_STACK *)allocate(PAP_sizeW(words));
3455 ap->fun = (StgClosure *)sp[0];
3457 for(i=0; i < (nat)words; ++i) {
3458 ap->payload[i] = (StgClosure *)*sp++;
3461 SET_HDR(ap,&stg_AP_STACK_info,
3462 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
3463 TICK_ALLOC_UP_THK(words+1,0);
3466 fprintf(stderr, "scheduler: Updating ");
3467 printPtr((P_)((StgUpdateFrame *)frame)->updatee);
3468 fprintf(stderr, " with ");
3469 printObj((StgClosure *)ap);
3472 // Replace the updatee with an indirection - happily
3473 // this will also wake up any threads currently
3474 // waiting on the result.
3476 // Warning: if we're in a loop, more than one update frame on
3477 // the stack may point to the same object. Be careful not to
3478 // overwrite an IND_OLDGEN in this case, because we'll screw
3479 // up the mutable lists. To be on the safe side, don't
3480 // overwrite any kind of indirection at all. See also
3481 // threadSqueezeStack in GC.c, where we have to make a similar
3484 if (!closure_IND(((StgUpdateFrame *)frame)->updatee)) {
3485 // revert the black hole
3486 UPD_IND_NOLOCK(((StgUpdateFrame *)frame)->updatee,ap);
3488 sp += sizeofW(StgUpdateFrame) - 1;
3489 sp[0] = (W_)ap; // push onto stack
3494 // We've stripped the entire stack, the thread is now dead.
3495 sp += sizeofW(StgStopFrame);
3496 tso->what_next = ThreadKilled;
3507 /* -----------------------------------------------------------------------------
3508 resurrectThreads is called after garbage collection on the list of
3509 threads found to be garbage. Each of these threads will be woken
3510 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
3511 on an MVar, or NonTermination if the thread was blocked on a Black
3514 Locks: sched_mutex isn't held upon entry nor exit.
3515 -------------------------------------------------------------------------- */
3518 resurrectThreads( StgTSO *threads )
3522 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
3523 next = tso->global_link;
3524 tso->global_link = all_threads;
3526 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
3528 switch (tso->why_blocked) {
3530 case BlockedOnException:
3531 /* Called by GC - sched_mutex lock is currently held. */
3532 raiseAsync(tso,(StgClosure *)BlockedOnDeadMVar_closure);
3534 case BlockedOnBlackHole:
3535 raiseAsync(tso,(StgClosure *)NonTermination_closure);
3538 /* This might happen if the thread was blocked on a black hole
3539 * belonging to a thread that we've just woken up (raiseAsync
3540 * can wake up threads, remember...).
3544 barf("resurrectThreads: thread blocked in a strange way");
3549 /* -----------------------------------------------------------------------------
3550 * Blackhole detection: if we reach a deadlock, test whether any
3551 * threads are blocked on themselves. Any threads which are found to
3552 * be self-blocked get sent a NonTermination exception.
3554 * This is only done in a deadlock situation in order to avoid
3555 * performance overhead in the normal case.
3557 * Locks: sched_mutex is held upon entry and exit.
3558 * -------------------------------------------------------------------------- */
3561 detectBlackHoles( void )
3563 StgTSO *tso = all_threads;
3565 StgClosure *blocked_on;
3566 StgRetInfoTable *info;
3568 for (tso = all_threads; tso != END_TSO_QUEUE; tso = tso->global_link) {
3570 while (tso->what_next == ThreadRelocated) {
3572 ASSERT(get_itbl(tso)->type == TSO);
3575 if (tso->why_blocked != BlockedOnBlackHole) {
3578 blocked_on = tso->block_info.closure;
3580 frame = (StgClosure *)tso->sp;
3583 info = get_ret_itbl(frame);
3584 switch (info->i.type) {
3586 if (((StgUpdateFrame *)frame)->updatee == blocked_on) {
3587 /* We are blocking on one of our own computations, so
3588 * send this thread the NonTermination exception.
3591 sched_belch("thread %d is blocked on itself", tso->id));
3592 raiseAsync(tso, (StgClosure *)NonTermination_closure);
3596 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3602 // normal stack frames; do nothing except advance the pointer
3604 (StgPtr)frame += stack_frame_sizeW(frame);
3611 //@node Debugging Routines, Index, Exception Handling Routines, Main scheduling code
3612 //@subsection Debugging Routines
3614 /* -----------------------------------------------------------------------------
3615 * Debugging: why is a thread blocked
3616 * [Also provides useful information when debugging threaded programs
3617 * at the Haskell source code level, so enable outside of DEBUG. --sof 7/02]
3618 -------------------------------------------------------------------------- */
3622 printThreadBlockage(StgTSO *tso)
3624 switch (tso->why_blocked) {
3626 fprintf(stderr,"is blocked on read from fd %d", tso->block_info.fd);
3628 case BlockedOnWrite:
3629 fprintf(stderr,"is blocked on write to fd %d", tso->block_info.fd);
3631 case BlockedOnDelay:
3632 fprintf(stderr,"is blocked until %d", tso->block_info.target);
3635 fprintf(stderr,"is blocked on an MVar");
3637 case BlockedOnException:
3638 fprintf(stderr,"is blocked on delivering an exception to thread %d",
3639 tso->block_info.tso->id);
3641 case BlockedOnBlackHole:
3642 fprintf(stderr,"is blocked on a black hole");
3645 fprintf(stderr,"is not blocked");
3649 fprintf(stderr,"is blocked on global address; local FM_BQ is %p (%s)",
3650 tso->block_info.closure, info_type(tso->block_info.closure));
3652 case BlockedOnGA_NoSend:
3653 fprintf(stderr,"is blocked on global address (no send); local FM_BQ is %p (%s)",
3654 tso->block_info.closure, info_type(tso->block_info.closure));
3657 #if defined(RTS_SUPPORTS_THREADS)
3658 case BlockedOnCCall:
3659 fprintf(stderr,"is blocked on an external call");
3661 case BlockedOnCCall_NoUnblockExc:
3662 fprintf(stderr,"is blocked on an external call (exceptions were already blocked)");
3666 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
3667 tso->why_blocked, tso->id, tso);
3673 printThreadStatus(StgTSO *tso)
3675 switch (tso->what_next) {
3677 fprintf(stderr,"has been killed");
3679 case ThreadComplete:
3680 fprintf(stderr,"has completed");
3683 printThreadBlockage(tso);
3688 printAllThreads(void)
3694 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3695 ullong_format_string(TIME_ON_PROC(CurrentProc),
3696 time_string, rtsFalse/*no commas!*/);
3698 fprintf(stderr, "all threads at [%s]:\n", time_string);
3700 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3701 ullong_format_string(CURRENT_TIME,
3702 time_string, rtsFalse/*no commas!*/);
3704 fprintf(stderr,"all threads at [%s]:\n", time_string);
3706 fprintf(stderr,"all threads:\n");
3709 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
3710 fprintf(stderr, "\tthread %d @ %p ", t->id, (void *)t);
3711 label = lookupThreadLabel((StgWord)t);
3712 if (label) fprintf(stderr,"[\"%s\"] ",(char *)label);
3713 printThreadStatus(t);
3714 fprintf(stderr,"\n");
3721 Print a whole blocking queue attached to node (debugging only).
3726 print_bq (StgClosure *node)
3728 StgBlockingQueueElement *bqe;
3732 fprintf(stderr,"## BQ of closure %p (%s): ",
3733 node, info_type(node));
3735 /* should cover all closures that may have a blocking queue */
3736 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3737 get_itbl(node)->type == FETCH_ME_BQ ||
3738 get_itbl(node)->type == RBH ||
3739 get_itbl(node)->type == MVAR);
3741 ASSERT(node!=(StgClosure*)NULL); // sanity check
3743 print_bqe(((StgBlockingQueue*)node)->blocking_queue);
3747 Print a whole blocking queue starting with the element bqe.
3750 print_bqe (StgBlockingQueueElement *bqe)
3755 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3757 for (end = (bqe==END_BQ_QUEUE);
3758 !end; // iterate until bqe points to a CONSTR
3759 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE),
3760 bqe = end ? END_BQ_QUEUE : bqe->link) {
3761 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3762 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3763 /* types of closures that may appear in a blocking queue */
3764 ASSERT(get_itbl(bqe)->type == TSO ||
3765 get_itbl(bqe)->type == BLOCKED_FETCH ||
3766 get_itbl(bqe)->type == CONSTR);
3767 /* only BQs of an RBH end with an RBH_Save closure */
3768 //ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3770 switch (get_itbl(bqe)->type) {
3772 fprintf(stderr," TSO %u (%x),",
3773 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
3776 fprintf(stderr," BF (node=%p, ga=((%x, %d, %x)),",
3777 ((StgBlockedFetch *)bqe)->node,
3778 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
3779 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
3780 ((StgBlockedFetch *)bqe)->ga.weight);
3783 fprintf(stderr," %s (IP %p),",
3784 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3785 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3786 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3787 "RBH_Save_?"), get_itbl(bqe));
3790 barf("Unexpected closure type %s in blocking queue", // of %p (%s)",
3791 info_type((StgClosure *)bqe)); // , node, info_type(node));
3795 fputc('\n', stderr);
3797 # elif defined(GRAN)
3799 print_bq (StgClosure *node)
3801 StgBlockingQueueElement *bqe;
3802 PEs node_loc, tso_loc;
3805 /* should cover all closures that may have a blocking queue */
3806 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3807 get_itbl(node)->type == FETCH_ME_BQ ||
3808 get_itbl(node)->type == RBH);
3810 ASSERT(node!=(StgClosure*)NULL); // sanity check
3811 node_loc = where_is(node);
3813 fprintf(stderr,"## BQ of closure %p (%s) on [PE %d]: ",
3814 node, info_type(node), node_loc);
3817 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3819 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
3820 !end; // iterate until bqe points to a CONSTR
3821 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
3822 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3823 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3824 /* types of closures that may appear in a blocking queue */
3825 ASSERT(get_itbl(bqe)->type == TSO ||
3826 get_itbl(bqe)->type == CONSTR);
3827 /* only BQs of an RBH end with an RBH_Save closure */
3828 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3830 tso_loc = where_is((StgClosure *)bqe);
3831 switch (get_itbl(bqe)->type) {
3833 fprintf(stderr," TSO %d (%p) on [PE %d],",
3834 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
3837 fprintf(stderr," %s (IP %p),",
3838 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3839 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3840 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3841 "RBH_Save_?"), get_itbl(bqe));
3844 barf("Unexpected closure type %s in blocking queue of %p (%s)",
3845 info_type((StgClosure *)bqe), node, info_type(node));
3849 fputc('\n', stderr);
3853 Nice and easy: only TSOs on the blocking queue
3856 print_bq (StgClosure *node)
3860 ASSERT(node!=(StgClosure*)NULL); // sanity check
3861 for (tso = ((StgBlockingQueue*)node)->blocking_queue;
3862 tso != END_TSO_QUEUE;
3864 ASSERT(tso!=NULL && tso!=END_TSO_QUEUE); // sanity check
3865 ASSERT(get_itbl(tso)->type == TSO); // guess what, sanity check
3866 fprintf(stderr," TSO %d (%p),", tso->id, tso);
3868 fputc('\n', stderr);
3879 for (i=0, tso=run_queue_hd;
3880 tso != END_TSO_QUEUE;
3889 sched_belch(char *s, ...)
3894 fprintf(stderr, "scheduler (task %ld): ", osThreadId());
3896 fprintf(stderr, "== ");
3898 fprintf(stderr, "scheduler: ");
3900 vfprintf(stderr, s, ap);
3901 fprintf(stderr, "\n");
3908 //@node Index, , Debugging Routines, Main scheduling code
3912 //* StgMainThread:: @cindex\s-+StgMainThread
3913 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
3914 //* blocked_queue_hd:: @cindex\s-+blocked_queue_hd
3915 //* blocked_queue_tl:: @cindex\s-+blocked_queue_tl
3916 //* context_switch:: @cindex\s-+context_switch
3917 //* createThread:: @cindex\s-+createThread
3918 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
3919 //* initScheduler:: @cindex\s-+initScheduler
3920 //* interrupted:: @cindex\s-+interrupted
3921 //* next_thread_id:: @cindex\s-+next_thread_id
3922 //* print_bq:: @cindex\s-+print_bq
3923 //* run_queue_hd:: @cindex\s-+run_queue_hd
3924 //* run_queue_tl:: @cindex\s-+run_queue_tl
3925 //* sched_mutex:: @cindex\s-+sched_mutex
3926 //* schedule:: @cindex\s-+schedule
3927 //* take_off_run_queue:: @cindex\s-+take_off_run_queue
3928 //* term_mutex:: @cindex\s-+term_mutex