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;
147 /* ---------------------------------------------------------------------------
148 * Function: initCapabilities()
150 * Purpose: set up the Capability handling. For the SMP build,
151 * we keep a table of them, the size of which is
152 * controlled by the user via the RTS flag -N.
154 * ------------------------------------------------------------------------- */
156 initCapabilities( void )
161 n_capabilities = n = RtsFlags.ParFlags.nNodes;
162 capabilities = stgMallocBytes(n * sizeof(Capability), "initCapabilities");
164 for (i = 0; i < n; i++) {
165 initCapability(&capabilities[i], i);
168 IF_DEBUG(scheduler, sched_belch("allocated %d capabilities", n));
171 capabilities = &MainCapability;
172 initCapability(&MainCapability, 0);
175 // There are no free capabilities to begin with. We will start
176 // a worker Task to each Capability, which will quickly put the
177 // Capability on the free list when it finds nothing to do.
178 last_free_capability = &capabilities[0];
181 /* ----------------------------------------------------------------------------
182 * Give a Capability to a Task. The task must currently be sleeping
183 * on its condition variable.
185 * Requires cap->lock (modifies cap->running_task).
187 * When migrating a Task, the migrater must take task->lock before
188 * modifying task->cap, to synchronise with the waking up Task.
189 * Additionally, the migrater should own the Capability (when
190 * migrating the run queue), or cap->lock (when migrating
191 * returning_workers).
193 * ------------------------------------------------------------------------- */
195 #if defined(THREADED_RTS)
197 giveCapabilityToTask (Capability *cap, Task *task)
199 ASSERT_LOCK_HELD(&cap->lock);
200 ASSERT(task->cap == cap);
201 // We are not modifying task->cap, so we do not need to take task->lock.
203 sched_belch("passing capability %d to %s %p",
204 cap->no, task->tso ? "bound task" : "worker",
206 ACQUIRE_LOCK(&task->lock);
207 task->wakeup = rtsTrue;
208 // the wakeup flag is needed because signalCondition() doesn't
209 // flag the condition if the thread is already runniing, but we want
211 signalCondition(&task->cond);
212 RELEASE_LOCK(&task->lock);
216 /* ----------------------------------------------------------------------------
217 * Function: releaseCapability(Capability*)
219 * Purpose: Letting go of a capability. Causes a
220 * 'returning worker' thread or a 'waiting worker'
221 * to wake up, in that order.
222 * ------------------------------------------------------------------------- */
224 #if defined(THREADED_RTS)
226 releaseCapability_ (Capability* cap)
230 task = cap->running_task;
232 ASSERT_CAPABILITY_INVARIANTS(cap,task);
234 cap->running_task = NULL;
236 // Check to see whether a worker thread can be given
237 // the go-ahead to return the result of an external call..
238 if (cap->returning_tasks_hd != NULL) {
239 giveCapabilityToTask(cap,cap->returning_tasks_hd);
240 // The Task pops itself from the queue (see waitForReturnCapability())
244 // If the next thread on the run queue is a bound thread,
245 // give this Capability to the appropriate Task.
246 if (!emptyRunQueue(cap) && cap->run_queue_hd->bound) {
247 // Make sure we're not about to try to wake ourselves up
248 ASSERT(task != cap->run_queue_hd->bound);
249 task = cap->run_queue_hd->bound;
250 giveCapabilityToTask(cap,task);
254 // If we have an unbound thread on the run queue, or if there's
255 // anything else to do, give the Capability to a worker thread.
256 if (!emptyRunQueue(cap) || globalWorkToDo()) {
257 if (cap->spare_workers) {
258 giveCapabilityToTask(cap,cap->spare_workers);
259 // The worker Task pops itself from the queue;
263 // Create a worker thread if we don't have one. If the system
264 // is interrupted, we only create a worker task if there
265 // are threads that need to be completed. If the system is
266 // shutting down, we never create a new worker.
267 if (!shutting_down_scheduler) {
269 sched_belch("starting new worker on capability %d", cap->no));
270 startWorkerTask(cap, workerStart);
275 last_free_capability = cap;
276 IF_DEBUG(scheduler, sched_belch("freeing capability %d", cap->no));
280 releaseCapability (Capability* cap UNUSED_IF_NOT_THREADS)
282 ACQUIRE_LOCK(&cap->lock);
283 releaseCapability_(cap);
284 RELEASE_LOCK(&cap->lock);
288 releaseCapabilityAndQueueWorker (Capability* cap UNUSED_IF_NOT_THREADS)
292 ACQUIRE_LOCK(&cap->lock);
294 task = cap->running_task;
296 // If the current task is a worker, save it on the spare_workers
297 // list of this Capability. A worker can mark itself as stopped,
298 // in which case it is not replaced on the spare_worker queue.
299 // This happens when the system is shutting down (see
300 // Schedule.c:workerStart()).
301 // Also, be careful to check that this task hasn't just exited
302 // Haskell to do a foreign call (task->suspended_tso).
303 if (!isBoundTask(task) && !task->stopped && !task->suspended_tso) {
304 task->next = cap->spare_workers;
305 cap->spare_workers = task;
307 // Bound tasks just float around attached to their TSOs.
309 releaseCapability_(cap);
311 RELEASE_LOCK(&cap->lock);
315 /* ----------------------------------------------------------------------------
316 * waitForReturnCapability( Task *task )
318 * Purpose: when an OS thread returns from an external call,
319 * it calls waitForReturnCapability() (via Schedule.resumeThread())
320 * to wait for permission to enter the RTS & communicate the
321 * result of the external call back to the Haskell thread that
324 * ------------------------------------------------------------------------- */
326 waitForReturnCapability (Capability **pCap,
327 Task *task UNUSED_IF_NOT_THREADS)
329 #if !defined(THREADED_RTS)
331 MainCapability.running_task = task;
332 task->cap = &MainCapability;
333 *pCap = &MainCapability;
336 Capability *cap = *pCap;
339 // Try last_free_capability first
340 cap = last_free_capability;
341 if (!cap->running_task) {
343 // otherwise, search for a free capability
344 for (i = 0; i < n_capabilities; i++) {
345 cap = &capabilities[i];
346 if (!cap->running_task) {
350 // Can't find a free one, use last_free_capability.
351 cap = last_free_capability;
354 // record the Capability as the one this Task is now assocated with.
358 ASSERT(task->cap == cap);
361 ACQUIRE_LOCK(&cap->lock);
364 sched_belch("returning; I want capability %d", cap->no));
366 if (!cap->running_task) {
367 // It's free; just grab it
368 cap->running_task = task;
369 RELEASE_LOCK(&cap->lock);
371 newReturningTask(cap,task);
372 RELEASE_LOCK(&cap->lock);
375 ACQUIRE_LOCK(&task->lock);
376 // task->lock held, cap->lock not held
377 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
379 task->wakeup = rtsFalse;
380 RELEASE_LOCK(&task->lock);
382 // now check whether we should wake up...
383 ACQUIRE_LOCK(&cap->lock);
384 if (cap->running_task == NULL) {
385 if (cap->returning_tasks_hd != task) {
386 giveCapabilityToTask(cap,cap->returning_tasks_hd);
387 RELEASE_LOCK(&cap->lock);
390 cap->running_task = task;
391 popReturningTask(cap);
392 RELEASE_LOCK(&cap->lock);
395 RELEASE_LOCK(&cap->lock);
400 ASSERT_CAPABILITY_INVARIANTS(cap,task);
403 sched_belch("returning; got capability %d", cap->no));
409 #if defined(THREADED_RTS)
410 /* ----------------------------------------------------------------------------
412 * ------------------------------------------------------------------------- */
415 yieldCapability (Capability** pCap, Task *task)
417 Capability *cap = *pCap;
419 // The fast path; no locking
420 if ( cap->returning_tasks_hd == NULL && anyWorkForMe(cap,task) )
423 while ( cap->returning_tasks_hd != NULL || !anyWorkForMe(cap,task) ) {
424 IF_DEBUG(scheduler, sched_belch("giving up capability %d", cap->no));
426 // We must now release the capability and wait to be woken up
428 releaseCapabilityAndQueueWorker(cap);
431 ACQUIRE_LOCK(&task->lock);
432 // task->lock held, cap->lock not held
433 if (!task->wakeup) waitCondition(&task->cond, &task->lock);
435 task->wakeup = rtsFalse;
436 RELEASE_LOCK(&task->lock);
438 IF_DEBUG(scheduler, sched_belch("woken up on capability %d", cap->no));
439 ACQUIRE_LOCK(&cap->lock);
440 if (cap->running_task != NULL) {
441 RELEASE_LOCK(&cap->lock);
445 if (task->tso == NULL) {
446 ASSERT(cap->spare_workers != NULL);
447 // if we're not at the front of the queue, release it
448 // again. This is unlikely to happen.
449 if (cap->spare_workers != task) {
450 giveCapabilityToTask(cap,cap->spare_workers);
451 RELEASE_LOCK(&cap->lock);
454 cap->spare_workers = task->next;
457 cap->running_task = task;
458 RELEASE_LOCK(&cap->lock);
462 IF_DEBUG(scheduler, sched_belch("got capability %d", cap->no));
463 ASSERT(cap->running_task == task);
468 ASSERT_CAPABILITY_INVARIANTS(cap,task);
473 /* ----------------------------------------------------------------------------
476 * Used to indicate that the interrupted flag is now set, or some
477 * other global condition that might require waking up a Task on each
479 * ------------------------------------------------------------------------- */
482 prodCapabilities(rtsBool all)
488 for (i=0; i < n_capabilities; i++) {
489 cap = &capabilities[i];
490 ACQUIRE_LOCK(&cap->lock);
491 if (!cap->running_task) {
492 if (cap->spare_workers) {
493 task = cap->spare_workers;
494 ASSERT(!task->stopped);
495 giveCapabilityToTask(cap,task);
497 RELEASE_LOCK(&cap->lock);
502 RELEASE_LOCK(&cap->lock);
507 prodAllCapabilities (void)
509 prodCapabilities(rtsTrue);
512 /* ----------------------------------------------------------------------------
515 * Like prodAllCapabilities, but we only require a single Task to wake
516 * up in order to service some global event, such as checking for
517 * deadlock after some idle time has passed.
518 * ------------------------------------------------------------------------- */
521 prodOneCapability (void)
523 prodCapabilities(rtsFalse);
526 /* ----------------------------------------------------------------------------
529 * At shutdown time, we want to let everything exit as cleanly as
530 * possible. For each capability, we let its run queue drain, and
531 * allow the workers to stop.
533 * This function should be called when interrupted and
534 * shutting_down_scheduler = rtsTrue, thus any worker that wakes up
535 * will exit the scheduler and call taskStop(), and any bound thread
536 * that wakes up will return to its caller. Runnable threads are
539 * ------------------------------------------------------------------------- */
542 shutdownCapability (Capability *cap, Task *task)
546 ASSERT(interrupted && shutting_down_scheduler);
550 for (i = 0; i < 50; i++) {
551 IF_DEBUG(scheduler, sched_belch("shutting down capability %d, attempt %d", cap->no, i));
552 ACQUIRE_LOCK(&cap->lock);
553 if (cap->running_task) {
554 RELEASE_LOCK(&cap->lock);
555 IF_DEBUG(scheduler, sched_belch("not owner, yielding"));
559 cap->running_task = task;
560 if (!emptyRunQueue(cap) || cap->spare_workers) {
561 IF_DEBUG(scheduler, sched_belch("runnable threads or workers still alive, yielding"));
562 releaseCapability_(cap); // this will wake up a worker
563 RELEASE_LOCK(&cap->lock);
567 IF_DEBUG(scheduler, sched_belch("capability %d is stopped.", cap->no));
568 RELEASE_LOCK(&cap->lock);
571 // we now have the Capability, its run queue and spare workers
572 // list are both empty.
575 #endif /* THREADED_RTS */