1 /* ---------------------------------------------------------------------------
2 * $Id: Schedule.c,v 1.180 2003/11/12 17:49:10 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>
134 #define USED_IN_THREADED_RTS
136 #define USED_IN_THREADED_RTS STG_UNUSED
139 #ifdef RTS_SUPPORTS_THREADS
140 #define USED_WHEN_RTS_SUPPORTS_THREADS
142 #define USED_WHEN_RTS_SUPPORTS_THREADS STG_UNUSED
145 //@node Variables and Data structures, Prototypes, Includes, Main scheduling code
146 //@subsection Variables and Data structures
148 /* Main thread queue.
149 * Locks required: sched_mutex.
151 StgMainThread *main_threads = NULL;
154 * Locks required: sched_mutex.
158 StgTSO* ActiveTSO = NULL; /* for assigning system costs; GranSim-Light only */
159 /* rtsTime TimeOfNextEvent, EndOfTimeSlice; now in GranSim.c */
162 In GranSim we have a runnable and a blocked queue for each processor.
163 In order to minimise code changes new arrays run_queue_hds/tls
164 are created. run_queue_hd is then a short cut (macro) for
165 run_queue_hds[CurrentProc] (see GranSim.h).
168 StgTSO *run_queue_hds[MAX_PROC], *run_queue_tls[MAX_PROC];
169 StgTSO *blocked_queue_hds[MAX_PROC], *blocked_queue_tls[MAX_PROC];
170 StgTSO *ccalling_threadss[MAX_PROC];
171 /* We use the same global list of threads (all_threads) in GranSim as in
172 the std RTS (i.e. we are cheating). However, we don't use this list in
173 the GranSim specific code at the moment (so we are only potentially
178 StgTSO *run_queue_hd = NULL;
179 StgTSO *run_queue_tl = NULL;
180 StgTSO *blocked_queue_hd = NULL;
181 StgTSO *blocked_queue_tl = NULL;
182 StgTSO *sleeping_queue = NULL; /* perhaps replace with a hash table? */
186 /* Linked list of all threads.
187 * Used for detecting garbage collected threads.
189 StgTSO *all_threads = NULL;
191 /* When a thread performs a safe C call (_ccall_GC, using old
192 * terminology), it gets put on the suspended_ccalling_threads
193 * list. Used by the garbage collector.
195 static StgTSO *suspended_ccalling_threads;
197 static StgTSO *threadStackOverflow(StgTSO *tso);
199 /* KH: The following two flags are shared memory locations. There is no need
200 to lock them, since they are only unset at the end of a scheduler
204 /* flag set by signal handler to precipitate a context switch */
205 //@cindex context_switch
206 nat context_switch = 0;
208 /* if this flag is set as well, give up execution */
209 //@cindex interrupted
210 rtsBool interrupted = rtsFalse;
212 /* Next thread ID to allocate.
213 * Locks required: thread_id_mutex
215 //@cindex next_thread_id
216 static StgThreadID next_thread_id = 1;
219 * Pointers to the state of the current thread.
220 * Rule of thumb: if CurrentTSO != NULL, then we're running a Haskell
221 * thread. If CurrentTSO == NULL, then we're at the scheduler level.
224 /* The smallest stack size that makes any sense is:
225 * RESERVED_STACK_WORDS (so we can get back from the stack overflow)
226 * + sizeofW(StgStopFrame) (the stg_stop_thread_info frame)
227 * + 1 (the closure to enter)
229 * + 1 (spare slot req'd by stg_ap_v_ret)
231 * A thread with this stack will bomb immediately with a stack
232 * overflow, which will increase its stack size.
235 #define MIN_STACK_WORDS (RESERVED_STACK_WORDS + sizeofW(StgStopFrame) + 3)
242 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
243 * exists - earlier gccs apparently didn't.
248 static rtsBool ready_to_gc;
251 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
252 * in an MT setting, needed to signal that a worker thread shouldn't hang around
253 * in the scheduler when it is out of work.
255 static rtsBool shutting_down_scheduler = rtsFalse;
257 void addToBlockedQueue ( StgTSO *tso );
259 static void schedule ( StgMainThread *mainThread, Capability *initialCapability );
260 void interruptStgRts ( void );
262 static void detectBlackHoles ( void );
265 static void sched_belch(char *s, ...);
268 #if defined(RTS_SUPPORTS_THREADS)
269 /* ToDo: carefully document the invariants that go together
270 * with these synchronisation objects.
272 Mutex sched_mutex = INIT_MUTEX_VAR;
273 Mutex term_mutex = INIT_MUTEX_VAR;
276 * A heavyweight solution to the problem of protecting
277 * the thread_id from concurrent update.
279 Mutex thread_id_mutex = INIT_MUTEX_VAR;
283 static Condition gc_pending_cond = INIT_COND_VAR;
287 #endif /* RTS_SUPPORTS_THREADS */
291 rtsTime TimeOfLastYield;
292 rtsBool emitSchedule = rtsTrue;
296 static char *whatNext_strs[] = {
306 StgTSO * createSparkThread(rtsSpark spark);
307 StgTSO * activateSpark (rtsSpark spark);
311 * The thread state for the main thread.
312 // ToDo: check whether not needed any more
316 #if defined(RTS_SUPPORTS_THREADS)
317 static rtsBool startingWorkerThread = rtsFalse;
319 static void taskStart(void);
325 ACQUIRE_LOCK(&sched_mutex);
326 startingWorkerThread = rtsFalse;
327 waitForWorkCapability(&sched_mutex, &cap, NULL);
328 RELEASE_LOCK(&sched_mutex);
334 startSchedulerTaskIfNecessary(void)
336 if(run_queue_hd != END_TSO_QUEUE
337 || blocked_queue_hd != END_TSO_QUEUE
338 || sleeping_queue != END_TSO_QUEUE)
340 if(!startingWorkerThread)
341 { // we don't want to start another worker thread
342 // just because the last one hasn't yet reached the
343 // "waiting for capability" state
344 startingWorkerThread = rtsTrue;
345 startTask(taskStart);
351 //@node Main scheduling loop, Suspend and Resume, Prototypes, Main scheduling code
352 //@subsection Main scheduling loop
354 /* ---------------------------------------------------------------------------
355 Main scheduling loop.
357 We use round-robin scheduling, each thread returning to the
358 scheduler loop when one of these conditions is detected:
361 * timer expires (thread yields)
366 Locking notes: we acquire the scheduler lock once at the beginning
367 of the scheduler loop, and release it when
369 * running a thread, or
370 * waiting for work, or
371 * waiting for a GC to complete.
374 In a GranSim setup this loop iterates over the global event queue.
375 This revolves around the global event queue, which determines what
376 to do next. Therefore, it's more complicated than either the
377 concurrent or the parallel (GUM) setup.
380 GUM iterates over incoming messages.
381 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
382 and sends out a fish whenever it has nothing to do; in-between
383 doing the actual reductions (shared code below) it processes the
384 incoming messages and deals with delayed operations
385 (see PendingFetches).
386 This is not the ugliest code you could imagine, but it's bloody close.
388 ------------------------------------------------------------------------ */
391 schedule( StgMainThread *mainThread USED_WHEN_RTS_SUPPORTS_THREADS,
392 Capability *initialCapability )
395 Capability *cap = initialCapability;
396 StgThreadReturnCode ret;
404 rtsBool receivedFinish = rtsFalse;
406 nat tp_size, sp_size; // stats only
409 rtsBool was_interrupted = rtsFalse;
410 StgTSOWhatNext prev_what_next;
412 ACQUIRE_LOCK(&sched_mutex);
414 #if defined(RTS_SUPPORTS_THREADS)
415 /* in the threaded case, the capability is either passed in via the initialCapability
416 parameter, or initialized inside the scheduler loop */
419 fprintf(stderr,"### NEW SCHEDULER LOOP in os thread %u(%p)\n",
420 osThreadId(), osThreadId()));
422 fprintf(stderr,"### main thread: %p\n",mainThread));
424 fprintf(stderr,"### initial cap: %p\n",initialCapability));
426 /* simply initialise it in the non-threaded case */
427 grabCapability(&cap);
431 /* set up first event to get things going */
432 /* ToDo: assign costs for system setup and init MainTSO ! */
433 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
435 CurrentTSO, (StgClosure*)NULL, (rtsSpark*)NULL);
438 fprintf(stderr, "GRAN: Init CurrentTSO (in schedule) = %p\n", CurrentTSO);
439 G_TSO(CurrentTSO, 5));
441 if (RtsFlags.GranFlags.Light) {
442 /* Save current time; GranSim Light only */
443 CurrentTSO->gran.clock = CurrentTime[CurrentProc];
446 event = get_next_event();
448 while (event!=(rtsEvent*)NULL) {
449 /* Choose the processor with the next event */
450 CurrentProc = event->proc;
451 CurrentTSO = event->tso;
455 while (!receivedFinish) { /* set by processMessages */
456 /* when receiving PP_FINISH message */
463 IF_DEBUG(scheduler, printAllThreads());
465 #if defined(RTS_SUPPORTS_THREADS)
466 /* Check to see whether there are any worker threads
467 waiting to deposit external call results. If so,
468 yield our capability... if we have a capability, that is. */
470 yieldToReturningWorker(&sched_mutex, &cap,
471 mainThread ? &mainThread->bound_thread_cond : NULL);
473 /* If we do not currently hold a capability, we wait for one */
476 waitForWorkCapability(&sched_mutex, &cap,
477 mainThread ? &mainThread->bound_thread_cond : NULL);
478 IF_DEBUG(scheduler, sched_belch("worker thread (osthread %p): got cap",
483 /* If we're interrupted (the user pressed ^C, or some other
484 * termination condition occurred), kill all the currently running
488 IF_DEBUG(scheduler, sched_belch("interrupted"));
489 interrupted = rtsFalse;
490 was_interrupted = rtsTrue;
491 #if defined(RTS_SUPPORTS_THREADS)
492 // In the threaded RTS, deadlock detection doesn't work,
493 // so just exit right away.
494 prog_belch("interrupted");
495 releaseCapability(cap);
496 RELEASE_LOCK(&sched_mutex);
497 shutdownHaskellAndExit(EXIT_SUCCESS);
503 /* Go through the list of main threads and wake up any
504 * clients whose computations have finished. ToDo: this
505 * should be done more efficiently without a linear scan
506 * of the main threads list, somehow...
508 #if defined(RTS_SUPPORTS_THREADS)
510 StgMainThread *m, **prev;
511 prev = &main_threads;
512 for (m = main_threads; m != NULL; prev = &m->link, m = m->link) {
513 if (m->tso->what_next == ThreadComplete
514 || m->tso->what_next == ThreadKilled)
518 if(m->tso->what_next == ThreadComplete)
522 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
523 *(m->ret) = (StgClosure *)m->tso->sp[1];
535 m->stat = Interrupted;
545 removeThreadLabel((StgWord)m->tso);
547 releaseCapability(cap);
548 RELEASE_LOCK(&sched_mutex);
553 // The current OS thread can not handle the fact that the Haskell
554 // thread "m" has ended.
555 // "m" is bound; the scheduler loop in it's bound OS thread has
556 // to return, so let's pass our capability directly to that thread.
557 passCapability(&sched_mutex, cap, &m->bound_thread_cond);
564 if(!cap) // If we gave our capability away,
565 continue; // go to the top to get it back
567 #else /* not threaded */
570 /* in GUM do this only on the Main PE */
573 /* If our main thread has finished or been killed, return.
576 StgMainThread *m = main_threads;
577 if (m->tso->what_next == ThreadComplete
578 || m->tso->what_next == ThreadKilled) {
580 removeThreadLabel((StgWord)m->tso);
582 main_threads = main_threads->link;
583 if (m->tso->what_next == ThreadComplete) {
584 // We finished successfully, fill in the return value
585 // NOTE: return val is tso->sp[1] (see StgStartup.hc)
586 if (m->ret) { *(m->ret) = (StgClosure *)m->tso->sp[1]; };
590 if (m->ret) { *(m->ret) = NULL; };
591 if (was_interrupted) {
592 m->stat = Interrupted;
602 /* Top up the run queue from our spark pool. We try to make the
603 * number of threads in the run queue equal to the number of
606 * Disable spark support in SMP for now, non-essential & requires
607 * a little bit of work to make it compile cleanly. -- sof 1/02.
609 #if 0 /* defined(SMP) */
611 nat n = getFreeCapabilities();
612 StgTSO *tso = run_queue_hd;
614 /* Count the run queue */
615 while (n > 0 && tso != END_TSO_QUEUE) {
622 spark = findSpark(rtsFalse);
624 break; /* no more sparks in the pool */
626 /* I'd prefer this to be done in activateSpark -- HWL */
627 /* tricky - it needs to hold the scheduler lock and
628 * not try to re-acquire it -- SDM */
629 createSparkThread(spark);
631 sched_belch("==^^ turning spark of closure %p into a thread",
632 (StgClosure *)spark));
635 /* We need to wake up the other tasks if we just created some
638 if (getFreeCapabilities() - n > 1) {
639 signalCondition( &thread_ready_cond );
644 /* check for signals each time around the scheduler */
645 #if defined(RTS_USER_SIGNALS)
646 if (signals_pending()) {
647 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
648 startSignalHandlers();
649 ACQUIRE_LOCK(&sched_mutex);
653 /* Check whether any waiting threads need to be woken up. If the
654 * run queue is empty, and there are no other tasks running, we
655 * can wait indefinitely for something to happen.
657 if ( !EMPTY_QUEUE(blocked_queue_hd) || !EMPTY_QUEUE(sleeping_queue)
658 #if defined(RTS_SUPPORTS_THREADS) && !defined(SMP)
663 awaitEvent( EMPTY_RUN_QUEUE()
665 && allFreeCapabilities()
669 /* we can be interrupted while waiting for I/O... */
670 if (interrupted) continue;
673 * Detect deadlock: when we have no threads to run, there are no
674 * threads waiting on I/O or sleeping, and all the other tasks are
675 * waiting for work, we must have a deadlock of some description.
677 * We first try to find threads blocked on themselves (ie. black
678 * holes), and generate NonTermination exceptions where necessary.
680 * If no threads are black holed, we have a deadlock situation, so
681 * inform all the main threads.
683 #if !defined(PAR) && !defined(RTS_SUPPORTS_THREADS)
684 if ( EMPTY_THREAD_QUEUES()
685 #if defined(RTS_SUPPORTS_THREADS)
686 && EMPTY_QUEUE(suspended_ccalling_threads)
689 && allFreeCapabilities()
693 IF_DEBUG(scheduler, sched_belch("deadlocked, forcing major GC..."));
694 #if defined(THREADED_RTS)
695 /* and SMP mode ..? */
696 releaseCapability(cap);
698 // Garbage collection can release some new threads due to
699 // either (a) finalizers or (b) threads resurrected because
700 // they are about to be send BlockedOnDeadMVar. Any threads
701 // thus released will be immediately runnable.
702 GarbageCollect(GetRoots,rtsTrue);
704 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
707 sched_belch("still deadlocked, checking for black holes..."));
710 if ( !EMPTY_RUN_QUEUE() ) { goto not_deadlocked; }
712 #if defined(RTS_USER_SIGNALS)
713 /* If we have user-installed signal handlers, then wait
714 * for signals to arrive rather then bombing out with a
717 #if defined(RTS_SUPPORTS_THREADS)
718 if ( 0 ) { /* hmm..what to do? Simply stop waiting for
719 a signal with no runnable threads (or I/O
720 suspended ones) leads nowhere quick.
721 For now, simply shut down when we reach this
724 ToDo: define precisely under what conditions
725 the Scheduler should shut down in an MT setting.
728 if ( anyUserHandlers() ) {
731 sched_belch("still deadlocked, waiting for signals..."));
735 // we might be interrupted...
736 if (interrupted) { continue; }
738 if (signals_pending()) {
739 RELEASE_LOCK(&sched_mutex);
740 startSignalHandlers();
741 ACQUIRE_LOCK(&sched_mutex);
743 ASSERT(!EMPTY_RUN_QUEUE());
748 /* Probably a real deadlock. Send the current main thread the
749 * Deadlock exception (or in the SMP build, send *all* main
750 * threads the deadlock exception, since none of them can make
755 #if defined(RTS_SUPPORTS_THREADS)
756 for (m = main_threads; m != NULL; m = m->link) {
757 switch (m->tso->why_blocked) {
758 case BlockedOnBlackHole:
759 raiseAsync(m->tso, (StgClosure *)NonTermination_closure);
761 case BlockedOnException:
763 raiseAsync(m->tso, (StgClosure *)Deadlock_closure);
766 barf("deadlock: main thread blocked in a strange way");
771 switch (m->tso->why_blocked) {
772 case BlockedOnBlackHole:
773 raiseAsync(m->tso, (StgClosure *)NonTermination_closure);
775 case BlockedOnException:
777 raiseAsync(m->tso, (StgClosure *)Deadlock_closure);
780 barf("deadlock: main thread blocked in a strange way");
785 #if defined(RTS_SUPPORTS_THREADS)
786 /* ToDo: revisit conditions (and mechanism) for shutting
787 down a multi-threaded world */
788 IF_DEBUG(scheduler, sched_belch("all done, i think...shutting down."));
789 RELEASE_LOCK(&sched_mutex);
796 #elif defined(RTS_SUPPORTS_THREADS)
797 /* ToDo: add deadlock detection in threaded RTS */
799 /* ToDo: add deadlock detection in GUM (similar to SMP) -- HWL */
803 /* If there's a GC pending, don't do anything until it has
807 IF_DEBUG(scheduler,sched_belch("waiting for GC"));
808 waitCondition( &gc_pending_cond, &sched_mutex );
812 #if defined(RTS_SUPPORTS_THREADS)
814 /* block until we've got a thread on the run queue and a free
818 if ( EMPTY_RUN_QUEUE() ) {
819 /* Give up our capability */
820 releaseCapability(cap);
822 /* If we're in the process of shutting down (& running the
823 * a batch of finalisers), don't wait around.
825 if ( shutting_down_scheduler ) {
826 RELEASE_LOCK(&sched_mutex);
829 IF_DEBUG(scheduler, sched_belch("thread %d: waiting for work", osThreadId()));
830 waitForWorkCapability(&sched_mutex, &cap, rtsTrue);
831 IF_DEBUG(scheduler, sched_belch("thread %d: work now available", osThreadId()));
834 if ( EMPTY_RUN_QUEUE() ) {
835 continue; // nothing to do
841 if (RtsFlags.GranFlags.Light)
842 GranSimLight_enter_system(event, &ActiveTSO); // adjust ActiveTSO etc
844 /* adjust time based on time-stamp */
845 if (event->time > CurrentTime[CurrentProc] &&
846 event->evttype != ContinueThread)
847 CurrentTime[CurrentProc] = event->time;
849 /* Deal with the idle PEs (may issue FindWork or MoveSpark events) */
850 if (!RtsFlags.GranFlags.Light)
853 IF_DEBUG(gran, fprintf(stderr, "GRAN: switch by event-type\n"));
855 /* main event dispatcher in GranSim */
856 switch (event->evttype) {
857 /* Should just be continuing execution */
859 IF_DEBUG(gran, fprintf(stderr, "GRAN: doing ContinueThread\n"));
860 /* ToDo: check assertion
861 ASSERT(run_queue_hd != (StgTSO*)NULL &&
862 run_queue_hd != END_TSO_QUEUE);
864 /* Ignore ContinueThreads for fetching threads (if synchr comm) */
865 if (!RtsFlags.GranFlags.DoAsyncFetch &&
866 procStatus[CurrentProc]==Fetching) {
867 belch("ghuH: Spurious ContinueThread while Fetching ignored; TSO %d (%p) [PE %d]",
868 CurrentTSO->id, CurrentTSO, CurrentProc);
871 /* Ignore ContinueThreads for completed threads */
872 if (CurrentTSO->what_next == ThreadComplete) {
873 belch("ghuH: found a ContinueThread event for completed thread %d (%p) [PE %d] (ignoring ContinueThread)",
874 CurrentTSO->id, CurrentTSO, CurrentProc);
877 /* Ignore ContinueThreads for threads that are being migrated */
878 if (PROCS(CurrentTSO)==Nowhere) {
879 belch("ghuH: trying to run the migrating TSO %d (%p) [PE %d] (ignoring ContinueThread)",
880 CurrentTSO->id, CurrentTSO, CurrentProc);
883 /* The thread should be at the beginning of the run queue */
884 if (CurrentTSO!=run_queue_hds[CurrentProc]) {
885 belch("ghuH: TSO %d (%p) [PE %d] is not at the start of the run_queue when doing a ContinueThread",
886 CurrentTSO->id, CurrentTSO, CurrentProc);
887 break; // run the thread anyway
890 new_event(proc, proc, CurrentTime[proc],
892 (StgTSO*)NULL, (StgClosure*)NULL, (rtsSpark*)NULL);
894 */ /* Catches superfluous CONTINUEs -- should be unnecessary */
895 break; // now actually run the thread; DaH Qu'vam yImuHbej
898 do_the_fetchnode(event);
899 goto next_thread; /* handle next event in event queue */
902 do_the_globalblock(event);
903 goto next_thread; /* handle next event in event queue */
906 do_the_fetchreply(event);
907 goto next_thread; /* handle next event in event queue */
909 case UnblockThread: /* Move from the blocked queue to the tail of */
910 do_the_unblock(event);
911 goto next_thread; /* handle next event in event queue */
913 case ResumeThread: /* Move from the blocked queue to the tail of */
914 /* the runnable queue ( i.e. Qu' SImqa'lu') */
915 event->tso->gran.blocktime +=
916 CurrentTime[CurrentProc] - event->tso->gran.blockedat;
917 do_the_startthread(event);
918 goto next_thread; /* handle next event in event queue */
921 do_the_startthread(event);
922 goto next_thread; /* handle next event in event queue */
925 do_the_movethread(event);
926 goto next_thread; /* handle next event in event queue */
929 do_the_movespark(event);
930 goto next_thread; /* handle next event in event queue */
933 do_the_findwork(event);
934 goto next_thread; /* handle next event in event queue */
937 barf("Illegal event type %u\n", event->evttype);
940 /* This point was scheduler_loop in the old RTS */
942 IF_DEBUG(gran, belch("GRAN: after main switch"));
944 TimeOfLastEvent = CurrentTime[CurrentProc];
945 TimeOfNextEvent = get_time_of_next_event();
946 IgnoreEvents=(TimeOfNextEvent==0); // HWL HACK
947 // CurrentTSO = ThreadQueueHd;
949 IF_DEBUG(gran, belch("GRAN: time of next event is: %ld",
952 if (RtsFlags.GranFlags.Light)
953 GranSimLight_leave_system(event, &ActiveTSO);
955 EndOfTimeSlice = CurrentTime[CurrentProc]+RtsFlags.GranFlags.time_slice;
958 belch("GRAN: end of time-slice is %#lx", EndOfTimeSlice));
960 /* in a GranSim setup the TSO stays on the run queue */
962 /* Take a thread from the run queue. */
963 POP_RUN_QUEUE(t); // take_off_run_queue(t);
966 fprintf(stderr, "GRAN: About to run current thread, which is\n");
969 context_switch = 0; // turned on via GranYield, checking events and time slice
972 DumpGranEvent(GR_SCHEDULE, t));
974 procStatus[CurrentProc] = Busy;
977 if (PendingFetches != END_BF_QUEUE) {
981 /* ToDo: phps merge with spark activation above */
982 /* check whether we have local work and send requests if we have none */
983 if (EMPTY_RUN_QUEUE()) { /* no runnable threads */
984 /* :-[ no local threads => look out for local sparks */
985 /* the spark pool for the current PE */
986 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
987 if (advisory_thread_count < RtsFlags.ParFlags.maxThreads &&
988 pool->hd < pool->tl) {
990 * ToDo: add GC code check that we really have enough heap afterwards!!
992 * If we're here (no runnable threads) and we have pending
993 * sparks, we must have a space problem. Get enough space
994 * to turn one of those pending sparks into a
998 spark = findSpark(rtsFalse); /* get a spark */
999 if (spark != (rtsSpark) NULL) {
1000 tso = activateSpark(spark); /* turn the spark into a thread */
1001 IF_PAR_DEBUG(schedule,
1002 belch("==== schedule: Created TSO %d (%p); %d threads active",
1003 tso->id, tso, advisory_thread_count));
1005 if (tso==END_TSO_QUEUE) { /* failed to activate spark->back to loop */
1006 belch("==^^ failed to activate spark");
1008 } /* otherwise fall through & pick-up new tso */
1010 IF_PAR_DEBUG(verbose,
1011 belch("==^^ no local sparks (spark pool contains only NFs: %d)",
1012 spark_queue_len(pool)));
1017 /* If we still have no work we need to send a FISH to get a spark
1020 if (EMPTY_RUN_QUEUE()) {
1021 /* =8-[ no local sparks => look for work on other PEs */
1023 * We really have absolutely no work. Send out a fish
1024 * (there may be some out there already), and wait for
1025 * something to arrive. We clearly can't run any threads
1026 * until a SCHEDULE or RESUME arrives, and so that's what
1027 * we're hoping to see. (Of course, we still have to
1028 * respond to other types of messages.)
1030 TIME now = msTime() /*CURRENT_TIME*/;
1031 IF_PAR_DEBUG(verbose,
1032 belch("-- now=%ld", now));
1033 IF_PAR_DEBUG(verbose,
1034 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1035 (last_fish_arrived_at!=0 &&
1036 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay > now)) {
1037 belch("--$$ delaying FISH until %ld (last fish %ld, delay %ld, now %ld)",
1038 last_fish_arrived_at+RtsFlags.ParFlags.fishDelay,
1039 last_fish_arrived_at,
1040 RtsFlags.ParFlags.fishDelay, now);
1043 if (outstandingFishes < RtsFlags.ParFlags.maxFishes &&
1044 (last_fish_arrived_at==0 ||
1045 (last_fish_arrived_at+RtsFlags.ParFlags.fishDelay <= now))) {
1046 /* outstandingFishes is set in sendFish, processFish;
1047 avoid flooding system with fishes via delay */
1049 sendFish(pe, mytid, NEW_FISH_AGE, NEW_FISH_HISTORY,
1052 // Global statistics: count no. of fishes
1053 if (RtsFlags.ParFlags.ParStats.Global &&
1054 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
1055 globalParStats.tot_fish_mess++;
1059 receivedFinish = processMessages();
1062 } else if (PacketsWaiting()) { /* Look for incoming messages */
1063 receivedFinish = processMessages();
1066 /* Now we are sure that we have some work available */
1067 ASSERT(run_queue_hd != END_TSO_QUEUE);
1069 /* Take a thread from the run queue, if we have work */
1070 POP_RUN_QUEUE(t); // take_off_run_queue(END_TSO_QUEUE);
1071 IF_DEBUG(sanity,checkTSO(t));
1073 /* ToDo: write something to the log-file
1074 if (RTSflags.ParFlags.granSimStats && !sameThread)
1075 DumpGranEvent(GR_SCHEDULE, RunnableThreadsHd);
1079 /* the spark pool for the current PE */
1080 pool = &(MainRegTable.rSparks); // generalise to cap = &MainRegTable
1083 belch("--=^ %d threads, %d sparks on [%#x]",
1084 run_queue_len(), spark_queue_len(pool), CURRENT_PROC));
1087 if (0 && RtsFlags.ParFlags.ParStats.Full &&
1088 t && LastTSO && t->id != LastTSO->id &&
1089 LastTSO->why_blocked == NotBlocked &&
1090 LastTSO->what_next != ThreadComplete) {
1091 // if previously scheduled TSO not blocked we have to record the context switch
1092 DumpVeryRawGranEvent(TimeOfLastYield, CURRENT_PROC, CURRENT_PROC,
1093 GR_DESCHEDULE, LastTSO, (StgClosure *)NULL, 0, 0);
1096 if (RtsFlags.ParFlags.ParStats.Full &&
1097 (emitSchedule /* forced emit */ ||
1098 (t && LastTSO && t->id != LastTSO->id))) {
1100 we are running a different TSO, so write a schedule event to log file
1101 NB: If we use fair scheduling we also have to write a deschedule
1102 event for LastTSO; with unfair scheduling we know that the
1103 previous tso has blocked whenever we switch to another tso, so
1104 we don't need it in GUM for now
1106 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1107 GR_SCHEDULE, t, (StgClosure *)NULL, 0, 0);
1108 emitSchedule = rtsFalse;
1112 #else /* !GRAN && !PAR */
1114 /* grab a thread from the run queue */
1115 ASSERT(run_queue_hd != END_TSO_QUEUE);
1117 // Sanity check the thread we're about to run. This can be
1118 // expensive if there is lots of thread switching going on...
1119 IF_DEBUG(sanity,checkTSO(t));
1125 for(m = main_threads; m; m = m->link)
1136 fprintf(stderr,"### Running TSO %p in bound OS thread %u\n",
1138 // yes, the Haskell thread is bound to the current native thread
1143 fprintf(stderr,"### TSO %p bound to other OS thread than %u\n",
1145 // no, bound to a different Haskell thread: pass to that thread
1146 PUSH_ON_RUN_QUEUE(t);
1147 passCapability(&sched_mutex,cap,&m->bound_thread_cond);
1154 // The thread we want to run is not bound.
1155 if(mainThread == NULL)
1158 fprintf(stderr,"### Running TSO %p in worker OS thread %u\n",
1160 // if we are a worker thread,
1161 // we may run it here
1166 fprintf(stderr,"### TSO %p is not appropriate for main thread %p in OS thread %u\n",
1167 t, mainThread, osThreadId()));
1168 // no, the current native thread is bound to a different
1169 // Haskell thread, so pass it to any worker thread
1170 PUSH_ON_RUN_QUEUE(t);
1171 passCapabilityToWorker(&sched_mutex, cap);
1179 cap->r.rCurrentTSO = t;
1181 /* context switches are now initiated by the timer signal, unless
1182 * the user specified "context switch as often as possible", with
1185 if ((RtsFlags.ConcFlags.ctxtSwitchTicks == 0
1186 && (run_queue_hd != END_TSO_QUEUE
1187 || blocked_queue_hd != END_TSO_QUEUE
1188 || sleeping_queue != END_TSO_QUEUE)))
1195 RELEASE_LOCK(&sched_mutex);
1197 IF_DEBUG(scheduler, sched_belch("-->> running thread %ld %s ...",
1198 t->id, whatNext_strs[t->what_next]));
1201 startHeapProfTimer();
1204 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1205 /* Run the current thread
1207 prev_what_next = t->what_next;
1208 switch (prev_what_next) {
1210 case ThreadComplete:
1211 /* Thread already finished, return to scheduler. */
1212 ret = ThreadFinished;
1215 errno = t->saved_errno;
1216 ret = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
1217 t->saved_errno = errno;
1219 case ThreadInterpret:
1220 ret = interpretBCO(cap);
1223 barf("schedule: invalid what_next field");
1225 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1227 /* Costs for the scheduler are assigned to CCS_SYSTEM */
1229 stopHeapProfTimer();
1233 ACQUIRE_LOCK(&sched_mutex);
1235 #ifdef RTS_SUPPORTS_THREADS
1236 IF_DEBUG(scheduler,fprintf(stderr,"scheduler (task %p): ", osThreadId()););
1237 #elif !defined(GRAN) && !defined(PAR)
1238 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: "););
1240 t = cap->r.rCurrentTSO;
1243 /* HACK 675: if the last thread didn't yield, make sure to print a
1244 SCHEDULE event to the log file when StgRunning the next thread, even
1245 if it is the same one as before */
1247 TimeOfLastYield = CURRENT_TIME;
1253 IF_DEBUG(gran, DumpGranEvent(GR_DESCHEDULE, t));
1254 globalGranStats.tot_heapover++;
1256 globalParStats.tot_heapover++;
1259 // did the task ask for a large block?
1260 if (cap->r.rHpAlloc > BLOCK_SIZE_W) {
1261 // if so, get one and push it on the front of the nursery.
1265 blocks = (nat)BLOCK_ROUND_UP(cap->r.rHpAlloc * sizeof(W_)) / BLOCK_SIZE;
1267 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: requesting a large block (size %d)",
1268 t->id, whatNext_strs[t->what_next], blocks));
1270 // don't do this if it would push us over the
1271 // alloc_blocks_lim limit; we'll GC first.
1272 if (alloc_blocks + blocks < alloc_blocks_lim) {
1274 alloc_blocks += blocks;
1275 bd = allocGroup( blocks );
1277 // link the new group into the list
1278 bd->link = cap->r.rCurrentNursery;
1279 bd->u.back = cap->r.rCurrentNursery->u.back;
1280 if (cap->r.rCurrentNursery->u.back != NULL) {
1281 cap->r.rCurrentNursery->u.back->link = bd;
1283 ASSERT(g0s0->blocks == cap->r.rCurrentNursery &&
1284 g0s0->blocks == cap->r.rNursery);
1285 cap->r.rNursery = g0s0->blocks = bd;
1287 cap->r.rCurrentNursery->u.back = bd;
1289 // initialise it as a nursery block. We initialise the
1290 // step, gen_no, and flags field of *every* sub-block in
1291 // this large block, because this is easier than making
1292 // sure that we always find the block head of a large
1293 // block whenever we call Bdescr() (eg. evacuate() and
1294 // isAlive() in the GC would both have to do this, at
1298 for (x = bd; x < bd + blocks; x++) {
1305 // don't forget to update the block count in g0s0.
1306 g0s0->n_blocks += blocks;
1307 // This assert can be a killer if the app is doing lots
1308 // of large block allocations.
1309 ASSERT(countBlocks(g0s0->blocks) == g0s0->n_blocks);
1311 // now update the nursery to point to the new block
1312 cap->r.rCurrentNursery = bd;
1314 // we might be unlucky and have another thread get on the
1315 // run queue before us and steal the large block, but in that
1316 // case the thread will just end up requesting another large
1318 PUSH_ON_RUN_QUEUE(t);
1323 /* make all the running tasks block on a condition variable,
1324 * maybe set context_switch and wait till they all pile in,
1325 * then have them wait on a GC condition variable.
1327 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped: HeapOverflow",
1328 t->id, whatNext_strs[t->what_next]));
1331 ASSERT(!is_on_queue(t,CurrentProc));
1333 /* Currently we emit a DESCHEDULE event before GC in GUM.
1334 ToDo: either add separate event to distinguish SYSTEM time from rest
1335 or just nuke this DESCHEDULE (and the following SCHEDULE) */
1336 if (0 && RtsFlags.ParFlags.ParStats.Full) {
1337 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
1338 GR_DESCHEDULE, t, (StgClosure *)NULL, 0, 0);
1339 emitSchedule = rtsTrue;
1343 ready_to_gc = rtsTrue;
1344 context_switch = 1; /* stop other threads ASAP */
1345 PUSH_ON_RUN_QUEUE(t);
1346 /* actual GC is done at the end of the while loop */
1352 DumpGranEvent(GR_DESCHEDULE, t));
1353 globalGranStats.tot_stackover++;
1356 // DumpGranEvent(GR_DESCHEDULE, t);
1357 globalParStats.tot_stackover++;
1359 IF_DEBUG(scheduler,belch("--<< thread %ld (%s) stopped, StackOverflow",
1360 t->id, whatNext_strs[t->what_next]));
1361 /* just adjust the stack for this thread, then pop it back
1367 /* enlarge the stack */
1368 StgTSO *new_t = threadStackOverflow(t);
1370 /* This TSO has moved, so update any pointers to it from the
1371 * main thread stack. It better not be on any other queues...
1372 * (it shouldn't be).
1374 for (m = main_threads; m != NULL; m = m->link) {
1379 threadPaused(new_t);
1380 PUSH_ON_RUN_QUEUE(new_t);
1384 case ThreadYielding:
1387 DumpGranEvent(GR_DESCHEDULE, t));
1388 globalGranStats.tot_yields++;
1391 // DumpGranEvent(GR_DESCHEDULE, t);
1392 globalParStats.tot_yields++;
1394 /* put the thread back on the run queue. Then, if we're ready to
1395 * GC, check whether this is the last task to stop. If so, wake
1396 * up the GC thread. getThread will block during a GC until the
1400 if (t->what_next != prev_what_next) {
1401 belch("--<< thread %ld (%s) stopped to switch evaluators",
1402 t->id, whatNext_strs[t->what_next]);
1404 belch("--<< thread %ld (%s) stopped, yielding",
1405 t->id, whatNext_strs[t->what_next]);
1410 //belch("&& Doing sanity check on yielding TSO %ld.", t->id);
1412 ASSERT(t->link == END_TSO_QUEUE);
1414 // Shortcut if we're just switching evaluators: don't bother
1415 // doing stack squeezing (which can be expensive), just run the
1417 if (t->what_next != prev_what_next) {
1424 ASSERT(!is_on_queue(t,CurrentProc));
1427 //belch("&& Doing sanity check on all ThreadQueues (and their TSOs).");
1428 checkThreadQsSanity(rtsTrue));
1432 if (RtsFlags.ParFlags.doFairScheduling) {
1433 /* this does round-robin scheduling; good for concurrency */
1434 APPEND_TO_RUN_QUEUE(t);
1436 /* this does unfair scheduling; good for parallelism */
1437 PUSH_ON_RUN_QUEUE(t);
1440 // this does round-robin scheduling; good for concurrency
1441 APPEND_TO_RUN_QUEUE(t);
1445 /* add a ContinueThread event to actually process the thread */
1446 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1448 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1450 belch("GRAN: eventq and runnableq after adding yielded thread to queue again:");
1459 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p [PE %d] with BQ: ",
1460 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)));
1461 if (t->block_info.closure!=(StgClosure*)NULL) print_bq(t->block_info.closure));
1463 // ??? needed; should emit block before
1465 DumpGranEvent(GR_DESCHEDULE, t));
1466 prune_eventq(t, (StgClosure *)NULL); // prune ContinueThreads for t
1469 ASSERT(procStatus[CurrentProc]==Busy ||
1470 ((procStatus[CurrentProc]==Fetching) &&
1471 (t->block_info.closure!=(StgClosure*)NULL)));
1472 if (run_queue_hds[CurrentProc] == END_TSO_QUEUE &&
1473 !(!RtsFlags.GranFlags.DoAsyncFetch &&
1474 procStatus[CurrentProc]==Fetching))
1475 procStatus[CurrentProc] = Idle;
1479 belch("--<< thread %ld (%p; %s) stopped, blocking on node %p with BQ: ",
1480 t->id, t, whatNext_strs[t->what_next], t->block_info.closure));
1483 if (t->block_info.closure!=(StgClosure*)NULL)
1484 print_bq(t->block_info.closure));
1486 /* Send a fetch (if BlockedOnGA) and dump event to log file */
1489 /* whatever we schedule next, we must log that schedule */
1490 emitSchedule = rtsTrue;
1493 /* don't need to do anything. Either the thread is blocked on
1494 * I/O, in which case we'll have called addToBlockedQueue
1495 * previously, or it's blocked on an MVar or Blackhole, in which
1496 * case it'll be on the relevant queue already.
1499 fprintf(stderr, "--<< thread %d (%s) stopped: ",
1500 t->id, whatNext_strs[t->what_next]);
1501 printThreadBlockage(t);
1502 fprintf(stderr, "\n"));
1504 /* Only for dumping event to log file
1505 ToDo: do I need this in GranSim, too?
1512 case ThreadFinished:
1513 /* Need to check whether this was a main thread, and if so, signal
1514 * the task that started it with the return value. If we have no
1515 * more main threads, we probably need to stop all the tasks until
1518 /* We also end up here if the thread kills itself with an
1519 * uncaught exception, see Exception.hc.
1521 IF_DEBUG(scheduler,belch("--++ thread %d (%s) finished",
1522 t->id, whatNext_strs[t->what_next]));
1524 endThread(t, CurrentProc); // clean-up the thread
1526 /* For now all are advisory -- HWL */
1527 //if(t->priority==AdvisoryPriority) ??
1528 advisory_thread_count--;
1531 if(t->dist.priority==RevalPriority)
1535 if (RtsFlags.ParFlags.ParStats.Full &&
1536 !RtsFlags.ParFlags.ParStats.Suppressed)
1537 DumpEndEvent(CURRENT_PROC, t, rtsFalse /* not mandatory */);
1542 barf("schedule: invalid thread return code %d", (int)ret);
1546 // When we have +RTS -i0 and we're heap profiling, do a census at
1547 // every GC. This lets us get repeatable runs for debugging.
1548 if (performHeapProfile ||
1549 (RtsFlags.ProfFlags.profileInterval==0 &&
1550 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1551 GarbageCollect(GetRoots, rtsTrue);
1553 performHeapProfile = rtsFalse;
1554 ready_to_gc = rtsFalse; // we already GC'd
1560 && allFreeCapabilities()
1563 /* everybody back, start the GC.
1564 * Could do it in this thread, or signal a condition var
1565 * to do it in another thread. Either way, we need to
1566 * broadcast on gc_pending_cond afterward.
1568 #if defined(RTS_SUPPORTS_THREADS)
1569 IF_DEBUG(scheduler,sched_belch("doing GC"));
1571 GarbageCollect(GetRoots,rtsFalse);
1572 ready_to_gc = rtsFalse;
1574 broadcastCondition(&gc_pending_cond);
1577 /* add a ContinueThread event to continue execution of current thread */
1578 new_event(CurrentProc, CurrentProc, CurrentTime[CurrentProc],
1580 t, (StgClosure*)NULL, (rtsSpark*)NULL);
1582 fprintf(stderr, "GRAN: eventq and runnableq after Garbage collection:\n");
1590 IF_GRAN_DEBUG(unused,
1591 print_eventq(EventHd));
1593 event = get_next_event();
1596 /* ToDo: wait for next message to arrive rather than busy wait */
1599 } /* end of while(1) */
1601 IF_PAR_DEBUG(verbose,
1602 belch("== Leaving schedule() after having received Finish"));
1605 /* ---------------------------------------------------------------------------
1606 * rtsSupportsBoundThreads(): is the RTS built to support bound threads?
1607 * used by Control.Concurrent for error checking.
1608 * ------------------------------------------------------------------------- */
1611 rtsSupportsBoundThreads(void)
1620 /* ---------------------------------------------------------------------------
1621 * isThreadBound(tso): check whether tso is bound to an OS thread.
1622 * ------------------------------------------------------------------------- */
1625 isThreadBound(StgTSO* tso USED_IN_THREADED_RTS)
1629 for(m = main_threads; m; m = m->link)
1638 /* ---------------------------------------------------------------------------
1639 * Singleton fork(). Do not copy any running threads.
1640 * ------------------------------------------------------------------------- */
1643 deleteThreadImmediately(StgTSO *tso);
1646 forkProcess(HsStablePtr *entry)
1648 #ifndef mingw32_TARGET_OS
1654 IF_DEBUG(scheduler,sched_belch("forking!"));
1655 rts_lock(); // This not only acquires sched_mutex, it also
1656 // makes sure that no other threads are running
1660 if (pid) { /* parent */
1662 /* just return the pid */
1666 } else { /* child */
1669 // delete all threads
1670 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1672 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1675 // don't allow threads to catch the ThreadKilled exception
1676 deleteThreadImmediately(t);
1679 // wipe the main thread list
1680 while((m = main_threads) != NULL) {
1681 main_threads = m->link;
1683 closeCondition(&m->bound_thread_cond);
1688 #ifdef RTS_SUPPORTS_THREADS
1689 resetTaskManagerAfterFork(); // tell startTask() and friends that
1690 startingWorkerThread = rtsFalse; // we have no worker threads any more
1691 resetWorkerWakeupPipeAfterFork();
1694 rc = rts_evalStableIO(entry, NULL); // run the action
1695 rts_checkSchedStatus("forkProcess",rc);
1699 hs_exit(); // clean up and exit
1703 barf("forkProcess#: primop not implemented for mingw32, sorry!\n");
1705 #endif /* mingw32 */
1708 /* ---------------------------------------------------------------------------
1709 * deleteAllThreads(): kill all the live threads.
1711 * This is used when we catch a user interrupt (^C), before performing
1712 * any necessary cleanups and running finalizers.
1714 * Locks: sched_mutex held.
1715 * ------------------------------------------------------------------------- */
1718 deleteAllThreads ( void )
1721 IF_DEBUG(scheduler,sched_belch("deleting all threads"));
1722 for (t = all_threads; t != END_TSO_QUEUE; t = next) {
1723 next = t->global_link;
1726 run_queue_hd = run_queue_tl = END_TSO_QUEUE;
1727 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
1728 sleeping_queue = END_TSO_QUEUE;
1731 /* startThread and insertThread are now in GranSim.c -- HWL */
1734 //@node Suspend and Resume, Run queue code, Main scheduling loop, Main scheduling code
1735 //@subsection Suspend and Resume
1737 /* ---------------------------------------------------------------------------
1738 * Suspending & resuming Haskell threads.
1740 * When making a "safe" call to C (aka _ccall_GC), the task gives back
1741 * its capability before calling the C function. This allows another
1742 * task to pick up the capability and carry on running Haskell
1743 * threads. It also means that if the C call blocks, it won't lock
1746 * The Haskell thread making the C call is put to sleep for the
1747 * duration of the call, on the susepended_ccalling_threads queue. We
1748 * give out a token to the task, which it can use to resume the thread
1749 * on return from the C function.
1750 * ------------------------------------------------------------------------- */
1753 suspendThread( StgRegTable *reg,
1755 #if !defined(RTS_SUPPORTS_THREADS) && !defined(DEBUG)
1762 int saved_errno = errno;
1764 /* assume that *reg is a pointer to the StgRegTable part
1767 cap = (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
1769 ACQUIRE_LOCK(&sched_mutex);
1772 sched_belch("thread %d did a _ccall_gc (is_concurrent: %d)", cap->r.rCurrentTSO->id,concCall));
1774 // XXX this might not be necessary --SDM
1775 cap->r.rCurrentTSO->what_next = ThreadRunGHC;
1777 threadPaused(cap->r.rCurrentTSO);
1778 cap->r.rCurrentTSO->link = suspended_ccalling_threads;
1779 suspended_ccalling_threads = cap->r.rCurrentTSO;
1781 #if defined(RTS_SUPPORTS_THREADS)
1782 if(cap->r.rCurrentTSO->blocked_exceptions == NULL)
1784 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall;
1785 cap->r.rCurrentTSO->blocked_exceptions = END_TSO_QUEUE;
1789 cap->r.rCurrentTSO->why_blocked = BlockedOnCCall_NoUnblockExc;
1793 /* Use the thread ID as the token; it should be unique */
1794 tok = cap->r.rCurrentTSO->id;
1796 /* Hand back capability */
1797 releaseCapability(cap);
1799 #if defined(RTS_SUPPORTS_THREADS)
1800 /* Preparing to leave the RTS, so ensure there's a native thread/task
1801 waiting to take over.
1803 IF_DEBUG(scheduler, sched_belch("worker thread (%d, osthread %p): leaving RTS", tok, osThreadId()));
1806 /* Other threads _might_ be available for execution; signal this */
1808 RELEASE_LOCK(&sched_mutex);
1810 errno = saved_errno;
1815 resumeThread( StgInt tok,
1816 rtsBool concCall STG_UNUSED )
1818 StgTSO *tso, **prev;
1820 int saved_errno = errno;
1822 #if defined(RTS_SUPPORTS_THREADS)
1823 /* Wait for permission to re-enter the RTS with the result. */
1824 ACQUIRE_LOCK(&sched_mutex);
1825 grabReturnCapability(&sched_mutex, &cap);
1827 IF_DEBUG(scheduler, sched_belch("worker thread (%d, osthread %p): re-entering RTS", tok, osThreadId()));
1829 grabCapability(&cap);
1832 /* Remove the thread off of the suspended list */
1833 prev = &suspended_ccalling_threads;
1834 for (tso = suspended_ccalling_threads;
1835 tso != END_TSO_QUEUE;
1836 prev = &tso->link, tso = tso->link) {
1837 if (tso->id == (StgThreadID)tok) {
1842 if (tso == END_TSO_QUEUE) {
1843 barf("resumeThread: thread not found");
1845 tso->link = END_TSO_QUEUE;
1847 #if defined(RTS_SUPPORTS_THREADS)
1848 if(tso->why_blocked == BlockedOnCCall)
1850 awakenBlockedQueueNoLock(tso->blocked_exceptions);
1851 tso->blocked_exceptions = NULL;
1855 /* Reset blocking status */
1856 tso->why_blocked = NotBlocked;
1858 cap->r.rCurrentTSO = tso;
1859 #if defined(RTS_SUPPORTS_THREADS)
1860 RELEASE_LOCK(&sched_mutex);
1862 errno = saved_errno;
1867 /* ---------------------------------------------------------------------------
1869 * ------------------------------------------------------------------------ */
1870 static void unblockThread(StgTSO *tso);
1872 /* ---------------------------------------------------------------------------
1873 * Comparing Thread ids.
1875 * This is used from STG land in the implementation of the
1876 * instances of Eq/Ord for ThreadIds.
1877 * ------------------------------------------------------------------------ */
1880 cmp_thread(StgPtr tso1, StgPtr tso2)
1882 StgThreadID id1 = ((StgTSO *)tso1)->id;
1883 StgThreadID id2 = ((StgTSO *)tso2)->id;
1885 if (id1 < id2) return (-1);
1886 if (id1 > id2) return 1;
1890 /* ---------------------------------------------------------------------------
1891 * Fetching the ThreadID from an StgTSO.
1893 * This is used in the implementation of Show for ThreadIds.
1894 * ------------------------------------------------------------------------ */
1896 rts_getThreadId(StgPtr tso)
1898 return ((StgTSO *)tso)->id;
1903 labelThread(StgPtr tso, char *label)
1908 /* Caveat: Once set, you can only set the thread name to "" */
1909 len = strlen(label)+1;
1910 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
1911 strncpy(buf,label,len);
1912 /* Update will free the old memory for us */
1913 updateThreadLabel((StgWord)tso,buf);
1917 /* ---------------------------------------------------------------------------
1918 Create a new thread.
1920 The new thread starts with the given stack size. Before the
1921 scheduler can run, however, this thread needs to have a closure
1922 (and possibly some arguments) pushed on its stack. See
1923 pushClosure() in Schedule.h.
1925 createGenThread() and createIOThread() (in SchedAPI.h) are
1926 convenient packaged versions of this function.
1928 currently pri (priority) is only used in a GRAN setup -- HWL
1929 ------------------------------------------------------------------------ */
1930 //@cindex createThread
1932 /* currently pri (priority) is only used in a GRAN setup -- HWL */
1934 createThread(nat size, StgInt pri)
1937 createThread(nat size)
1944 /* First check whether we should create a thread at all */
1946 /* check that no more than RtsFlags.ParFlags.maxThreads threads are created */
1947 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads) {
1949 belch("{createThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
1950 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
1951 return END_TSO_QUEUE;
1957 ASSERT(!RtsFlags.GranFlags.Light || CurrentProc==0);
1960 // ToDo: check whether size = stack_size - TSO_STRUCT_SIZEW
1962 /* catch ridiculously small stack sizes */
1963 if (size < MIN_STACK_WORDS + TSO_STRUCT_SIZEW) {
1964 size = MIN_STACK_WORDS + TSO_STRUCT_SIZEW;
1967 stack_size = size - TSO_STRUCT_SIZEW;
1969 tso = (StgTSO *)allocate(size);
1970 TICK_ALLOC_TSO(stack_size, 0);
1972 SET_HDR(tso, &stg_TSO_info, CCS_SYSTEM);
1974 SET_GRAN_HDR(tso, ThisPE);
1977 // Always start with the compiled code evaluator
1978 tso->what_next = ThreadRunGHC;
1980 /* tso->id needs to be unique. For now we use a heavyweight mutex to
1981 * protect the increment operation on next_thread_id.
1982 * In future, we could use an atomic increment instead.
1984 ACQUIRE_LOCK(&thread_id_mutex);
1985 tso->id = next_thread_id++;
1986 RELEASE_LOCK(&thread_id_mutex);
1988 tso->why_blocked = NotBlocked;
1989 tso->blocked_exceptions = NULL;
1991 tso->saved_errno = 0;
1993 tso->stack_size = stack_size;
1994 tso->max_stack_size = round_to_mblocks(RtsFlags.GcFlags.maxStkSize)
1996 tso->sp = (P_)&(tso->stack) + stack_size;
1999 tso->prof.CCCS = CCS_MAIN;
2002 /* put a stop frame on the stack */
2003 tso->sp -= sizeofW(StgStopFrame);
2004 SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
2007 tso->link = END_TSO_QUEUE;
2008 /* uses more flexible routine in GranSim */
2009 insertThread(tso, CurrentProc);
2011 /* In a non-GranSim setup the pushing of a TSO onto the runq is separated
2017 if (RtsFlags.GranFlags.GranSimStats.Full)
2018 DumpGranEvent(GR_START,tso);
2020 if (RtsFlags.ParFlags.ParStats.Full)
2021 DumpGranEvent(GR_STARTQ,tso);
2022 /* HACk to avoid SCHEDULE
2026 /* Link the new thread on the global thread list.
2028 tso->global_link = all_threads;
2032 tso->dist.priority = MandatoryPriority; //by default that is...
2036 tso->gran.pri = pri;
2038 tso->gran.magic = TSO_MAGIC; // debugging only
2040 tso->gran.sparkname = 0;
2041 tso->gran.startedat = CURRENT_TIME;
2042 tso->gran.exported = 0;
2043 tso->gran.basicblocks = 0;
2044 tso->gran.allocs = 0;
2045 tso->gran.exectime = 0;
2046 tso->gran.fetchtime = 0;
2047 tso->gran.fetchcount = 0;
2048 tso->gran.blocktime = 0;
2049 tso->gran.blockcount = 0;
2050 tso->gran.blockedat = 0;
2051 tso->gran.globalsparks = 0;
2052 tso->gran.localsparks = 0;
2053 if (RtsFlags.GranFlags.Light)
2054 tso->gran.clock = Now; /* local clock */
2056 tso->gran.clock = 0;
2058 IF_DEBUG(gran,printTSO(tso));
2061 tso->par.magic = TSO_MAGIC; // debugging only
2063 tso->par.sparkname = 0;
2064 tso->par.startedat = CURRENT_TIME;
2065 tso->par.exported = 0;
2066 tso->par.basicblocks = 0;
2067 tso->par.allocs = 0;
2068 tso->par.exectime = 0;
2069 tso->par.fetchtime = 0;
2070 tso->par.fetchcount = 0;
2071 tso->par.blocktime = 0;
2072 tso->par.blockcount = 0;
2073 tso->par.blockedat = 0;
2074 tso->par.globalsparks = 0;
2075 tso->par.localsparks = 0;
2079 globalGranStats.tot_threads_created++;
2080 globalGranStats.threads_created_on_PE[CurrentProc]++;
2081 globalGranStats.tot_sq_len += spark_queue_len(CurrentProc);
2082 globalGranStats.tot_sq_probes++;
2084 // collect parallel global statistics (currently done together with GC stats)
2085 if (RtsFlags.ParFlags.ParStats.Global &&
2086 RtsFlags.GcFlags.giveStats > NO_GC_STATS) {
2087 //fprintf(stderr, "Creating thread %d @ %11.2f\n", tso->id, usertime());
2088 globalParStats.tot_threads_created++;
2094 belch("==__ schedule: Created TSO %d (%p);",
2095 CurrentProc, tso, tso->id));
2097 IF_PAR_DEBUG(verbose,
2098 belch("==__ schedule: Created TSO %d (%p); %d threads active",
2099 tso->id, tso, advisory_thread_count));
2101 IF_DEBUG(scheduler,sched_belch("created thread %ld, stack size = %lx words",
2102 tso->id, tso->stack_size));
2109 all parallel thread creation calls should fall through the following routine.
2112 createSparkThread(rtsSpark spark)
2114 ASSERT(spark != (rtsSpark)NULL);
2115 if (advisory_thread_count >= RtsFlags.ParFlags.maxThreads)
2117 barf("{createSparkThread}Daq ghuH: refusing to create another thread; no more than %d threads allowed (currently %d)",
2118 RtsFlags.ParFlags.maxThreads, advisory_thread_count);
2119 return END_TSO_QUEUE;
2123 tso = createThread(RtsFlags.GcFlags.initialStkSize);
2124 if (tso==END_TSO_QUEUE)
2125 barf("createSparkThread: Cannot create TSO");
2127 tso->priority = AdvisoryPriority;
2129 pushClosure(tso,spark);
2130 PUSH_ON_RUN_QUEUE(tso);
2131 advisory_thread_count++;
2138 Turn a spark into a thread.
2139 ToDo: fix for SMP (needs to acquire SCHED_MUTEX!)
2142 //@cindex activateSpark
2144 activateSpark (rtsSpark spark)
2148 tso = createSparkThread(spark);
2149 if (RtsFlags.ParFlags.ParStats.Full) {
2150 //ASSERT(run_queue_hd == END_TSO_QUEUE); // I think ...
2151 IF_PAR_DEBUG(verbose,
2152 belch("==^^ activateSpark: turning spark of closure %p (%s) into a thread",
2153 (StgClosure *)spark, info_type((StgClosure *)spark)));
2155 // ToDo: fwd info on local/global spark to thread -- HWL
2156 // tso->gran.exported = spark->exported;
2157 // tso->gran.locked = !spark->global;
2158 // tso->gran.sparkname = spark->name;
2164 static SchedulerStatus waitThread_(/*out*/StgMainThread* m,
2165 Capability *initialCapability
2169 /* ---------------------------------------------------------------------------
2172 * scheduleThread puts a thread on the head of the runnable queue.
2173 * This will usually be done immediately after a thread is created.
2174 * The caller of scheduleThread must create the thread using e.g.
2175 * createThread and push an appropriate closure
2176 * on this thread's stack before the scheduler is invoked.
2177 * ------------------------------------------------------------------------ */
2179 static void scheduleThread_ (StgTSO* tso);
2182 scheduleThread_(StgTSO *tso)
2184 // Precondition: sched_mutex must be held.
2186 /* Put the new thread on the head of the runnable queue. The caller
2187 * better push an appropriate closure on this thread's stack
2188 * beforehand. In the SMP case, the thread may start running as
2189 * soon as we release the scheduler lock below.
2191 PUSH_ON_RUN_QUEUE(tso);
2195 IF_DEBUG(scheduler,printTSO(tso));
2199 void scheduleThread(StgTSO* tso)
2201 ACQUIRE_LOCK(&sched_mutex);
2202 scheduleThread_(tso);
2203 RELEASE_LOCK(&sched_mutex);
2207 scheduleWaitThread(StgTSO* tso, /*[out]*/HaskellObj* ret, Capability *initialCapability)
2208 { // Precondition: sched_mutex must be held
2211 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
2215 #if defined(RTS_SUPPORTS_THREADS)
2216 #if defined(THREADED_RTS)
2217 initCondition(&m->bound_thread_cond);
2219 initCondition(&m->wakeup);
2223 /* Put the thread on the main-threads list prior to scheduling the TSO.
2224 Failure to do so introduces a race condition in the MT case (as
2225 identified by Wolfgang Thaller), whereby the new task/OS thread
2226 created by scheduleThread_() would complete prior to the thread
2227 that spawned it managed to put 'itself' on the main-threads list.
2228 The upshot of it all being that the worker thread wouldn't get to
2229 signal the completion of the its work item for the main thread to
2230 see (==> it got stuck waiting.) -- sof 6/02.
2232 IF_DEBUG(scheduler, sched_belch("waiting for thread (%d)\n", tso->id));
2234 m->link = main_threads;
2237 scheduleThread_(tso);
2239 return waitThread_(m, initialCapability);
2242 /* ---------------------------------------------------------------------------
2245 * Initialise the scheduler. This resets all the queues - if the
2246 * queues contained any threads, they'll be garbage collected at the
2249 * ------------------------------------------------------------------------ */
2253 term_handler(int sig STG_UNUSED)
2256 ACQUIRE_LOCK(&term_mutex);
2258 RELEASE_LOCK(&term_mutex);
2269 for (i=0; i<=MAX_PROC; i++) {
2270 run_queue_hds[i] = END_TSO_QUEUE;
2271 run_queue_tls[i] = END_TSO_QUEUE;
2272 blocked_queue_hds[i] = END_TSO_QUEUE;
2273 blocked_queue_tls[i] = END_TSO_QUEUE;
2274 ccalling_threadss[i] = END_TSO_QUEUE;
2275 sleeping_queue = END_TSO_QUEUE;
2278 run_queue_hd = END_TSO_QUEUE;
2279 run_queue_tl = END_TSO_QUEUE;
2280 blocked_queue_hd = END_TSO_QUEUE;
2281 blocked_queue_tl = END_TSO_QUEUE;
2282 sleeping_queue = END_TSO_QUEUE;
2285 suspended_ccalling_threads = END_TSO_QUEUE;
2287 main_threads = NULL;
2288 all_threads = END_TSO_QUEUE;
2293 RtsFlags.ConcFlags.ctxtSwitchTicks =
2294 RtsFlags.ConcFlags.ctxtSwitchTime / TICK_MILLISECS;
2296 #if defined(RTS_SUPPORTS_THREADS)
2297 /* Initialise the mutex and condition variables used by
2299 initMutex(&sched_mutex);
2300 initMutex(&term_mutex);
2301 initMutex(&thread_id_mutex);
2303 initCondition(&thread_ready_cond);
2307 initCondition(&gc_pending_cond);
2310 #if defined(RTS_SUPPORTS_THREADS)
2311 ACQUIRE_LOCK(&sched_mutex);
2314 /* Install the SIGHUP handler */
2317 struct sigaction action,oact;
2319 action.sa_handler = term_handler;
2320 sigemptyset(&action.sa_mask);
2321 action.sa_flags = 0;
2322 if (sigaction(SIGTERM, &action, &oact) != 0) {
2323 barf("can't install TERM handler");
2328 /* A capability holds the state a native thread needs in
2329 * order to execute STG code. At least one capability is
2330 * floating around (only SMP builds have more than one).
2334 #if defined(RTS_SUPPORTS_THREADS)
2335 /* start our haskell execution tasks */
2337 startTaskManager(RtsFlags.ParFlags.nNodes, taskStart);
2339 startTaskManager(0,taskStart);
2343 #if /* defined(SMP) ||*/ defined(PAR)
2347 #if defined(RTS_SUPPORTS_THREADS)
2348 RELEASE_LOCK(&sched_mutex);
2354 exitScheduler( void )
2356 #if defined(RTS_SUPPORTS_THREADS)
2359 shutting_down_scheduler = rtsTrue;
2362 /* -----------------------------------------------------------------------------
2363 Managing the per-task allocation areas.
2365 Each capability comes with an allocation area. These are
2366 fixed-length block lists into which allocation can be done.
2368 ToDo: no support for two-space collection at the moment???
2369 -------------------------------------------------------------------------- */
2371 /* -----------------------------------------------------------------------------
2372 * waitThread is the external interface for running a new computation
2373 * and waiting for the result.
2375 * In the non-SMP case, we create a new main thread, push it on the
2376 * main-thread stack, and invoke the scheduler to run it. The
2377 * scheduler will return when the top main thread on the stack has
2378 * completed or died, and fill in the necessary fields of the
2379 * main_thread structure.
2381 * In the SMP case, we create a main thread as before, but we then
2382 * create a new condition variable and sleep on it. When our new
2383 * main thread has completed, we'll be woken up and the status/result
2384 * will be in the main_thread struct.
2385 * -------------------------------------------------------------------------- */
2388 howManyThreadsAvail ( void )
2392 for (q = run_queue_hd; q != END_TSO_QUEUE; q = q->link)
2394 for (q = blocked_queue_hd; q != END_TSO_QUEUE; q = q->link)
2396 for (q = sleeping_queue; q != END_TSO_QUEUE; q = q->link)
2402 finishAllThreads ( void )
2405 while (run_queue_hd != END_TSO_QUEUE) {
2406 waitThread ( run_queue_hd, NULL, NULL );
2408 while (blocked_queue_hd != END_TSO_QUEUE) {
2409 waitThread ( blocked_queue_hd, NULL, NULL );
2411 while (sleeping_queue != END_TSO_QUEUE) {
2412 waitThread ( blocked_queue_hd, NULL, NULL );
2415 (blocked_queue_hd != END_TSO_QUEUE ||
2416 run_queue_hd != END_TSO_QUEUE ||
2417 sleeping_queue != END_TSO_QUEUE);
2421 waitThread(StgTSO *tso, /*out*/StgClosure **ret, Capability *initialCapability)
2424 SchedulerStatus stat;
2426 m = stgMallocBytes(sizeof(StgMainThread), "waitThread");
2430 #if defined(RTS_SUPPORTS_THREADS)
2431 #if defined(THREADED_RTS)
2432 initCondition(&m->bound_thread_cond);
2434 initCondition(&m->wakeup);
2438 /* see scheduleWaitThread() comment */
2439 ACQUIRE_LOCK(&sched_mutex);
2440 m->link = main_threads;
2443 IF_DEBUG(scheduler, sched_belch("waiting for thread %d", tso->id));
2445 stat = waitThread_(m,initialCapability);
2447 RELEASE_LOCK(&sched_mutex);
2453 waitThread_(StgMainThread* m, Capability *initialCapability)
2455 SchedulerStatus stat;
2457 // Precondition: sched_mutex must be held.
2458 IF_DEBUG(scheduler, sched_belch("== scheduler: new main thread (%d)\n", m->tso->id));
2460 #if defined(RTS_SUPPORTS_THREADS) && !defined(THREADED_RTS)
2461 { // FIXME: does this still make sense?
2462 // It's not for the threaded rts => SMP only
2464 waitCondition(&m->wakeup, &sched_mutex);
2465 } while (m->stat == NoStatus);
2468 /* GranSim specific init */
2469 CurrentTSO = m->tso; // the TSO to run
2470 procStatus[MainProc] = Busy; // status of main PE
2471 CurrentProc = MainProc; // PE to run it on
2473 RELEASE_LOCK(&sched_mutex);
2474 schedule(m,initialCapability);
2476 RELEASE_LOCK(&sched_mutex);
2477 schedule(m,initialCapability);
2478 ACQUIRE_LOCK(&sched_mutex);
2479 ASSERT(m->stat != NoStatus);
2484 #if defined(RTS_SUPPORTS_THREADS)
2485 #if defined(THREADED_RTS)
2486 closeCondition(&m->bound_thread_cond);
2488 closeCondition(&m->wakeup);
2492 IF_DEBUG(scheduler, fprintf(stderr, "== scheduler: main thread (%d) finished\n",
2496 // Postcondition: sched_mutex still held
2500 //@node Run queue code, Garbage Collextion Routines, Suspend and Resume, Main scheduling code
2501 //@subsection Run queue code
2505 NB: In GranSim we have many run queues; run_queue_hd is actually a macro
2506 unfolding to run_queue_hds[CurrentProc], thus CurrentProc is an
2507 implicit global variable that has to be correct when calling these
2511 /* Put the new thread on the head of the runnable queue.
2512 * The caller of createThread better push an appropriate closure
2513 * on this thread's stack before the scheduler is invoked.
2515 static /* inline */ void
2516 add_to_run_queue(tso)
2519 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
2520 tso->link = run_queue_hd;
2522 if (run_queue_tl == END_TSO_QUEUE) {
2527 /* Put the new thread at the end of the runnable queue. */
2528 static /* inline */ void
2529 push_on_run_queue(tso)
2532 ASSERT(get_itbl((StgClosure *)tso)->type == TSO);
2533 ASSERT(run_queue_hd!=NULL && run_queue_tl!=NULL);
2534 ASSERT(tso!=run_queue_hd && tso!=run_queue_tl);
2535 if (run_queue_hd == END_TSO_QUEUE) {
2538 run_queue_tl->link = tso;
2544 Should be inlined because it's used very often in schedule. The tso
2545 argument is actually only needed in GranSim, where we want to have the
2546 possibility to schedule *any* TSO on the run queue, irrespective of the
2547 actual ordering. Therefore, if tso is not the nil TSO then we traverse
2548 the run queue and dequeue the tso, adjusting the links in the queue.
2550 //@cindex take_off_run_queue
2551 static /* inline */ StgTSO*
2552 take_off_run_queue(StgTSO *tso) {
2556 qetlaHbogh Qu' ngaSbogh ghomDaQ {tso} yIteq!
2558 if tso is specified, unlink that tso from the run_queue (doesn't have
2559 to be at the beginning of the queue); GranSim only
2561 if (tso!=END_TSO_QUEUE) {
2562 /* find tso in queue */
2563 for (t=run_queue_hd, prev=END_TSO_QUEUE;
2564 t!=END_TSO_QUEUE && t!=tso;
2568 /* now actually dequeue the tso */
2569 if (prev!=END_TSO_QUEUE) {
2570 ASSERT(run_queue_hd!=t);
2571 prev->link = t->link;
2573 /* t is at beginning of thread queue */
2574 ASSERT(run_queue_hd==t);
2575 run_queue_hd = t->link;
2577 /* t is at end of thread queue */
2578 if (t->link==END_TSO_QUEUE) {
2579 ASSERT(t==run_queue_tl);
2580 run_queue_tl = prev;
2582 ASSERT(run_queue_tl!=t);
2584 t->link = END_TSO_QUEUE;
2586 /* take tso from the beginning of the queue; std concurrent code */
2588 if (t != END_TSO_QUEUE) {
2589 run_queue_hd = t->link;
2590 t->link = END_TSO_QUEUE;
2591 if (run_queue_hd == END_TSO_QUEUE) {
2592 run_queue_tl = END_TSO_QUEUE;
2601 //@node Garbage Collextion Routines, Blocking Queue Routines, Run queue code, Main scheduling code
2602 //@subsection Garbage Collextion Routines
2604 /* ---------------------------------------------------------------------------
2605 Where are the roots that we know about?
2607 - all the threads on the runnable queue
2608 - all the threads on the blocked queue
2609 - all the threads on the sleeping queue
2610 - all the thread currently executing a _ccall_GC
2611 - all the "main threads"
2613 ------------------------------------------------------------------------ */
2615 /* This has to be protected either by the scheduler monitor, or by the
2616 garbage collection monitor (probably the latter).
2621 GetRoots(evac_fn evac)
2626 for (i=0; i<=RtsFlags.GranFlags.proc; i++) {
2627 if ((run_queue_hds[i] != END_TSO_QUEUE) && ((run_queue_hds[i] != NULL)))
2628 evac((StgClosure **)&run_queue_hds[i]);
2629 if ((run_queue_tls[i] != END_TSO_QUEUE) && ((run_queue_tls[i] != NULL)))
2630 evac((StgClosure **)&run_queue_tls[i]);
2632 if ((blocked_queue_hds[i] != END_TSO_QUEUE) && ((blocked_queue_hds[i] != NULL)))
2633 evac((StgClosure **)&blocked_queue_hds[i]);
2634 if ((blocked_queue_tls[i] != END_TSO_QUEUE) && ((blocked_queue_tls[i] != NULL)))
2635 evac((StgClosure **)&blocked_queue_tls[i]);
2636 if ((ccalling_threadss[i] != END_TSO_QUEUE) && ((ccalling_threadss[i] != NULL)))
2637 evac((StgClosure **)&ccalling_threads[i]);
2644 if (run_queue_hd != END_TSO_QUEUE) {
2645 ASSERT(run_queue_tl != END_TSO_QUEUE);
2646 evac((StgClosure **)&run_queue_hd);
2647 evac((StgClosure **)&run_queue_tl);
2650 if (blocked_queue_hd != END_TSO_QUEUE) {
2651 ASSERT(blocked_queue_tl != END_TSO_QUEUE);
2652 evac((StgClosure **)&blocked_queue_hd);
2653 evac((StgClosure **)&blocked_queue_tl);
2656 if (sleeping_queue != END_TSO_QUEUE) {
2657 evac((StgClosure **)&sleeping_queue);
2661 if (suspended_ccalling_threads != END_TSO_QUEUE) {
2662 evac((StgClosure **)&suspended_ccalling_threads);
2665 #if defined(PAR) || defined(GRAN)
2666 markSparkQueue(evac);
2669 #if defined(RTS_USER_SIGNALS)
2670 // mark the signal handlers (signals should be already blocked)
2671 markSignalHandlers(evac);
2674 // main threads which have completed need to be retained until they
2675 // are dealt with in the main scheduler loop. They won't be
2676 // retained any other way: the GC will drop them from the
2677 // all_threads list, so we have to be careful to treat them as roots
2681 for (m = main_threads; m != NULL; m = m->link) {
2682 switch (m->tso->what_next) {
2683 case ThreadComplete:
2685 evac((StgClosure **)&m->tso);
2694 /* -----------------------------------------------------------------------------
2697 This is the interface to the garbage collector from Haskell land.
2698 We provide this so that external C code can allocate and garbage
2699 collect when called from Haskell via _ccall_GC.
2701 It might be useful to provide an interface whereby the programmer
2702 can specify more roots (ToDo).
2704 This needs to be protected by the GC condition variable above. KH.
2705 -------------------------------------------------------------------------- */
2707 static void (*extra_roots)(evac_fn);
2712 /* Obligated to hold this lock upon entry */
2713 ACQUIRE_LOCK(&sched_mutex);
2714 GarbageCollect(GetRoots,rtsFalse);
2715 RELEASE_LOCK(&sched_mutex);
2719 performMajorGC(void)
2721 ACQUIRE_LOCK(&sched_mutex);
2722 GarbageCollect(GetRoots,rtsTrue);
2723 RELEASE_LOCK(&sched_mutex);
2727 AllRoots(evac_fn evac)
2729 GetRoots(evac); // the scheduler's roots
2730 extra_roots(evac); // the user's roots
2734 performGCWithRoots(void (*get_roots)(evac_fn))
2736 ACQUIRE_LOCK(&sched_mutex);
2737 extra_roots = get_roots;
2738 GarbageCollect(AllRoots,rtsFalse);
2739 RELEASE_LOCK(&sched_mutex);
2742 /* -----------------------------------------------------------------------------
2745 If the thread has reached its maximum stack size, then raise the
2746 StackOverflow exception in the offending thread. Otherwise
2747 relocate the TSO into a larger chunk of memory and adjust its stack
2749 -------------------------------------------------------------------------- */
2752 threadStackOverflow(StgTSO *tso)
2754 nat new_stack_size, new_tso_size, stack_words;
2758 IF_DEBUG(sanity,checkTSO(tso));
2759 if (tso->stack_size >= tso->max_stack_size) {
2762 belch("@@ threadStackOverflow of TSO %d (%p): stack too large (now %ld; max is %ld)",
2763 tso->id, tso, tso->stack_size, tso->max_stack_size);
2764 /* If we're debugging, just print out the top of the stack */
2765 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2768 /* Send this thread the StackOverflow exception */
2769 raiseAsync(tso, (StgClosure *)stackOverflow_closure);
2773 /* Try to double the current stack size. If that takes us over the
2774 * maximum stack size for this thread, then use the maximum instead.
2775 * Finally round up so the TSO ends up as a whole number of blocks.
2777 new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
2778 new_tso_size = (nat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) +
2779 TSO_STRUCT_SIZE)/sizeof(W_);
2780 new_tso_size = round_to_mblocks(new_tso_size); /* Be MBLOCK-friendly */
2781 new_stack_size = new_tso_size - TSO_STRUCT_SIZEW;
2783 IF_DEBUG(scheduler, fprintf(stderr,"== scheduler: increasing stack size from %d words to %d.\n", tso->stack_size, new_stack_size));
2785 dest = (StgTSO *)allocate(new_tso_size);
2786 TICK_ALLOC_TSO(new_stack_size,0);
2788 /* copy the TSO block and the old stack into the new area */
2789 memcpy(dest,tso,TSO_STRUCT_SIZE);
2790 stack_words = tso->stack + tso->stack_size - tso->sp;
2791 new_sp = (P_)dest + new_tso_size - stack_words;
2792 memcpy(new_sp, tso->sp, stack_words * sizeof(W_));
2794 /* relocate the stack pointers... */
2796 dest->stack_size = new_stack_size;
2798 /* Mark the old TSO as relocated. We have to check for relocated
2799 * TSOs in the garbage collector and any primops that deal with TSOs.
2801 * It's important to set the sp value to just beyond the end
2802 * of the stack, so we don't attempt to scavenge any part of the
2805 tso->what_next = ThreadRelocated;
2807 tso->sp = (P_)&(tso->stack[tso->stack_size]);
2808 tso->why_blocked = NotBlocked;
2809 dest->mut_link = NULL;
2811 IF_PAR_DEBUG(verbose,
2812 belch("@@ threadStackOverflow of TSO %d (now at %p): stack size increased to %ld",
2813 tso->id, tso, tso->stack_size);
2814 /* If we're debugging, just print out the top of the stack */
2815 printStackChunk(tso->sp, stg_min(tso->stack+tso->stack_size,
2818 IF_DEBUG(sanity,checkTSO(tso));
2820 IF_DEBUG(scheduler,printTSO(dest));
2826 //@node Blocking Queue Routines, Exception Handling Routines, Garbage Collextion Routines, Main scheduling code
2827 //@subsection Blocking Queue Routines
2829 /* ---------------------------------------------------------------------------
2830 Wake up a queue that was blocked on some resource.
2831 ------------------------------------------------------------------------ */
2835 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2840 unblockCount ( StgBlockingQueueElement *bqe, StgClosure *node )
2842 /* write RESUME events to log file and
2843 update blocked and fetch time (depending on type of the orig closure) */
2844 if (RtsFlags.ParFlags.ParStats.Full) {
2845 DumpRawGranEvent(CURRENT_PROC, CURRENT_PROC,
2846 GR_RESUMEQ, ((StgTSO *)bqe), ((StgTSO *)bqe)->block_info.closure,
2847 0, 0 /* spark_queue_len(ADVISORY_POOL) */);
2848 if (EMPTY_RUN_QUEUE())
2849 emitSchedule = rtsTrue;
2851 switch (get_itbl(node)->type) {
2853 ((StgTSO *)bqe)->par.fetchtime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2858 ((StgTSO *)bqe)->par.blocktime += CURRENT_TIME-((StgTSO *)bqe)->par.blockedat;
2865 barf("{unblockOneLocked}Daq Qagh: unexpected closure in blocking queue");
2872 static StgBlockingQueueElement *
2873 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2876 PEs node_loc, tso_loc;
2878 node_loc = where_is(node); // should be lifted out of loop
2879 tso = (StgTSO *)bqe; // wastes an assignment to get the type right
2880 tso_loc = where_is((StgClosure *)tso);
2881 if (IS_LOCAL_TO(PROCS(node),tso_loc)) { // TSO is local
2882 /* !fake_fetch => TSO is on CurrentProc is same as IS_LOCAL_TO */
2883 ASSERT(CurrentProc!=node_loc || tso_loc==CurrentProc);
2884 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.lunblocktime;
2885 // insertThread(tso, node_loc);
2886 new_event(tso_loc, tso_loc, CurrentTime[CurrentProc],
2888 tso, node, (rtsSpark*)NULL);
2889 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2892 } else { // TSO is remote (actually should be FMBQ)
2893 CurrentTime[CurrentProc] += RtsFlags.GranFlags.Costs.mpacktime +
2894 RtsFlags.GranFlags.Costs.gunblocktime +
2895 RtsFlags.GranFlags.Costs.latency;
2896 new_event(tso_loc, CurrentProc, CurrentTime[CurrentProc],
2898 tso, node, (rtsSpark*)NULL);
2899 tso->link = END_TSO_QUEUE; // overwrite link just to be sure
2902 /* the thread-queue-overhead is accounted for in either Resume or UnblockThread */
2904 fprintf(stderr," %s TSO %d (%p) [PE %d] (block_info.closure=%p) (next=%p) ,",
2905 (node_loc==tso_loc ? "Local" : "Global"),
2906 tso->id, tso, CurrentProc, tso->block_info.closure, tso->link));
2907 tso->block_info.closure = NULL;
2908 IF_DEBUG(scheduler,belch("-- Waking up thread %ld (%p)",
2912 static StgBlockingQueueElement *
2913 unblockOneLocked(StgBlockingQueueElement *bqe, StgClosure *node)
2915 StgBlockingQueueElement *next;
2917 switch (get_itbl(bqe)->type) {
2919 ASSERT(((StgTSO *)bqe)->why_blocked != NotBlocked);
2920 /* if it's a TSO just push it onto the run_queue */
2922 // ((StgTSO *)bqe)->link = END_TSO_QUEUE; // debugging?
2923 PUSH_ON_RUN_QUEUE((StgTSO *)bqe);
2925 unblockCount(bqe, node);
2926 /* reset blocking status after dumping event */
2927 ((StgTSO *)bqe)->why_blocked = NotBlocked;
2931 /* if it's a BLOCKED_FETCH put it on the PendingFetches list */
2933 bqe->link = (StgBlockingQueueElement *)PendingFetches;
2934 PendingFetches = (StgBlockedFetch *)bqe;
2938 /* can ignore this case in a non-debugging setup;
2939 see comments on RBHSave closures above */
2941 /* check that the closure is an RBHSave closure */
2942 ASSERT(get_itbl((StgClosure *)bqe) == &stg_RBH_Save_0_info ||
2943 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_1_info ||
2944 get_itbl((StgClosure *)bqe) == &stg_RBH_Save_2_info);
2948 barf("{unblockOneLocked}Daq Qagh: Unexpected IP (%#lx; %s) in blocking queue at %#lx\n",
2949 get_itbl((StgClosure *)bqe), info_type((StgClosure *)bqe),
2953 IF_PAR_DEBUG(bq, fprintf(stderr, ", %p (%s)", bqe, info_type((StgClosure*)bqe)));
2957 #else /* !GRAN && !PAR */
2959 unblockOneLocked(StgTSO *tso)
2963 ASSERT(get_itbl(tso)->type == TSO);
2964 ASSERT(tso->why_blocked != NotBlocked);
2965 tso->why_blocked = NotBlocked;
2967 PUSH_ON_RUN_QUEUE(tso);
2969 IF_DEBUG(scheduler,sched_belch("waking up thread %ld", tso->id));
2974 #if defined(GRAN) || defined(PAR)
2975 INLINE_ME StgBlockingQueueElement *
2976 unblockOne(StgBlockingQueueElement *bqe, StgClosure *node)
2978 ACQUIRE_LOCK(&sched_mutex);
2979 bqe = unblockOneLocked(bqe, node);
2980 RELEASE_LOCK(&sched_mutex);
2985 unblockOne(StgTSO *tso)
2987 ACQUIRE_LOCK(&sched_mutex);
2988 tso = unblockOneLocked(tso);
2989 RELEASE_LOCK(&sched_mutex);
2996 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
2998 StgBlockingQueueElement *bqe;
3003 belch("##-_ AwBQ for node %p on PE %d @ %ld by TSO %d (%p): ", \
3004 node, CurrentProc, CurrentTime[CurrentProc],
3005 CurrentTSO->id, CurrentTSO));
3007 node_loc = where_is(node);
3009 ASSERT(q == END_BQ_QUEUE ||
3010 get_itbl(q)->type == TSO || // q is either a TSO or an RBHSave
3011 get_itbl(q)->type == CONSTR); // closure (type constructor)
3012 ASSERT(is_unique(node));
3014 /* FAKE FETCH: magically copy the node to the tso's proc;
3015 no Fetch necessary because in reality the node should not have been
3016 moved to the other PE in the first place
3018 if (CurrentProc!=node_loc) {
3020 belch("## node %p is on PE %d but CurrentProc is %d (TSO %d); assuming fake fetch and adjusting bitmask (old: %#x)",
3021 node, node_loc, CurrentProc, CurrentTSO->id,
3022 // CurrentTSO, where_is(CurrentTSO),
3023 node->header.gran.procs));
3024 node->header.gran.procs = (node->header.gran.procs) | PE_NUMBER(CurrentProc);
3026 belch("## new bitmask of node %p is %#x",
3027 node, node->header.gran.procs));
3028 if (RtsFlags.GranFlags.GranSimStats.Global) {
3029 globalGranStats.tot_fake_fetches++;
3034 // ToDo: check: ASSERT(CurrentProc==node_loc);
3035 while (get_itbl(bqe)->type==TSO) { // q != END_TSO_QUEUE) {
3038 bqe points to the current element in the queue
3039 next points to the next element in the queue
3041 //tso = (StgTSO *)bqe; // wastes an assignment to get the type right
3042 //tso_loc = where_is(tso);
3044 bqe = unblockOneLocked(bqe, node);
3047 /* if this is the BQ of an RBH, we have to put back the info ripped out of
3048 the closure to make room for the anchor of the BQ */
3049 if (bqe!=END_BQ_QUEUE) {
3050 ASSERT(get_itbl(node)->type == RBH && get_itbl(bqe)->type == CONSTR);
3052 ASSERT((info_ptr==&RBH_Save_0_info) ||
3053 (info_ptr==&RBH_Save_1_info) ||
3054 (info_ptr==&RBH_Save_2_info));
3056 /* cf. convertToRBH in RBH.c for writing the RBHSave closure */
3057 ((StgRBH *)node)->blocking_queue = (StgBlockingQueueElement *)((StgRBHSave *)bqe)->payload[0];
3058 ((StgRBH *)node)->mut_link = (StgMutClosure *)((StgRBHSave *)bqe)->payload[1];
3061 belch("## Filled in RBH_Save for %p (%s) at end of AwBQ",
3062 node, info_type(node)));
3065 /* statistics gathering */
3066 if (RtsFlags.GranFlags.GranSimStats.Global) {
3067 // globalGranStats.tot_bq_processing_time += bq_processing_time;
3068 globalGranStats.tot_bq_len += len; // total length of all bqs awakened
3069 // globalGranStats.tot_bq_len_local += len_local; // same for local TSOs only
3070 globalGranStats.tot_awbq++; // total no. of bqs awakened
3073 fprintf(stderr,"## BQ Stats of %p: [%d entries] %s\n",
3074 node, len, (bqe!=END_BQ_QUEUE) ? "RBH" : ""));
3078 awakenBlockedQueue(StgBlockingQueueElement *q, StgClosure *node)
3080 StgBlockingQueueElement *bqe;
3082 ACQUIRE_LOCK(&sched_mutex);
3084 IF_PAR_DEBUG(verbose,
3085 belch("##-_ AwBQ for node %p on [%x]: ",
3089 if(get_itbl(q)->type == CONSTR || q==END_BQ_QUEUE) {
3090 IF_PAR_DEBUG(verbose, belch("## ... nothing to unblock so lets just return. RFP (BUG?)"));
3095 ASSERT(q == END_BQ_QUEUE ||
3096 get_itbl(q)->type == TSO ||
3097 get_itbl(q)->type == BLOCKED_FETCH ||
3098 get_itbl(q)->type == CONSTR);
3101 while (get_itbl(bqe)->type==TSO ||
3102 get_itbl(bqe)->type==BLOCKED_FETCH) {
3103 bqe = unblockOneLocked(bqe, node);
3105 RELEASE_LOCK(&sched_mutex);
3108 #else /* !GRAN && !PAR */
3110 #ifdef RTS_SUPPORTS_THREADS
3112 awakenBlockedQueueNoLock(StgTSO *tso)
3114 while (tso != END_TSO_QUEUE) {
3115 tso = unblockOneLocked(tso);
3121 awakenBlockedQueue(StgTSO *tso)
3123 ACQUIRE_LOCK(&sched_mutex);
3124 while (tso != END_TSO_QUEUE) {
3125 tso = unblockOneLocked(tso);
3127 RELEASE_LOCK(&sched_mutex);
3131 //@node Exception Handling Routines, Debugging Routines, Blocking Queue Routines, Main scheduling code
3132 //@subsection Exception Handling Routines
3134 /* ---------------------------------------------------------------------------
3136 - usually called inside a signal handler so it mustn't do anything fancy.
3137 ------------------------------------------------------------------------ */
3140 interruptStgRts(void)
3144 #ifdef RTS_SUPPORTS_THREADS
3145 wakeBlockedWorkerThread();
3149 /* -----------------------------------------------------------------------------
3152 This is for use when we raise an exception in another thread, which
3154 This has nothing to do with the UnblockThread event in GranSim. -- HWL
3155 -------------------------------------------------------------------------- */
3157 #if defined(GRAN) || defined(PAR)
3159 NB: only the type of the blocking queue is different in GranSim and GUM
3160 the operations on the queue-elements are the same
3161 long live polymorphism!
3163 Locks: sched_mutex is held upon entry and exit.
3167 unblockThread(StgTSO *tso)
3169 StgBlockingQueueElement *t, **last;
3171 switch (tso->why_blocked) {
3174 return; /* not blocked */
3177 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3179 StgBlockingQueueElement *last_tso = END_BQ_QUEUE;
3180 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3182 last = (StgBlockingQueueElement **)&mvar->head;
3183 for (t = (StgBlockingQueueElement *)mvar->head;
3185 last = &t->link, last_tso = t, t = t->link) {
3186 if (t == (StgBlockingQueueElement *)tso) {
3187 *last = (StgBlockingQueueElement *)tso->link;
3188 if (mvar->tail == tso) {
3189 mvar->tail = (StgTSO *)last_tso;
3194 barf("unblockThread (MVAR): TSO not found");
3197 case BlockedOnBlackHole:
3198 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3200 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3202 last = &bq->blocking_queue;
3203 for (t = bq->blocking_queue;
3205 last = &t->link, t = t->link) {
3206 if (t == (StgBlockingQueueElement *)tso) {
3207 *last = (StgBlockingQueueElement *)tso->link;
3211 barf("unblockThread (BLACKHOLE): TSO not found");
3214 case BlockedOnException:
3216 StgTSO *target = tso->block_info.tso;
3218 ASSERT(get_itbl(target)->type == TSO);
3220 if (target->what_next == ThreadRelocated) {
3221 target = target->link;
3222 ASSERT(get_itbl(target)->type == TSO);
3225 ASSERT(target->blocked_exceptions != NULL);
3227 last = (StgBlockingQueueElement **)&target->blocked_exceptions;
3228 for (t = (StgBlockingQueueElement *)target->blocked_exceptions;
3230 last = &t->link, t = t->link) {
3231 ASSERT(get_itbl(t)->type == TSO);
3232 if (t == (StgBlockingQueueElement *)tso) {
3233 *last = (StgBlockingQueueElement *)tso->link;
3237 barf("unblockThread (Exception): TSO not found");
3241 case BlockedOnWrite:
3242 #if defined(mingw32_TARGET_OS)
3243 case BlockedOnDoProc:
3246 /* take TSO off blocked_queue */
3247 StgBlockingQueueElement *prev = NULL;
3248 for (t = (StgBlockingQueueElement *)blocked_queue_hd; t != END_BQ_QUEUE;
3249 prev = t, t = t->link) {
3250 if (t == (StgBlockingQueueElement *)tso) {
3252 blocked_queue_hd = (StgTSO *)t->link;
3253 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3254 blocked_queue_tl = END_TSO_QUEUE;
3257 prev->link = t->link;
3258 if ((StgBlockingQueueElement *)blocked_queue_tl == t) {
3259 blocked_queue_tl = (StgTSO *)prev;
3265 barf("unblockThread (I/O): TSO not found");
3268 case BlockedOnDelay:
3270 /* take TSO off sleeping_queue */
3271 StgBlockingQueueElement *prev = NULL;
3272 for (t = (StgBlockingQueueElement *)sleeping_queue; t != END_BQ_QUEUE;
3273 prev = t, t = t->link) {
3274 if (t == (StgBlockingQueueElement *)tso) {
3276 sleeping_queue = (StgTSO *)t->link;
3278 prev->link = t->link;
3283 barf("unblockThread (delay): TSO not found");
3287 barf("unblockThread");
3291 tso->link = END_TSO_QUEUE;
3292 tso->why_blocked = NotBlocked;
3293 tso->block_info.closure = NULL;
3294 PUSH_ON_RUN_QUEUE(tso);
3298 unblockThread(StgTSO *tso)
3302 /* To avoid locking unnecessarily. */
3303 if (tso->why_blocked == NotBlocked) {
3307 switch (tso->why_blocked) {
3310 ASSERT(get_itbl(tso->block_info.closure)->type == MVAR);
3312 StgTSO *last_tso = END_TSO_QUEUE;
3313 StgMVar *mvar = (StgMVar *)(tso->block_info.closure);
3316 for (t = mvar->head; t != END_TSO_QUEUE;
3317 last = &t->link, last_tso = t, t = t->link) {
3320 if (mvar->tail == tso) {
3321 mvar->tail = last_tso;
3326 barf("unblockThread (MVAR): TSO not found");
3329 case BlockedOnBlackHole:
3330 ASSERT(get_itbl(tso->block_info.closure)->type == BLACKHOLE_BQ);
3332 StgBlockingQueue *bq = (StgBlockingQueue *)(tso->block_info.closure);
3334 last = &bq->blocking_queue;
3335 for (t = bq->blocking_queue; t != END_TSO_QUEUE;
3336 last = &t->link, t = t->link) {
3342 barf("unblockThread (BLACKHOLE): TSO not found");
3345 case BlockedOnException:
3347 StgTSO *target = tso->block_info.tso;
3349 ASSERT(get_itbl(target)->type == TSO);
3351 while (target->what_next == ThreadRelocated) {
3352 target = target->link;
3353 ASSERT(get_itbl(target)->type == TSO);
3356 ASSERT(target->blocked_exceptions != NULL);
3358 last = &target->blocked_exceptions;
3359 for (t = target->blocked_exceptions; t != END_TSO_QUEUE;
3360 last = &t->link, t = t->link) {
3361 ASSERT(get_itbl(t)->type == TSO);
3367 barf("unblockThread (Exception): TSO not found");
3371 case BlockedOnWrite:
3372 #if defined(mingw32_TARGET_OS)
3373 case BlockedOnDoProc:
3376 StgTSO *prev = NULL;
3377 for (t = blocked_queue_hd; t != END_TSO_QUEUE;
3378 prev = t, t = t->link) {
3381 blocked_queue_hd = t->link;
3382 if (blocked_queue_tl == t) {
3383 blocked_queue_tl = END_TSO_QUEUE;
3386 prev->link = t->link;
3387 if (blocked_queue_tl == t) {
3388 blocked_queue_tl = prev;
3394 barf("unblockThread (I/O): TSO not found");
3397 case BlockedOnDelay:
3399 StgTSO *prev = NULL;
3400 for (t = sleeping_queue; t != END_TSO_QUEUE;
3401 prev = t, t = t->link) {
3404 sleeping_queue = t->link;
3406 prev->link = t->link;
3411 barf("unblockThread (delay): TSO not found");
3415 barf("unblockThread");
3419 tso->link = END_TSO_QUEUE;
3420 tso->why_blocked = NotBlocked;
3421 tso->block_info.closure = NULL;
3422 PUSH_ON_RUN_QUEUE(tso);
3426 /* -----------------------------------------------------------------------------
3429 * The following function implements the magic for raising an
3430 * asynchronous exception in an existing thread.
3432 * We first remove the thread from any queue on which it might be
3433 * blocked. The possible blockages are MVARs and BLACKHOLE_BQs.
3435 * We strip the stack down to the innermost CATCH_FRAME, building
3436 * thunks in the heap for all the active computations, so they can
3437 * be restarted if necessary. When we reach a CATCH_FRAME, we build
3438 * an application of the handler to the exception, and push it on
3439 * the top of the stack.
3441 * How exactly do we save all the active computations? We create an
3442 * AP_STACK for every UpdateFrame on the stack. Entering one of these
3443 * AP_STACKs pushes everything from the corresponding update frame
3444 * upwards onto the stack. (Actually, it pushes everything up to the
3445 * next update frame plus a pointer to the next AP_STACK object.
3446 * Entering the next AP_STACK object pushes more onto the stack until we
3447 * reach the last AP_STACK object - at which point the stack should look
3448 * exactly as it did when we killed the TSO and we can continue
3449 * execution by entering the closure on top of the stack.
3451 * We can also kill a thread entirely - this happens if either (a) the
3452 * exception passed to raiseAsync is NULL, or (b) there's no
3453 * CATCH_FRAME on the stack. In either case, we strip the entire
3454 * stack and replace the thread with a zombie.
3456 * Locks: sched_mutex held upon entry nor exit.
3458 * -------------------------------------------------------------------------- */
3461 deleteThread(StgTSO *tso)
3463 raiseAsync(tso,NULL);
3467 deleteThreadImmediately(StgTSO *tso)
3468 { // for forkProcess only:
3469 // delete thread without giving it a chance to catch the KillThread exception
3471 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3474 #if defined(RTS_SUPPORTS_THREADS)
3475 if (tso->why_blocked != BlockedOnCCall
3476 && tso->why_blocked != BlockedOnCCall_NoUnblockExc)
3479 tso->what_next = ThreadKilled;
3483 raiseAsyncWithLock(StgTSO *tso, StgClosure *exception)
3485 /* When raising async exs from contexts where sched_mutex isn't held;
3486 use raiseAsyncWithLock(). */
3487 ACQUIRE_LOCK(&sched_mutex);
3488 raiseAsync(tso,exception);
3489 RELEASE_LOCK(&sched_mutex);
3493 raiseAsync(StgTSO *tso, StgClosure *exception)
3495 StgRetInfoTable *info;
3498 // Thread already dead?
3499 if (tso->what_next == ThreadComplete || tso->what_next == ThreadKilled) {
3504 sched_belch("raising exception in thread %ld.", tso->id));
3506 // Remove it from any blocking queues
3511 // The stack freezing code assumes there's a closure pointer on
3512 // the top of the stack, so we have to arrange that this is the case...
3514 if (sp[0] == (W_)&stg_enter_info) {
3518 sp[0] = (W_)&stg_dummy_ret_closure;
3524 // 1. Let the top of the stack be the "current closure"
3526 // 2. Walk up the stack until we find either an UPDATE_FRAME or a
3529 // 3. If it's an UPDATE_FRAME, then make an AP_STACK containing the
3530 // current closure applied to the chunk of stack up to (but not
3531 // including) the update frame. This closure becomes the "current
3532 // closure". Go back to step 2.
3534 // 4. If it's a CATCH_FRAME, then leave the exception handler on
3535 // top of the stack applied to the exception.
3537 // 5. If it's a STOP_FRAME, then kill the thread.
3542 info = get_ret_itbl((StgClosure *)frame);
3544 while (info->i.type != UPDATE_FRAME
3545 && (info->i.type != CATCH_FRAME || exception == NULL)
3546 && info->i.type != STOP_FRAME) {
3547 frame += stack_frame_sizeW((StgClosure *)frame);
3548 info = get_ret_itbl((StgClosure *)frame);
3551 switch (info->i.type) {
3554 // If we find a CATCH_FRAME, and we've got an exception to raise,
3555 // then build the THUNK raise(exception), and leave it on
3556 // top of the CATCH_FRAME ready to enter.
3560 StgCatchFrame *cf = (StgCatchFrame *)frame;
3564 // we've got an exception to raise, so let's pass it to the
3565 // handler in this frame.
3567 raise = (StgClosure *)allocate(sizeofW(StgClosure)+1);
3568 TICK_ALLOC_SE_THK(1,0);
3569 SET_HDR(raise,&stg_raise_info,cf->header.prof.ccs);
3570 raise->payload[0] = exception;
3572 // throw away the stack from Sp up to the CATCH_FRAME.
3576 /* Ensure that async excpetions are blocked now, so we don't get
3577 * a surprise exception before we get around to executing the
3580 if (tso->blocked_exceptions == NULL) {
3581 tso->blocked_exceptions = END_TSO_QUEUE;
3584 /* Put the newly-built THUNK on top of the stack, ready to execute
3585 * when the thread restarts.
3588 sp[-1] = (W_)&stg_enter_info;
3590 tso->what_next = ThreadRunGHC;
3591 IF_DEBUG(sanity, checkTSO(tso));
3600 // First build an AP_STACK consisting of the stack chunk above the
3601 // current update frame, with the top word on the stack as the
3604 words = frame - sp - 1;
3605 ap = (StgAP_STACK *)allocate(PAP_sizeW(words));
3608 ap->fun = (StgClosure *)sp[0];
3610 for(i=0; i < (nat)words; ++i) {
3611 ap->payload[i] = (StgClosure *)*sp++;
3614 SET_HDR(ap,&stg_AP_STACK_info,
3615 ((StgClosure *)frame)->header.prof.ccs /* ToDo */);
3616 TICK_ALLOC_UP_THK(words+1,0);
3619 fprintf(stderr, "scheduler: Updating ");
3620 printPtr((P_)((StgUpdateFrame *)frame)->updatee);
3621 fprintf(stderr, " with ");
3622 printObj((StgClosure *)ap);
3625 // Replace the updatee with an indirection - happily
3626 // this will also wake up any threads currently
3627 // waiting on the result.
3629 // Warning: if we're in a loop, more than one update frame on
3630 // the stack may point to the same object. Be careful not to
3631 // overwrite an IND_OLDGEN in this case, because we'll screw
3632 // up the mutable lists. To be on the safe side, don't
3633 // overwrite any kind of indirection at all. See also
3634 // threadSqueezeStack in GC.c, where we have to make a similar
3637 if (!closure_IND(((StgUpdateFrame *)frame)->updatee)) {
3638 // revert the black hole
3639 UPD_IND_NOLOCK(((StgUpdateFrame *)frame)->updatee,ap);
3641 sp += sizeofW(StgUpdateFrame) - 1;
3642 sp[0] = (W_)ap; // push onto stack
3647 // We've stripped the entire stack, the thread is now dead.
3648 sp += sizeofW(StgStopFrame);
3649 tso->what_next = ThreadKilled;
3660 /* -----------------------------------------------------------------------------
3661 resurrectThreads is called after garbage collection on the list of
3662 threads found to be garbage. Each of these threads will be woken
3663 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
3664 on an MVar, or NonTermination if the thread was blocked on a Black
3667 Locks: sched_mutex isn't held upon entry nor exit.
3668 -------------------------------------------------------------------------- */
3671 resurrectThreads( StgTSO *threads )
3675 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
3676 next = tso->global_link;
3677 tso->global_link = all_threads;
3679 IF_DEBUG(scheduler, sched_belch("resurrecting thread %d", tso->id));
3681 switch (tso->why_blocked) {
3683 case BlockedOnException:
3684 /* Called by GC - sched_mutex lock is currently held. */
3685 raiseAsync(tso,(StgClosure *)BlockedOnDeadMVar_closure);
3687 case BlockedOnBlackHole:
3688 raiseAsync(tso,(StgClosure *)NonTermination_closure);
3691 /* This might happen if the thread was blocked on a black hole
3692 * belonging to a thread that we've just woken up (raiseAsync
3693 * can wake up threads, remember...).
3697 barf("resurrectThreads: thread blocked in a strange way");
3702 /* -----------------------------------------------------------------------------
3703 * Blackhole detection: if we reach a deadlock, test whether any
3704 * threads are blocked on themselves. Any threads which are found to
3705 * be self-blocked get sent a NonTermination exception.
3707 * This is only done in a deadlock situation in order to avoid
3708 * performance overhead in the normal case.
3710 * Locks: sched_mutex is held upon entry and exit.
3711 * -------------------------------------------------------------------------- */
3714 detectBlackHoles( void )
3716 StgTSO *tso = all_threads;
3718 StgClosure *blocked_on;
3719 StgRetInfoTable *info;
3721 for (tso = all_threads; tso != END_TSO_QUEUE; tso = tso->global_link) {
3723 while (tso->what_next == ThreadRelocated) {
3725 ASSERT(get_itbl(tso)->type == TSO);
3728 if (tso->why_blocked != BlockedOnBlackHole) {
3731 blocked_on = tso->block_info.closure;
3733 frame = (StgClosure *)tso->sp;
3736 info = get_ret_itbl(frame);
3737 switch (info->i.type) {
3739 if (((StgUpdateFrame *)frame)->updatee == blocked_on) {
3740 /* We are blocking on one of our own computations, so
3741 * send this thread the NonTermination exception.
3744 sched_belch("thread %d is blocked on itself", tso->id));
3745 raiseAsync(tso, (StgClosure *)NonTermination_closure);
3749 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3755 // normal stack frames; do nothing except advance the pointer
3757 (StgPtr)frame += stack_frame_sizeW(frame);
3764 //@node Debugging Routines, Index, Exception Handling Routines, Main scheduling code
3765 //@subsection Debugging Routines
3767 /* -----------------------------------------------------------------------------
3768 * Debugging: why is a thread blocked
3769 * [Also provides useful information when debugging threaded programs
3770 * at the Haskell source code level, so enable outside of DEBUG. --sof 7/02]
3771 -------------------------------------------------------------------------- */
3775 printThreadBlockage(StgTSO *tso)
3777 switch (tso->why_blocked) {
3779 fprintf(stderr,"is blocked on read from fd %d", tso->block_info.fd);
3781 case BlockedOnWrite:
3782 fprintf(stderr,"is blocked on write to fd %d", tso->block_info.fd);
3784 #if defined(mingw32_TARGET_OS)
3785 case BlockedOnDoProc:
3786 fprintf(stderr,"is blocked on proc (request: %d)", tso->block_info.async_result->reqID);
3789 case BlockedOnDelay:
3790 fprintf(stderr,"is blocked until %d", tso->block_info.target);
3793 fprintf(stderr,"is blocked on an MVar");
3795 case BlockedOnException:
3796 fprintf(stderr,"is blocked on delivering an exception to thread %d",
3797 tso->block_info.tso->id);
3799 case BlockedOnBlackHole:
3800 fprintf(stderr,"is blocked on a black hole");
3803 fprintf(stderr,"is not blocked");
3807 fprintf(stderr,"is blocked on global address; local FM_BQ is %p (%s)",
3808 tso->block_info.closure, info_type(tso->block_info.closure));
3810 case BlockedOnGA_NoSend:
3811 fprintf(stderr,"is blocked on global address (no send); local FM_BQ is %p (%s)",
3812 tso->block_info.closure, info_type(tso->block_info.closure));
3815 #if defined(RTS_SUPPORTS_THREADS)
3816 case BlockedOnCCall:
3817 fprintf(stderr,"is blocked on an external call");
3819 case BlockedOnCCall_NoUnblockExc:
3820 fprintf(stderr,"is blocked on an external call (exceptions were already blocked)");
3824 barf("printThreadBlockage: strange tso->why_blocked: %d for TSO %d (%d)",
3825 tso->why_blocked, tso->id, tso);
3831 printThreadStatus(StgTSO *tso)
3833 switch (tso->what_next) {
3835 fprintf(stderr,"has been killed");
3837 case ThreadComplete:
3838 fprintf(stderr,"has completed");
3841 printThreadBlockage(tso);
3846 printAllThreads(void)
3852 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3853 ullong_format_string(TIME_ON_PROC(CurrentProc),
3854 time_string, rtsFalse/*no commas!*/);
3856 fprintf(stderr, "all threads at [%s]:\n", time_string);
3858 char time_string[TIME_STR_LEN], node_str[NODE_STR_LEN];
3859 ullong_format_string(CURRENT_TIME,
3860 time_string, rtsFalse/*no commas!*/);
3862 fprintf(stderr,"all threads at [%s]:\n", time_string);
3864 fprintf(stderr,"all threads:\n");
3867 for (t = all_threads; t != END_TSO_QUEUE; t = t->global_link) {
3868 fprintf(stderr, "\tthread %d @ %p ", t->id, (void *)t);
3869 label = lookupThreadLabel((StgWord)t);
3870 if (label) fprintf(stderr,"[\"%s\"] ",(char *)label);
3871 printThreadStatus(t);
3872 fprintf(stderr,"\n");
3879 Print a whole blocking queue attached to node (debugging only).
3884 print_bq (StgClosure *node)
3886 StgBlockingQueueElement *bqe;
3890 fprintf(stderr,"## BQ of closure %p (%s): ",
3891 node, info_type(node));
3893 /* should cover all closures that may have a blocking queue */
3894 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3895 get_itbl(node)->type == FETCH_ME_BQ ||
3896 get_itbl(node)->type == RBH ||
3897 get_itbl(node)->type == MVAR);
3899 ASSERT(node!=(StgClosure*)NULL); // sanity check
3901 print_bqe(((StgBlockingQueue*)node)->blocking_queue);
3905 Print a whole blocking queue starting with the element bqe.
3908 print_bqe (StgBlockingQueueElement *bqe)
3913 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3915 for (end = (bqe==END_BQ_QUEUE);
3916 !end; // iterate until bqe points to a CONSTR
3917 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE),
3918 bqe = end ? END_BQ_QUEUE : bqe->link) {
3919 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3920 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3921 /* types of closures that may appear in a blocking queue */
3922 ASSERT(get_itbl(bqe)->type == TSO ||
3923 get_itbl(bqe)->type == BLOCKED_FETCH ||
3924 get_itbl(bqe)->type == CONSTR);
3925 /* only BQs of an RBH end with an RBH_Save closure */
3926 //ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3928 switch (get_itbl(bqe)->type) {
3930 fprintf(stderr," TSO %u (%x),",
3931 ((StgTSO *)bqe)->id, ((StgTSO *)bqe));
3934 fprintf(stderr," BF (node=%p, ga=((%x, %d, %x)),",
3935 ((StgBlockedFetch *)bqe)->node,
3936 ((StgBlockedFetch *)bqe)->ga.payload.gc.gtid,
3937 ((StgBlockedFetch *)bqe)->ga.payload.gc.slot,
3938 ((StgBlockedFetch *)bqe)->ga.weight);
3941 fprintf(stderr," %s (IP %p),",
3942 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3943 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3944 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3945 "RBH_Save_?"), get_itbl(bqe));
3948 barf("Unexpected closure type %s in blocking queue", // of %p (%s)",
3949 info_type((StgClosure *)bqe)); // , node, info_type(node));
3953 fputc('\n', stderr);
3955 # elif defined(GRAN)
3957 print_bq (StgClosure *node)
3959 StgBlockingQueueElement *bqe;
3960 PEs node_loc, tso_loc;
3963 /* should cover all closures that may have a blocking queue */
3964 ASSERT(get_itbl(node)->type == BLACKHOLE_BQ ||
3965 get_itbl(node)->type == FETCH_ME_BQ ||
3966 get_itbl(node)->type == RBH);
3968 ASSERT(node!=(StgClosure*)NULL); // sanity check
3969 node_loc = where_is(node);
3971 fprintf(stderr,"## BQ of closure %p (%s) on [PE %d]: ",
3972 node, info_type(node), node_loc);
3975 NB: In a parallel setup a BQ of an RBH must end with an RBH_Save closure;
3977 for (bqe = ((StgBlockingQueue*)node)->blocking_queue, end = (bqe==END_BQ_QUEUE);
3978 !end; // iterate until bqe points to a CONSTR
3979 end = (get_itbl(bqe)->type == CONSTR) || (bqe->link==END_BQ_QUEUE), bqe = end ? END_BQ_QUEUE : bqe->link) {
3980 ASSERT(bqe != END_BQ_QUEUE); // sanity check
3981 ASSERT(bqe != (StgBlockingQueueElement *)NULL); // sanity check
3982 /* types of closures that may appear in a blocking queue */
3983 ASSERT(get_itbl(bqe)->type == TSO ||
3984 get_itbl(bqe)->type == CONSTR);
3985 /* only BQs of an RBH end with an RBH_Save closure */
3986 ASSERT(get_itbl(bqe)->type != CONSTR || get_itbl(node)->type == RBH);
3988 tso_loc = where_is((StgClosure *)bqe);
3989 switch (get_itbl(bqe)->type) {
3991 fprintf(stderr," TSO %d (%p) on [PE %d],",
3992 ((StgTSO *)bqe)->id, (StgTSO *)bqe, tso_loc);
3995 fprintf(stderr," %s (IP %p),",
3996 (get_itbl(bqe) == &stg_RBH_Save_0_info ? "RBH_Save_0" :
3997 get_itbl(bqe) == &stg_RBH_Save_1_info ? "RBH_Save_1" :
3998 get_itbl(bqe) == &stg_RBH_Save_2_info ? "RBH_Save_2" :
3999 "RBH_Save_?"), get_itbl(bqe));
4002 barf("Unexpected closure type %s in blocking queue of %p (%s)",
4003 info_type((StgClosure *)bqe), node, info_type(node));
4007 fputc('\n', stderr);
4011 Nice and easy: only TSOs on the blocking queue
4014 print_bq (StgClosure *node)
4018 ASSERT(node!=(StgClosure*)NULL); // sanity check
4019 for (tso = ((StgBlockingQueue*)node)->blocking_queue;
4020 tso != END_TSO_QUEUE;
4022 ASSERT(tso!=NULL && tso!=END_TSO_QUEUE); // sanity check
4023 ASSERT(get_itbl(tso)->type == TSO); // guess what, sanity check
4024 fprintf(stderr," TSO %d (%p),", tso->id, tso);
4026 fputc('\n', stderr);
4037 for (i=0, tso=run_queue_hd;
4038 tso != END_TSO_QUEUE;
4047 sched_belch(char *s, ...)
4052 fprintf(stderr, "scheduler (task %ld): ", osThreadId());
4054 fprintf(stderr, "== ");
4056 fprintf(stderr, "scheduler: ");
4058 vfprintf(stderr, s, ap);
4059 fprintf(stderr, "\n");
4066 //@node Index, , Debugging Routines, Main scheduling code
4070 //* StgMainThread:: @cindex\s-+StgMainThread
4071 //* awaken_blocked_queue:: @cindex\s-+awaken_blocked_queue
4072 //* blocked_queue_hd:: @cindex\s-+blocked_queue_hd
4073 //* blocked_queue_tl:: @cindex\s-+blocked_queue_tl
4074 //* context_switch:: @cindex\s-+context_switch
4075 //* createThread:: @cindex\s-+createThread
4076 //* gc_pending_cond:: @cindex\s-+gc_pending_cond
4077 //* initScheduler:: @cindex\s-+initScheduler
4078 //* interrupted:: @cindex\s-+interrupted
4079 //* next_thread_id:: @cindex\s-+next_thread_id
4080 //* print_bq:: @cindex\s-+print_bq
4081 //* run_queue_hd:: @cindex\s-+run_queue_hd
4082 //* run_queue_tl:: @cindex\s-+run_queue_tl
4083 //* sched_mutex:: @cindex\s-+sched_mutex
4084 //* schedule:: @cindex\s-+schedule
4085 //* take_off_run_queue:: @cindex\s-+take_off_run_queue
4086 //* term_mutex:: @cindex\s-+term_mutex