1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2003-2005
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"
23 #include "OSThreads.h"
24 #include "Capability.h"
28 Capability MainCapability; // for non-SMP, we have one global capability
32 Capability *capabilities = NULL;
34 // Holds the Capability which last became free. This is used so that
35 // an in-call has a chance of quickly finding a free Capability.
36 // Maintaining a global free list of Capabilities would require global
37 // locking, so we don't do that.
38 Capability *last_free_capability;
41 #define UNUSED_IF_NOT_SMP
43 #define UNUSED_IF_NOT_SMP STG_UNUSED
46 #ifdef RTS_USER_SIGNALS
47 #define UNUSED_IF_NOT_THREADS
49 #define UNUSED_IF_NOT_THREADS STG_UNUSED
56 return blackholes_need_checking
58 #if defined(RTS_USER_SIGNALS)
64 #if defined(THREADED_RTS)
66 anyWorkForMe( Capability *cap, Task *task )
68 // If the run queue is not empty, then we only wake up the guy who
69 // can run the thread at the head, even if there is some other
70 // reason for this task to run (eg. interrupted=rtsTrue).
71 if (!emptyRunQueue(cap)) {
72 if (cap->run_queue_hd->bound == NULL) {
73 return (task->tso == NULL);
75 return (cap->run_queue_hd->bound == task);
78 return globalWorkToDo();
82 /* -----------------------------------------------------------------------------
83 * Manage the returning_tasks lists.
85 * These functions require cap->lock
86 * -------------------------------------------------------------------------- */
88 #if defined(THREADED_RTS)
90 newReturningTask (Capability *cap, Task *task)
92 ASSERT_LOCK_HELD(&cap->lock);
93 ASSERT(task->return_link == NULL);
94 if (cap->returning_tasks_hd) {
95 ASSERT(cap->returning_tasks_tl->return_link == NULL);
96 cap->returning_tasks_tl->return_link = task;
98 cap->returning_tasks_hd = task;
100 cap->returning_tasks_tl = task;
104 popReturningTask (Capability *cap)
106 ASSERT_LOCK_HELD(&cap->lock);
108 task = cap->returning_tasks_hd;
110 cap->returning_tasks_hd = task->return_link;
111 if (!cap->returning_tasks_hd) {
112 cap->returning_tasks_tl = NULL;
114 task->return_link = NULL;
119 /* ----------------------------------------------------------------------------
122 * The Capability is initially marked not free.
123 * ------------------------------------------------------------------------- */
126 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;
143 cap->f.stgGCEnter1 = (F_)__stg_gc_enter_1;
144 cap->f.stgGCFun = (F_)__stg_gc_fun;
146 cap->mut_lists = stgMallocBytes(sizeof(bdescr *) *
147 RtsFlags.GcFlags.generations,
151 /* ---------------------------------------------------------------------------
152 * Function: initCapabilities()
154 * Purpose: set up the Capability handling. For the SMP build,
155 * we keep a table of them, the size of which is
156 * controlled by the user via the RTS flag -N.
158 * ------------------------------------------------------------------------- */
160 initCapabilities( void )
165 n_capabilities = n = RtsFlags.ParFlags.nNodes;
166 capabilities = stgMallocBytes(n * sizeof(Capability), "initCapabilities");
168 for (i = 0; i < n; i++) {
169 initCapability(&capabilities[i], i);
172 IF_DEBUG(scheduler, sched_belch("allocated %d capabilities", n));
175 capabilities = &MainCapability;
176 initCapability(&MainCapability, 0);
179 // There are no free capabilities to begin with. We will start
180 // a worker Task to each Capability, which will quickly put the
181 // Capability on the free list when it finds nothing to do.
182 last_free_capability = &capabilities[0];
185 /* ----------------------------------------------------------------------------
186 * Give a Capability to a Task. The task must currently be sleeping
187 * on its condition variable.
189 * Requires cap->lock (modifies cap->running_task).
191 * When migrating a Task, the migrater must take task->lock before
192 * modifying task->cap, to synchronise with the waking up Task.
193 * Additionally, the migrater should own the Capability (when
194 * migrating the run queue), or cap->lock (when migrating
195 * returning_workers).
197 * ------------------------------------------------------------------------- */
199 #if defined(THREADED_RTS)
201 giveCapabilityToTask (Capability *cap, Task *task)
203 ASSERT_LOCK_HELD(&cap->lock);
204 ASSERT(task->cap == cap);
205 // We are not modifying task->cap, so we do not need to take task->lock.
207 sched_belch("passing capability %d to %s %p",
208 cap->no, task->tso ? "bound task" : "worker",
210 ACQUIRE_LOCK(&task->lock);
211 task->wakeup = rtsTrue;
212 // the wakeup flag is needed because signalCondition() doesn't
213 // flag the condition if the thread is already runniing, but we want
215 signalCondition(&task->cond);
216 RELEASE_LOCK(&task->lock);
220 /* ----------------------------------------------------------------------------
221 * Function: releaseCapability(Capability*)
223 * Purpose: Letting go of a capability. Causes a
224 * 'returning worker' thread or a 'waiting worker'
225 * to wake up, in that order.
226 * ------------------------------------------------------------------------- */
228 #if defined(THREADED_RTS)
230 releaseCapability_ (Capability* cap)
234 task = cap->running_task;
236 ASSERT_CAPABILITY_INVARIANTS(cap,task);
238 cap->running_task = NULL;
240 // Check to see whether a worker thread can be given
241 // the go-ahead to return the result of an external call..
242 if (cap->returning_tasks_hd != NULL) {
243 giveCapabilityToTask(cap,cap->returning_tasks_hd);
244 // The Task pops itself from the queue (see waitForReturnCapability())
248 // If the next thread on the run queue is a bound thread,
249 // give this Capability to the appropriate Task.
250 if (!emptyRunQueue(cap) && cap->run_queue_hd->bound) {
251 // Make sure we're not about to try to wake ourselves up
252 ASSERT(task != cap->run_queue_hd->bound);
253 task = cap->run_queue_hd->bound;
254 giveCapabilityToTask(cap,task);
258 // If we have an unbound thread on the run queue, or if there's
259 // anything else to do, give the Capability to a worker thread.
260 if (!emptyRunQueue(cap) || globalWorkToDo()) {
261 if (cap->spare_workers) {
262 giveCapabilityToTask(cap,cap->spare_workers);
263 // The worker Task pops itself from the queue;
267 // Create a worker thread if we don't have one. If the system
268 // is interrupted, we only create a worker task if there
269 // are threads that need to be completed. If the system is
270 // shutting down, we never create a new worker.
271 if (!shutting_down_scheduler) {
273 sched_belch("starting new worker on capability %d", cap->no));
274 startWorkerTask(cap, workerStart);
279 last_free_capability = cap;
280 IF_DEBUG(scheduler, sched_belch("freeing capability %d", cap->no));
284 releaseCapability (Capability* cap UNUSED_IF_NOT_THREADS)
286 ACQUIRE_LOCK(&cap->lock);
287 releaseCapability_(cap);
288 RELEASE_LOCK(&cap->lock);
292 releaseCapabilityAndQueueWorker (Capability* cap UNUSED_IF_NOT_THREADS)
296 ACQUIRE_LOCK(&cap->lock);
298 task = cap->running_task;
300 // If the current task is a worker, save it on the spare_workers
301 // list of this Capability. A worker can mark itself as stopped,
302 // in which case it is not replaced on the spare_worker queue.
303 // This happens when the system is shutting down (see
304 // Schedule.c:workerStart()).
305 // Also, be careful to check that this task hasn't just exited
306 // Haskell to do a foreign call (task->suspended_tso).
307 if (!isBoundTask(task) && !task->stopped && !task->suspended_tso) {
308 task->next = cap->spare_workers;
309 cap->spare_workers = task;
311 // Bound tasks just float around attached to their TSOs.
313 releaseCapability_(cap);
315 RELEASE_LOCK(&cap->lock);
319 /* ----------------------------------------------------------------------------
320 * waitForReturnCapability( Task *task )
322 * Purpose: when an OS thread returns from an external call,
323 * it calls waitForReturnCapability() (via Schedule.resumeThread())
324 * to wait for permission to enter the RTS & communicate the
325 * result of the external call back to the Haskell thread that
328 * ------------------------------------------------------------------------- */
330 waitForReturnCapability (Capability **pCap,
331 Task *task UNUSED_IF_NOT_THREADS)
333 #if !defined(THREADED_RTS)
335 MainCapability.running_task = task;
336 task->cap = &MainCapability;
337 *pCap = &MainCapability;
340 Capability *cap = *pCap;
343 // Try last_free_capability first
344 cap = last_free_capability;
345 if (!cap->running_task) {
347 // otherwise, search for a free capability
348 for (i = 0; i < n_capabilities; i++) {
349 cap = &capabilities[i];
350 if (!cap->running_task) {
354 // Can't find a free one, use last_free_capability.
355 cap = last_free_capability;
358 // record the Capability as the one this Task is now assocated with.
362 ASSERT(task->cap == cap);
365 ACQUIRE_LOCK(&cap->lock);
368 sched_belch("returning; I want capability %d", cap->no));
370 if (!cap->running_task) {
371 // It's free; just grab it
372 cap->running_task = task;
373 RELEASE_LOCK(&cap->lock);
375 newReturningTask(cap,task);
376 RELEASE_LOCK(&cap->lock);
379 ACQUIRE_LOCK(&task->lock);
380 // task->lock held, cap->lock not held
381 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
383 task->wakeup = rtsFalse;
384 RELEASE_LOCK(&task->lock);
386 // now check whether we should wake up...
387 ACQUIRE_LOCK(&cap->lock);
388 if (cap->running_task == NULL) {
389 if (cap->returning_tasks_hd != task) {
390 giveCapabilityToTask(cap,cap->returning_tasks_hd);
391 RELEASE_LOCK(&cap->lock);
394 cap->running_task = task;
395 popReturningTask(cap);
396 RELEASE_LOCK(&cap->lock);
399 RELEASE_LOCK(&cap->lock);
404 ASSERT_CAPABILITY_INVARIANTS(cap,task);
407 sched_belch("returning; got capability %d", cap->no));
413 #if defined(THREADED_RTS)
414 /* ----------------------------------------------------------------------------
416 * ------------------------------------------------------------------------- */
419 yieldCapability (Capability** pCap, Task *task)
421 Capability *cap = *pCap;
423 // The fast path; no locking
424 if ( cap->returning_tasks_hd == NULL && anyWorkForMe(cap,task) )
427 while ( cap->returning_tasks_hd != NULL || !anyWorkForMe(cap,task) ) {
428 IF_DEBUG(scheduler, sched_belch("giving up capability %d", cap->no));
430 // We must now release the capability and wait to be woken up
432 releaseCapabilityAndQueueWorker(cap);
435 ACQUIRE_LOCK(&task->lock);
436 // task->lock held, cap->lock not held
437 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
439 task->wakeup = rtsFalse;
440 RELEASE_LOCK(&task->lock);
442 IF_DEBUG(scheduler, sched_belch("woken up on capability %d", cap->no));
443 ACQUIRE_LOCK(&cap->lock);
444 if (cap->running_task != NULL) {
445 RELEASE_LOCK(&cap->lock);
449 if (task->tso == NULL) {
450 ASSERT(cap->spare_workers != NULL);
451 // if we're not at the front of the queue, release it
452 // again. This is unlikely to happen.
453 if (cap->spare_workers != task) {
454 giveCapabilityToTask(cap,cap->spare_workers);
455 RELEASE_LOCK(&cap->lock);
458 cap->spare_workers = task->next;
461 cap->running_task = task;
462 RELEASE_LOCK(&cap->lock);
466 IF_DEBUG(scheduler, sched_belch("got capability %d", cap->no));
467 ASSERT(cap->running_task == task);
472 ASSERT_CAPABILITY_INVARIANTS(cap,task);
477 /* ----------------------------------------------------------------------------
480 * Used to indicate that the interrupted flag is now set, or some
481 * other global condition that might require waking up a Task on each
483 * ------------------------------------------------------------------------- */
486 prodCapabilities(rtsBool all)
492 for (i=0; i < n_capabilities; i++) {
493 cap = &capabilities[i];
494 ACQUIRE_LOCK(&cap->lock);
495 if (!cap->running_task) {
496 if (cap->spare_workers) {
497 task = cap->spare_workers;
498 ASSERT(!task->stopped);
499 giveCapabilityToTask(cap,task);
501 RELEASE_LOCK(&cap->lock);
506 RELEASE_LOCK(&cap->lock);
511 prodAllCapabilities (void)
513 prodCapabilities(rtsTrue);
516 /* ----------------------------------------------------------------------------
519 * Like prodAllCapabilities, but we only require a single Task to wake
520 * up in order to service some global event, such as checking for
521 * deadlock after some idle time has passed.
522 * ------------------------------------------------------------------------- */
525 prodOneCapability (void)
527 prodCapabilities(rtsFalse);
530 /* ----------------------------------------------------------------------------
533 * At shutdown time, we want to let everything exit as cleanly as
534 * possible. For each capability, we let its run queue drain, and
535 * allow the workers to stop.
537 * This function should be called when interrupted and
538 * shutting_down_scheduler = rtsTrue, thus any worker that wakes up
539 * will exit the scheduler and call taskStop(), and any bound thread
540 * that wakes up will return to its caller. Runnable threads are
543 * ------------------------------------------------------------------------- */
546 shutdownCapability (Capability *cap, Task *task)
550 ASSERT(interrupted && shutting_down_scheduler);
554 for (i = 0; i < 50; i++) {
555 IF_DEBUG(scheduler, sched_belch("shutting down capability %d, attempt %d", cap->no, i));
556 ACQUIRE_LOCK(&cap->lock);
557 if (cap->running_task) {
558 RELEASE_LOCK(&cap->lock);
559 IF_DEBUG(scheduler, sched_belch("not owner, yielding"));
563 cap->running_task = task;
564 if (!emptyRunQueue(cap) || cap->spare_workers) {
565 IF_DEBUG(scheduler, sched_belch("runnable threads or workers still alive, yielding"));
566 releaseCapability_(cap); // this will wake up a worker
567 RELEASE_LOCK(&cap->lock);
571 IF_DEBUG(scheduler, sched_belch("capability %d is stopped.", cap->no));
572 RELEASE_LOCK(&cap->lock);
575 // we now have the Capability, its run queue and spare workers
576 // list are both empty.
579 /* ----------------------------------------------------------------------------
582 * Attempt to gain control of a Capability if it is free.
584 * ------------------------------------------------------------------------- */
587 tryGrabCapability (Capability *cap, Task *task)
589 if (cap->running_task != NULL) return rtsFalse;
590 ACQUIRE_LOCK(&cap->lock);
591 if (cap->running_task != NULL) {
592 RELEASE_LOCK(&cap->lock);
596 cap->running_task = task;
597 RELEASE_LOCK(&cap->lock);
602 #endif /* THREADED_RTS */