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 SMP build will there be multiple capabilities, for
14 * the threaded RTS and other non-threaded builds, there is only
15 * one global capability, namely MainCapability.
17 * --------------------------------------------------------------------------*/
19 #include "PosixSource.h"
24 #include "OSThreads.h"
25 #include "Capability.h"
30 Capability MainCapability; // for non-SMP, we have one global capability
34 Capability *capabilities = NULL;
36 // Holds the Capability which last became free. This is used so that
37 // an in-call has a chance of quickly finding a free Capability.
38 // Maintaining a global free list of Capabilities would require global
39 // locking, so we don't do that.
40 Capability *last_free_capability;
42 #if defined(THREADED_RTS)
46 return blackholes_need_checking
52 #if defined(THREADED_RTS)
54 anyWorkForMe( Capability *cap, Task *task )
56 // If the run queue is not empty, then we only wake up the guy who
57 // can run the thread at the head, even if there is some other
58 // reason for this task to run (eg. interrupted=rtsTrue).
59 if (!emptyRunQueue(cap)) {
60 if (cap->run_queue_hd->bound == NULL) {
61 return (task->tso == NULL);
63 return (cap->run_queue_hd->bound == task);
65 } else if (task->tso == NULL && !emptySparkPoolCap(cap)) {
68 return globalWorkToDo();
72 /* -----------------------------------------------------------------------------
73 * Manage the returning_tasks lists.
75 * These functions require cap->lock
76 * -------------------------------------------------------------------------- */
78 #if defined(THREADED_RTS)
80 newReturningTask (Capability *cap, Task *task)
82 ASSERT_LOCK_HELD(&cap->lock);
83 ASSERT(task->return_link == NULL);
84 if (cap->returning_tasks_hd) {
85 ASSERT(cap->returning_tasks_tl->return_link == NULL);
86 cap->returning_tasks_tl->return_link = task;
88 cap->returning_tasks_hd = task;
90 cap->returning_tasks_tl = task;
94 popReturningTask (Capability *cap)
96 ASSERT_LOCK_HELD(&cap->lock);
98 task = cap->returning_tasks_hd;
100 cap->returning_tasks_hd = task->return_link;
101 if (!cap->returning_tasks_hd) {
102 cap->returning_tasks_tl = NULL;
104 task->return_link = NULL;
109 /* ----------------------------------------------------------------------------
112 * The Capability is initially marked not free.
113 * ------------------------------------------------------------------------- */
116 initCapability( Capability *cap, nat i )
121 cap->in_haskell = rtsFalse;
123 cap->run_queue_hd = END_TSO_QUEUE;
124 cap->run_queue_tl = END_TSO_QUEUE;
126 #if defined(THREADED_RTS)
127 initMutex(&cap->lock);
128 cap->running_task = NULL; // indicates cap is free
129 cap->spare_workers = NULL;
130 cap->suspended_ccalling_tasks = NULL;
131 cap->returning_tasks_hd = NULL;
132 cap->returning_tasks_tl = NULL;
135 cap->f.stgGCEnter1 = (F_)__stg_gc_enter_1;
136 cap->f.stgGCFun = (F_)__stg_gc_fun;
138 cap->mut_lists = stgMallocBytes(sizeof(bdescr *) *
139 RtsFlags.GcFlags.generations,
142 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
143 cap->mut_lists[g] = NULL;
146 cap->free_tvar_wait_queues = END_STM_WAIT_QUEUE;
147 cap->free_trec_chunks = END_STM_CHUNK_LIST;
148 cap->free_trec_headers = NO_TREC;
149 cap->transaction_tokens = 0;
152 /* ---------------------------------------------------------------------------
153 * Function: initCapabilities()
155 * Purpose: set up the Capability handling. For the SMP build,
156 * we keep a table of them, the size of which is
157 * controlled by the user via the RTS flag -N.
159 * ------------------------------------------------------------------------- */
161 initCapabilities( void )
167 // We can't support multiple CPUs if BaseReg is not a register
168 if (RtsFlags.ParFlags.nNodes > 1) {
169 errorBelch("warning: multiple CPUs not supported in this build, reverting to 1");
170 RtsFlags.ParFlags.nNodes = 1;
174 n_capabilities = n = RtsFlags.ParFlags.nNodes;
175 capabilities = stgMallocBytes(n * sizeof(Capability), "initCapabilities");
177 for (i = 0; i < n; i++) {
178 initCapability(&capabilities[i], i);
181 IF_DEBUG(scheduler, sched_belch("allocated %d capabilities", n));
184 capabilities = &MainCapability;
185 initCapability(&MainCapability, 0);
188 // There are no free capabilities to begin with. We will start
189 // a worker Task to each Capability, which will quickly put the
190 // Capability on the free list when it finds nothing to do.
191 last_free_capability = &capabilities[0];
194 /* ----------------------------------------------------------------------------
195 * Give a Capability to a Task. The task must currently be sleeping
196 * on its condition variable.
198 * Requires cap->lock (modifies cap->running_task).
200 * When migrating a Task, the migrater must take task->lock before
201 * modifying task->cap, to synchronise with the waking up Task.
202 * Additionally, the migrater should own the Capability (when
203 * migrating the run queue), or cap->lock (when migrating
204 * returning_workers).
206 * ------------------------------------------------------------------------- */
208 #if defined(THREADED_RTS)
210 giveCapabilityToTask (Capability *cap USED_IF_DEBUG, Task *task)
212 ASSERT_LOCK_HELD(&cap->lock);
213 ASSERT(task->cap == cap);
215 sched_belch("passing capability %d to %s %p",
216 cap->no, task->tso ? "bound task" : "worker",
218 ACQUIRE_LOCK(&task->lock);
219 task->wakeup = rtsTrue;
220 // the wakeup flag is needed because signalCondition() doesn't
221 // flag the condition if the thread is already runniing, but we want
223 signalCondition(&task->cond);
224 RELEASE_LOCK(&task->lock);
228 /* ----------------------------------------------------------------------------
229 * Function: releaseCapability(Capability*)
231 * Purpose: Letting go of a capability. Causes a
232 * 'returning worker' thread or a 'waiting worker'
233 * to wake up, in that order.
234 * ------------------------------------------------------------------------- */
236 #if defined(THREADED_RTS)
238 releaseCapability_ (Capability* cap)
242 task = cap->running_task;
244 ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task);
246 cap->running_task = NULL;
248 // Check to see whether a worker thread can be given
249 // the go-ahead to return the result of an external call..
250 if (cap->returning_tasks_hd != NULL) {
251 giveCapabilityToTask(cap,cap->returning_tasks_hd);
252 // The Task pops itself from the queue (see waitForReturnCapability())
256 // If the next thread on the run queue is a bound thread,
257 // give this Capability to the appropriate Task.
258 if (!emptyRunQueue(cap) && cap->run_queue_hd->bound) {
259 // Make sure we're not about to try to wake ourselves up
260 ASSERT(task != cap->run_queue_hd->bound);
261 task = cap->run_queue_hd->bound;
262 giveCapabilityToTask(cap,task);
266 // If we have an unbound thread on the run queue, or if there's
267 // anything else to do, give the Capability to a worker thread.
268 if (!emptyRunQueue(cap) || !emptySparkPoolCap(cap) || globalWorkToDo()) {
269 if (cap->spare_workers) {
270 giveCapabilityToTask(cap,cap->spare_workers);
271 // The worker Task pops itself from the queue;
275 // Create a worker thread if we don't have one. If the system
276 // is interrupted, we only create a worker task if there
277 // are threads that need to be completed. If the system is
278 // shutting down, we never create a new worker.
279 if (!shutting_down_scheduler) {
281 sched_belch("starting new worker on capability %d", cap->no));
282 startWorkerTask(cap, workerStart);
287 last_free_capability = cap;
288 IF_DEBUG(scheduler, sched_belch("freeing capability %d", cap->no));
292 releaseCapability (Capability* cap USED_IF_THREADS)
294 ACQUIRE_LOCK(&cap->lock);
295 releaseCapability_(cap);
296 RELEASE_LOCK(&cap->lock);
300 releaseCapabilityAndQueueWorker (Capability* cap USED_IF_THREADS)
304 ACQUIRE_LOCK(&cap->lock);
306 task = cap->running_task;
308 // If the current task is a worker, save it on the spare_workers
309 // list of this Capability. A worker can mark itself as stopped,
310 // in which case it is not replaced on the spare_worker queue.
311 // This happens when the system is shutting down (see
312 // Schedule.c:workerStart()).
313 // Also, be careful to check that this task hasn't just exited
314 // Haskell to do a foreign call (task->suspended_tso).
315 if (!isBoundTask(task) && !task->stopped && !task->suspended_tso) {
316 task->next = cap->spare_workers;
317 cap->spare_workers = task;
319 // Bound tasks just float around attached to their TSOs.
321 releaseCapability_(cap);
323 RELEASE_LOCK(&cap->lock);
327 /* ----------------------------------------------------------------------------
328 * waitForReturnCapability( Task *task )
330 * Purpose: when an OS thread returns from an external call,
331 * it calls waitForReturnCapability() (via Schedule.resumeThread())
332 * to wait for permission to enter the RTS & communicate the
333 * result of the external call back to the Haskell thread that
336 * ------------------------------------------------------------------------- */
338 waitForReturnCapability (Capability **pCap, Task *task)
340 #if !defined(THREADED_RTS)
342 MainCapability.running_task = task;
343 task->cap = &MainCapability;
344 *pCap = &MainCapability;
347 Capability *cap = *pCap;
350 // Try last_free_capability first
351 cap = last_free_capability;
352 if (!cap->running_task) {
354 // otherwise, search for a free capability
355 for (i = 0; i < n_capabilities; i++) {
356 cap = &capabilities[i];
357 if (!cap->running_task) {
361 // Can't find a free one, use last_free_capability.
362 cap = last_free_capability;
365 // record the Capability as the one this Task is now assocated with.
369 ASSERT(task->cap == cap);
372 ACQUIRE_LOCK(&cap->lock);
375 sched_belch("returning; I want capability %d", cap->no));
377 if (!cap->running_task) {
378 // It's free; just grab it
379 cap->running_task = task;
380 RELEASE_LOCK(&cap->lock);
382 newReturningTask(cap,task);
383 RELEASE_LOCK(&cap->lock);
386 ACQUIRE_LOCK(&task->lock);
387 // task->lock held, cap->lock not held
388 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
390 task->wakeup = rtsFalse;
391 RELEASE_LOCK(&task->lock);
393 // now check whether we should wake up...
394 ACQUIRE_LOCK(&cap->lock);
395 if (cap->running_task == NULL) {
396 if (cap->returning_tasks_hd != task) {
397 giveCapabilityToTask(cap,cap->returning_tasks_hd);
398 RELEASE_LOCK(&cap->lock);
401 cap->running_task = task;
402 popReturningTask(cap);
403 RELEASE_LOCK(&cap->lock);
406 RELEASE_LOCK(&cap->lock);
411 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
414 sched_belch("returning; got capability %d", cap->no));
420 #if defined(THREADED_RTS)
421 /* ----------------------------------------------------------------------------
423 * ------------------------------------------------------------------------- */
426 yieldCapability (Capability** pCap, Task *task)
428 Capability *cap = *pCap;
430 // The fast path has no locking, if we don't enter this while loop
432 while ( cap->returning_tasks_hd != NULL || !anyWorkForMe(cap,task) ) {
433 IF_DEBUG(scheduler, sched_belch("giving up capability %d", cap->no));
435 // We must now release the capability and wait to be woken up
437 task->wakeup = rtsFalse;
438 releaseCapabilityAndQueueWorker(cap);
441 ACQUIRE_LOCK(&task->lock);
442 // task->lock held, cap->lock not held
443 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
445 task->wakeup = rtsFalse;
446 RELEASE_LOCK(&task->lock);
448 IF_DEBUG(scheduler, sched_belch("woken up on capability %d", cap->no));
449 ACQUIRE_LOCK(&cap->lock);
450 if (cap->running_task != NULL) {
451 IF_DEBUG(scheduler, sched_belch("capability %d is owned by another task", cap->no));
452 RELEASE_LOCK(&cap->lock);
456 if (task->tso == NULL) {
457 ASSERT(cap->spare_workers != NULL);
458 // if we're not at the front of the queue, release it
459 // again. This is unlikely to happen.
460 if (cap->spare_workers != task) {
461 giveCapabilityToTask(cap,cap->spare_workers);
462 RELEASE_LOCK(&cap->lock);
465 cap->spare_workers = task->next;
468 cap->running_task = task;
469 RELEASE_LOCK(&cap->lock);
473 IF_DEBUG(scheduler, sched_belch("got capability %d", cap->no));
474 ASSERT(cap->running_task == task);
479 ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task);
484 /* ----------------------------------------------------------------------------
487 * Used to indicate that the interrupted flag is now set, or some
488 * other global condition that might require waking up a Task on each
490 * ------------------------------------------------------------------------- */
493 prodCapabilities(rtsBool all)
499 for (i=0; i < n_capabilities; i++) {
500 cap = &capabilities[i];
501 ACQUIRE_LOCK(&cap->lock);
502 if (!cap->running_task) {
503 if (cap->spare_workers) {
504 task = cap->spare_workers;
505 ASSERT(!task->stopped);
506 giveCapabilityToTask(cap,task);
508 RELEASE_LOCK(&cap->lock);
513 RELEASE_LOCK(&cap->lock);
518 prodAllCapabilities (void)
520 prodCapabilities(rtsTrue);
523 /* ----------------------------------------------------------------------------
526 * Like prodAllCapabilities, but we only require a single Task to wake
527 * up in order to service some global event, such as checking for
528 * deadlock after some idle time has passed.
529 * ------------------------------------------------------------------------- */
532 prodOneCapability (void)
534 prodCapabilities(rtsFalse);
537 /* ----------------------------------------------------------------------------
540 * At shutdown time, we want to let everything exit as cleanly as
541 * possible. For each capability, we let its run queue drain, and
542 * allow the workers to stop.
544 * This function should be called when interrupted and
545 * shutting_down_scheduler = rtsTrue, thus any worker that wakes up
546 * will exit the scheduler and call taskStop(), and any bound thread
547 * that wakes up will return to its caller. Runnable threads are
550 * ------------------------------------------------------------------------- */
553 shutdownCapability (Capability *cap, Task *task)
557 ASSERT(interrupted && shutting_down_scheduler);
561 for (i = 0; i < 50; i++) {
562 IF_DEBUG(scheduler, sched_belch("shutting down capability %d, attempt %d", cap->no, i));
563 ACQUIRE_LOCK(&cap->lock);
564 if (cap->running_task) {
565 RELEASE_LOCK(&cap->lock);
566 IF_DEBUG(scheduler, sched_belch("not owner, yielding"));
570 cap->running_task = task;
571 if (!emptyRunQueue(cap) || cap->spare_workers) {
572 IF_DEBUG(scheduler, sched_belch("runnable threads or workers still alive, yielding"));
573 releaseCapability_(cap); // this will wake up a worker
574 RELEASE_LOCK(&cap->lock);
578 IF_DEBUG(scheduler, sched_belch("capability %d is stopped.", cap->no));
579 RELEASE_LOCK(&cap->lock);
582 // we now have the Capability, its run queue and spare workers
583 // list are both empty.
586 /* ----------------------------------------------------------------------------
589 * Attempt to gain control of a Capability if it is free.
591 * ------------------------------------------------------------------------- */
594 tryGrabCapability (Capability *cap, Task *task)
596 if (cap->running_task != NULL) return rtsFalse;
597 ACQUIRE_LOCK(&cap->lock);
598 if (cap->running_task != NULL) {
599 RELEASE_LOCK(&cap->lock);
603 cap->running_task = task;
604 RELEASE_LOCK(&cap->lock);
609 #endif /* THREADED_RTS */