| ... |
... |
@@ -58,11 +58,25 @@ Capability **capabilities; |
|
58
|
58
|
// locking, so we don't do that.
|
|
59
|
59
|
static Capability *last_free_capability[MAX_NUMA_NODES];
|
|
60
|
60
|
|
|
|
61
|
+#if defined(THREADED_RTS)
|
|
61
|
62
|
/*
|
|
62
|
63
|
* Indicates that the RTS wants to synchronise all the Capabilities
|
|
63
|
64
|
* for some reason. All Capabilities should yieldCapability().
|
|
|
65
|
+ *
|
|
|
66
|
+ * This is an atomic variable, all accesses must use appropriate atomics.
|
|
|
67
|
+ */
|
|
|
68
|
+PendingSync *pending_sync = NULL;
|
|
|
69
|
+
|
|
|
70
|
+/*
|
|
|
71
|
+ * sync_finished_cond allows threads which do not own any capability (e.g. the
|
|
|
72
|
+ * concurrent mark thread) to participate in the sync protocol. In particular,
|
|
|
73
|
+ * if such a thread requests a sync while sync is already in progress it will
|
|
|
74
|
+ * block on sync_finished_cond, which will be signalled when the sync is
|
|
|
75
|
+ * finished (by releaseAllCapabilities).
|
|
64
|
76
|
*/
|
|
65
|
|
-PendingSync * volatile pending_sync = 0;
|
|
|
77
|
+static Condition sync_finished_cond;
|
|
|
78
|
+static Mutex sync_finished_mutex;
|
|
|
79
|
+#endif
|
|
66
|
80
|
|
|
67
|
81
|
// Number of logical NUMA nodes
|
|
68
|
82
|
uint32_t n_numa_nodes;
|
| ... |
... |
@@ -204,7 +218,7 @@ anySparks (void) |
|
204
|
218
|
|
|
205
|
219
|
#if defined(THREADED_RTS)
|
|
206
|
220
|
STATIC_INLINE void
|
|
207
|
|
-newReturningTask (Capability *cap, Task *task)
|
|
|
221
|
+appendToReturningTaskQueue (Capability *cap, Task *task)
|
|
208
|
222
|
{
|
|
209
|
223
|
ASSERT_LOCK_HELD(&cap->lock);
|
|
210
|
224
|
ASSERT(task->next == NULL);
|
| ... |
... |
@@ -222,8 +236,25 @@ newReturningTask (Capability *cap, Task *task) |
|
222
|
236
|
ASSERT_RETURNING_TASKS(cap,task);
|
|
223
|
237
|
}
|
|
224
|
238
|
|
|
|
239
|
+STATIC_INLINE void
|
|
|
240
|
+prependToReturningTaskQueue (Capability *cap, Task *task)
|
|
|
241
|
+{
|
|
|
242
|
+ ASSERT_LOCK_HELD(&cap->lock);
|
|
|
243
|
+ ASSERT(task->next == NULL);
|
|
|
244
|
+ task->next = cap->returning_tasks_hd;
|
|
|
245
|
+ cap->returning_tasks_hd = task;
|
|
|
246
|
+ if (cap->returning_tasks_tl == NULL) {
|
|
|
247
|
+ cap->returning_tasks_tl = task;
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ // See Note [Data race in shouldYieldCapability] in Schedule.c.
|
|
|
251
|
+ RELAXED_ADD(&cap->n_returning_tasks, 1);
|
|
|
252
|
+
|
|
|
253
|
+ ASSERT_RETURNING_TASKS(cap,task);
|
|
|
254
|
+}
|
|
|
255
|
+
|
|
225
|
256
|
STATIC_INLINE Task *
|
|
226
|
|
-popReturningTask (Capability *cap)
|
|
|
257
|
+popReturningTaskQueue (Capability *cap)
|
|
227
|
258
|
{
|
|
228
|
259
|
ASSERT_LOCK_HELD(&cap->lock);
|
|
229
|
260
|
Task *task;
|
| ... |
... |
@@ -349,6 +380,11 @@ initCapability (Capability *cap, uint32_t i) |
|
349
|
380
|
* ------------------------------------------------------------------------- */
|
|
350
|
381
|
void initCapabilities (void)
|
|
351
|
382
|
{
|
|
|
383
|
+#if defined(THREADED_RTS)
|
|
|
384
|
+ initMutex(&sync_finished_mutex);
|
|
|
385
|
+ initCondition(&sync_finished_cond);
|
|
|
386
|
+#endif
|
|
|
387
|
+
|
|
352
|
388
|
/* Declare a couple capability sets representing the process and
|
|
353
|
389
|
clock domain. Each capability will get added to these capsets. */
|
|
354
|
390
|
traceCapsetCreate(CAPSET_OSPROCESS_DEFAULT, CapsetTypeOsProcess);
|
| ... |
... |
@@ -523,7 +559,7 @@ giveCapabilityToTask (Capability *cap USED_IF_DEBUG, Task *task) |
|
523
|
559
|
#endif
|
|
524
|
560
|
|
|
525
|
561
|
/* ----------------------------------------------------------------------------
|
|
526
|
|
- * releaseCapability_
|
|
|
562
|
+ * releaseCapability and releaseCapability_
|
|
527
|
563
|
*
|
|
528
|
564
|
* This serves two purposes:
|
|
529
|
565
|
*
|
| ... |
... |
@@ -533,32 +569,60 @@ giveCapabilityToTask (Capability *cap USED_IF_DEBUG, Task *task) |
|
533
|
569
|
*
|
|
534
|
570
|
* 2. There is no current task (cap->task == NULL), and thus the Capability
|
|
535
|
571
|
* is idle, and we want to wake up an idle Task to animate the Capability.
|
|
536
|
|
- * In this case set always_wakeup. See also prodCapability.
|
|
537
|
|
- *
|
|
538
|
|
- * Setting the always_wakeup parameter (almost) ensures that the capability is
|
|
539
|
|
- * not left idle: even if there is no known work to do, the capability will be
|
|
540
|
|
- * given to a worker task. There are two exceptions to this:
|
|
541
|
|
- * 1. if there is a pending sync then the capability is left idle, but in
|
|
542
|
|
- * anticipation of whichever task initiated the sync picking it up shortly.
|
|
543
|
|
- * 2. if the scheduler is shutting down and there are no threads on the run
|
|
544
|
|
- * queue and there are no spare workers then the capability is left idle.
|
|
545
|
|
- * It is not entirely clear if this corner case is intentional.
|
|
|
572
|
+ * See also prodCapability.
|
|
546
|
573
|
*
|
|
547
|
|
- * The caller must hold cap->lock and will still hold it after the call returns.
|
|
|
574
|
+ * Difference:
|
|
|
575
|
+ * - releaseCapability the caller /must not/ hold cap->lock.
|
|
|
576
|
+ * - releaseCapability_ the caller /must/ hold cap->lock.
|
|
548
|
577
|
*
|
|
549
|
578
|
* N.B. May need to take all_tasks_mutex, if it needs to start a new task.
|
|
550
|
579
|
*
|
|
551
|
580
|
* ------------------------------------------------------------------------- */
|
|
552
|
581
|
|
|
553
|
582
|
#if defined(THREADED_RTS)
|
|
554
|
|
-void
|
|
555
|
|
-releaseCapability_ (Capability* cap,
|
|
556
|
|
- bool always_wakeup)
|
|
|
583
|
+static void releaseCapability__ (Capability* cap,
|
|
|
584
|
+ bool always_wakeup,
|
|
|
585
|
+ bool wakeup_worker);
|
|
|
586
|
+
|
|
|
587
|
+void releaseCapability (Capability* cap)
|
|
|
588
|
+{
|
|
|
589
|
+ ACQUIRE_LOCK(&cap->lock);
|
|
|
590
|
+ releaseCapability__(cap, false /*always_wakeup*/,
|
|
|
591
|
+ false /*wakeup_worker*/);
|
|
|
592
|
+ RELEASE_LOCK(&cap->lock);
|
|
|
593
|
+}
|
|
|
594
|
+
|
|
|
595
|
+void releaseCapability_ (Capability* cap)
|
|
|
596
|
+{
|
|
|
597
|
+ releaseCapability__(cap, false /*always_wakeup*/,
|
|
|
598
|
+ false /*wakeup_worker*/);
|
|
|
599
|
+}
|
|
|
600
|
+
|
|
|
601
|
+/* The fact that we need an always_wakeup parameter for releaseCapability__ is
|
|
|
602
|
+ * a design wart. The Capability layer knows about most but not all sources of
|
|
|
603
|
+ * work for a capability. The always_wakeup parameter is there to account for
|
|
|
604
|
+ * the ones it does /not/ know about (which is I/O manager stuff: I/O, timers
|
|
|
605
|
+ * & signals).
|
|
|
606
|
+ *
|
|
|
607
|
+ * There are two sane designs:
|
|
|
608
|
+ * 1. the Capability layer knows nothing about the sources of work that the
|
|
|
609
|
+ * scheduler might want to do
|
|
|
610
|
+ * 2. the Capability layer knows *everything* about the sources of work.
|
|
|
611
|
+ *
|
|
|
612
|
+ * In neither sane design would we need this parameter. In the first design we
|
|
|
613
|
+ * would know externally if we should be waking or releasing a task and would
|
|
|
614
|
+ * instruct accordingly (probably by splitting releaseCapability to cover the
|
|
|
615
|
+ * two cases). In the second design, it would simply know about all the sources
|
|
|
616
|
+ * and so again there would be no need.
|
|
|
617
|
+ */
|
|
|
618
|
+
|
|
|
619
|
+static void releaseCapability__ (Capability* cap,
|
|
|
620
|
+ bool always_wakeup,
|
|
|
621
|
+ bool wakeup_worker)
|
|
557
|
622
|
{
|
|
558
|
623
|
{
|
|
559
|
624
|
Task *task = cap->running_task;
|
|
560
|
625
|
|
|
561
|
|
- ASSERT(task || always_wakeup);
|
|
562
|
626
|
// To cover purpose 2 above, we allow the cap->running_task to be
|
|
563
|
627
|
// NULL, to handle cases where a thread (that is not itself a Task)
|
|
564
|
628
|
// needs to wake up an idle task for the capability.
|
| ... |
... |
@@ -572,36 +636,101 @@ releaseCapability_ (Capability* cap, |
|
572
|
636
|
// Remove the current Task owning the Capability (if any, see purpose 2).
|
|
573
|
637
|
RELAXED_STORE(&cap->running_task, NULL);
|
|
574
|
638
|
|
|
575
|
|
- // Check to see whether a worker thread can be given
|
|
576
|
|
- // the go-ahead to return the result of an external call..
|
|
577
|
|
- if (cap->n_returning_tasks != 0) {
|
|
578
|
|
- giveCapabilityToTask(cap,cap->returning_tasks_hd);
|
|
579
|
|
- // The Task pops itself from the queue (see waitForCapability())
|
|
580
|
|
- return;
|
|
581
|
|
- }
|
|
|
639
|
+ // We now look for a task to give the capability to, or otherwise we leave
|
|
|
640
|
+ // the capability free.
|
|
|
641
|
+ //
|
|
|
642
|
+ // We take one of these guarded actions, in priority order:
|
|
|
643
|
+ //
|
|
|
644
|
+ // 1. If there's a pending synchronisation of all capabilities (e.g. GC),
|
|
|
645
|
+ // then give the capability to the task performing the sync.
|
|
|
646
|
+ // 2. If there's a task returning (e.g. from safe FFI) on this capability,
|
|
|
647
|
+ // then give the capability to the first such task.
|
|
|
648
|
+ // 3. If the next runnable thread on this capability is a bound thread,
|
|
|
649
|
+ // then give the capability to the corresponding bound task.
|
|
|
650
|
+ // 4. If there are no spare worker tasks for this capability,
|
|
|
651
|
+ // then start one and give the capability to the new task.
|
|
|
652
|
+ // 5. If there is some work to do on this capability (e.g. runnable thread),
|
|
|
653
|
+ // then give the capability to a worker task.
|
|
|
654
|
+ // 6. Otherwise leave the capability free/idle.
|
|
|
655
|
+ //
|
|
|
656
|
+ // There are two modifiers to this priority list:
|
|
|
657
|
+ //
|
|
|
658
|
+ // * Setting always_wakeup modifies the case 5 predicate to be always true.
|
|
|
659
|
+ // This has the effect of giving the cap to a worker task, rather than
|
|
|
660
|
+ // leaving the cap idle, even if there is no obvious work to do.
|
|
|
661
|
+ //
|
|
|
662
|
+ // * Setting wakeup_worker skips cases 2 & 3. This prioritises waking a
|
|
|
663
|
+ // worker over returning tasks or bound tasks. It is also usually used in
|
|
|
664
|
+ // combination with always_wakeup.
|
|
|
665
|
+
|
|
582
|
666
|
|
|
583
|
|
- // If there is a pending sync, then we should just leave the Capability
|
|
584
|
|
- // free. The thread trying to sync will be about to call
|
|
585
|
|
- // waitForCapability().
|
|
|
667
|
+ // Guarded action 1:
|
|
|
668
|
+ // If there's a pending synchronisation of all capabilities (e.g. GC),
|
|
|
669
|
+ // then give the capability to the task performing the sync.
|
|
|
670
|
+ //
|
|
|
671
|
+ // If there is a pending sync, the task that requested the sync will
|
|
|
672
|
+ // subsequently use acquireAllCapabilities to place itself on the (front of
|
|
|
673
|
+ // the) returning_task list (of all capabilities). We will then be in one
|
|
|
674
|
+ // of two cases:
|
|
|
675
|
+ //
|
|
|
676
|
+ // 1. the task that requested the pending sync has already put itself onto
|
|
|
677
|
+ // the returning_tasks list; or
|
|
|
678
|
+ // 2. the task that requested the pending sync has not yet put itself onto
|
|
|
679
|
+ // the returning_tasks list. This is unlikely but possible depending on
|
|
|
680
|
+ // how the race is resolved.
|
|
586
|
681
|
//
|
|
587
|
|
- // Note: this is *after* we check for a returning task above,
|
|
588
|
|
- // because the task attempting to acquire all the capabilities may
|
|
589
|
|
- // be currently in waitForCapability() waiting for this
|
|
590
|
|
- // capability, in which case simply setting it as free would not
|
|
591
|
|
- // wake up the waiting task.
|
|
|
682
|
+ // The cap->lock is used by both waitForCapability and releaseCapability_
|
|
|
683
|
+ // to ensure we are definitely in one of the two cases above, and not some
|
|
|
684
|
+ // hideous mish-mash.
|
|
|
685
|
+ //
|
|
|
686
|
+ // In the first case we can give the capability to that task. It is highly
|
|
|
687
|
+ // likely that the task has prepended itself to the returning task queue,
|
|
|
688
|
+ // so we can give the capability to the task at the head of the returning
|
|
|
689
|
+ // task queue. It is not a correctness issue however if another returning
|
|
|
690
|
+ // task gets run first (indeed this was the historical behaviour).
|
|
|
691
|
+ //
|
|
|
692
|
+ // Note that there can be false positives for this case: if any other
|
|
|
693
|
+ // returning task is queued on the returning tasks list. We will still
|
|
|
694
|
+ // incur delays if this occurs, scheduling those returning tasks. It is
|
|
|
695
|
+ // likely however that the task performing the sync gets to waiting before
|
|
|
696
|
+ // the task running the capability responds to the interrupt signal.
|
|
|
697
|
+ //
|
|
|
698
|
+ // In the second case we leave the capability free since the task trying to
|
|
|
699
|
+ // sync will be about to call waitForCapability().
|
|
592
|
700
|
//
|
|
593
|
701
|
// FIXME: this pending_sync approach is a poor design, hard to understand
|
|
594
|
702
|
// and subject to various unnecessary delays. See issues #27460 and #27473.
|
|
595
|
703
|
//
|
|
596
|
704
|
PendingSync *sync = SEQ_CST_LOAD(&pending_sync);
|
|
597
|
705
|
if (sync && (sync->type != SYNC_GC_PAR || sync->idle[cap->no])) {
|
|
598
|
|
- debugTrace(DEBUG_sched, "sync pending, freeing capability %d", cap->no);
|
|
|
706
|
+ if (cap->n_returning_tasks != 0) {
|
|
|
707
|
+ // The task doing the sync should have used waitForCapability_
|
|
|
708
|
+ // using high_priority, so it should be at the head of the queue:
|
|
|
709
|
+ debugTrace(DEBUG_sched, "sync pending, passing capability %d", cap->no);
|
|
|
710
|
+ giveCapabilityToTask(cap,cap->returning_tasks_hd);
|
|
|
711
|
+ // The Task pops itself from the queue (see waitForCapability())
|
|
|
712
|
+ } else {
|
|
|
713
|
+ debugTrace(DEBUG_sched, "sync pending, freeing capability %d", cap->no);
|
|
|
714
|
+ }
|
|
599
|
715
|
return;
|
|
600
|
716
|
}
|
|
601
|
717
|
|
|
602
|
|
- // If the next thread on the run queue is a bound thread,
|
|
603
|
|
- // give this Capability to the appropriate Task.
|
|
604
|
|
- if (!emptyRunQueue(cap) && peekRunQueue(cap)->bound) {
|
|
|
718
|
+ // Skip guarded actions 2 & 3 if wakeup_worker. See the list of actions and
|
|
|
719
|
+ // modifiers above.
|
|
|
720
|
+
|
|
|
721
|
+ // Guarded action 2:
|
|
|
722
|
+ // If there's a task returning (e.g. from safe FFI) on this capability,
|
|
|
723
|
+ // then give the capability to the first such task.
|
|
|
724
|
+ if (!wakeup_worker && cap->n_returning_tasks != 0) {
|
|
|
725
|
+ giveCapabilityToTask(cap,cap->returning_tasks_hd);
|
|
|
726
|
+ // The Task pops itself from the queue (see waitForCapability())
|
|
|
727
|
+ return;
|
|
|
728
|
+ }
|
|
|
729
|
+
|
|
|
730
|
+ // Guarded action 3:
|
|
|
731
|
+ // If the next runnable thread on this capability is a bound thread,
|
|
|
732
|
+ // then give the capability to the bound thread's corresponding task.
|
|
|
733
|
+ if (!wakeup_worker && !emptyRunQueue(cap) && peekRunQueue(cap)->bound) {
|
|
605
|
734
|
// Make sure we're not about to try to wake ourselves up
|
|
606
|
735
|
// ASSERT(task != cap->run_queue_hd->bound);
|
|
607
|
736
|
// assertion is false: in schedule() we force a yield after
|
| ... |
... |
@@ -612,11 +741,13 @@ releaseCapability_ (Capability* cap, |
|
612
|
741
|
return;
|
|
613
|
742
|
}
|
|
614
|
743
|
|
|
|
744
|
+ // Guarded action 4:
|
|
|
745
|
+ // If there are no spare worker tasks for this capability,
|
|
|
746
|
+ // then start one and give the capability to the new task.
|
|
615
|
747
|
if (!cap->spare_workers) {
|
|
616
|
|
- // Create a worker thread if we don't have one. If the system
|
|
617
|
|
- // is interrupted, we only create a worker task if there
|
|
618
|
|
- // are threads that need to be completed. If the system is
|
|
619
|
|
- // shutting down, we never create a new worker.
|
|
|
748
|
+ // If the system is interrupted, we only create a worker task if there
|
|
|
749
|
+ // are threads that need to be completed. If the system is shutting
|
|
|
750
|
+ // down, we never create a new worker.
|
|
620
|
751
|
if (getSchedState() < SCHED_SHUTTING_DOWN || !emptyRunQueue(cap)) {
|
|
621
|
752
|
debugTrace(DEBUG_sched,
|
|
622
|
753
|
"starting new worker on capability %d", cap->no);
|
| ... |
... |
@@ -625,8 +756,9 @@ releaseCapability_ (Capability* cap, |
|
625
|
756
|
}
|
|
626
|
757
|
}
|
|
627
|
758
|
|
|
628
|
|
- // If we have an unbound thread on the run queue, or if there's
|
|
629
|
|
- // anything else to do, give the Capability to a worker thread.
|
|
|
759
|
+ // Guarded action 5:
|
|
|
760
|
+ // If there is some work to do on this capability (e.g. runnable thread),
|
|
|
761
|
+ // then give the capability to a worker task.
|
|
630
|
762
|
if (always_wakeup ||
|
|
631
|
763
|
!emptyRunQueue(cap) || !emptyInbox(cap) ||
|
|
632
|
764
|
(!cap->disabled && !emptySparkPoolCap(cap)) || globalWorkToDo()) {
|
| ... |
... |
@@ -637,59 +769,14 @@ releaseCapability_ (Capability* cap, |
|
637
|
769
|
}
|
|
638
|
770
|
}
|
|
639
|
771
|
|
|
|
772
|
+ // Guarded action 6:
|
|
|
773
|
+ // Otherwise leave the capability free/idle.
|
|
640
|
774
|
#if defined(PROFILING)
|
|
641
|
775
|
cap->r.rCCCS = CCS_IDLE;
|
|
642
|
776
|
#endif
|
|
643
|
777
|
RELAXED_STORE(&last_free_capability[cap->node], cap);
|
|
644
|
778
|
debugTrace(DEBUG_sched, "freeing capability %d", cap->no);
|
|
645
|
779
|
}
|
|
646
|
|
-
|
|
647
|
|
-void
|
|
648
|
|
-releaseCapability (Capability* cap)
|
|
649
|
|
-{
|
|
650
|
|
- ACQUIRE_LOCK(&cap->lock);
|
|
651
|
|
- releaseCapability_(cap, false);
|
|
652
|
|
- RELEASE_LOCK(&cap->lock);
|
|
653
|
|
-}
|
|
654
|
|
-
|
|
655
|
|
-void
|
|
656
|
|
-releaseAndWakeupCapability (Capability* cap)
|
|
657
|
|
-{
|
|
658
|
|
- ACQUIRE_LOCK(&cap->lock);
|
|
659
|
|
- releaseCapability_(cap, true);
|
|
660
|
|
- RELEASE_LOCK(&cap->lock);
|
|
661
|
|
-}
|
|
662
|
|
-
|
|
663
|
|
-static void
|
|
664
|
|
-enqueueWorker (Capability* cap)
|
|
665
|
|
-{
|
|
666
|
|
- Task *task;
|
|
667
|
|
-
|
|
668
|
|
- task = cap->running_task;
|
|
669
|
|
-
|
|
670
|
|
- // If the Task is stopped, we shouldn't be yielding, we should
|
|
671
|
|
- // be just exiting.
|
|
672
|
|
- ASSERT(!task->stopped);
|
|
673
|
|
- ASSERT(task->worker);
|
|
674
|
|
-
|
|
675
|
|
- if (cap->n_spare_workers < MAX_SPARE_WORKERS)
|
|
676
|
|
- {
|
|
677
|
|
- task->next = cap->spare_workers;
|
|
678
|
|
- cap->spare_workers = task;
|
|
679
|
|
- cap->n_spare_workers++;
|
|
680
|
|
- }
|
|
681
|
|
- else
|
|
682
|
|
- {
|
|
683
|
|
- debugTrace(DEBUG_sched, "%d spare workers already, exiting",
|
|
684
|
|
- cap->n_spare_workers);
|
|
685
|
|
- releaseCapability_(cap,false);
|
|
686
|
|
- // hold the lock until after workerTaskStop; c.f. scheduleWorker()
|
|
687
|
|
- workerTaskStop(task);
|
|
688
|
|
- RELEASE_LOCK(&cap->lock);
|
|
689
|
|
- shutdownThread();
|
|
690
|
|
- }
|
|
691
|
|
-}
|
|
692
|
|
-
|
|
693
|
780
|
#endif
|
|
694
|
781
|
|
|
695
|
782
|
/*
|
| ... |
... |
@@ -822,7 +909,7 @@ static Capability * waitForReturnCapability (Task *task) |
|
822
|
909
|
continue;
|
|
823
|
910
|
}
|
|
824
|
911
|
RELAXED_STORE(&cap->running_task, task);
|
|
825
|
|
- popReturningTask(cap);
|
|
|
912
|
+ popReturningTaskQueue(cap);
|
|
826
|
913
|
RELEASE_LOCK(&cap->lock);
|
|
827
|
914
|
break;
|
|
828
|
915
|
}
|
| ... |
... |
@@ -893,38 +980,65 @@ static Capability * find_capability_for_task(const Task * task) |
|
893
|
980
|
#endif /* THREADED_RTS */
|
|
894
|
981
|
|
|
895
|
982
|
/* ----------------------------------------------------------------------------
|
|
896
|
|
- * waitForCapability (Capability **pCap, Task *task)
|
|
|
983
|
+ * Capability *waitForCapability (Task *task)
|
|
897
|
984
|
*
|
|
898
|
|
- * Purpose: when an OS thread returns from an external call,
|
|
899
|
|
- * it calls waitForCapability() (via Schedule.resumeThread())
|
|
900
|
|
- * to wait for permission to enter the RTS & communicate the
|
|
901
|
|
- * result of the external call back to the Haskell thread that
|
|
902
|
|
- * made it.
|
|
|
985
|
+ * Purpose: when an OS thread returns from an external call, it calls
|
|
|
986
|
+ * waitForCapability() (via Schedule.resumeThread()) to wait for
|
|
|
987
|
+ * permission to enter the RTS & communicate the result of the external
|
|
|
988
|
+ * call back to the Haskell thread that made it. The task must already
|
|
|
989
|
+ * be associated with a capability.
|
|
903
|
990
|
*
|
|
904
|
|
- * pCap is strictly an output.
|
|
|
991
|
+ * Capability *waitForSomeCapability (Task *task)
|
|
905
|
992
|
*
|
|
|
993
|
+ * Like waitForCapability but the task need not already be associated
|
|
|
994
|
+ * with a capability. If it is not associated, an appropriate one will
|
|
|
995
|
+ * be chosen. Used in rts_lock(), for calling into the RTS from outside.
|
|
906
|
996
|
* ------------------------------------------------------------------------- */
|
|
907
|
997
|
|
|
908
|
|
-void waitForCapability (Capability **pCap, Task *task)
|
|
909
|
|
-{
|
|
910
|
998
|
#if !defined(THREADED_RTS)
|
|
911
|
|
-
|
|
|
999
|
+Capability *waitForSomeCapability (Task *task)
|
|
|
1000
|
+{
|
|
912
|
1001
|
MainCapability.running_task = task;
|
|
913
|
1002
|
task->cap = &MainCapability;
|
|
914
|
|
- *pCap = &MainCapability;
|
|
|
1003
|
+ return &MainCapability;
|
|
|
1004
|
+}
|
|
|
1005
|
+
|
|
|
1006
|
+void waitForCapability (Task *task)
|
|
|
1007
|
+{
|
|
|
1008
|
+ waitForSomeCapability(task);
|
|
|
1009
|
+}
|
|
915
|
1010
|
|
|
916
|
1011
|
#else
|
|
917
|
|
- Capability *cap = *pCap;
|
|
918
|
1012
|
|
|
|
1013
|
+static void waitForCapability_ (Task *task,
|
|
|
1014
|
+ bool high_priority);
|
|
|
1015
|
+
|
|
|
1016
|
+Capability *waitForSomeCapability (Task *task)
|
|
|
1017
|
+{
|
|
|
1018
|
+ Capability *cap = task->cap;
|
|
919
|
1019
|
if (cap == NULL) {
|
|
920
|
1020
|
cap = find_capability_for_task(task);
|
|
921
|
1021
|
|
|
922
|
1022
|
// record the Capability as the one this Task is now associated with.
|
|
923
|
1023
|
task->cap = cap;
|
|
924
|
|
- } else {
|
|
925
|
|
- ASSERT(task->cap == cap);
|
|
926
|
1024
|
}
|
|
927
|
1025
|
|
|
|
1026
|
+ waitForCapability_(task, false /*high_priority*/);
|
|
|
1027
|
+
|
|
|
1028
|
+ return task->cap;
|
|
|
1029
|
+}
|
|
|
1030
|
+
|
|
|
1031
|
+void waitForCapability (Task *task)
|
|
|
1032
|
+{
|
|
|
1033
|
+ waitForCapability_(task, false /*high_priority*/);
|
|
|
1034
|
+}
|
|
|
1035
|
+
|
|
|
1036
|
+static void waitForCapability_ (Task *task,
|
|
|
1037
|
+ bool high_priority)
|
|
|
1038
|
+{
|
|
|
1039
|
+ Capability *cap = task->cap;
|
|
|
1040
|
+ ASSERT(task->cap);
|
|
|
1041
|
+
|
|
928
|
1042
|
debugTrace(DEBUG_sched, "returning; I want capability %d", cap->no);
|
|
929
|
1043
|
|
|
930
|
1044
|
ACQUIRE_LOCK(&cap->lock);
|
| ... |
... |
@@ -933,7 +1047,11 @@ void waitForCapability (Capability **pCap, Task *task) |
|
933
|
1047
|
RELAXED_STORE(&cap->running_task, task);
|
|
934
|
1048
|
RELEASE_LOCK(&cap->lock);
|
|
935
|
1049
|
} else {
|
|
936
|
|
- newReturningTask(cap,task);
|
|
|
1050
|
+ if (high_priority) {
|
|
|
1051
|
+ prependToReturningTaskQueue(cap,task);
|
|
|
1052
|
+ } else {
|
|
|
1053
|
+ appendToReturningTaskQueue(cap,task);
|
|
|
1054
|
+ }
|
|
937
|
1055
|
RELEASE_LOCK(&cap->lock);
|
|
938
|
1056
|
cap = waitForReturnCapability(task);
|
|
939
|
1057
|
}
|
| ... |
... |
@@ -945,10 +1063,8 @@ void waitForCapability (Capability **pCap, Task *task) |
|
945
|
1063
|
ASSERT_FULL_CAPABILITY_INVARIANTS(cap, task);
|
|
946
|
1064
|
|
|
947
|
1065
|
debugTrace(DEBUG_sched, "resuming capability %d", cap->no);
|
|
948
|
|
-
|
|
949
|
|
- *pCap = cap;
|
|
950
|
|
-#endif
|
|
951
|
1066
|
}
|
|
|
1067
|
+#endif
|
|
952
|
1068
|
|
|
953
|
1069
|
/* ----------------------------------------------------------------------------
|
|
954
|
1070
|
* yieldCapability
|
| ... |
... |
@@ -957,12 +1073,6 @@ void waitForCapability (Capability **pCap, Task *task) |
|
957
|
1073
|
* when either we know that the Capability should be given to another Task, or
|
|
958
|
1074
|
* there is nothing to do right now. One of the following is true:
|
|
959
|
1075
|
*
|
|
960
|
|
- * - The current Task is a worker, and there's a bound thread at the head of
|
|
961
|
|
- * the run queue (or vice versa)
|
|
962
|
|
- *
|
|
963
|
|
- * - The run queue is empty. We'll be woken up again when there's work to
|
|
964
|
|
- * do.
|
|
965
|
|
- *
|
|
966
|
1076
|
* - Another Task is trying to do parallel GC (pending_sync == SYNC_GC_PAR).
|
|
967
|
1077
|
* We should become a GC worker for a while.
|
|
968
|
1078
|
*
|
| ... |
... |
@@ -970,12 +1080,21 @@ void waitForCapability (Capability **pCap, Task *task) |
|
970
|
1080
|
* SYNC_GC_PAR), either to do a sequential GC, forkProcess, or
|
|
971
|
1081
|
* setNumCapabilities. We should give up the Capability temporarily.
|
|
972
|
1082
|
*
|
|
|
1083
|
+ * - There is a Task returning from a safe FFI call.
|
|
|
1084
|
+ *
|
|
|
1085
|
+ * - The current Task is a worker, and there's a bound thread at the head of
|
|
|
1086
|
+ * the run queue (or vice versa)
|
|
|
1087
|
+ *
|
|
|
1088
|
+ * - There is no work to do (empty run queue, inbox etc). We'll be woken up
|
|
|
1089
|
+ * again when there's work to do.
|
|
|
1090
|
+ *
|
|
973
|
1091
|
* When yieldCapability returns *pCap will have been updated to the new
|
|
974
|
1092
|
* capability held by the caller.
|
|
975
|
1093
|
*
|
|
976
|
1094
|
* ------------------------------------------------------------------------- */
|
|
977
|
1095
|
|
|
978
|
1096
|
#if defined(THREADED_RTS)
|
|
|
1097
|
+static bool tryEnqueueWorker (Capability* cap);
|
|
979
|
1098
|
|
|
980
|
1099
|
/* See Note [GC livelock] in Schedule.c for why we have gcAllowed
|
|
981
|
1100
|
and return the bool */
|
| ... |
... |
@@ -1030,28 +1149,61 @@ yieldCapability |
|
1030
|
1149
|
// We must now release the capability and wait to be woken up again.
|
|
1031
|
1150
|
task->wakeup = false;
|
|
1032
|
1151
|
|
|
|
1152
|
+ // What happens next is a bit complicated. It has the following outline:
|
|
|
1153
|
+ //
|
|
|
1154
|
+ // 1. take the cap->lock
|
|
|
1155
|
+ // 2. "various stuff part A", pre-releaseCapability_ holding cap->lock
|
|
|
1156
|
+ // 3. release the capability
|
|
|
1157
|
+ // 4. "various stuff part B", post-releaseCapability_ holding cap->lock
|
|
|
1158
|
+ // 5. release the cap->lock
|
|
|
1159
|
+ // 6. "various stuff part C", post release cap->lock
|
|
|
1160
|
+ //
|
|
|
1161
|
+ // Much of the "various stuff" is also conditional, which complicates
|
|
|
1162
|
+ // matters further. To try and maintain clarity we use the following
|
|
|
1163
|
+ // variables in the conditions for the in-between steps.
|
|
|
1164
|
+ //
|
|
|
1165
|
+ bool terminate_worker = false;
|
|
|
1166
|
+ bool task_is_worker = isWorker(task);
|
|
|
1167
|
+ bool task_is_bound = isBoundTask(task);
|
|
|
1168
|
+
|
|
|
1169
|
+ // Step 1: take the cap->lock
|
|
1033
|
1170
|
ACQUIRE_LOCK(&cap->lock);
|
|
1034
|
1171
|
|
|
1035
|
|
- // If this is a worker thread, put it on the spare_workers queue
|
|
1036
|
|
- if (isWorker(task)) {
|
|
1037
|
|
- enqueueWorker(cap);
|
|
|
1172
|
+ // Step 2: "various stuff part A", pre-releaseCapability_ holding cap->lock
|
|
|
1173
|
+ if (task_is_worker) {
|
|
|
1174
|
+ // If this is a worker thread, try to put it on the spare_workers
|
|
|
1175
|
+ // queue or if it is surplus then we will terminate it.
|
|
|
1176
|
+ terminate_worker = !tryEnqueueWorker(cap);
|
|
1038
|
1177
|
}
|
|
1039
|
1178
|
|
|
1040
|
|
- releaseCapability_(cap, false);
|
|
|
1179
|
+ // Step 3: release the capability
|
|
|
1180
|
+ releaseCapability_(cap);
|
|
1041
|
1181
|
|
|
1042
|
|
- if (isWorker(task) || isBoundTask(task)) {
|
|
1043
|
|
- RELEASE_LOCK(&cap->lock);
|
|
1044
|
|
- cap = waitForWorkerCapability(task);
|
|
1045
|
|
- } else {
|
|
|
1182
|
+ // Step 4: "various stuff part B", post-releaseCapability_ holding cap->lock
|
|
|
1183
|
+ if (terminate_worker) {
|
|
|
1184
|
+ // hold the lock until after workerTaskStop; c.f. scheduleWorker()
|
|
|
1185
|
+ workerTaskStop(task);
|
|
|
1186
|
+ } else if (!task_is_worker && !task_is_bound) {
|
|
1046
|
1187
|
// Not a worker Task, or a bound Task. The only way we can be woken up
|
|
1047
|
1188
|
// again is to put ourselves on the returning_tasks queue, so that's
|
|
1048
|
|
- // what we do. We still hold cap->lock at this point
|
|
1049
|
|
- // The Task waiting for this Capability does not have it
|
|
1050
|
|
- // yet, so we can be sure to be woken up later. (see #10545)
|
|
1051
|
|
- newReturningTask(cap,task);
|
|
1052
|
|
- RELEASE_LOCK(&cap->lock);
|
|
|
1189
|
+ // what we do. We still hold cap->lock at this point. The Task waiting
|
|
|
1190
|
+ // for this Capability does not have it yet, so we can be sure to be
|
|
|
1191
|
+ // woken up later. (see #10545)
|
|
|
1192
|
+ appendToReturningTaskQueue(cap,task);
|
|
|
1193
|
+ }
|
|
|
1194
|
+
|
|
|
1195
|
+ // Step 5: release the cap->lock
|
|
|
1196
|
+ RELEASE_LOCK(&cap->lock);
|
|
|
1197
|
+
|
|
|
1198
|
+ // Step 6. "various stuff part C", post release cap->lock
|
|
|
1199
|
+ if (terminate_worker) {
|
|
|
1200
|
+ shutdownThread();
|
|
|
1201
|
+ } else if (task_is_worker || task_is_bound) {
|
|
|
1202
|
+ cap = waitForWorkerCapability(task);
|
|
|
1203
|
+ } else {
|
|
1053
|
1204
|
cap = waitForReturnCapability(task);
|
|
1054
|
1205
|
}
|
|
|
1206
|
+ // End of step 6.
|
|
1055
|
1207
|
|
|
1056
|
1208
|
debugTrace(DEBUG_sched, "resuming capability %d", cap->no);
|
|
1057
|
1209
|
ASSERT(cap->running_task == task);
|
| ... |
... |
@@ -1067,8 +1219,226 @@ yieldCapability |
|
1067
|
1219
|
return false;
|
|
1068
|
1220
|
}
|
|
1069
|
1221
|
|
|
|
1222
|
+// Returns true if it could enqueue, and false if the worker is surplus to
|
|
|
1223
|
+// requirements and should be terminated.
|
|
|
1224
|
+static bool tryEnqueueWorker (Capability* cap)
|
|
|
1225
|
+{
|
|
|
1226
|
+ Task *task = cap->running_task;
|
|
|
1227
|
+
|
|
|
1228
|
+ // If the Task is stopped, we shouldn't be yielding, we should
|
|
|
1229
|
+ // be just exiting.
|
|
|
1230
|
+ ASSERT(!task->stopped);
|
|
|
1231
|
+ ASSERT(task->worker);
|
|
|
1232
|
+ ASSERT_LOCK_HELD(&cap->lock);
|
|
|
1233
|
+
|
|
|
1234
|
+ if (cap->n_spare_workers < MAX_SPARE_WORKERS)
|
|
|
1235
|
+ {
|
|
|
1236
|
+ task->next = cap->spare_workers;
|
|
|
1237
|
+ cap->spare_workers = task;
|
|
|
1238
|
+ cap->n_spare_workers++;
|
|
|
1239
|
+ return true;
|
|
|
1240
|
+ }
|
|
|
1241
|
+ else
|
|
|
1242
|
+ {
|
|
|
1243
|
+ debugTrace(DEBUG_sched, "%d spare workers already, exiting",
|
|
|
1244
|
+ cap->n_spare_workers);
|
|
|
1245
|
+ return false;
|
|
|
1246
|
+ }
|
|
|
1247
|
+}
|
|
|
1248
|
+
|
|
1070
|
1249
|
#endif /* THREADED_RTS */
|
|
1071
|
1250
|
|
|
|
1251
|
+
|
|
|
1252
|
+/* -----------------------------------------------------------------------------
|
|
|
1253
|
+ * stopAllCapabilities()
|
|
|
1254
|
+ *
|
|
|
1255
|
+ * Stop all Haskell execution. This is used when we need to make some global
|
|
|
1256
|
+ * change to the system, such as altering the number of capabilities, or
|
|
|
1257
|
+ * forking.
|
|
|
1258
|
+ *
|
|
|
1259
|
+ * pCap may be NULL in the event that the caller doesn't yet own a capability.
|
|
|
1260
|
+ *
|
|
|
1261
|
+ * To resume after stopAllCapabilities(), use releaseAllCapabilities().
|
|
|
1262
|
+ * -------------------------------------------------------------------------- */
|
|
|
1263
|
+
|
|
|
1264
|
+#if defined(THREADED_RTS)
|
|
|
1265
|
+void stopAllCapabilities
|
|
|
1266
|
+ ( Capability **pCap // [in/out] This thread's task's owned capability.
|
|
|
1267
|
+ // pCap may be NULL if no capability is owned.
|
|
|
1268
|
+ // Else *pCap != NULL
|
|
|
1269
|
+ // On return, set to the task's newly owned
|
|
|
1270
|
+ // capability (task->cap). Though, the Task will
|
|
|
1271
|
+ // technically own all capabilities.
|
|
|
1272
|
+ , Task *task // [in] This thread's task.
|
|
|
1273
|
+ )
|
|
|
1274
|
+{
|
|
|
1275
|
+ stopAllCapabilitiesWith(pCap, task, SYNC_OTHER);
|
|
|
1276
|
+}
|
|
|
1277
|
+
|
|
|
1278
|
+void stopAllCapabilitiesWith (Capability **pCap, Task *task, SyncType sync_type)
|
|
|
1279
|
+{
|
|
|
1280
|
+ bool was_syncing;
|
|
|
1281
|
+ SyncType prev_sync_type;
|
|
|
1282
|
+
|
|
|
1283
|
+ PendingSync sync = {
|
|
|
1284
|
+ .type = sync_type,
|
|
|
1285
|
+ .idle = NULL,
|
|
|
1286
|
+ .task = task
|
|
|
1287
|
+ };
|
|
|
1288
|
+
|
|
|
1289
|
+ do {
|
|
|
1290
|
+ was_syncing = requestSync(pCap, task, &sync, &prev_sync_type);
|
|
|
1291
|
+ } while (was_syncing);
|
|
|
1292
|
+
|
|
|
1293
|
+ acquireAllCapabilities(pCap ? *pCap : NULL, task);
|
|
|
1294
|
+
|
|
|
1295
|
+ resetSync();
|
|
|
1296
|
+}
|
|
|
1297
|
+#endif
|
|
|
1298
|
+
|
|
|
1299
|
+/* -----------------------------------------------------------------------------
|
|
|
1300
|
+ * requestSync()
|
|
|
1301
|
+ *
|
|
|
1302
|
+ * Commence a synchronisation between all capabilities. Normally not called
|
|
|
1303
|
+ * directly, instead use stopAllCapabilities(). This is used by the GC, which
|
|
|
1304
|
+ * has some special synchronisation requirements.
|
|
|
1305
|
+ *
|
|
|
1306
|
+ * Note that this can be called in two ways:
|
|
|
1307
|
+ *
|
|
|
1308
|
+ * - where *pcap points to a capability owned by the caller: in this case
|
|
|
1309
|
+ * *prev_sync_type will reflect the in-progress sync type on return, if one
|
|
|
1310
|
+ * *was found
|
|
|
1311
|
+ *
|
|
|
1312
|
+ * - where pcap == NULL: in this case the caller doesn't hold a capability.
|
|
|
1313
|
+ * we only return whether or not a pending sync was found and prev_sync_type
|
|
|
1314
|
+ * is unchanged.
|
|
|
1315
|
+ *
|
|
|
1316
|
+ * Returns:
|
|
|
1317
|
+ * false if we successfully got a sync
|
|
|
1318
|
+ * true if there was another sync request in progress,
|
|
|
1319
|
+ * and we yielded to it. The value returned is the
|
|
|
1320
|
+ * type of the other sync request.
|
|
|
1321
|
+ * -------------------------------------------------------------------------- */
|
|
|
1322
|
+
|
|
|
1323
|
+#if defined(THREADED_RTS)
|
|
|
1324
|
+bool requestSync
|
|
|
1325
|
+ ( Capability **pcap // [in/out] This thread's task's owned capability.
|
|
|
1326
|
+ // May change if there is an existing sync (true is returned).
|
|
|
1327
|
+ // Precondition:
|
|
|
1328
|
+ // pcap may be NULL
|
|
|
1329
|
+ // *pcap != NULL
|
|
|
1330
|
+ , Task *task // [in] This thread's task.
|
|
|
1331
|
+ , PendingSync *new_sync // [in] The new requested sync.
|
|
|
1332
|
+ , SyncType *prev_sync_type // [out] Only set if there is an existing sync (true is returned).
|
|
|
1333
|
+ )
|
|
|
1334
|
+{
|
|
|
1335
|
+ PendingSync *sync;
|
|
|
1336
|
+
|
|
|
1337
|
+ sync = (PendingSync*)cas((StgVolatilePtr)&pending_sync,
|
|
|
1338
|
+ (StgWord)NULL,
|
|
|
1339
|
+ (StgWord)new_sync);
|
|
|
1340
|
+
|
|
|
1341
|
+ if (sync != NULL)
|
|
|
1342
|
+ {
|
|
|
1343
|
+ // sync is valid until we have called yieldCapability().
|
|
|
1344
|
+ // After the sync is completed, we cannot read that struct any
|
|
|
1345
|
+ // more because it has been freed.
|
|
|
1346
|
+ *prev_sync_type = sync->type;
|
|
|
1347
|
+ if (pcap == NULL) {
|
|
|
1348
|
+ // The caller does not hold a capability (e.g. may be a concurrent
|
|
|
1349
|
+ // mark thread). Consequently we must wait until the pending sync is
|
|
|
1350
|
+ // finished before proceeding to ensure we don't loop.
|
|
|
1351
|
+ // TODO: Don't busy-wait
|
|
|
1352
|
+ ACQUIRE_LOCK(&sync_finished_mutex);
|
|
|
1353
|
+ while (pending_sync) {
|
|
|
1354
|
+ waitCondition(&sync_finished_cond, &sync_finished_mutex);
|
|
|
1355
|
+ }
|
|
|
1356
|
+ RELEASE_LOCK(&sync_finished_mutex);
|
|
|
1357
|
+ } else {
|
|
|
1358
|
+ do {
|
|
|
1359
|
+ debugTrace(DEBUG_sched, "someone else is trying to sync (%d)...",
|
|
|
1360
|
+ sync->type);
|
|
|
1361
|
+ ASSERT(*pcap);
|
|
|
1362
|
+ yieldCapability(pcap,task,true);
|
|
|
1363
|
+ sync = SEQ_CST_LOAD(&pending_sync);
|
|
|
1364
|
+ } while (sync != NULL);
|
|
|
1365
|
+ }
|
|
|
1366
|
+
|
|
|
1367
|
+ // NOTE: task->cap might have changed now
|
|
|
1368
|
+ return true;
|
|
|
1369
|
+ }
|
|
|
1370
|
+ else
|
|
|
1371
|
+ {
|
|
|
1372
|
+ return false;
|
|
|
1373
|
+ }
|
|
|
1374
|
+}
|
|
|
1375
|
+
|
|
|
1376
|
+void resetSync (void)
|
|
|
1377
|
+{
|
|
|
1378
|
+ RELAXED_STORE(&pending_sync, 0);
|
|
|
1379
|
+ signalCondition(&sync_finished_cond);
|
|
|
1380
|
+}
|
|
|
1381
|
+#endif
|
|
|
1382
|
+
|
|
|
1383
|
+/* -----------------------------------------------------------------------------
|
|
|
1384
|
+ * acquireAllCapabilities()
|
|
|
1385
|
+ *
|
|
|
1386
|
+ * Grab all the capabilities except the one we already hold (cap may be NULL if
|
|
|
1387
|
+ * the caller does not currently hold a capability). Used when synchronising
|
|
|
1388
|
+ * before a single-threaded GC (SYNC_SEQ_GC), and before a fork (SYNC_OTHER).
|
|
|
1389
|
+ *
|
|
|
1390
|
+ * Only call this after requestSync(), otherwise a deadlock might
|
|
|
1391
|
+ * ensue if another thread is trying to synchronise.
|
|
|
1392
|
+ * -------------------------------------------------------------------------- */
|
|
|
1393
|
+
|
|
|
1394
|
+#if defined(THREADED_RTS)
|
|
|
1395
|
+void acquireAllCapabilities(Capability *cap, Task *task)
|
|
|
1396
|
+{
|
|
|
1397
|
+ Capability *tmpcap = NULL;
|
|
|
1398
|
+ uint32_t i;
|
|
|
1399
|
+
|
|
|
1400
|
+ ASSERT(SEQ_CST_LOAD(&pending_sync) != NULL);
|
|
|
1401
|
+ for (i=0; i < getNumCapabilities(); i++) {
|
|
|
1402
|
+ debugTrace(DEBUG_sched, "grabbing all the capabilities (%d/%d)",
|
|
|
1403
|
+ i, getNumCapabilities());
|
|
|
1404
|
+ tmpcap = getCapability(i);
|
|
|
1405
|
+ if (tmpcap != cap) {
|
|
|
1406
|
+ task->cap = tmpcap;
|
|
|
1407
|
+ waitForCapability_(task, true /*high_priority*/);
|
|
|
1408
|
+
|
|
|
1409
|
+ // Note that waitForCapability only waits for the capability
|
|
|
1410
|
+ // the task is associated with. There's no task migration here.
|
|
|
1411
|
+ ASSERT(task->cap == tmpcap);
|
|
|
1412
|
+ }
|
|
|
1413
|
+ }
|
|
|
1414
|
+ ASSERT(tmpcap != NULL);
|
|
|
1415
|
+ task->cap = cap == NULL ? tmpcap : cap;
|
|
|
1416
|
+}
|
|
|
1417
|
+#endif
|
|
|
1418
|
+
|
|
|
1419
|
+/* -----------------------------------------------------------------------------
|
|
|
1420
|
+ * releaseAllCapabilities()
|
|
|
1421
|
+ *
|
|
|
1422
|
+ * Assuming this thread holds all the capabilities, release them all (except for
|
|
|
1423
|
+ * the one passed in as keep_cap, if non-NULL).
|
|
|
1424
|
+ * -------------------------------------------------------------------------- */
|
|
|
1425
|
+
|
|
|
1426
|
+#if defined(THREADED_RTS)
|
|
|
1427
|
+void releaseAllCapabilities(uint32_t n, Capability *keep_cap, Task *task)
|
|
|
1428
|
+{
|
|
|
1429
|
+ uint32_t i;
|
|
|
1430
|
+ ASSERT( task != NULL);
|
|
|
1431
|
+ for (i = 0; i < n; i++) {
|
|
|
1432
|
+ Capability *tmpcap = getCapability(i);
|
|
|
1433
|
+ if (keep_cap != tmpcap) {
|
|
|
1434
|
+ task->cap = tmpcap;
|
|
|
1435
|
+ releaseCapability(tmpcap);
|
|
|
1436
|
+ }
|
|
|
1437
|
+ }
|
|
|
1438
|
+ task->cap = keep_cap;
|
|
|
1439
|
+}
|
|
|
1440
|
+#endif
|
|
|
1441
|
+
|
|
1072
|
1442
|
/*
|
|
1073
|
1443
|
* Note [migrated bound threads]
|
|
1074
|
1444
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -1117,7 +1487,14 @@ prodCapability (Capability *cap) |
|
1117
|
1487
|
{
|
|
1118
|
1488
|
ACQUIRE_LOCK(&cap->lock);
|
|
1119
|
1489
|
if (!cap->running_task) {
|
|
1120
|
|
- releaseCapability_(cap,true);
|
|
|
1490
|
+ /* We have to use always_wakeup here because when prodCapability is
|
|
|
1491
|
+ * used for ctl-c, releaseCapability__ does not know about pending
|
|
|
1492
|
+ * signals (or the I/O managers generally) as one of the set of
|
|
|
1493
|
+ * conditions to look for when deciding if a Task should be woken up
|
|
|
1494
|
+ * to run the Capability. This is a bit of a design wart.
|
|
|
1495
|
+ */
|
|
|
1496
|
+ releaseCapability__(cap, true /*always_wakeup*/,
|
|
|
1497
|
+ false /*wakeup_worker*/);
|
|
1121
|
1498
|
}
|
|
1122
|
1499
|
RELEASE_LOCK(&cap->lock);
|
|
1123
|
1500
|
}
|
| ... |
... |
@@ -1238,7 +1615,7 @@ shutdownCapability (Capability *cap USED_IF_THREADS, |
|
1238
|
1615
|
if (!emptyRunQueue(cap) || cap->spare_workers) {
|
|
1239
|
1616
|
debugTrace(DEBUG_sched,
|
|
1240
|
1617
|
"runnable threads or workers still alive, yielding");
|
|
1241
|
|
- releaseCapability_(cap,false); // this will wake up a worker
|
|
|
1618
|
+ releaseCapability_(cap); // this will wake up a worker
|
|
1242
|
1619
|
RELEASE_LOCK(&cap->lock);
|
|
1243
|
1620
|
yieldThread();
|
|
1244
|
1621
|
continue;
|