Merge remote-tracking branch 'origin/pretty-build'
[ghc.git] / rts / Schedule.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2006
4 *
5 * The scheduler and thread-related functionality
6 *
7 * --------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #define KEEP_LOCKCLOSURE
11 #include "Rts.h"
12
13 #include "sm/Storage.h"
14 #include "RtsUtils.h"
15 #include "StgRun.h"
16 #include "Schedule.h"
17 #include "Interpreter.h"
18 #include "Printer.h"
19 #include "RtsSignals.h"
20 #include "sm/Sanity.h"
21 #include "Stats.h"
22 #include "STM.h"
23 #include "Prelude.h"
24 #include "ThreadLabels.h"
25 #include "Updates.h"
26 #include "Proftimer.h"
27 #include "ProfHeap.h"
28 #include "Weak.h"
29 #include "sm/GC.h" // waitForGcThreads, releaseGCThreads, N
30 #include "sm/GCThread.h"
31 #include "Sparks.h"
32 #include "Capability.h"
33 #include "Task.h"
34 #include "AwaitEvent.h"
35 #if defined(mingw32_HOST_OS)
36 #include "win32/IOManager.h"
37 #endif
38 #include "Trace.h"
39 #include "RaiseAsync.h"
40 #include "Threads.h"
41 #include "Timer.h"
42 #include "ThreadPaused.h"
43 #include "Messages.h"
44 #include "Stable.h"
45
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #include <string.h>
54 #include <stdlib.h>
55 #include <stdarg.h>
56
57 #ifdef HAVE_ERRNO_H
58 #include <errno.h>
59 #endif
60
61 #ifdef TRACING
62 #include "eventlog/EventLog.h"
63 #endif
64 /* -----------------------------------------------------------------------------
65 * Global variables
66 * -------------------------------------------------------------------------- */
67
68 #if !defined(THREADED_RTS)
69 // Blocked/sleeping thrads
70 StgTSO *blocked_queue_hd = NULL;
71 StgTSO *blocked_queue_tl = NULL;
72 StgTSO *sleeping_queue = NULL; // perhaps replace with a hash table?
73 #endif
74
75 /* Set to true when the latest garbage collection failed to reclaim
76 * enough space, and the runtime should proceed to shut itself down in
77 * an orderly fashion (emitting profiling info etc.)
78 */
79 rtsBool heap_overflow = rtsFalse;
80
81 /* flag that tracks whether we have done any execution in this time slice.
82 * LOCK: currently none, perhaps we should lock (but needs to be
83 * updated in the fast path of the scheduler).
84 *
85 * NB. must be StgWord, we do xchg() on it.
86 */
87 volatile StgWord recent_activity = ACTIVITY_YES;
88
89 /* if this flag is set as well, give up execution
90 * LOCK: none (changes monotonically)
91 */
92 volatile StgWord sched_state = SCHED_RUNNING;
93
94 /* This is used in `TSO.h' and gcc 2.96 insists that this variable actually
95 * exists - earlier gccs apparently didn't.
96 * -= chak
97 */
98 StgTSO dummy_tso;
99
100 /*
101 * Set to TRUE when entering a shutdown state (via shutdownHaskellAndExit()) --
102 * in an MT setting, needed to signal that a worker thread shouldn't hang around
103 * in the scheduler when it is out of work.
104 */
105 rtsBool shutting_down_scheduler = rtsFalse;
106
107 /*
108 * This mutex protects most of the global scheduler data in
109 * the THREADED_RTS runtime.
110 */
111 #if defined(THREADED_RTS)
112 Mutex sched_mutex;
113 #endif
114
115 #if !defined(mingw32_HOST_OS)
116 #define FORKPROCESS_PRIMOP_SUPPORTED
117 #endif
118
119 // Local stats
120 #ifdef THREADED_RTS
121 static nat n_failed_trygrab_idles = 0, n_idle_caps = 0;
122 #endif
123
124 /* -----------------------------------------------------------------------------
125 * static function prototypes
126 * -------------------------------------------------------------------------- */
127
128 static Capability *schedule (Capability *initialCapability, Task *task);
129
130 //
131 // These functions all encapsulate parts of the scheduler loop, and are
132 // abstracted only to make the structure and control flow of the
133 // scheduler clearer.
134 //
135 static void schedulePreLoop (void);
136 static void scheduleFindWork (Capability **pcap);
137 #if defined(THREADED_RTS)
138 static void scheduleYield (Capability **pcap, Task *task);
139 #endif
140 #if defined(THREADED_RTS)
141 static nat requestSync (Capability **pcap, Task *task, nat sync_type);
142 static void acquireAllCapabilities(Capability *cap, Task *task);
143 static void releaseAllCapabilities(Capability *cap, Task *task);
144 static void startWorkerTasks (nat from USED_IF_THREADS, nat to USED_IF_THREADS);
145 #endif
146 static void scheduleStartSignalHandlers (Capability *cap);
147 static void scheduleCheckBlockedThreads (Capability *cap);
148 static void scheduleProcessInbox(Capability **cap);
149 static void scheduleDetectDeadlock (Capability **pcap, Task *task);
150 static void schedulePushWork(Capability *cap, Task *task);
151 #if defined(THREADED_RTS)
152 static void scheduleActivateSpark(Capability *cap);
153 #endif
154 static void schedulePostRunThread(Capability *cap, StgTSO *t);
155 static rtsBool scheduleHandleHeapOverflow( Capability *cap, StgTSO *t );
156 static rtsBool scheduleHandleYield( Capability *cap, StgTSO *t,
157 nat prev_what_next );
158 static void scheduleHandleThreadBlocked( StgTSO *t );
159 static rtsBool scheduleHandleThreadFinished( Capability *cap, Task *task,
160 StgTSO *t );
161 static rtsBool scheduleNeedHeapProfile(rtsBool ready_to_gc);
162 static void scheduleDoGC(Capability **pcap, Task *task, rtsBool force_major);
163
164 static void deleteThread (Capability *cap, StgTSO *tso);
165 static void deleteAllThreads (Capability *cap);
166
167 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
168 static void deleteThread_(Capability *cap, StgTSO *tso);
169 #endif
170
171 /* ---------------------------------------------------------------------------
172 Main scheduling loop.
173
174 We use round-robin scheduling, each thread returning to the
175 scheduler loop when one of these conditions is detected:
176
177 * out of heap space
178 * timer expires (thread yields)
179 * thread blocks
180 * thread ends
181 * stack overflow
182
183 GRAN version:
184 In a GranSim setup this loop iterates over the global event queue.
185 This revolves around the global event queue, which determines what
186 to do next. Therefore, it's more complicated than either the
187 concurrent or the parallel (GUM) setup.
188 This version has been entirely removed (JB 2008/08).
189
190 GUM version:
191 GUM iterates over incoming messages.
192 It starts with nothing to do (thus CurrentTSO == END_TSO_QUEUE),
193 and sends out a fish whenever it has nothing to do; in-between
194 doing the actual reductions (shared code below) it processes the
195 incoming messages and deals with delayed operations
196 (see PendingFetches).
197 This is not the ugliest code you could imagine, but it's bloody close.
198
199 (JB 2008/08) This version was formerly indicated by a PP-Flag PAR,
200 now by PP-flag PARALLEL_HASKELL. The Eden RTS (in GHC-6.x) uses it,
201 as well as future GUM versions. This file has been refurbished to
202 only contain valid code, which is however incomplete, refers to
203 invalid includes etc.
204
205 ------------------------------------------------------------------------ */
206
207 static Capability *
208 schedule (Capability *initialCapability, Task *task)
209 {
210 StgTSO *t;
211 Capability *cap;
212 StgThreadReturnCode ret;
213 nat prev_what_next;
214 rtsBool ready_to_gc;
215 #if defined(THREADED_RTS)
216 rtsBool first = rtsTrue;
217 #endif
218
219 cap = initialCapability;
220
221 // Pre-condition: this task owns initialCapability.
222 // The sched_mutex is *NOT* held
223 // NB. on return, we still hold a capability.
224
225 debugTrace (DEBUG_sched, "cap %d: schedule()", initialCapability->no);
226
227 schedulePreLoop();
228
229 // -----------------------------------------------------------
230 // Scheduler loop starts here:
231
232 while (1) {
233
234 // Check whether we have re-entered the RTS from Haskell without
235 // going via suspendThread()/resumeThread (i.e. a 'safe' foreign
236 // call).
237 if (cap->in_haskell) {
238 errorBelch("schedule: re-entered unsafely.\n"
239 " Perhaps a 'foreign import unsafe' should be 'safe'?");
240 stg_exit(EXIT_FAILURE);
241 }
242
243 // The interruption / shutdown sequence.
244 //
245 // In order to cleanly shut down the runtime, we want to:
246 // * make sure that all main threads return to their callers
247 // with the state 'Interrupted'.
248 // * clean up all OS threads assocated with the runtime
249 // * free all memory etc.
250 //
251 // So the sequence for ^C goes like this:
252 //
253 // * ^C handler sets sched_state := SCHED_INTERRUPTING and
254 // arranges for some Capability to wake up
255 //
256 // * all threads in the system are halted, and the zombies are
257 // placed on the run queue for cleaning up. We acquire all
258 // the capabilities in order to delete the threads, this is
259 // done by scheduleDoGC() for convenience (because GC already
260 // needs to acquire all the capabilities). We can't kill
261 // threads involved in foreign calls.
262 //
263 // * somebody calls shutdownHaskell(), which calls exitScheduler()
264 //
265 // * sched_state := SCHED_SHUTTING_DOWN
266 //
267 // * all workers exit when the run queue on their capability
268 // drains. All main threads will also exit when their TSO
269 // reaches the head of the run queue and they can return.
270 //
271 // * eventually all Capabilities will shut down, and the RTS can
272 // exit.
273 //
274 // * We might be left with threads blocked in foreign calls,
275 // we should really attempt to kill these somehow (TODO);
276
277 switch (sched_state) {
278 case SCHED_RUNNING:
279 break;
280 case SCHED_INTERRUPTING:
281 debugTrace(DEBUG_sched, "SCHED_INTERRUPTING");
282 /* scheduleDoGC() deletes all the threads */
283 scheduleDoGC(&cap,task,rtsFalse);
284
285 // after scheduleDoGC(), we must be shutting down. Either some
286 // other Capability did the final GC, or we did it above,
287 // either way we can fall through to the SCHED_SHUTTING_DOWN
288 // case now.
289 ASSERT(sched_state == SCHED_SHUTTING_DOWN);
290 // fall through
291
292 case SCHED_SHUTTING_DOWN:
293 debugTrace(DEBUG_sched, "SCHED_SHUTTING_DOWN");
294 // If we are a worker, just exit. If we're a bound thread
295 // then we will exit below when we've removed our TSO from
296 // the run queue.
297 if (!isBoundTask(task) && emptyRunQueue(cap)) {
298 return cap;
299 }
300 break;
301 default:
302 barf("sched_state: %d", sched_state);
303 }
304
305 scheduleFindWork(&cap);
306
307 /* work pushing, currently relevant only for THREADED_RTS:
308 (pushes threads, wakes up idle capabilities for stealing) */
309 schedulePushWork(cap,task);
310
311 scheduleDetectDeadlock(&cap,task);
312
313 // Normally, the only way we can get here with no threads to
314 // run is if a keyboard interrupt received during
315 // scheduleCheckBlockedThreads() or scheduleDetectDeadlock().
316 // Additionally, it is not fatal for the
317 // threaded RTS to reach here with no threads to run.
318 //
319 // win32: might be here due to awaitEvent() being abandoned
320 // as a result of a console event having been delivered.
321
322 #if defined(THREADED_RTS)
323 if (first)
324 {
325 // XXX: ToDo
326 // // don't yield the first time, we want a chance to run this
327 // // thread for a bit, even if there are others banging at the
328 // // door.
329 // first = rtsFalse;
330 // ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
331 }
332
333 scheduleYield(&cap,task);
334
335 if (emptyRunQueue(cap)) continue; // look for work again
336 #endif
337
338 #if !defined(THREADED_RTS) && !defined(mingw32_HOST_OS)
339 if ( emptyRunQueue(cap) ) {
340 ASSERT(sched_state >= SCHED_INTERRUPTING);
341 }
342 #endif
343
344 //
345 // Get a thread to run
346 //
347 t = popRunQueue(cap);
348
349 // Sanity check the thread we're about to run. This can be
350 // expensive if there is lots of thread switching going on...
351 IF_DEBUG(sanity,checkTSO(t));
352
353 #if defined(THREADED_RTS)
354 // Check whether we can run this thread in the current task.
355 // If not, we have to pass our capability to the right task.
356 {
357 InCall *bound = t->bound;
358
359 if (bound) {
360 if (bound->task == task) {
361 // yes, the Haskell thread is bound to the current native thread
362 } else {
363 debugTrace(DEBUG_sched,
364 "thread %lu bound to another OS thread",
365 (unsigned long)t->id);
366 // no, bound to a different Haskell thread: pass to that thread
367 pushOnRunQueue(cap,t);
368 continue;
369 }
370 } else {
371 // The thread we want to run is unbound.
372 if (task->incall->tso) {
373 debugTrace(DEBUG_sched,
374 "this OS thread cannot run thread %lu",
375 (unsigned long)t->id);
376 // no, the current native thread is bound to a different
377 // Haskell thread, so pass it to any worker thread
378 pushOnRunQueue(cap,t);
379 continue;
380 }
381 }
382 }
383 #endif
384
385 // If we're shutting down, and this thread has not yet been
386 // killed, kill it now. This sometimes happens when a finalizer
387 // thread is created by the final GC, or a thread previously
388 // in a foreign call returns.
389 if (sched_state >= SCHED_INTERRUPTING &&
390 !(t->what_next == ThreadComplete || t->what_next == ThreadKilled)) {
391 deleteThread(cap,t);
392 }
393
394 // If this capability is disabled, migrate the thread away rather
395 // than running it. NB. but not if the thread is bound: it is
396 // really hard for a bound thread to migrate itself. Believe me,
397 // I tried several ways and couldn't find a way to do it.
398 // Instead, when everything is stopped for GC, we migrate all the
399 // threads on the run queue then (see scheduleDoGC()).
400 //
401 // ToDo: what about TSO_LOCKED? Currently we're migrating those
402 // when the number of capabilities drops, but we never migrate
403 // them back if it rises again. Presumably we should, but after
404 // the thread has been migrated we no longer know what capability
405 // it was originally on.
406 #ifdef THREADED_RTS
407 if (cap->disabled && !t->bound) {
408 Capability *dest_cap = &capabilities[cap->no % enabled_capabilities];
409 migrateThread(cap, t, dest_cap);
410 continue;
411 }
412 #endif
413
414 /* context switches are initiated by the timer signal, unless
415 * the user specified "context switch as often as possible", with
416 * +RTS -C0
417 */
418 if (RtsFlags.ConcFlags.ctxtSwitchTicks == 0
419 && !emptyThreadQueues(cap)) {
420 cap->context_switch = 1;
421 }
422
423 run_thread:
424
425 // CurrentTSO is the thread to run. t might be different if we
426 // loop back to run_thread, so make sure to set CurrentTSO after
427 // that.
428 cap->r.rCurrentTSO = t;
429
430 startHeapProfTimer();
431
432 // ----------------------------------------------------------------------
433 // Run the current thread
434
435 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
436 ASSERT(t->cap == cap);
437 ASSERT(t->bound ? t->bound->task->cap == cap : 1);
438
439 prev_what_next = t->what_next;
440
441 errno = t->saved_errno;
442 #if mingw32_HOST_OS
443 SetLastError(t->saved_winerror);
444 #endif
445
446 // reset the interrupt flag before running Haskell code
447 cap->interrupt = 0;
448
449 cap->in_haskell = rtsTrue;
450 cap->idle = 0;
451
452 dirty_TSO(cap,t);
453 dirty_STACK(cap,t->stackobj);
454
455 #if defined(THREADED_RTS)
456 if (recent_activity == ACTIVITY_DONE_GC) {
457 // ACTIVITY_DONE_GC means we turned off the timer signal to
458 // conserve power (see #1623). Re-enable it here.
459 nat prev;
460 prev = xchg((P_)&recent_activity, ACTIVITY_YES);
461 if (prev == ACTIVITY_DONE_GC) {
462 startTimer();
463 }
464 } else if (recent_activity != ACTIVITY_INACTIVE) {
465 // If we reached ACTIVITY_INACTIVE, then don't reset it until
466 // we've done the GC. The thread running here might just be
467 // the IO manager thread that handle_tick() woke up via
468 // wakeUpRts().
469 recent_activity = ACTIVITY_YES;
470 }
471 #endif
472
473 traceEventRunThread(cap, t);
474
475 switch (prev_what_next) {
476
477 case ThreadKilled:
478 case ThreadComplete:
479 /* Thread already finished, return to scheduler. */
480 ret = ThreadFinished;
481 break;
482
483 case ThreadRunGHC:
484 {
485 StgRegTable *r;
486 r = StgRun((StgFunPtr) stg_returnToStackTop, &cap->r);
487 cap = regTableToCapability(r);
488 ret = r->rRet;
489 break;
490 }
491
492 case ThreadInterpret:
493 cap = interpretBCO(cap);
494 ret = cap->r.rRet;
495 break;
496
497 default:
498 barf("schedule: invalid what_next field");
499 }
500
501 cap->in_haskell = rtsFalse;
502
503 // The TSO might have moved, eg. if it re-entered the RTS and a GC
504 // happened. So find the new location:
505 t = cap->r.rCurrentTSO;
506
507 // And save the current errno in this thread.
508 // XXX: possibly bogus for SMP because this thread might already
509 // be running again, see code below.
510 t->saved_errno = errno;
511 #if mingw32_HOST_OS
512 // Similarly for Windows error code
513 t->saved_winerror = GetLastError();
514 #endif
515
516 if (ret == ThreadBlocked) {
517 if (t->why_blocked == BlockedOnBlackHole) {
518 StgTSO *owner = blackHoleOwner(t->block_info.bh->bh);
519 traceEventStopThread(cap, t, t->why_blocked + 6,
520 owner != NULL ? owner->id : 0);
521 } else {
522 traceEventStopThread(cap, t, t->why_blocked + 6, 0);
523 }
524 } else {
525 traceEventStopThread(cap, t, ret, 0);
526 }
527
528 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
529 ASSERT(t->cap == cap);
530
531 // ----------------------------------------------------------------------
532
533 // Costs for the scheduler are assigned to CCS_SYSTEM
534 stopHeapProfTimer();
535 #if defined(PROFILING)
536 cap->r.rCCCS = CCS_SYSTEM;
537 #endif
538
539 schedulePostRunThread(cap,t);
540
541 ready_to_gc = rtsFalse;
542
543 switch (ret) {
544 case HeapOverflow:
545 ready_to_gc = scheduleHandleHeapOverflow(cap,t);
546 break;
547
548 case StackOverflow:
549 // just adjust the stack for this thread, then pop it back
550 // on the run queue.
551 threadStackOverflow(cap, t);
552 pushOnRunQueue(cap,t);
553 break;
554
555 case ThreadYielding:
556 if (scheduleHandleYield(cap, t, prev_what_next)) {
557 // shortcut for switching between compiler/interpreter:
558 goto run_thread;
559 }
560 break;
561
562 case ThreadBlocked:
563 scheduleHandleThreadBlocked(t);
564 break;
565
566 case ThreadFinished:
567 if (scheduleHandleThreadFinished(cap, task, t)) return cap;
568 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
569 break;
570
571 default:
572 barf("schedule: invalid thread return code %d", (int)ret);
573 }
574
575 if (ready_to_gc || scheduleNeedHeapProfile(ready_to_gc)) {
576 scheduleDoGC(&cap,task,rtsFalse);
577 }
578 } /* end of while() */
579 }
580
581 /* -----------------------------------------------------------------------------
582 * Run queue operations
583 * -------------------------------------------------------------------------- */
584
585 void
586 removeFromRunQueue (Capability *cap, StgTSO *tso)
587 {
588 if (tso->block_info.prev == END_TSO_QUEUE) {
589 ASSERT(cap->run_queue_hd == tso);
590 cap->run_queue_hd = tso->_link;
591 } else {
592 setTSOLink(cap, tso->block_info.prev, tso->_link);
593 }
594 if (tso->_link == END_TSO_QUEUE) {
595 ASSERT(cap->run_queue_tl == tso);
596 cap->run_queue_tl = tso->block_info.prev;
597 } else {
598 setTSOPrev(cap, tso->_link, tso->block_info.prev);
599 }
600 tso->_link = tso->block_info.prev = END_TSO_QUEUE;
601
602 IF_DEBUG(sanity, checkRunQueue(cap));
603 }
604
605 /* ----------------------------------------------------------------------------
606 * Setting up the scheduler loop
607 * ------------------------------------------------------------------------- */
608
609 static void
610 schedulePreLoop(void)
611 {
612 // initialisation for scheduler - what cannot go into initScheduler()
613
614 #if defined(mingw32_HOST_OS) && !defined(USE_MINIINTERPRETER)
615 win32AllocStack();
616 #endif
617 }
618
619 /* -----------------------------------------------------------------------------
620 * scheduleFindWork()
621 *
622 * Search for work to do, and handle messages from elsewhere.
623 * -------------------------------------------------------------------------- */
624
625 static void
626 scheduleFindWork (Capability **pcap)
627 {
628 scheduleStartSignalHandlers(*pcap);
629
630 scheduleProcessInbox(pcap);
631
632 scheduleCheckBlockedThreads(*pcap);
633
634 #if defined(THREADED_RTS)
635 if (emptyRunQueue(*pcap)) { scheduleActivateSpark(*pcap); }
636 #endif
637 }
638
639 #if defined(THREADED_RTS)
640 STATIC_INLINE rtsBool
641 shouldYieldCapability (Capability *cap, Task *task, rtsBool didGcLast)
642 {
643 // we need to yield this capability to someone else if..
644 // - another thread is initiating a GC, and we didn't just do a GC
645 // (see Note [GC livelock])
646 // - another Task is returning from a foreign call
647 // - the thread at the head of the run queue cannot be run
648 // by this Task (it is bound to another Task, or it is unbound
649 // and this task it bound).
650 //
651 // Note [GC livelock]
652 //
653 // If we are interrupted to do a GC, then we do not immediately do
654 // another one. This avoids a starvation situation where one
655 // Capability keeps forcing a GC and the other Capabilities make no
656 // progress at all.
657
658 return ((pending_sync && !didGcLast) ||
659 cap->returning_tasks_hd != NULL ||
660 (!emptyRunQueue(cap) && (task->incall->tso == NULL
661 ? cap->run_queue_hd->bound != NULL
662 : cap->run_queue_hd->bound != task->incall)));
663 }
664
665 // This is the single place where a Task goes to sleep. There are
666 // two reasons it might need to sleep:
667 // - there are no threads to run
668 // - we need to yield this Capability to someone else
669 // (see shouldYieldCapability())
670 //
671 // Careful: the scheduler loop is quite delicate. Make sure you run
672 // the tests in testsuite/concurrent (all ways) after modifying this,
673 // and also check the benchmarks in nofib/parallel for regressions.
674
675 static void
676 scheduleYield (Capability **pcap, Task *task)
677 {
678 Capability *cap = *pcap;
679 int didGcLast = rtsFalse;
680
681 // if we have work, and we don't need to give up the Capability, continue.
682 //
683 if (!shouldYieldCapability(cap,task,rtsFalse) &&
684 (!emptyRunQueue(cap) ||
685 !emptyInbox(cap) ||
686 sched_state >= SCHED_INTERRUPTING)) {
687 return;
688 }
689
690 // otherwise yield (sleep), and keep yielding if necessary.
691 do {
692 didGcLast = yieldCapability(&cap,task, !didGcLast);
693 }
694 while (shouldYieldCapability(cap,task,didGcLast));
695
696 // note there may still be no threads on the run queue at this
697 // point, the caller has to check.
698
699 *pcap = cap;
700 return;
701 }
702 #endif
703
704 /* -----------------------------------------------------------------------------
705 * schedulePushWork()
706 *
707 * Push work to other Capabilities if we have some.
708 * -------------------------------------------------------------------------- */
709
710 static void
711 schedulePushWork(Capability *cap USED_IF_THREADS,
712 Task *task USED_IF_THREADS)
713 {
714 /* following code not for PARALLEL_HASKELL. I kept the call general,
715 future GUM versions might use pushing in a distributed setup */
716 #if defined(THREADED_RTS)
717
718 Capability *free_caps[n_capabilities], *cap0;
719 nat i, n_free_caps;
720
721 // migration can be turned off with +RTS -qm
722 if (!RtsFlags.ParFlags.migrate) return;
723
724 // Check whether we have more threads on our run queue, or sparks
725 // in our pool, that we could hand to another Capability.
726 if (cap->run_queue_hd == END_TSO_QUEUE) {
727 if (sparkPoolSizeCap(cap) < 2) return;
728 } else {
729 if (cap->run_queue_hd->_link == END_TSO_QUEUE &&
730 sparkPoolSizeCap(cap) < 1) return;
731 }
732
733 // First grab as many free Capabilities as we can.
734 for (i=0, n_free_caps=0; i < n_capabilities; i++) {
735 cap0 = &capabilities[i];
736 if (cap != cap0 && !cap0->disabled && tryGrabCapability(cap0,task)) {
737 if (!emptyRunQueue(cap0)
738 || cap0->returning_tasks_hd != NULL
739 || cap0->inbox != (Message*)END_TSO_QUEUE) {
740 // it already has some work, we just grabbed it at
741 // the wrong moment. Or maybe it's deadlocked!
742 releaseCapability(cap0);
743 } else {
744 free_caps[n_free_caps++] = cap0;
745 }
746 }
747 }
748
749 // we now have n_free_caps free capabilities stashed in
750 // free_caps[]. Share our run queue equally with them. This is
751 // probably the simplest thing we could do; improvements we might
752 // want to do include:
753 //
754 // - giving high priority to moving relatively new threads, on
755 // the gournds that they haven't had time to build up a
756 // working set in the cache on this CPU/Capability.
757 //
758 // - giving low priority to moving long-lived threads
759
760 if (n_free_caps > 0) {
761 StgTSO *prev, *t, *next;
762 #ifdef SPARK_PUSHING
763 rtsBool pushed_to_all;
764 #endif
765
766 debugTrace(DEBUG_sched,
767 "cap %d: %s and %d free capabilities, sharing...",
768 cap->no,
769 (!emptyRunQueue(cap) && cap->run_queue_hd->_link != END_TSO_QUEUE)?
770 "excess threads on run queue":"sparks to share (>=2)",
771 n_free_caps);
772
773 i = 0;
774 #ifdef SPARK_PUSHING
775 pushed_to_all = rtsFalse;
776 #endif
777
778 if (cap->run_queue_hd != END_TSO_QUEUE) {
779 prev = cap->run_queue_hd;
780 t = prev->_link;
781 prev->_link = END_TSO_QUEUE;
782 for (; t != END_TSO_QUEUE; t = next) {
783 next = t->_link;
784 t->_link = END_TSO_QUEUE;
785 if (t->bound == task->incall // don't move my bound thread
786 || tsoLocked(t)) { // don't move a locked thread
787 setTSOLink(cap, prev, t);
788 setTSOPrev(cap, t, prev);
789 prev = t;
790 } else if (i == n_free_caps) {
791 #ifdef SPARK_PUSHING
792 pushed_to_all = rtsTrue;
793 #endif
794 i = 0;
795 // keep one for us
796 setTSOLink(cap, prev, t);
797 setTSOPrev(cap, t, prev);
798 prev = t;
799 } else {
800 appendToRunQueue(free_caps[i],t);
801
802 traceEventMigrateThread (cap, t, free_caps[i]->no);
803
804 if (t->bound) { t->bound->task->cap = free_caps[i]; }
805 t->cap = free_caps[i];
806 i++;
807 }
808 }
809 cap->run_queue_tl = prev;
810
811 IF_DEBUG(sanity, checkRunQueue(cap));
812 }
813
814 #ifdef SPARK_PUSHING
815 /* JB I left this code in place, it would work but is not necessary */
816
817 // If there are some free capabilities that we didn't push any
818 // threads to, then try to push a spark to each one.
819 if (!pushed_to_all) {
820 StgClosure *spark;
821 // i is the next free capability to push to
822 for (; i < n_free_caps; i++) {
823 if (emptySparkPoolCap(free_caps[i])) {
824 spark = tryStealSpark(cap->sparks);
825 if (spark != NULL) {
826 /* TODO: if anyone wants to re-enable this code then
827 * they must consider the fizzledSpark(spark) case
828 * and update the per-cap spark statistics.
829 */
830 debugTrace(DEBUG_sched, "pushing spark %p to capability %d", spark, free_caps[i]->no);
831
832 traceEventStealSpark(free_caps[i], t, cap->no);
833
834 newSpark(&(free_caps[i]->r), spark);
835 }
836 }
837 }
838 }
839 #endif /* SPARK_PUSHING */
840
841 // release the capabilities
842 for (i = 0; i < n_free_caps; i++) {
843 task->cap = free_caps[i];
844 releaseAndWakeupCapability(free_caps[i]);
845 }
846 }
847 task->cap = cap; // reset to point to our Capability.
848
849 #endif /* THREADED_RTS */
850
851 }
852
853 /* ----------------------------------------------------------------------------
854 * Start any pending signal handlers
855 * ------------------------------------------------------------------------- */
856
857 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
858 static void
859 scheduleStartSignalHandlers(Capability *cap)
860 {
861 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
862 // safe outside the lock
863 startSignalHandlers(cap);
864 }
865 }
866 #else
867 static void
868 scheduleStartSignalHandlers(Capability *cap STG_UNUSED)
869 {
870 }
871 #endif
872
873 /* ----------------------------------------------------------------------------
874 * Check for blocked threads that can be woken up.
875 * ------------------------------------------------------------------------- */
876
877 static void
878 scheduleCheckBlockedThreads(Capability *cap USED_IF_NOT_THREADS)
879 {
880 #if !defined(THREADED_RTS)
881 //
882 // Check whether any waiting threads need to be woken up. If the
883 // run queue is empty, and there are no other tasks running, we
884 // can wait indefinitely for something to happen.
885 //
886 if ( !emptyQueue(blocked_queue_hd) || !emptyQueue(sleeping_queue) )
887 {
888 awaitEvent (emptyRunQueue(cap));
889 }
890 #endif
891 }
892
893 /* ----------------------------------------------------------------------------
894 * Detect deadlock conditions and attempt to resolve them.
895 * ------------------------------------------------------------------------- */
896
897 static void
898 scheduleDetectDeadlock (Capability **pcap, Task *task)
899 {
900 Capability *cap = *pcap;
901 /*
902 * Detect deadlock: when we have no threads to run, there are no
903 * threads blocked, waiting for I/O, or sleeping, and all the
904 * other tasks are waiting for work, we must have a deadlock of
905 * some description.
906 */
907 if ( emptyThreadQueues(cap) )
908 {
909 #if defined(THREADED_RTS)
910 /*
911 * In the threaded RTS, we only check for deadlock if there
912 * has been no activity in a complete timeslice. This means
913 * we won't eagerly start a full GC just because we don't have
914 * any threads to run currently.
915 */
916 if (recent_activity != ACTIVITY_INACTIVE) return;
917 #endif
918
919 debugTrace(DEBUG_sched, "deadlocked, forcing major GC...");
920
921 // Garbage collection can release some new threads due to
922 // either (a) finalizers or (b) threads resurrected because
923 // they are unreachable and will therefore be sent an
924 // exception. Any threads thus released will be immediately
925 // runnable.
926 scheduleDoGC (pcap, task, rtsTrue/*force major GC*/);
927 cap = *pcap;
928 // when force_major == rtsTrue. scheduleDoGC sets
929 // recent_activity to ACTIVITY_DONE_GC and turns off the timer
930 // signal.
931
932 if ( !emptyRunQueue(cap) ) return;
933
934 #if defined(RTS_USER_SIGNALS) && !defined(THREADED_RTS)
935 /* If we have user-installed signal handlers, then wait
936 * for signals to arrive rather then bombing out with a
937 * deadlock.
938 */
939 if ( RtsFlags.MiscFlags.install_signal_handlers && anyUserHandlers() ) {
940 debugTrace(DEBUG_sched,
941 "still deadlocked, waiting for signals...");
942
943 awaitUserSignals();
944
945 if (signals_pending()) {
946 startSignalHandlers(cap);
947 }
948
949 // either we have threads to run, or we were interrupted:
950 ASSERT(!emptyRunQueue(cap) || sched_state >= SCHED_INTERRUPTING);
951
952 return;
953 }
954 #endif
955
956 #if !defined(THREADED_RTS)
957 /* Probably a real deadlock. Send the current main thread the
958 * Deadlock exception.
959 */
960 if (task->incall->tso) {
961 switch (task->incall->tso->why_blocked) {
962 case BlockedOnSTM:
963 case BlockedOnBlackHole:
964 case BlockedOnMsgThrowTo:
965 case BlockedOnMVar:
966 throwToSingleThreaded(cap, task->incall->tso,
967 (StgClosure *)nonTermination_closure);
968 return;
969 default:
970 barf("deadlock: main thread blocked in a strange way");
971 }
972 }
973 return;
974 #endif
975 }
976 }
977
978
979 /* ----------------------------------------------------------------------------
980 * Send pending messages (PARALLEL_HASKELL only)
981 * ------------------------------------------------------------------------- */
982
983 #if defined(PARALLEL_HASKELL)
984 static void
985 scheduleSendPendingMessages(void)
986 {
987
988 # if defined(PAR) // global Mem.Mgmt., omit for now
989 if (PendingFetches != END_BF_QUEUE) {
990 processFetches();
991 }
992 # endif
993
994 if (RtsFlags.ParFlags.BufferTime) {
995 // if we use message buffering, we must send away all message
996 // packets which have become too old...
997 sendOldBuffers();
998 }
999 }
1000 #endif
1001
1002 /* ----------------------------------------------------------------------------
1003 * Process message in the current Capability's inbox
1004 * ------------------------------------------------------------------------- */
1005
1006 static void
1007 scheduleProcessInbox (Capability **pcap USED_IF_THREADS)
1008 {
1009 #if defined(THREADED_RTS)
1010 Message *m, *next;
1011 int r;
1012 Capability *cap = *pcap;
1013
1014 while (!emptyInbox(cap)) {
1015 if (cap->r.rCurrentNursery->link == NULL ||
1016 g0->n_new_large_words >= large_alloc_lim) {
1017 scheduleDoGC(pcap, cap->running_task, rtsFalse);
1018 cap = *pcap;
1019 }
1020
1021 // don't use a blocking acquire; if the lock is held by
1022 // another thread then just carry on. This seems to avoid
1023 // getting stuck in a message ping-pong situation with other
1024 // processors. We'll check the inbox again later anyway.
1025 //
1026 // We should really use a more efficient queue data structure
1027 // here. The trickiness is that we must ensure a Capability
1028 // never goes idle if the inbox is non-empty, which is why we
1029 // use cap->lock (cap->lock is released as the last thing
1030 // before going idle; see Capability.c:releaseCapability()).
1031 r = TRY_ACQUIRE_LOCK(&cap->lock);
1032 if (r != 0) return;
1033
1034 m = cap->inbox;
1035 cap->inbox = (Message*)END_TSO_QUEUE;
1036
1037 RELEASE_LOCK(&cap->lock);
1038
1039 while (m != (Message*)END_TSO_QUEUE) {
1040 next = m->link;
1041 executeMessage(cap, m);
1042 m = next;
1043 }
1044 }
1045 #endif
1046 }
1047
1048 /* ----------------------------------------------------------------------------
1049 * Activate spark threads (PARALLEL_HASKELL and THREADED_RTS)
1050 * ------------------------------------------------------------------------- */
1051
1052 #if defined(THREADED_RTS)
1053 static void
1054 scheduleActivateSpark(Capability *cap)
1055 {
1056 if (anySparks() && !cap->disabled)
1057 {
1058 createSparkThread(cap);
1059 debugTrace(DEBUG_sched, "creating a spark thread");
1060 }
1061 }
1062 #endif // PARALLEL_HASKELL || THREADED_RTS
1063
1064 /* ----------------------------------------------------------------------------
1065 * After running a thread...
1066 * ------------------------------------------------------------------------- */
1067
1068 static void
1069 schedulePostRunThread (Capability *cap, StgTSO *t)
1070 {
1071 // We have to be able to catch transactions that are in an
1072 // infinite loop as a result of seeing an inconsistent view of
1073 // memory, e.g.
1074 //
1075 // atomically $ do
1076 // [a,b] <- mapM readTVar [ta,tb]
1077 // when (a == b) loop
1078 //
1079 // and a is never equal to b given a consistent view of memory.
1080 //
1081 if (t -> trec != NO_TREC && t -> why_blocked == NotBlocked) {
1082 if (!stmValidateNestOfTransactions (t -> trec)) {
1083 debugTrace(DEBUG_sched | DEBUG_stm,
1084 "trec %p found wasting its time", t);
1085
1086 // strip the stack back to the
1087 // ATOMICALLY_FRAME, aborting the (nested)
1088 // transaction, and saving the stack of any
1089 // partially-evaluated thunks on the heap.
1090 throwToSingleThreaded_(cap, t, NULL, rtsTrue);
1091
1092 // ASSERT(get_itbl((StgClosure *)t->sp)->type == ATOMICALLY_FRAME);
1093 }
1094 }
1095
1096 /* some statistics gathering in the parallel case */
1097 }
1098
1099 /* -----------------------------------------------------------------------------
1100 * Handle a thread that returned to the scheduler with ThreadHeepOverflow
1101 * -------------------------------------------------------------------------- */
1102
1103 static rtsBool
1104 scheduleHandleHeapOverflow( Capability *cap, StgTSO *t )
1105 {
1106 // did the task ask for a large block?
1107 if (cap->r.rHpAlloc > BLOCK_SIZE) {
1108 // if so, get one and push it on the front of the nursery.
1109 bdescr *bd;
1110 lnat blocks;
1111
1112 blocks = (lnat)BLOCK_ROUND_UP(cap->r.rHpAlloc) / BLOCK_SIZE;
1113
1114 if (blocks > BLOCKS_PER_MBLOCK) {
1115 barf("allocation of %ld bytes too large (GHC should have complained at compile-time)", (long)cap->r.rHpAlloc);
1116 }
1117
1118 debugTrace(DEBUG_sched,
1119 "--<< thread %ld (%s) stopped: requesting a large block (size %ld)\n",
1120 (long)t->id, what_next_strs[t->what_next], blocks);
1121
1122 // don't do this if the nursery is (nearly) full, we'll GC first.
1123 if (cap->r.rCurrentNursery->link != NULL ||
1124 cap->r.rNursery->n_blocks == 1) { // paranoia to prevent infinite loop
1125 // if the nursery has only one block.
1126
1127 bd = allocGroup_lock(blocks);
1128 cap->r.rNursery->n_blocks += blocks;
1129
1130 // link the new group into the list
1131 bd->link = cap->r.rCurrentNursery;
1132 bd->u.back = cap->r.rCurrentNursery->u.back;
1133 if (cap->r.rCurrentNursery->u.back != NULL) {
1134 cap->r.rCurrentNursery->u.back->link = bd;
1135 } else {
1136 cap->r.rNursery->blocks = bd;
1137 }
1138 cap->r.rCurrentNursery->u.back = bd;
1139
1140 // initialise it as a nursery block. We initialise the
1141 // step, gen_no, and flags field of *every* sub-block in
1142 // this large block, because this is easier than making
1143 // sure that we always find the block head of a large
1144 // block whenever we call Bdescr() (eg. evacuate() and
1145 // isAlive() in the GC would both have to do this, at
1146 // least).
1147 {
1148 bdescr *x;
1149 for (x = bd; x < bd + blocks; x++) {
1150 initBdescr(x,g0,g0);
1151 x->free = x->start;
1152 x->flags = 0;
1153 }
1154 }
1155
1156 // This assert can be a killer if the app is doing lots
1157 // of large block allocations.
1158 IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
1159
1160 // now update the nursery to point to the new block
1161 cap->r.rCurrentNursery = bd;
1162
1163 // we might be unlucky and have another thread get on the
1164 // run queue before us and steal the large block, but in that
1165 // case the thread will just end up requesting another large
1166 // block.
1167 pushOnRunQueue(cap,t);
1168 return rtsFalse; /* not actually GC'ing */
1169 }
1170 }
1171
1172 if (cap->r.rHpLim == NULL || cap->context_switch) {
1173 // Sometimes we miss a context switch, e.g. when calling
1174 // primitives in a tight loop, MAYBE_GC() doesn't check the
1175 // context switch flag, and we end up waiting for a GC.
1176 // See #1984, and concurrent/should_run/1984
1177 cap->context_switch = 0;
1178 appendToRunQueue(cap,t);
1179 } else {
1180 pushOnRunQueue(cap,t);
1181 }
1182 return rtsTrue;
1183 /* actual GC is done at the end of the while loop in schedule() */
1184 }
1185
1186 /* -----------------------------------------------------------------------------
1187 * Handle a thread that returned to the scheduler with ThreadYielding
1188 * -------------------------------------------------------------------------- */
1189
1190 static rtsBool
1191 scheduleHandleYield( Capability *cap, StgTSO *t, nat prev_what_next )
1192 {
1193 /* put the thread back on the run queue. Then, if we're ready to
1194 * GC, check whether this is the last task to stop. If so, wake
1195 * up the GC thread. getThread will block during a GC until the
1196 * GC is finished.
1197 */
1198
1199 ASSERT(t->_link == END_TSO_QUEUE);
1200
1201 // Shortcut if we're just switching evaluators: don't bother
1202 // doing stack squeezing (which can be expensive), just run the
1203 // thread.
1204 if (cap->context_switch == 0 && t->what_next != prev_what_next) {
1205 debugTrace(DEBUG_sched,
1206 "--<< thread %ld (%s) stopped to switch evaluators",
1207 (long)t->id, what_next_strs[t->what_next]);
1208 return rtsTrue;
1209 }
1210
1211 // Reset the context switch flag. We don't do this just before
1212 // running the thread, because that would mean we would lose ticks
1213 // during GC, which can lead to unfair scheduling (a thread hogs
1214 // the CPU because the tick always arrives during GC). This way
1215 // penalises threads that do a lot of allocation, but that seems
1216 // better than the alternative.
1217 if (cap->context_switch != 0) {
1218 cap->context_switch = 0;
1219 appendToRunQueue(cap,t);
1220 } else {
1221 pushOnRunQueue(cap,t);
1222 }
1223
1224 IF_DEBUG(sanity,
1225 //debugBelch("&& Doing sanity check on yielding TSO %ld.", t->id);
1226 checkTSO(t));
1227
1228 return rtsFalse;
1229 }
1230
1231 /* -----------------------------------------------------------------------------
1232 * Handle a thread that returned to the scheduler with ThreadBlocked
1233 * -------------------------------------------------------------------------- */
1234
1235 static void
1236 scheduleHandleThreadBlocked( StgTSO *t
1237 #if !defined(DEBUG)
1238 STG_UNUSED
1239 #endif
1240 )
1241 {
1242
1243 // We don't need to do anything. The thread is blocked, and it
1244 // has tidied up its stack and placed itself on whatever queue
1245 // it needs to be on.
1246
1247 // ASSERT(t->why_blocked != NotBlocked);
1248 // Not true: for example,
1249 // - the thread may have woken itself up already, because
1250 // threadPaused() might have raised a blocked throwTo
1251 // exception, see maybePerformBlockedException().
1252
1253 #ifdef DEBUG
1254 traceThreadStatus(DEBUG_sched, t);
1255 #endif
1256 }
1257
1258 /* -----------------------------------------------------------------------------
1259 * Handle a thread that returned to the scheduler with ThreadFinished
1260 * -------------------------------------------------------------------------- */
1261
1262 static rtsBool
1263 scheduleHandleThreadFinished (Capability *cap STG_UNUSED, Task *task, StgTSO *t)
1264 {
1265 /* Need to check whether this was a main thread, and if so,
1266 * return with the return value.
1267 *
1268 * We also end up here if the thread kills itself with an
1269 * uncaught exception, see Exception.cmm.
1270 */
1271
1272 // blocked exceptions can now complete, even if the thread was in
1273 // blocked mode (see #2910).
1274 awakenBlockedExceptionQueue (cap, t);
1275
1276 //
1277 // Check whether the thread that just completed was a bound
1278 // thread, and if so return with the result.
1279 //
1280 // There is an assumption here that all thread completion goes
1281 // through this point; we need to make sure that if a thread
1282 // ends up in the ThreadKilled state, that it stays on the run
1283 // queue so it can be dealt with here.
1284 //
1285
1286 if (t->bound) {
1287
1288 if (t->bound != task->incall) {
1289 #if !defined(THREADED_RTS)
1290 // Must be a bound thread that is not the topmost one. Leave
1291 // it on the run queue until the stack has unwound to the
1292 // point where we can deal with this. Leaving it on the run
1293 // queue also ensures that the garbage collector knows about
1294 // this thread and its return value (it gets dropped from the
1295 // step->threads list so there's no other way to find it).
1296 appendToRunQueue(cap,t);
1297 return rtsFalse;
1298 #else
1299 // this cannot happen in the threaded RTS, because a
1300 // bound thread can only be run by the appropriate Task.
1301 barf("finished bound thread that isn't mine");
1302 #endif
1303 }
1304
1305 ASSERT(task->incall->tso == t);
1306
1307 if (t->what_next == ThreadComplete) {
1308 if (task->incall->ret) {
1309 // NOTE: return val is stack->sp[1] (see StgStartup.hc)
1310 *(task->incall->ret) = (StgClosure *)task->incall->tso->stackobj->sp[1];
1311 }
1312 task->incall->stat = Success;
1313 } else {
1314 if (task->incall->ret) {
1315 *(task->incall->ret) = NULL;
1316 }
1317 if (sched_state >= SCHED_INTERRUPTING) {
1318 if (heap_overflow) {
1319 task->incall->stat = HeapExhausted;
1320 } else {
1321 task->incall->stat = Interrupted;
1322 }
1323 } else {
1324 task->incall->stat = Killed;
1325 }
1326 }
1327 #ifdef DEBUG
1328 removeThreadLabel((StgWord)task->incall->tso->id);
1329 #endif
1330
1331 // We no longer consider this thread and task to be bound to
1332 // each other. The TSO lives on until it is GC'd, but the
1333 // task is about to be released by the caller, and we don't
1334 // want anyone following the pointer from the TSO to the
1335 // defunct task (which might have already been
1336 // re-used). This was a real bug: the GC updated
1337 // tso->bound->tso which lead to a deadlock.
1338 t->bound = NULL;
1339 task->incall->tso = NULL;
1340
1341 return rtsTrue; // tells schedule() to return
1342 }
1343
1344 return rtsFalse;
1345 }
1346
1347 /* -----------------------------------------------------------------------------
1348 * Perform a heap census
1349 * -------------------------------------------------------------------------- */
1350
1351 static rtsBool
1352 scheduleNeedHeapProfile( rtsBool ready_to_gc STG_UNUSED )
1353 {
1354 // When we have +RTS -i0 and we're heap profiling, do a census at
1355 // every GC. This lets us get repeatable runs for debugging.
1356 if (performHeapProfile ||
1357 (RtsFlags.ProfFlags.heapProfileInterval==0 &&
1358 RtsFlags.ProfFlags.doHeapProfile && ready_to_gc)) {
1359 return rtsTrue;
1360 } else {
1361 return rtsFalse;
1362 }
1363 }
1364
1365 /* -----------------------------------------------------------------------------
1366 * Start a synchronisation of all capabilities
1367 * -------------------------------------------------------------------------- */
1368
1369 // Returns:
1370 // 0 if we successfully got a sync
1371 // non-0 if there was another sync request in progress,
1372 // and we yielded to it. The value returned is the
1373 // type of the other sync request.
1374 //
1375 #if defined(THREADED_RTS)
1376 static nat requestSync (Capability **pcap, Task *task, nat sync_type)
1377 {
1378 nat prev_pending_sync;
1379
1380 prev_pending_sync = cas(&pending_sync, 0, sync_type);
1381
1382 if (prev_pending_sync)
1383 {
1384 do {
1385 debugTrace(DEBUG_sched, "someone else is trying to sync (%d)...",
1386 prev_pending_sync);
1387 ASSERT(*pcap);
1388 yieldCapability(pcap,task,rtsTrue);
1389 } while (pending_sync);
1390 return prev_pending_sync; // NOTE: task->cap might have changed now
1391 }
1392 else
1393 {
1394 return 0;
1395 }
1396 }
1397
1398 //
1399 // Grab all the capabilities except the one we already hold. Used
1400 // when synchronising before a single-threaded GC (SYNC_SEQ_GC), and
1401 // before a fork (SYNC_OTHER).
1402 //
1403 // Only call this after requestSync(), otherwise a deadlock might
1404 // ensue if another thread is trying to synchronise.
1405 //
1406 static void acquireAllCapabilities(Capability *cap, Task *task)
1407 {
1408 Capability *tmpcap;
1409 nat i;
1410
1411 for (i=0; i < n_capabilities; i++) {
1412 debugTrace(DEBUG_sched, "grabbing all the capabilies (%d/%d)", i, n_capabilities);
1413 tmpcap = &capabilities[i];
1414 if (tmpcap != cap) {
1415 // we better hope this task doesn't get migrated to
1416 // another Capability while we're waiting for this one.
1417 // It won't, because load balancing happens while we have
1418 // all the Capabilities, but even so it's a slightly
1419 // unsavoury invariant.
1420 task->cap = tmpcap;
1421 waitForReturnCapability(&tmpcap, task);
1422 if (tmpcap->no != i) {
1423 barf("acquireAllCapabilities: got the wrong capability");
1424 }
1425 }
1426 }
1427 task->cap = cap;
1428 }
1429
1430 static void releaseAllCapabilities(Capability *cap, Task *task)
1431 {
1432 nat i;
1433
1434 for (i = 0; i < n_capabilities; i++) {
1435 if (cap->no != i) {
1436 task->cap = &capabilities[i];
1437 releaseCapability(&capabilities[i]);
1438 }
1439 }
1440 task->cap = cap;
1441 }
1442 #endif
1443
1444 /* -----------------------------------------------------------------------------
1445 * Perform a garbage collection if necessary
1446 * -------------------------------------------------------------------------- */
1447
1448 static void
1449 scheduleDoGC (Capability **pcap, Task *task USED_IF_THREADS,
1450 rtsBool force_major)
1451 {
1452 Capability *cap = *pcap;
1453 rtsBool heap_census;
1454 #ifdef THREADED_RTS
1455 rtsBool idle_cap[n_capabilities];
1456 rtsBool gc_type;
1457 nat i, sync;
1458 StgTSO *tso;
1459 #endif
1460
1461 if (sched_state == SCHED_SHUTTING_DOWN) {
1462 // The final GC has already been done, and the system is
1463 // shutting down. We'll probably deadlock if we try to GC
1464 // now.
1465 return;
1466 }
1467
1468 #ifdef THREADED_RTS
1469 if (sched_state < SCHED_INTERRUPTING
1470 && RtsFlags.ParFlags.parGcEnabled
1471 && N >= RtsFlags.ParFlags.parGcGen
1472 && ! oldest_gen->mark)
1473 {
1474 gc_type = SYNC_GC_PAR;
1475 } else {
1476 gc_type = SYNC_GC_SEQ;
1477 }
1478
1479 // In order to GC, there must be no threads running Haskell code.
1480 // Therefore, the GC thread needs to hold *all* the capabilities,
1481 // and release them after the GC has completed.
1482 //
1483 // This seems to be the simplest way: previous attempts involved
1484 // making all the threads with capabilities give up their
1485 // capabilities and sleep except for the *last* one, which
1486 // actually did the GC. But it's quite hard to arrange for all
1487 // the other tasks to sleep and stay asleep.
1488 //
1489
1490 /* Other capabilities are prevented from running yet more Haskell
1491 threads if pending_sync is set. Tested inside
1492 yieldCapability() and releaseCapability() in Capability.c */
1493
1494 do {
1495 sync = requestSync(pcap, task, gc_type);
1496 cap = *pcap;
1497 if (sync == SYNC_GC_SEQ || sync == SYNC_GC_PAR) {
1498 // someone else had a pending sync request for a GC, so
1499 // let's assume GC has been done and we don't need to GC
1500 // again.
1501 return;
1502 }
1503 if (sched_state == SCHED_SHUTTING_DOWN) {
1504 // The scheduler might now be shutting down. We tested
1505 // this above, but it might have become true since then as
1506 // we yielded the capability in requestSync().
1507 return;
1508 }
1509 } while (sync);
1510
1511 interruptAllCapabilities();
1512
1513 // The final shutdown GC is always single-threaded, because it's
1514 // possible that some of the Capabilities have no worker threads.
1515
1516 if (gc_type == SYNC_GC_SEQ)
1517 {
1518 traceEventRequestSeqGc(cap);
1519 }
1520 else
1521 {
1522 traceEventRequestParGc(cap);
1523 debugTrace(DEBUG_sched, "ready_to_gc, grabbing GC threads");
1524 }
1525
1526 if (gc_type == SYNC_GC_SEQ)
1527 {
1528 // single-threaded GC: grab all the capabilities
1529 acquireAllCapabilities(cap,task);
1530 }
1531 else
1532 {
1533 // If we are load-balancing collections in this
1534 // generation, then we require all GC threads to participate
1535 // in the collection. Otherwise, we only require active
1536 // threads to participate, and we set gc_threads[i]->idle for
1537 // any idle capabilities. The rationale here is that waking
1538 // up an idle Capability takes much longer than just doing any
1539 // GC work on its behalf.
1540
1541 if (RtsFlags.ParFlags.parGcNoSyncWithIdle == 0
1542 || (RtsFlags.ParFlags.parGcLoadBalancingEnabled &&
1543 N >= RtsFlags.ParFlags.parGcLoadBalancingGen)) {
1544 for (i=0; i < n_capabilities; i++) {
1545 if (capabilities[i].disabled) {
1546 idle_cap[i] = tryGrabCapability(&capabilities[i], task);
1547 } else {
1548 idle_cap[i] = rtsFalse;
1549 }
1550 }
1551 } else {
1552 for (i=0; i < n_capabilities; i++) {
1553 if (capabilities[i].disabled) {
1554 idle_cap[i] = tryGrabCapability(&capabilities[i], task);
1555 } else if (i == cap->no ||
1556 capabilities[i].idle < RtsFlags.ParFlags.parGcNoSyncWithIdle) {
1557 idle_cap[i] = rtsFalse;
1558 } else {
1559 idle_cap[i] = tryGrabCapability(&capabilities[i], task);
1560 if (!idle_cap[i]) {
1561 n_failed_trygrab_idles++;
1562 } else {
1563 n_idle_caps++;
1564 }
1565 }
1566 }
1567 }
1568
1569 // We set the gc_thread[i]->idle flag if that
1570 // capability/thread is not participating in this collection.
1571 // We also keep a local record of which capabilities are idle
1572 // in idle_cap[], because scheduleDoGC() is re-entrant:
1573 // another thread might start a GC as soon as we've finished
1574 // this one, and thus the gc_thread[]->idle flags are invalid
1575 // as soon as we release any threads after GC. Getting this
1576 // wrong leads to a rare and hard to debug deadlock!
1577
1578 for (i=0; i < n_capabilities; i++) {
1579 gc_threads[i]->idle = idle_cap[i];
1580 capabilities[i].idle++;
1581 }
1582
1583 // For all capabilities participating in this GC, wait until
1584 // they have stopped mutating and are standing by for GC.
1585 waitForGcThreads(cap);
1586
1587 #if defined(THREADED_RTS)
1588 // Stable point where we can do a global check on our spark counters
1589 ASSERT(checkSparkCountInvariant());
1590 #endif
1591 }
1592
1593 #endif
1594
1595 IF_DEBUG(scheduler, printAllThreads());
1596
1597 delete_threads_and_gc:
1598 /*
1599 * We now have all the capabilities; if we're in an interrupting
1600 * state, then we should take the opportunity to delete all the
1601 * threads in the system.
1602 */
1603 if (sched_state == SCHED_INTERRUPTING) {
1604 deleteAllThreads(cap);
1605 #if defined(THREADED_RTS)
1606 // Discard all the sparks from every Capability. Why?
1607 // They'll probably be GC'd anyway since we've killed all the
1608 // threads. It just avoids the GC having to do any work to
1609 // figure out that any remaining sparks are garbage.
1610 for (i = 0; i < n_capabilities; i++) {
1611 capabilities[i].spark_stats.gcd +=
1612 sparkPoolSize(capabilities[i].sparks);
1613 // No race here since all Caps are stopped.
1614 discardSparksCap(&capabilities[i]);
1615 }
1616 #endif
1617 sched_state = SCHED_SHUTTING_DOWN;
1618 }
1619
1620 /*
1621 * When there are disabled capabilities, we want to migrate any
1622 * threads away from them. Normally this happens in the
1623 * scheduler's loop, but only for unbound threads - it's really
1624 * hard for a bound thread to migrate itself. So we have another
1625 * go here.
1626 */
1627 #if defined(THREADED_RTS)
1628 for (i = enabled_capabilities; i < n_capabilities; i++) {
1629 Capability *tmp_cap, *dest_cap;
1630 tmp_cap = &capabilities[i];
1631 ASSERT(tmp_cap->disabled);
1632 if (i != cap->no) {
1633 dest_cap = &capabilities[i % enabled_capabilities];
1634 while (!emptyRunQueue(tmp_cap)) {
1635 tso = popRunQueue(tmp_cap);
1636 migrateThread(tmp_cap, tso, dest_cap);
1637 if (tso->bound) { tso->bound->task->cap = dest_cap; }
1638 }
1639 }
1640 }
1641 #endif
1642
1643 heap_census = scheduleNeedHeapProfile(rtsTrue);
1644
1645 #if defined(THREADED_RTS)
1646 // reset pending_sync *before* GC, so that when the GC threads
1647 // emerge they don't immediately re-enter the GC.
1648 pending_sync = 0;
1649 GarbageCollect(force_major || heap_census, heap_census, gc_type, cap);
1650 #else
1651 GarbageCollect(force_major || heap_census, heap_census, 0, cap);
1652 #endif
1653
1654 traceSparkCounters(cap);
1655
1656 if (recent_activity == ACTIVITY_INACTIVE && force_major)
1657 {
1658 // We are doing a GC because the system has been idle for a
1659 // timeslice and we need to check for deadlock. Record the
1660 // fact that we've done a GC and turn off the timer signal;
1661 // it will get re-enabled if we run any threads after the GC.
1662 recent_activity = ACTIVITY_DONE_GC;
1663 stopTimer();
1664 }
1665 else
1666 {
1667 // the GC might have taken long enough for the timer to set
1668 // recent_activity = ACTIVITY_INACTIVE, but we aren't
1669 // necessarily deadlocked:
1670 recent_activity = ACTIVITY_YES;
1671 }
1672
1673 #if defined(THREADED_RTS)
1674 // Stable point where we can do a global check on our spark counters
1675 ASSERT(checkSparkCountInvariant());
1676 #endif
1677
1678 // The heap census itself is done during GarbageCollect().
1679 if (heap_census) {
1680 performHeapProfile = rtsFalse;
1681 }
1682
1683 #if defined(THREADED_RTS)
1684 if (gc_type == SYNC_GC_PAR)
1685 {
1686 releaseGCThreads(cap);
1687 for (i = 0; i < n_capabilities; i++) {
1688 if (i != cap->no) {
1689 if (idle_cap[i]) {
1690 ASSERT(capabilities[i].running_task == task);
1691 task->cap = &capabilities[i];
1692 releaseCapability(&capabilities[i]);
1693 } else {
1694 ASSERT(capabilities[i].running_task != task);
1695 }
1696 }
1697 }
1698 task->cap = cap;
1699 }
1700 #endif
1701
1702 if (heap_overflow && sched_state < SCHED_INTERRUPTING) {
1703 // GC set the heap_overflow flag, so we should proceed with
1704 // an orderly shutdown now. Ultimately we want the main
1705 // thread to return to its caller with HeapExhausted, at which
1706 // point the caller should call hs_exit(). The first step is
1707 // to delete all the threads.
1708 //
1709 // Another way to do this would be to raise an exception in
1710 // the main thread, which we really should do because it gives
1711 // the program a chance to clean up. But how do we find the
1712 // main thread? It should presumably be the same one that
1713 // gets ^C exceptions, but that's all done on the Haskell side
1714 // (GHC.TopHandler).
1715 sched_state = SCHED_INTERRUPTING;
1716 goto delete_threads_and_gc;
1717 }
1718
1719 #ifdef SPARKBALANCE
1720 /* JB
1721 Once we are all together... this would be the place to balance all
1722 spark pools. No concurrent stealing or adding of new sparks can
1723 occur. Should be defined in Sparks.c. */
1724 balanceSparkPoolsCaps(n_capabilities, capabilities);
1725 #endif
1726
1727 #if defined(THREADED_RTS)
1728 if (gc_type == SYNC_GC_SEQ) {
1729 // release our stash of capabilities.
1730 releaseAllCapabilities(cap, task);
1731 }
1732 #endif
1733
1734 return;
1735 }
1736
1737 /* ---------------------------------------------------------------------------
1738 * Singleton fork(). Do not copy any running threads.
1739 * ------------------------------------------------------------------------- */
1740
1741 pid_t
1742 forkProcess(HsStablePtr *entry
1743 #ifndef FORKPROCESS_PRIMOP_SUPPORTED
1744 STG_UNUSED
1745 #endif
1746 )
1747 {
1748 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
1749 pid_t pid;
1750 StgTSO* t,*next;
1751 Capability *cap;
1752 nat g;
1753 Task *task = NULL;
1754 nat i;
1755 #ifdef THREADED_RTS
1756 nat sync;
1757 #endif
1758
1759 debugTrace(DEBUG_sched, "forking!");
1760
1761 task = newBoundTask();
1762
1763 cap = NULL;
1764 waitForReturnCapability(&cap, task);
1765
1766 #ifdef THREADED_RTS
1767 do {
1768 sync = requestSync(&cap, task, SYNC_OTHER);
1769 } while (sync);
1770
1771 acquireAllCapabilities(cap,task);
1772
1773 pending_sync = 0;
1774 #endif
1775
1776 // no funny business: hold locks while we fork, otherwise if some
1777 // other thread is holding a lock when the fork happens, the data
1778 // structure protected by the lock will forever be in an
1779 // inconsistent state in the child. See also #1391.
1780 ACQUIRE_LOCK(&sched_mutex);
1781 ACQUIRE_LOCK(&sm_mutex);
1782 ACQUIRE_LOCK(&stable_mutex);
1783 ACQUIRE_LOCK(&task->lock);
1784
1785 for (i=0; i < n_capabilities; i++) {
1786 ACQUIRE_LOCK(&capabilities[i].lock);
1787 }
1788
1789 stopTimer(); // See #4074
1790
1791 #if defined(TRACING)
1792 flushEventLog(); // so that child won't inherit dirty file buffers
1793 #endif
1794
1795 pid = fork();
1796
1797 if (pid) { // parent
1798
1799 startTimer(); // #4074
1800
1801 RELEASE_LOCK(&sched_mutex);
1802 RELEASE_LOCK(&sm_mutex);
1803 RELEASE_LOCK(&stable_mutex);
1804 RELEASE_LOCK(&task->lock);
1805
1806 for (i=0; i < n_capabilities; i++) {
1807 releaseCapability_(&capabilities[i],rtsFalse);
1808 RELEASE_LOCK(&capabilities[i].lock);
1809 }
1810 boundTaskExiting(task);
1811
1812 // just return the pid
1813 return pid;
1814
1815 } else { // child
1816
1817 #if defined(THREADED_RTS)
1818 initMutex(&sched_mutex);
1819 initMutex(&sm_mutex);
1820 initMutex(&stable_mutex);
1821 initMutex(&task->lock);
1822
1823 for (i=0; i < n_capabilities; i++) {
1824 initMutex(&capabilities[i].lock);
1825 }
1826 #endif
1827
1828 #ifdef TRACING
1829 resetTracing();
1830 #endif
1831
1832 // Now, all OS threads except the thread that forked are
1833 // stopped. We need to stop all Haskell threads, including
1834 // those involved in foreign calls. Also we need to delete
1835 // all Tasks, because they correspond to OS threads that are
1836 // now gone.
1837
1838 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1839 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
1840 next = t->global_link;
1841 // don't allow threads to catch the ThreadKilled
1842 // exception, but we do want to raiseAsync() because these
1843 // threads may be evaluating thunks that we need later.
1844 deleteThread_(t->cap,t);
1845
1846 // stop the GC from updating the InCall to point to
1847 // the TSO. This is only necessary because the
1848 // OSThread bound to the TSO has been killed, and
1849 // won't get a chance to exit in the usual way (see
1850 // also scheduleHandleThreadFinished).
1851 t->bound = NULL;
1852 }
1853 }
1854
1855 discardTasksExcept(task);
1856
1857 for (i=0; i < n_capabilities; i++) {
1858 cap = &capabilities[i];
1859
1860 // Empty the run queue. It seems tempting to let all the
1861 // killed threads stay on the run queue as zombies to be
1862 // cleaned up later, but some of them may correspond to
1863 // bound threads for which the corresponding Task does not
1864 // exist.
1865 cap->run_queue_hd = END_TSO_QUEUE;
1866 cap->run_queue_tl = END_TSO_QUEUE;
1867
1868 // Any suspended C-calling Tasks are no more, their OS threads
1869 // don't exist now:
1870 cap->suspended_ccalls = NULL;
1871
1872 #if defined(THREADED_RTS)
1873 // Wipe our spare workers list, they no longer exist. New
1874 // workers will be created if necessary.
1875 cap->spare_workers = NULL;
1876 cap->n_spare_workers = 0;
1877 cap->returning_tasks_hd = NULL;
1878 cap->returning_tasks_tl = NULL;
1879 #endif
1880
1881 // Release all caps except 0, we'll use that for starting
1882 // the IO manager and running the client action below.
1883 if (cap->no != 0) {
1884 task->cap = cap;
1885 releaseCapability(cap);
1886 }
1887 }
1888 cap = &capabilities[0];
1889 task->cap = cap;
1890
1891 // Empty the threads lists. Otherwise, the garbage
1892 // collector may attempt to resurrect some of these threads.
1893 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1894 generations[g].threads = END_TSO_QUEUE;
1895 }
1896
1897 // On Unix, all timers are reset in the child, so we need to start
1898 // the timer again.
1899 initTimer();
1900 startTimer();
1901
1902 #if defined(THREADED_RTS)
1903 ioManagerStartCap(&cap);
1904 #endif
1905
1906 rts_evalStableIO(&cap, entry, NULL); // run the action
1907 rts_checkSchedStatus("forkProcess",cap);
1908
1909 rts_unlock(cap);
1910 hs_exit(); // clean up and exit
1911 stg_exit(EXIT_SUCCESS);
1912 }
1913 #else /* !FORKPROCESS_PRIMOP_SUPPORTED */
1914 barf("forkProcess#: primop not supported on this platform, sorry!\n");
1915 #endif
1916 }
1917
1918 /* ---------------------------------------------------------------------------
1919 * Changing the number of Capabilities
1920 *
1921 * Changing the number of Capabilities is very tricky! We can only do
1922 * it with the system fully stopped, so we do a full sync with
1923 * requestSync(SYNC_OTHER) and grab all the capabilities.
1924 *
1925 * Then we resize the appropriate data structures, and update all
1926 * references to the old data structures which have now moved.
1927 * Finally we release the Capabilities we are holding, and start
1928 * worker Tasks on the new Capabilities we created.
1929 *
1930 * ------------------------------------------------------------------------- */
1931
1932 void
1933 setNumCapabilities (nat new_n_capabilities USED_IF_THREADS)
1934 {
1935 #if !defined(THREADED_RTS)
1936 if (new_n_capabilities != 1) {
1937 errorBelch("setNumCapabilities: not supported in the non-threaded RTS");
1938 }
1939 return;
1940 #elif defined(NOSMP)
1941 if (new_n_capabilities != 1) {
1942 errorBelch("setNumCapabilities: not supported on this platform");
1943 }
1944 return;
1945 #else
1946 Task *task;
1947 Capability *cap;
1948 nat sync;
1949 StgTSO* t;
1950 nat g, n;
1951 Capability *old_capabilities = NULL;
1952
1953 if (new_n_capabilities == enabled_capabilities) return;
1954
1955 debugTrace(DEBUG_sched, "changing the number of Capabilities from %d to %d",
1956 enabled_capabilities, new_n_capabilities);
1957
1958 cap = rts_lock();
1959 task = cap->running_task;
1960
1961 do {
1962 sync = requestSync(&cap, task, SYNC_OTHER);
1963 } while (sync);
1964
1965 acquireAllCapabilities(cap,task);
1966
1967 pending_sync = 0;
1968
1969 if (new_n_capabilities < enabled_capabilities)
1970 {
1971 // Reducing the number of capabilities: we do not actually
1972 // remove the extra capabilities, we just mark them as
1973 // "disabled". This has the following effects:
1974 //
1975 // - threads on a disabled capability are migrated away by the
1976 // scheduler loop
1977 //
1978 // - disabled capabilities do not participate in GC
1979 // (see scheduleDoGC())
1980 //
1981 // - No spark threads are created on this capability
1982 // (see scheduleActivateSpark())
1983 //
1984 // - We do not attempt to migrate threads *to* a disabled
1985 // capability (see schedulePushWork()).
1986 //
1987 // but in other respects, a disabled capability remains
1988 // alive. Threads may be woken up on a disabled capability,
1989 // but they will be immediately migrated away.
1990 //
1991 // This approach is much easier than trying to actually remove
1992 // the capability; we don't have to worry about GC data
1993 // structures, the nursery, etc.
1994 //
1995 for (n = new_n_capabilities; n < enabled_capabilities; n++) {
1996 capabilities[n].disabled = rtsTrue;
1997 traceCapDisable(&capabilities[n]);
1998 }
1999 enabled_capabilities = new_n_capabilities;
2000 }
2001 else
2002 {
2003 // Increasing the number of enabled capabilities.
2004 //
2005 // enable any disabled capabilities, up to the required number
2006 for (n = enabled_capabilities;
2007 n < new_n_capabilities && n < n_capabilities; n++) {
2008 capabilities[n].disabled = rtsFalse;
2009 traceCapEnable(&capabilities[n]);
2010 }
2011 enabled_capabilities = n;
2012
2013 if (new_n_capabilities > n_capabilities) {
2014 #if defined(TRACING)
2015 // Allocate eventlog buffers for the new capabilities. Note this
2016 // must be done before calling moreCapabilities(), because that
2017 // will emit events about creating the new capabilities and adding
2018 // them to existing capsets.
2019 tracingAddCapapilities(n_capabilities, new_n_capabilities);
2020 #endif
2021
2022 // Resize the capabilities array
2023 // NB. after this, capabilities points somewhere new. Any pointers
2024 // of type (Capability *) are now invalid.
2025 old_capabilities = moreCapabilities(n_capabilities, new_n_capabilities);
2026
2027 // update our own cap pointer
2028 cap = &capabilities[cap->no];
2029
2030 // Resize and update storage manager data structures
2031 storageAddCapabilities(n_capabilities, new_n_capabilities);
2032
2033 // Update (Capability *) refs in the Task manager.
2034 updateCapabilityRefs();
2035
2036 // Update (Capability *) refs from TSOs
2037 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
2038 for (t = generations[g].threads; t != END_TSO_QUEUE; t = t->global_link) {
2039 t->cap = &capabilities[t->cap->no];
2040 }
2041 }
2042 }
2043 }
2044
2045 // We're done: release the original Capabilities
2046 releaseAllCapabilities(cap,task);
2047
2048 // Start worker tasks on the new Capabilities
2049 startWorkerTasks(n_capabilities, new_n_capabilities);
2050
2051 // finally, update n_capabilities
2052 if (new_n_capabilities > n_capabilities) {
2053 n_capabilities = enabled_capabilities = new_n_capabilities;
2054 }
2055
2056 // We can't free the old array until now, because we access it
2057 // while updating pointers in updateCapabilityRefs().
2058 if (old_capabilities) {
2059 stgFree(old_capabilities);
2060 }
2061
2062 rts_unlock(cap);
2063
2064 #endif // THREADED_RTS
2065 }
2066
2067
2068
2069 /* ---------------------------------------------------------------------------
2070 * Delete all the threads in the system
2071 * ------------------------------------------------------------------------- */
2072
2073 static void
2074 deleteAllThreads ( Capability *cap )
2075 {
2076 // NOTE: only safe to call if we own all capabilities.
2077
2078 StgTSO* t, *next;
2079 nat g;
2080
2081 debugTrace(DEBUG_sched,"deleting all threads");
2082 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
2083 for (t = generations[g].threads; t != END_TSO_QUEUE; t = next) {
2084 next = t->global_link;
2085 deleteThread(cap,t);
2086 }
2087 }
2088
2089 // The run queue now contains a bunch of ThreadKilled threads. We
2090 // must not throw these away: the main thread(s) will be in there
2091 // somewhere, and the main scheduler loop has to deal with it.
2092 // Also, the run queue is the only thing keeping these threads from
2093 // being GC'd, and we don't want the "main thread has been GC'd" panic.
2094
2095 #if !defined(THREADED_RTS)
2096 ASSERT(blocked_queue_hd == END_TSO_QUEUE);
2097 ASSERT(sleeping_queue == END_TSO_QUEUE);
2098 #endif
2099 }
2100
2101 /* -----------------------------------------------------------------------------
2102 Managing the suspended_ccalls list.
2103 Locks required: sched_mutex
2104 -------------------------------------------------------------------------- */
2105
2106 STATIC_INLINE void
2107 suspendTask (Capability *cap, Task *task)
2108 {
2109 InCall *incall;
2110
2111 incall = task->incall;
2112 ASSERT(incall->next == NULL && incall->prev == NULL);
2113 incall->next = cap->suspended_ccalls;
2114 incall->prev = NULL;
2115 if (cap->suspended_ccalls) {
2116 cap->suspended_ccalls->prev = incall;
2117 }
2118 cap->suspended_ccalls = incall;
2119 }
2120
2121 STATIC_INLINE void
2122 recoverSuspendedTask (Capability *cap, Task *task)
2123 {
2124 InCall *incall;
2125
2126 incall = task->incall;
2127 if (incall->prev) {
2128 incall->prev->next = incall->next;
2129 } else {
2130 ASSERT(cap->suspended_ccalls == incall);
2131 cap->suspended_ccalls = incall->next;
2132 }
2133 if (incall->next) {
2134 incall->next->prev = incall->prev;
2135 }
2136 incall->next = incall->prev = NULL;
2137 }
2138
2139 /* ---------------------------------------------------------------------------
2140 * Suspending & resuming Haskell threads.
2141 *
2142 * When making a "safe" call to C (aka _ccall_GC), the task gives back
2143 * its capability before calling the C function. This allows another
2144 * task to pick up the capability and carry on running Haskell
2145 * threads. It also means that if the C call blocks, it won't lock
2146 * the whole system.
2147 *
2148 * The Haskell thread making the C call is put to sleep for the
2149 * duration of the call, on the suspended_ccalling_threads queue. We
2150 * give out a token to the task, which it can use to resume the thread
2151 * on return from the C function.
2152 *
2153 * If this is an interruptible C call, this means that the FFI call may be
2154 * unceremoniously terminated and should be scheduled on an
2155 * unbound worker thread.
2156 * ------------------------------------------------------------------------- */
2157
2158 void *
2159 suspendThread (StgRegTable *reg, rtsBool interruptible)
2160 {
2161 Capability *cap;
2162 int saved_errno;
2163 StgTSO *tso;
2164 Task *task;
2165 #if mingw32_HOST_OS
2166 StgWord32 saved_winerror;
2167 #endif
2168
2169 saved_errno = errno;
2170 #if mingw32_HOST_OS
2171 saved_winerror = GetLastError();
2172 #endif
2173
2174 /* assume that *reg is a pointer to the StgRegTable part of a Capability.
2175 */
2176 cap = regTableToCapability(reg);
2177
2178 task = cap->running_task;
2179 tso = cap->r.rCurrentTSO;
2180
2181 traceEventStopThread(cap, tso, THREAD_SUSPENDED_FOREIGN_CALL, 0);
2182
2183 // XXX this might not be necessary --SDM
2184 tso->what_next = ThreadRunGHC;
2185
2186 threadPaused(cap,tso);
2187
2188 if (interruptible) {
2189 tso->why_blocked = BlockedOnCCall_Interruptible;
2190 } else {
2191 tso->why_blocked = BlockedOnCCall;
2192 }
2193
2194 // Hand back capability
2195 task->incall->suspended_tso = tso;
2196 task->incall->suspended_cap = cap;
2197
2198 ACQUIRE_LOCK(&cap->lock);
2199
2200 suspendTask(cap,task);
2201 cap->in_haskell = rtsFalse;
2202 releaseCapability_(cap,rtsFalse);
2203
2204 RELEASE_LOCK(&cap->lock);
2205
2206 errno = saved_errno;
2207 #if mingw32_HOST_OS
2208 SetLastError(saved_winerror);
2209 #endif
2210 return task;
2211 }
2212
2213 StgRegTable *
2214 resumeThread (void *task_)
2215 {
2216 StgTSO *tso;
2217 InCall *incall;
2218 Capability *cap;
2219 Task *task = task_;
2220 int saved_errno;
2221 #if mingw32_HOST_OS
2222 StgWord32 saved_winerror;
2223 #endif
2224
2225 saved_errno = errno;
2226 #if mingw32_HOST_OS
2227 saved_winerror = GetLastError();
2228 #endif
2229
2230 incall = task->incall;
2231 cap = incall->suspended_cap;
2232 task->cap = cap;
2233
2234 // Wait for permission to re-enter the RTS with the result.
2235 waitForReturnCapability(&cap,task);
2236 // we might be on a different capability now... but if so, our
2237 // entry on the suspended_ccalls list will also have been
2238 // migrated.
2239
2240 // Remove the thread from the suspended list
2241 recoverSuspendedTask(cap,task);
2242
2243 tso = incall->suspended_tso;
2244 incall->suspended_tso = NULL;
2245 incall->suspended_cap = NULL;
2246 tso->_link = END_TSO_QUEUE; // no write barrier reqd
2247
2248 traceEventRunThread(cap, tso);
2249
2250 /* Reset blocking status */
2251 tso->why_blocked = NotBlocked;
2252
2253 if ((tso->flags & TSO_BLOCKEX) == 0) {
2254 // avoid locking the TSO if we don't have to
2255 if (tso->blocked_exceptions != END_BLOCKED_EXCEPTIONS_QUEUE) {
2256 maybePerformBlockedException(cap,tso);
2257 }
2258 }
2259
2260 cap->r.rCurrentTSO = tso;
2261 cap->in_haskell = rtsTrue;
2262 errno = saved_errno;
2263 #if mingw32_HOST_OS
2264 SetLastError(saved_winerror);
2265 #endif
2266
2267 /* We might have GC'd, mark the TSO dirty again */
2268 dirty_TSO(cap,tso);
2269 dirty_STACK(cap,tso->stackobj);
2270
2271 IF_DEBUG(sanity, checkTSO(tso));
2272
2273 return &cap->r;
2274 }
2275
2276 /* ---------------------------------------------------------------------------
2277 * scheduleThread()
2278 *
2279 * scheduleThread puts a thread on the end of the runnable queue.
2280 * This will usually be done immediately after a thread is created.
2281 * The caller of scheduleThread must create the thread using e.g.
2282 * createThread and push an appropriate closure
2283 * on this thread's stack before the scheduler is invoked.
2284 * ------------------------------------------------------------------------ */
2285
2286 void
2287 scheduleThread(Capability *cap, StgTSO *tso)
2288 {
2289 // The thread goes at the *end* of the run-queue, to avoid possible
2290 // starvation of any threads already on the queue.
2291 appendToRunQueue(cap,tso);
2292 }
2293
2294 void
2295 scheduleThreadOn(Capability *cap, StgWord cpu USED_IF_THREADS, StgTSO *tso)
2296 {
2297 tso->flags |= TSO_LOCKED; // we requested explicit affinity; don't
2298 // move this thread from now on.
2299 #if defined(THREADED_RTS)
2300 cpu %= enabled_capabilities;
2301 if (cpu == cap->no) {
2302 appendToRunQueue(cap,tso);
2303 } else {
2304 migrateThread(cap, tso, &capabilities[cpu]);
2305 }
2306 #else
2307 appendToRunQueue(cap,tso);
2308 #endif
2309 }
2310
2311 void
2312 scheduleWaitThread (StgTSO* tso, /*[out]*/HaskellObj* ret, Capability **pcap)
2313 {
2314 Task *task;
2315 DEBUG_ONLY( StgThreadID id );
2316 Capability *cap;
2317
2318 cap = *pcap;
2319
2320 // We already created/initialised the Task
2321 task = cap->running_task;
2322
2323 // This TSO is now a bound thread; make the Task and TSO
2324 // point to each other.
2325 tso->bound = task->incall;
2326 tso->cap = cap;
2327
2328 task->incall->tso = tso;
2329 task->incall->ret = ret;
2330 task->incall->stat = NoStatus;
2331
2332 appendToRunQueue(cap,tso);
2333
2334 DEBUG_ONLY( id = tso->id );
2335 debugTrace(DEBUG_sched, "new bound thread (%lu)", (unsigned long)id);
2336
2337 cap = schedule(cap,task);
2338
2339 ASSERT(task->incall->stat != NoStatus);
2340 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
2341
2342 debugTrace(DEBUG_sched, "bound thread (%lu) finished", (unsigned long)id);
2343 *pcap = cap;
2344 }
2345
2346 /* ----------------------------------------------------------------------------
2347 * Starting Tasks
2348 * ------------------------------------------------------------------------- */
2349
2350 #if defined(THREADED_RTS)
2351 void scheduleWorker (Capability *cap, Task *task)
2352 {
2353 // schedule() runs without a lock.
2354 cap = schedule(cap,task);
2355
2356 // On exit from schedule(), we have a Capability, but possibly not
2357 // the same one we started with.
2358
2359 // During shutdown, the requirement is that after all the
2360 // Capabilities are shut down, all workers that are shutting down
2361 // have finished workerTaskStop(). This is why we hold on to
2362 // cap->lock until we've finished workerTaskStop() below.
2363 //
2364 // There may be workers still involved in foreign calls; those
2365 // will just block in waitForReturnCapability() because the
2366 // Capability has been shut down.
2367 //
2368 ACQUIRE_LOCK(&cap->lock);
2369 releaseCapability_(cap,rtsFalse);
2370 workerTaskStop(task);
2371 RELEASE_LOCK(&cap->lock);
2372 }
2373 #endif
2374
2375 /* ---------------------------------------------------------------------------
2376 * Start new worker tasks on Capabilities from--to
2377 * -------------------------------------------------------------------------- */
2378
2379 static void
2380 startWorkerTasks (nat from USED_IF_THREADS, nat to USED_IF_THREADS)
2381 {
2382 #if defined(THREADED_RTS)
2383 nat i;
2384 Capability *cap;
2385
2386 for (i = from; i < to; i++) {
2387 cap = &capabilities[i];
2388 ACQUIRE_LOCK(&cap->lock);
2389 startWorkerTask(cap);
2390 RELEASE_LOCK(&cap->lock);
2391 }
2392 #endif
2393 }
2394
2395 /* ---------------------------------------------------------------------------
2396 * initScheduler()
2397 *
2398 * Initialise the scheduler. This resets all the queues - if the
2399 * queues contained any threads, they'll be garbage collected at the
2400 * next pass.
2401 *
2402 * ------------------------------------------------------------------------ */
2403
2404 void
2405 initScheduler(void)
2406 {
2407 #if !defined(THREADED_RTS)
2408 blocked_queue_hd = END_TSO_QUEUE;
2409 blocked_queue_tl = END_TSO_QUEUE;
2410 sleeping_queue = END_TSO_QUEUE;
2411 #endif
2412
2413 sched_state = SCHED_RUNNING;
2414 recent_activity = ACTIVITY_YES;
2415
2416 #if defined(THREADED_RTS)
2417 /* Initialise the mutex and condition variables used by
2418 * the scheduler. */
2419 initMutex(&sched_mutex);
2420 #endif
2421
2422 ACQUIRE_LOCK(&sched_mutex);
2423
2424 /* A capability holds the state a native thread needs in
2425 * order to execute STG code. At least one capability is
2426 * floating around (only THREADED_RTS builds have more than one).
2427 */
2428 initCapabilities();
2429
2430 initTaskManager();
2431
2432 /*
2433 * Eagerly start one worker to run each Capability, except for
2434 * Capability 0. The idea is that we're probably going to start a
2435 * bound thread on Capability 0 pretty soon, so we don't want a
2436 * worker task hogging it.
2437 */
2438 startWorkerTasks(1, n_capabilities);
2439
2440 RELEASE_LOCK(&sched_mutex);
2441
2442 }
2443
2444 void
2445 exitScheduler (rtsBool wait_foreign USED_IF_THREADS)
2446 /* see Capability.c, shutdownCapability() */
2447 {
2448 Task *task = NULL;
2449
2450 task = newBoundTask();
2451
2452 // If we haven't killed all the threads yet, do it now.
2453 if (sched_state < SCHED_SHUTTING_DOWN) {
2454 sched_state = SCHED_INTERRUPTING;
2455 Capability *cap = task->cap;
2456 waitForReturnCapability(&cap,task);
2457 scheduleDoGC(&cap,task,rtsFalse);
2458 ASSERT(task->incall->tso == NULL);
2459 releaseCapability(cap);
2460 }
2461 sched_state = SCHED_SHUTTING_DOWN;
2462
2463 shutdownCapabilities(task, wait_foreign);
2464
2465 // debugBelch("n_failed_trygrab_idles = %d, n_idle_caps = %d\n",
2466 // n_failed_trygrab_idles, n_idle_caps);
2467
2468 boundTaskExiting(task);
2469 }
2470
2471 void
2472 freeScheduler( void )
2473 {
2474 nat still_running;
2475
2476 ACQUIRE_LOCK(&sched_mutex);
2477 still_running = freeTaskManager();
2478 // We can only free the Capabilities if there are no Tasks still
2479 // running. We might have a Task about to return from a foreign
2480 // call into waitForReturnCapability(), for example (actually,
2481 // this should be the *only* thing that a still-running Task can
2482 // do at this point, and it will block waiting for the
2483 // Capability).
2484 if (still_running == 0) {
2485 freeCapabilities();
2486 if (n_capabilities != 1) {
2487 stgFree(capabilities);
2488 }
2489 }
2490 RELEASE_LOCK(&sched_mutex);
2491 #if defined(THREADED_RTS)
2492 closeMutex(&sched_mutex);
2493 #endif
2494 }
2495
2496 void markScheduler (evac_fn evac USED_IF_NOT_THREADS,
2497 void *user USED_IF_NOT_THREADS)
2498 {
2499 #if !defined(THREADED_RTS)
2500 evac(user, (StgClosure **)(void *)&blocked_queue_hd);
2501 evac(user, (StgClosure **)(void *)&blocked_queue_tl);
2502 evac(user, (StgClosure **)(void *)&sleeping_queue);
2503 #endif
2504 }
2505
2506 /* -----------------------------------------------------------------------------
2507 performGC
2508
2509 This is the interface to the garbage collector from Haskell land.
2510 We provide this so that external C code can allocate and garbage
2511 collect when called from Haskell via _ccall_GC.
2512 -------------------------------------------------------------------------- */
2513
2514 static void
2515 performGC_(rtsBool force_major)
2516 {
2517 Task *task;
2518 Capability *cap = NULL;
2519
2520 // We must grab a new Task here, because the existing Task may be
2521 // associated with a particular Capability, and chained onto the
2522 // suspended_ccalls queue.
2523 task = newBoundTask();
2524
2525 waitForReturnCapability(&cap,task);
2526 scheduleDoGC(&cap,task,force_major);
2527 releaseCapability(cap);
2528 boundTaskExiting(task);
2529 }
2530
2531 void
2532 performGC(void)
2533 {
2534 performGC_(rtsFalse);
2535 }
2536
2537 void
2538 performMajorGC(void)
2539 {
2540 performGC_(rtsTrue);
2541 }
2542
2543 /* ---------------------------------------------------------------------------
2544 Interrupt execution
2545 - usually called inside a signal handler so it mustn't do anything fancy.
2546 ------------------------------------------------------------------------ */
2547
2548 void
2549 interruptStgRts(void)
2550 {
2551 sched_state = SCHED_INTERRUPTING;
2552 interruptAllCapabilities();
2553 #if defined(THREADED_RTS)
2554 wakeUpRts();
2555 #endif
2556 }
2557
2558 /* -----------------------------------------------------------------------------
2559 Wake up the RTS
2560
2561 This function causes at least one OS thread to wake up and run the
2562 scheduler loop. It is invoked when the RTS might be deadlocked, or
2563 an external event has arrived that may need servicing (eg. a
2564 keyboard interrupt).
2565
2566 In the single-threaded RTS we don't do anything here; we only have
2567 one thread anyway, and the event that caused us to want to wake up
2568 will have interrupted any blocking system call in progress anyway.
2569 -------------------------------------------------------------------------- */
2570
2571 #if defined(THREADED_RTS)
2572 void wakeUpRts(void)
2573 {
2574 // This forces the IO Manager thread to wakeup, which will
2575 // in turn ensure that some OS thread wakes up and runs the
2576 // scheduler loop, which will cause a GC and deadlock check.
2577 ioManagerWakeup();
2578 }
2579 #endif
2580
2581 /* -----------------------------------------------------------------------------
2582 Deleting threads
2583
2584 This is used for interruption (^C) and forking, and corresponds to
2585 raising an exception but without letting the thread catch the
2586 exception.
2587 -------------------------------------------------------------------------- */
2588
2589 static void
2590 deleteThread (Capability *cap STG_UNUSED, StgTSO *tso)
2591 {
2592 // NOTE: must only be called on a TSO that we have exclusive
2593 // access to, because we will call throwToSingleThreaded() below.
2594 // The TSO must be on the run queue of the Capability we own, or
2595 // we must own all Capabilities.
2596
2597 if (tso->why_blocked != BlockedOnCCall &&
2598 tso->why_blocked != BlockedOnCCall_Interruptible) {
2599 throwToSingleThreaded(tso->cap,tso,NULL);
2600 }
2601 }
2602
2603 #ifdef FORKPROCESS_PRIMOP_SUPPORTED
2604 static void
2605 deleteThread_(Capability *cap, StgTSO *tso)
2606 { // for forkProcess only:
2607 // like deleteThread(), but we delete threads in foreign calls, too.
2608
2609 if (tso->why_blocked == BlockedOnCCall ||
2610 tso->why_blocked == BlockedOnCCall_Interruptible) {
2611 tso->what_next = ThreadKilled;
2612 appendToRunQueue(tso->cap, tso);
2613 } else {
2614 deleteThread(cap,tso);
2615 }
2616 }
2617 #endif
2618
2619 /* -----------------------------------------------------------------------------
2620 raiseExceptionHelper
2621
2622 This function is called by the raise# primitve, just so that we can
2623 move some of the tricky bits of raising an exception from C-- into
2624 C. Who knows, it might be a useful re-useable thing here too.
2625 -------------------------------------------------------------------------- */
2626
2627 StgWord
2628 raiseExceptionHelper (StgRegTable *reg, StgTSO *tso, StgClosure *exception)
2629 {
2630 Capability *cap = regTableToCapability(reg);
2631 StgThunk *raise_closure = NULL;
2632 StgPtr p, next;
2633 StgRetInfoTable *info;
2634 //
2635 // This closure represents the expression 'raise# E' where E
2636 // is the exception raise. It is used to overwrite all the
2637 // thunks which are currently under evaluataion.
2638 //
2639
2640 // OLD COMMENT (we don't have MIN_UPD_SIZE now):
2641 // LDV profiling: stg_raise_info has THUNK as its closure
2642 // type. Since a THUNK takes at least MIN_UPD_SIZE words in its
2643 // payload, MIN_UPD_SIZE is more approprate than 1. It seems that
2644 // 1 does not cause any problem unless profiling is performed.
2645 // However, when LDV profiling goes on, we need to linearly scan
2646 // small object pool, where raise_closure is stored, so we should
2647 // use MIN_UPD_SIZE.
2648 //
2649 // raise_closure = (StgClosure *)RET_STGCALL1(P_,allocate,
2650 // sizeofW(StgClosure)+1);
2651 //
2652
2653 //
2654 // Walk up the stack, looking for the catch frame. On the way,
2655 // we update any closures pointed to from update frames with the
2656 // raise closure that we just built.
2657 //
2658 p = tso->stackobj->sp;
2659 while(1) {
2660 info = get_ret_itbl((StgClosure *)p);
2661 next = p + stack_frame_sizeW((StgClosure *)p);
2662 switch (info->i.type) {
2663
2664 case UPDATE_FRAME:
2665 // Only create raise_closure if we need to.
2666 if (raise_closure == NULL) {
2667 raise_closure =
2668 (StgThunk *)allocate(cap,sizeofW(StgThunk)+1);
2669 SET_HDR(raise_closure, &stg_raise_info, cap->r.rCCCS);
2670 raise_closure->payload[0] = exception;
2671 }
2672 updateThunk(cap, tso, ((StgUpdateFrame *)p)->updatee,
2673 (StgClosure *)raise_closure);
2674 p = next;
2675 continue;
2676
2677 case ATOMICALLY_FRAME:
2678 debugTrace(DEBUG_stm, "found ATOMICALLY_FRAME at %p", p);
2679 tso->stackobj->sp = p;
2680 return ATOMICALLY_FRAME;
2681
2682 case CATCH_FRAME:
2683 tso->stackobj->sp = p;
2684 return CATCH_FRAME;
2685
2686 case CATCH_STM_FRAME:
2687 debugTrace(DEBUG_stm, "found CATCH_STM_FRAME at %p", p);
2688 tso->stackobj->sp = p;
2689 return CATCH_STM_FRAME;
2690
2691 case UNDERFLOW_FRAME:
2692 tso->stackobj->sp = p;
2693 threadStackUnderflow(cap,tso);
2694 p = tso->stackobj->sp;
2695 continue;
2696
2697 case STOP_FRAME:
2698 tso->stackobj->sp = p;
2699 return STOP_FRAME;
2700
2701 case CATCH_RETRY_FRAME:
2702 default:
2703 p = next;
2704 continue;
2705 }
2706 }
2707 }
2708
2709
2710 /* -----------------------------------------------------------------------------
2711 findRetryFrameHelper
2712
2713 This function is called by the retry# primitive. It traverses the stack
2714 leaving tso->sp referring to the frame which should handle the retry.
2715
2716 This should either be a CATCH_RETRY_FRAME (if the retry# is within an orElse#)
2717 or should be a ATOMICALLY_FRAME (if the retry# reaches the top level).
2718
2719 We skip CATCH_STM_FRAMEs (aborting and rolling back the nested tx that they
2720 create) because retries are not considered to be exceptions, despite the
2721 similar implementation.
2722
2723 We should not expect to see CATCH_FRAME or STOP_FRAME because those should
2724 not be created within memory transactions.
2725 -------------------------------------------------------------------------- */
2726
2727 StgWord
2728 findRetryFrameHelper (Capability *cap, StgTSO *tso)
2729 {
2730 StgPtr p, next;
2731 StgRetInfoTable *info;
2732
2733 p = tso->stackobj->sp;
2734 while (1) {
2735 info = get_ret_itbl((StgClosure *)p);
2736 next = p + stack_frame_sizeW((StgClosure *)p);
2737 switch (info->i.type) {
2738
2739 case ATOMICALLY_FRAME:
2740 debugTrace(DEBUG_stm,
2741 "found ATOMICALLY_FRAME at %p during retry", p);
2742 tso->stackobj->sp = p;
2743 return ATOMICALLY_FRAME;
2744
2745 case CATCH_RETRY_FRAME:
2746 debugTrace(DEBUG_stm,
2747 "found CATCH_RETRY_FRAME at %p during retrry", p);
2748 tso->stackobj->sp = p;
2749 return CATCH_RETRY_FRAME;
2750
2751 case CATCH_STM_FRAME: {
2752 StgTRecHeader *trec = tso -> trec;
2753 StgTRecHeader *outer = trec -> enclosing_trec;
2754 debugTrace(DEBUG_stm,
2755 "found CATCH_STM_FRAME at %p during retry", p);
2756 debugTrace(DEBUG_stm, "trec=%p outer=%p", trec, outer);
2757 stmAbortTransaction(cap, trec);
2758 stmFreeAbortedTRec(cap, trec);
2759 tso -> trec = outer;
2760 p = next;
2761 continue;
2762 }
2763
2764 case UNDERFLOW_FRAME:
2765 threadStackUnderflow(cap,tso);
2766 p = tso->stackobj->sp;
2767 continue;
2768
2769 default:
2770 ASSERT(info->i.type != CATCH_FRAME);
2771 ASSERT(info->i.type != STOP_FRAME);
2772 p = next;
2773 continue;
2774 }
2775 }
2776 }
2777
2778 /* -----------------------------------------------------------------------------
2779 resurrectThreads is called after garbage collection on the list of
2780 threads found to be garbage. Each of these threads will be woken
2781 up and sent a signal: BlockedOnDeadMVar if the thread was blocked
2782 on an MVar, or NonTermination if the thread was blocked on a Black
2783 Hole.
2784
2785 Locks: assumes we hold *all* the capabilities.
2786 -------------------------------------------------------------------------- */
2787
2788 void
2789 resurrectThreads (StgTSO *threads)
2790 {
2791 StgTSO *tso, *next;
2792 Capability *cap;
2793 generation *gen;
2794
2795 for (tso = threads; tso != END_TSO_QUEUE; tso = next) {
2796 next = tso->global_link;
2797
2798 gen = Bdescr((P_)tso)->gen;
2799 tso->global_link = gen->threads;
2800 gen->threads = tso;
2801
2802 debugTrace(DEBUG_sched, "resurrecting thread %lu", (unsigned long)tso->id);
2803
2804 // Wake up the thread on the Capability it was last on
2805 cap = tso->cap;
2806
2807 switch (tso->why_blocked) {
2808 case BlockedOnMVar:
2809 /* Called by GC - sched_mutex lock is currently held. */
2810 throwToSingleThreaded(cap, tso,
2811 (StgClosure *)blockedIndefinitelyOnMVar_closure);
2812 break;
2813 case BlockedOnBlackHole:
2814 throwToSingleThreaded(cap, tso,
2815 (StgClosure *)nonTermination_closure);
2816 break;
2817 case BlockedOnSTM:
2818 throwToSingleThreaded(cap, tso,
2819 (StgClosure *)blockedIndefinitelyOnSTM_closure);
2820 break;
2821 case NotBlocked:
2822 /* This might happen if the thread was blocked on a black hole
2823 * belonging to a thread that we've just woken up (raiseAsync
2824 * can wake up threads, remember...).
2825 */
2826 continue;
2827 case BlockedOnMsgThrowTo:
2828 // This can happen if the target is masking, blocks on a
2829 // black hole, and then is found to be unreachable. In
2830 // this case, we want to let the target wake up and carry
2831 // on, and do nothing to this thread.
2832 continue;
2833 default:
2834 barf("resurrectThreads: thread blocked in a strange way: %d",
2835 tso->why_blocked);
2836 }
2837 }
2838 }