1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2003-2006
7 * A Capability represent the token required to execute STG code,
8 * and all the state an OS thread/task needs to run Haskell code:
9 * its STG registers, a pointer to its TSO, a nursery etc. During
10 * STG execution, a pointer to the capabilitity is kept in a
11 * register (BaseReg; actually it is a pointer to cap->r).
13 * Only in an THREADED_RTS build will there be multiple capabilities,
14 * for non-threaded builds there is only one global capability, namely
17 * --------------------------------------------------------------------------*/
19 #include "PosixSource.h"
24 #include "OSThreads.h"
25 #include "Capability.h"
30 // one global capability, this is the Capability for non-threaded
31 // builds, and for +RTS -N1
32 Capability MainCapability;
35 Capability *capabilities = NULL;
37 // Holds the Capability which last became free. This is used so that
38 // an in-call has a chance of quickly finding a free Capability.
39 // Maintaining a global free list of Capabilities would require global
40 // locking, so we don't do that.
41 Capability *last_free_capability;
43 #if defined(THREADED_RTS)
47 return blackholes_need_checking
48 || sched_state >= SCHED_INTERRUPTING
53 #if defined(THREADED_RTS)
55 anyWorkForMe( Capability *cap, Task *task )
57 if (task->tso != NULL) {
58 // A bound task only runs if its thread is on the run queue of
59 // the capability on which it was woken up. Otherwise, we
60 // can't be sure that we have the right capability: the thread
61 // might be woken up on some other capability, and task->cap
62 // could change under our feet.
63 return !emptyRunQueue(cap) && cap->run_queue_hd->bound == task;
65 // A vanilla worker task runs if either there is a lightweight
66 // thread at the head of the run queue, or the run queue is
67 // empty and (there are sparks to execute, or there is some
68 // other global condition to check, such as threads blocked on
70 if (emptyRunQueue(cap)) {
71 return !emptySparkPoolCap(cap)
72 || !emptyWakeupQueue(cap)
75 return cap->run_queue_hd->bound == NULL;
80 /* -----------------------------------------------------------------------------
81 * Manage the returning_tasks lists.
83 * These functions require cap->lock
84 * -------------------------------------------------------------------------- */
86 #if defined(THREADED_RTS)
88 newReturningTask (Capability *cap, Task *task)
90 ASSERT_LOCK_HELD(&cap->lock);
91 ASSERT(task->return_link == NULL);
92 if (cap->returning_tasks_hd) {
93 ASSERT(cap->returning_tasks_tl->return_link == NULL);
94 cap->returning_tasks_tl->return_link = task;
96 cap->returning_tasks_hd = task;
98 cap->returning_tasks_tl = task;
102 popReturningTask (Capability *cap)
104 ASSERT_LOCK_HELD(&cap->lock);
106 task = cap->returning_tasks_hd;
108 cap->returning_tasks_hd = task->return_link;
109 if (!cap->returning_tasks_hd) {
110 cap->returning_tasks_tl = NULL;
112 task->return_link = NULL;
117 /* ----------------------------------------------------------------------------
120 * The Capability is initially marked not free.
121 * ------------------------------------------------------------------------- */
124 initCapability( Capability *cap, nat i )
129 cap->in_haskell = rtsFalse;
131 cap->run_queue_hd = END_TSO_QUEUE;
132 cap->run_queue_tl = END_TSO_QUEUE;
134 #if defined(THREADED_RTS)
135 initMutex(&cap->lock);
136 cap->running_task = NULL; // indicates cap is free
137 cap->spare_workers = NULL;
138 cap->suspended_ccalling_tasks = NULL;
139 cap->returning_tasks_hd = NULL;
140 cap->returning_tasks_tl = NULL;
141 cap->wakeup_queue_hd = END_TSO_QUEUE;
142 cap->wakeup_queue_tl = END_TSO_QUEUE;
145 cap->f.stgGCEnter1 = (F_)__stg_gc_enter_1;
146 cap->f.stgGCFun = (F_)__stg_gc_fun;
148 cap->mut_lists = stgMallocBytes(sizeof(bdescr *) *
149 RtsFlags.GcFlags.generations,
152 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
153 cap->mut_lists[g] = NULL;
156 cap->free_tvar_watch_queues = END_STM_WATCH_QUEUE;
157 cap->free_invariant_check_queues = END_INVARIANT_CHECK_QUEUE;
158 cap->free_trec_chunks = END_STM_CHUNK_LIST;
159 cap->free_trec_headers = NO_TREC;
160 cap->transaction_tokens = 0;
163 /* ---------------------------------------------------------------------------
164 * Function: initCapabilities()
166 * Purpose: set up the Capability handling. For the THREADED_RTS build,
167 * we keep a table of them, the size of which is
168 * controlled by the user via the RTS flag -N.
170 * ------------------------------------------------------------------------- */
172 initCapabilities( void )
174 #if defined(THREADED_RTS)
178 // We can't support multiple CPUs if BaseReg is not a register
179 if (RtsFlags.ParFlags.nNodes > 1) {
180 errorBelch("warning: multiple CPUs not supported in this build, reverting to 1");
181 RtsFlags.ParFlags.nNodes = 1;
185 n_capabilities = RtsFlags.ParFlags.nNodes;
187 if (n_capabilities == 1) {
188 capabilities = &MainCapability;
189 // THREADED_RTS must work on builds that don't have a mutable
190 // BaseReg (eg. unregisterised), so in this case
191 // capabilities[0] must coincide with &MainCapability.
193 capabilities = stgMallocBytes(n_capabilities * sizeof(Capability),
197 for (i = 0; i < n_capabilities; i++) {
198 initCapability(&capabilities[i], i);
201 debugTrace(DEBUG_sched, "allocated %d capabilities", n_capabilities);
203 #else /* !THREADED_RTS */
206 capabilities = &MainCapability;
207 initCapability(&MainCapability, 0);
211 // There are no free capabilities to begin with. We will start
212 // a worker Task to each Capability, which will quickly put the
213 // Capability on the free list when it finds nothing to do.
214 last_free_capability = &capabilities[0];
217 /* ----------------------------------------------------------------------------
218 * Give a Capability to a Task. The task must currently be sleeping
219 * on its condition variable.
221 * Requires cap->lock (modifies cap->running_task).
223 * When migrating a Task, the migrater must take task->lock before
224 * modifying task->cap, to synchronise with the waking up Task.
225 * Additionally, the migrater should own the Capability (when
226 * migrating the run queue), or cap->lock (when migrating
227 * returning_workers).
229 * ------------------------------------------------------------------------- */
231 #if defined(THREADED_RTS)
233 giveCapabilityToTask (Capability *cap USED_IF_DEBUG, Task *task)
235 ASSERT_LOCK_HELD(&cap->lock);
236 ASSERT(task->cap == cap);
237 trace(TRACE_sched | DEBUG_sched,
238 "passing capability %d to %s %p",
239 cap->no, task->tso ? "bound task" : "worker",
241 ACQUIRE_LOCK(&task->lock);
242 task->wakeup = rtsTrue;
243 // the wakeup flag is needed because signalCondition() doesn't
244 // flag the condition if the thread is already runniing, but we want
246 signalCondition(&task->cond);
247 RELEASE_LOCK(&task->lock);
251 /* ----------------------------------------------------------------------------
252 * Function: releaseCapability(Capability*)
254 * Purpose: Letting go of a capability. Causes a
255 * 'returning worker' thread or a 'waiting worker'
256 * to wake up, in that order.
257 * ------------------------------------------------------------------------- */
259 #if defined(THREADED_RTS)
261 releaseCapability_ (Capability* cap)
265 task = cap->running_task;
267 ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task);
269 cap->running_task = NULL;
271 // Check to see whether a worker thread can be given
272 // the go-ahead to return the result of an external call..
273 if (cap->returning_tasks_hd != NULL) {
274 giveCapabilityToTask(cap,cap->returning_tasks_hd);
275 // The Task pops itself from the queue (see waitForReturnCapability())
279 // If the next thread on the run queue is a bound thread,
280 // give this Capability to the appropriate Task.
281 if (!emptyRunQueue(cap) && cap->run_queue_hd->bound) {
282 // Make sure we're not about to try to wake ourselves up
283 ASSERT(task != cap->run_queue_hd->bound);
284 task = cap->run_queue_hd->bound;
285 giveCapabilityToTask(cap,task);
289 if (!cap->spare_workers) {
290 // Create a worker thread if we don't have one. If the system
291 // is interrupted, we only create a worker task if there
292 // are threads that need to be completed. If the system is
293 // shutting down, we never create a new worker.
294 if (sched_state < SCHED_SHUTTING_DOWN || !emptyRunQueue(cap)) {
295 debugTrace(DEBUG_sched,
296 "starting new worker on capability %d", cap->no);
297 startWorkerTask(cap, workerStart);
302 // If we have an unbound thread on the run queue, or if there's
303 // anything else to do, give the Capability to a worker thread.
304 if (!emptyRunQueue(cap) || !emptyWakeupQueue(cap)
305 || !emptySparkPoolCap(cap) || globalWorkToDo()) {
306 if (cap->spare_workers) {
307 giveCapabilityToTask(cap,cap->spare_workers);
308 // The worker Task pops itself from the queue;
313 last_free_capability = cap;
314 trace(TRACE_sched | DEBUG_sched, "freeing capability %d", cap->no);
318 releaseCapability (Capability* cap USED_IF_THREADS)
320 ACQUIRE_LOCK(&cap->lock);
321 releaseCapability_(cap);
322 RELEASE_LOCK(&cap->lock);
326 releaseCapabilityAndQueueWorker (Capability* cap USED_IF_THREADS)
330 ACQUIRE_LOCK(&cap->lock);
332 task = cap->running_task;
334 // If the current task is a worker, save it on the spare_workers
335 // list of this Capability. A worker can mark itself as stopped,
336 // in which case it is not replaced on the spare_worker queue.
337 // This happens when the system is shutting down (see
338 // Schedule.c:workerStart()).
339 // Also, be careful to check that this task hasn't just exited
340 // Haskell to do a foreign call (task->suspended_tso).
341 if (!isBoundTask(task) && !task->stopped && !task->suspended_tso) {
342 task->next = cap->spare_workers;
343 cap->spare_workers = task;
345 // Bound tasks just float around attached to their TSOs.
347 releaseCapability_(cap);
349 RELEASE_LOCK(&cap->lock);
353 /* ----------------------------------------------------------------------------
354 * waitForReturnCapability( Task *task )
356 * Purpose: when an OS thread returns from an external call,
357 * it calls waitForReturnCapability() (via Schedule.resumeThread())
358 * to wait for permission to enter the RTS & communicate the
359 * result of the external call back to the Haskell thread that
362 * ------------------------------------------------------------------------- */
364 waitForReturnCapability (Capability **pCap, Task *task)
366 #if !defined(THREADED_RTS)
368 MainCapability.running_task = task;
369 task->cap = &MainCapability;
370 *pCap = &MainCapability;
373 Capability *cap = *pCap;
376 // Try last_free_capability first
377 cap = last_free_capability;
378 if (!cap->running_task) {
380 // otherwise, search for a free capability
381 for (i = 0; i < n_capabilities; i++) {
382 cap = &capabilities[i];
383 if (!cap->running_task) {
387 // Can't find a free one, use last_free_capability.
388 cap = last_free_capability;
391 // record the Capability as the one this Task is now assocated with.
395 ASSERT(task->cap == cap);
398 ACQUIRE_LOCK(&cap->lock);
400 debugTrace(DEBUG_sched, "returning; I want capability %d", cap->no);
402 if (!cap->running_task) {
403 // It's free; just grab it
404 cap->running_task = task;
405 RELEASE_LOCK(&cap->lock);
407 newReturningTask(cap,task);
408 RELEASE_LOCK(&cap->lock);
411 ACQUIRE_LOCK(&task->lock);
412 // task->lock held, cap->lock not held
413 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
415 task->wakeup = rtsFalse;
416 RELEASE_LOCK(&task->lock);
418 // now check whether we should wake up...
419 ACQUIRE_LOCK(&cap->lock);
420 if (cap->running_task == NULL) {
421 if (cap->returning_tasks_hd != task) {
422 giveCapabilityToTask(cap,cap->returning_tasks_hd);
423 RELEASE_LOCK(&cap->lock);
426 cap->running_task = task;
427 popReturningTask(cap);
428 RELEASE_LOCK(&cap->lock);
431 RELEASE_LOCK(&cap->lock);
436 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
438 trace(TRACE_sched | DEBUG_sched, "resuming capability %d", cap->no);
444 #if defined(THREADED_RTS)
445 /* ----------------------------------------------------------------------------
447 * ------------------------------------------------------------------------- */
450 yieldCapability (Capability** pCap, Task *task)
452 Capability *cap = *pCap;
454 // The fast path has no locking, if we don't enter this while loop
456 while ( cap->returning_tasks_hd != NULL || !anyWorkForMe(cap,task) ) {
457 debugTrace(DEBUG_sched, "giving up capability %d", cap->no);
459 // We must now release the capability and wait to be woken up
461 task->wakeup = rtsFalse;
462 releaseCapabilityAndQueueWorker(cap);
465 ACQUIRE_LOCK(&task->lock);
466 // task->lock held, cap->lock not held
467 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
469 task->wakeup = rtsFalse;
470 RELEASE_LOCK(&task->lock);
472 debugTrace(DEBUG_sched, "woken up on capability %d", cap->no);
474 ACQUIRE_LOCK(&cap->lock);
475 if (cap->running_task != NULL) {
476 debugTrace(DEBUG_sched,
477 "capability %d is owned by another task", cap->no);
478 RELEASE_LOCK(&cap->lock);
482 if (task->tso == NULL) {
483 ASSERT(cap->spare_workers != NULL);
484 // if we're not at the front of the queue, release it
485 // again. This is unlikely to happen.
486 if (cap->spare_workers != task) {
487 giveCapabilityToTask(cap,cap->spare_workers);
488 RELEASE_LOCK(&cap->lock);
491 cap->spare_workers = task->next;
494 cap->running_task = task;
495 RELEASE_LOCK(&cap->lock);
499 trace(TRACE_sched | DEBUG_sched, "resuming capability %d", cap->no);
500 ASSERT(cap->running_task == task);
505 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
510 /* ----------------------------------------------------------------------------
511 * Wake up a thread on a Capability.
513 * This is used when the current Task is running on a Capability and
514 * wishes to wake up a thread on a different Capability.
515 * ------------------------------------------------------------------------- */
518 wakeupThreadOnCapability (Capability *cap, StgTSO *tso)
520 ASSERT(tso->cap == cap);
521 ASSERT(tso->bound ? tso->bound->cap == cap : 1);
522 ASSERT_LOCK_HELD(&cap->lock);
526 if (cap->running_task == NULL) {
527 // nobody is running this Capability, we can add our thread
528 // directly onto the run queue and start up a Task to run it.
529 appendToRunQueue(cap,tso);
532 cap->running_task = myTask(); // precond for releaseCapability_()
533 trace(TRACE_sched, "resuming capability %d", cap->no);
534 releaseCapability_(cap);
536 appendToWakeupQueue(cap,tso);
537 // someone is running on this Capability, so it cannot be
538 // freed without first checking the wakeup queue (see
539 // releaseCapability_).
544 wakeupThreadOnCapability_lock (Capability *cap, StgTSO *tso)
546 ACQUIRE_LOCK(&cap->lock);
547 migrateThreadToCapability (cap, tso);
548 RELEASE_LOCK(&cap->lock);
552 migrateThreadToCapability (Capability *cap, StgTSO *tso)
554 // ASSUMES: cap->lock is held (asserted in wakeupThreadOnCapability)
556 ASSERT(tso->bound->cap == tso->cap);
557 tso->bound->cap = cap;
560 wakeupThreadOnCapability(cap,tso);
564 migrateThreadToCapability_lock (Capability *cap, StgTSO *tso)
566 ACQUIRE_LOCK(&cap->lock);
567 migrateThreadToCapability (cap, tso);
568 RELEASE_LOCK(&cap->lock);
571 /* ----------------------------------------------------------------------------
574 * Used to indicate that the interrupted flag is now set, or some
575 * other global condition that might require waking up a Task on each
577 * ------------------------------------------------------------------------- */
580 prodCapabilities(rtsBool all)
586 for (i=0; i < n_capabilities; i++) {
587 cap = &capabilities[i];
588 ACQUIRE_LOCK(&cap->lock);
589 if (!cap->running_task) {
590 if (cap->spare_workers) {
591 trace(TRACE_sched, "resuming capability %d", cap->no);
592 task = cap->spare_workers;
593 ASSERT(!task->stopped);
594 giveCapabilityToTask(cap,task);
596 RELEASE_LOCK(&cap->lock);
601 RELEASE_LOCK(&cap->lock);
607 prodAllCapabilities (void)
609 prodCapabilities(rtsTrue);
612 /* ----------------------------------------------------------------------------
615 * Like prodAllCapabilities, but we only require a single Task to wake
616 * up in order to service some global event, such as checking for
617 * deadlock after some idle time has passed.
618 * ------------------------------------------------------------------------- */
621 prodOneCapability (void)
623 prodCapabilities(rtsFalse);
626 /* ----------------------------------------------------------------------------
629 * At shutdown time, we want to let everything exit as cleanly as
630 * possible. For each capability, we let its run queue drain, and
631 * allow the workers to stop.
633 * This function should be called when interrupted and
634 * shutting_down_scheduler = rtsTrue, thus any worker that wakes up
635 * will exit the scheduler and call taskStop(), and any bound thread
636 * that wakes up will return to its caller. Runnable threads are
639 * ------------------------------------------------------------------------- */
642 shutdownCapability (Capability *cap, Task *task, rtsBool safe)
646 ASSERT(sched_state == SCHED_SHUTTING_DOWN);
650 // Loop indefinitely until all the workers have exited and there
651 // are no Haskell threads left. We used to bail out after 50
652 // iterations of this loop, but that occasionally left a worker
653 // running which caused problems later (the closeMutex() below
654 // isn't safe, for one thing).
656 for (i = 0; /* i < 50 */; i++) {
657 debugTrace(DEBUG_sched,
658 "shutting down capability %d, attempt %d", cap->no, i);
659 ACQUIRE_LOCK(&cap->lock);
660 if (cap->running_task) {
661 RELEASE_LOCK(&cap->lock);
662 debugTrace(DEBUG_sched, "not owner, yielding");
666 cap->running_task = task;
668 if (cap->spare_workers) {
669 // Look for workers that have died without removing
670 // themselves from the list; this could happen if the OS
671 // summarily killed the thread, for example. This
672 // actually happens on Windows when the system is
673 // terminating the program, and the RTS is running in a
677 for (t = cap->spare_workers; t != NULL; t = t->next) {
678 if (!osThreadIsAlive(t->id)) {
679 debugTrace(DEBUG_sched,
680 "worker thread %p has died unexpectedly", (void *)t->id);
682 cap->spare_workers = t->next;
684 prev->next = t->next;
691 if (!emptyRunQueue(cap) || cap->spare_workers) {
692 debugTrace(DEBUG_sched,
693 "runnable threads or workers still alive, yielding");
694 releaseCapability_(cap); // this will wake up a worker
695 RELEASE_LOCK(&cap->lock);
700 // If "safe", then busy-wait for any threads currently doing
701 // foreign calls. If we're about to unload this DLL, for
702 // example, we need to be sure that there are no OS threads
703 // that will try to return to code that has been unloaded.
704 // We can be a bit more relaxed when this is a standalone
705 // program that is about to terminate, and let safe=false.
706 if (cap->suspended_ccalling_tasks && safe) {
707 debugTrace(DEBUG_sched,
708 "thread(s) are involved in foreign calls, yielding");
709 cap->running_task = NULL;
710 RELEASE_LOCK(&cap->lock);
715 debugTrace(DEBUG_sched, "capability %d is stopped.", cap->no);
717 RELEASE_LOCK(&cap->lock);
720 // we now have the Capability, its run queue and spare workers
721 // list are both empty.
723 // ToDo: we can't drop this mutex, because there might still be
724 // threads performing foreign calls that will eventually try to
725 // return via resumeThread() and attempt to grab cap->lock.
726 // closeMutex(&cap->lock);
729 /* ----------------------------------------------------------------------------
732 * Attempt to gain control of a Capability if it is free.
734 * ------------------------------------------------------------------------- */
737 tryGrabCapability (Capability *cap, Task *task)
739 if (cap->running_task != NULL) return rtsFalse;
740 ACQUIRE_LOCK(&cap->lock);
741 if (cap->running_task != NULL) {
742 RELEASE_LOCK(&cap->lock);
746 cap->running_task = task;
747 RELEASE_LOCK(&cap->lock);
752 #endif /* THREADED_RTS */
755 freeCapability (Capability *cap) {
756 stgFree(cap->mut_lists);
757 #if defined(THREADED_RTS) || defined(PARALLEL_HASKELL)
758 freeSparkPool(&cap->r.rSparks);