1 /* ---------------------------------------------------------------------------
2 * (c) The GHC Team, 2003
6 * A Capability represent the token required to execute STG code,
7 * and all the state an OS thread/task needs to run Haskell code:
8 * its STG registers, a pointer to its TSO, a nursery etc. During
9 * STG execution, a pointer to the capabilitity is kept in a
12 * Only in an SMP build will there be multiple capabilities, for
13 * the threaded RTS and other non-threaded builds, there is only
14 * one global capability, namely MainCapability.
16 * --------------------------------------------------------------------------*/
18 #include "PosixSource.h"
22 #include "OSThreads.h"
23 #include "Capability.h"
24 #include "Schedule.h" /* to get at EMPTY_RUN_QUEUE() */
27 Capability MainCapability; /* for non-SMP, we have one global capability */
30 nat rts_n_free_capabilities;
32 #if defined(RTS_SUPPORTS_THREADS)
34 /* returning_worker_cond: when a worker thread returns from executing an
35 * external call, it needs to wait for an RTS Capability before passing
36 * on the result of the call to the Haskell thread that made it.
38 * returning_worker_cond is signalled in Capability.releaseCapability().
41 Condition returning_worker_cond = INIT_COND_VAR;
44 * To avoid starvation of threads blocked on worker_thread_cond,
45 * the task(s) that enter the Scheduler will check to see whether
46 * there are one or more worker threads blocked waiting on
47 * returning_worker_cond.
49 nat rts_n_waiting_workers = 0;
51 /* thread_ready_cond: when signalled, a thread has become runnable for a
54 * In the non-SMP case, it also implies that the thread that is woken up has
55 * exclusive access to the RTS and all its data structures (that are not
56 * locked by the Scheduler's mutex).
58 * thread_ready_cond is signalled whenever
59 * !noCapabilities && !EMPTY_RUN_QUEUE().
61 Condition thread_ready_cond = INIT_COND_VAR;
64 * To be able to make an informed decision about whether or not
65 * to create a new task when making an external call, keep track of
66 * the number of tasks currently blocked waiting on thread_ready_cond.
67 * (if > 0 => no need for a new task, just unblock an existing one).
69 * waitForWorkCapability() takes care of keeping it up-to-date;
70 * Task.startTask() uses its current value.
72 nat rts_n_waiting_tasks = 0;
74 static Condition *passTarget = NULL;
75 static rtsBool passingCapability = rtsFalse;
80 * Free capability list.
82 Capability *free_capabilities;
86 #define UNUSED_IF_NOT_SMP
88 #define UNUSED_IF_NOT_SMP STG_UNUSED
91 #if defined(RTS_USER_SIGNALS)
92 #define ANY_WORK_TO_DO() (!EMPTY_RUN_QUEUE() || interrupted || blackholes_need_checking || signals_pending())
94 #define ANY_WORK_TO_DO() (!EMPTY_RUN_QUEUE() || interrupted || blackholes_need_checking)
97 /* ----------------------------------------------------------------------------
99 ------------------------------------------------------------------------- */
102 initCapability( Capability *cap )
104 cap->f.stgGCEnter1 = (F_)__stg_gc_enter_1;
105 cap->f.stgGCFun = (F_)__stg_gc_fun;
108 /* -----------------------------------------------------------------------------
109 * Function: initCapabilities_(nat)
111 * Purpose: upon startup, allocate and fill in table
112 * holding 'n' Capabilities. Only for SMP, since
113 * it is the only build that supports multiple
114 * capabilities within the RTS.
115 * -------------------------------------------------------------------------- */
118 initCapabilities_(nat n)
121 Capability *cap, *prev;
124 for (i = 0; i < n; i++) {
125 cap = stgMallocBytes(sizeof(Capability), "initCapabilities");
130 free_capabilities = cap;
131 rts_n_free_capabilities = n;
133 sched_belch("allocated %d capabilities", rts_n_free_capabilities));
137 /* ---------------------------------------------------------------------------
138 * Function: initCapabilities()
140 * Purpose: set up the Capability handling. For the SMP build,
141 * we keep a table of them, the size of which is
142 * controlled by the user via the RTS flag RtsFlags.ParFlags.nNodes
144 * ------------------------------------------------------------------------- */
146 initCapabilities( void )
149 initCapabilities_(RtsFlags.ParFlags.nNodes);
151 initCapability(&MainCapability);
154 #if defined(RTS_SUPPORTS_THREADS)
155 initCondition(&returning_worker_cond);
156 initCondition(&thread_ready_cond);
159 rts_n_free_capabilities = 1;
162 /* ----------------------------------------------------------------------------
163 grabCapability( Capability** )
165 (only externally visible when !RTS_SUPPORTS_THREADS. In the
166 threaded RTS, clients must use waitFor*Capability()).
167 ------------------------------------------------------------------------- */
169 #if defined(RTS_SUPPORTS_THREADS)
173 grabCapability( Capability** cap )
176 ASSERT(rts_n_free_capabilities > 0);
177 *cap = free_capabilities;
178 free_capabilities = (*cap)->link;
179 rts_n_free_capabilities--;
181 # if defined(RTS_SUPPORTS_THREADS)
182 ASSERT(rts_n_free_capabilities == 1);
183 rts_n_free_capabilities = 0;
185 *cap = &MainCapability;
187 #if defined(RTS_SUPPORTS_THREADS)
188 IF_DEBUG(scheduler, sched_belch("worker: got capability"));
192 /* ----------------------------------------------------------------------------
193 * Function: releaseCapability(Capability*)
195 * Purpose: Letting go of a capability. Causes a
196 * 'returning worker' thread or a 'waiting worker'
197 * to wake up, in that order.
198 * ------------------------------------------------------------------------- */
201 releaseCapability( Capability* cap UNUSED_IF_NOT_SMP )
203 // Precondition: sched_mutex is held.
204 #if defined(RTS_SUPPORTS_THREADS)
206 ASSERT(rts_n_free_capabilities == 0);
209 cap->link = free_capabilities;
210 free_capabilities = cap;
212 // Check to see whether a worker thread can be given
213 // the go-ahead to return the result of an external call..
214 if (rts_n_waiting_workers > 0) {
215 // Decrement the counter here to avoid livelock where the
216 // thread that is yielding its capability will repeatedly
217 // signal returning_worker_cond.
219 rts_n_waiting_workers--;
220 signalCondition(&returning_worker_cond);
221 IF_DEBUG(scheduler, sched_belch("worker: released capability to returning worker"));
222 } else if (passingCapability) {
223 if (passTarget == NULL) {
224 signalCondition(&thread_ready_cond);
225 startSchedulerTaskIfNecessary();
227 signalCondition(passTarget);
230 rts_n_free_capabilities++;
232 rts_n_free_capabilities = 1;
234 IF_DEBUG(scheduler, sched_belch("worker: released capability, passing it"));
238 rts_n_free_capabilities++;
240 rts_n_free_capabilities = 1;
242 // Signal that a capability is available
243 if (rts_n_waiting_tasks > 0 && ANY_WORK_TO_DO()) {
244 signalCondition(&thread_ready_cond);
246 startSchedulerTaskIfNecessary();
247 IF_DEBUG(scheduler, sched_belch("worker: released capability"));
253 #if defined(RTS_SUPPORTS_THREADS)
255 * When a native thread has completed the execution of an external
256 * call, it needs to communicate the result back. This is done
259 * - in resumeThread(), the thread calls waitForReturnCapability().
260 * - If no capabilities are readily available, waitForReturnCapability()
261 * increments a counter rts_n_waiting_workers, and blocks
262 * waiting for the condition returning_worker_cond to become
264 * - upon entry to the Scheduler, a worker thread checks the
265 * value of rts_n_waiting_workers. If > 0, the worker thread
266 * will yield its capability to let a returning worker thread
267 * proceed with returning its result -- this is done via
268 * yieldToReturningWorker().
269 * - the worker thread that yielded its capability then tries
270 * to re-grab a capability and re-enter the Scheduler.
273 /* ----------------------------------------------------------------------------
274 * waitForReturnCapability( Mutext *pMutex, Capability** )
276 * Purpose: when an OS thread returns from an external call,
277 * it calls grabReturnCapability() (via Schedule.resumeThread())
278 * to wait for permissions to enter the RTS & communicate the
279 * result of the external call back to the Haskell thread that
282 * ------------------------------------------------------------------------- */
285 waitForReturnCapability( Mutex* pMutex, Capability** pCap )
287 // Pre-condition: pMutex is held.
290 sched_belch("worker: returning; workers waiting: %d",
291 rts_n_waiting_workers));
293 if ( noCapabilities() || passingCapability ) {
294 rts_n_waiting_workers++;
295 context_switch = 1; // make sure it's our turn soon
296 waitCondition(&returning_worker_cond, pMutex);
298 *pCap = free_capabilities;
299 free_capabilities = (*pCap)->link;
300 ASSERT(pCap != NULL);
302 *pCap = &MainCapability;
303 ASSERT(rts_n_free_capabilities == 0);
306 grabCapability(pCap);
309 // Post-condition: pMutex is held, pCap points to a capability
310 // which is now held by the current thread.
315 /* ----------------------------------------------------------------------------
316 * yieldCapability( Mutex* pMutex, Capability** pCap )
317 * ------------------------------------------------------------------------- */
320 yieldCapability( Capability** pCap )
322 // Pre-condition: pMutex is assumed held, the current thread
323 // holds the capability pointed to by pCap.
325 if ( rts_n_waiting_workers > 0 || passingCapability || !ANY_WORK_TO_DO()) {
327 if (rts_n_waiting_workers > 0) {
328 sched_belch("worker: giving up capability (returning wkr)");
329 } else if (passingCapability) {
330 sched_belch("worker: giving up capability (passing capability)");
332 sched_belch("worker: giving up capability (no threads to run)");
335 releaseCapability(*pCap);
339 // Post-condition: either:
341 // 1. *pCap is NULL, in which case the current thread does not
342 // hold a capability now, or
343 // 2. *pCap is not NULL, in which case the current thread still
344 // holds the capability.
350 /* ----------------------------------------------------------------------------
351 * waitForCapability( Mutex*, Capability**, Condition* )
353 * Purpose: wait for a Capability to become available. In
354 * the process of doing so, updates the number
355 * of tasks currently blocked waiting for a capability/more
356 * work. That counter is used when deciding whether or
357 * not to create a new worker thread when an external
359 * If pThreadCond is not NULL, a capability can be specifically
360 * passed to this thread using passCapability.
361 * ------------------------------------------------------------------------- */
364 waitForCapability( Mutex* pMutex, Capability** pCap, Condition* pThreadCond )
366 // Pre-condition: pMutex is held.
368 while ( noCapabilities() ||
369 (passingCapability && passTarget != pThreadCond) ||
372 sched_belch("worker: wait for capability (cond: %p)",
375 if (pThreadCond != NULL) {
376 waitCondition(pThreadCond, pMutex);
377 IF_DEBUG(scheduler, sched_belch("worker: get passed capability"));
379 rts_n_waiting_tasks++;
380 waitCondition(&thread_ready_cond, pMutex);
381 rts_n_waiting_tasks--;
382 IF_DEBUG(scheduler, sched_belch("worker: get normal capability"));
385 passingCapability = rtsFalse;
386 grabCapability(pCap);
388 // Post-condition: pMutex is held and *pCap is held by the current thread
392 /* ----------------------------------------------------------------------------
393 passCapability, passCapabilityToWorker
394 ------------------------------------------------------------------------- */
397 passCapability( Condition *pTargetThreadCond )
399 // Pre-condition: pMutex is held and cap is held by the current thread
401 passTarget = pTargetThreadCond;
402 passingCapability = rtsTrue;
403 IF_DEBUG(scheduler, sched_belch("worker: passCapability"));
405 // Post-condition: pMutex is held; cap is still held, but will be
406 // passed to the target thread when next released.
410 passCapabilityToWorker( void )
412 // Pre-condition: pMutex is held and cap is held by the current thread
415 passingCapability = rtsTrue;
416 IF_DEBUG(scheduler, sched_belch("worker: passCapabilityToWorker"));
418 // Post-condition: pMutex is held; cap is still held, but will be
419 // passed to a worker thread when next released.
422 #endif /* RTS_SUPPORTS_THREADS */
424 /* ----------------------------------------------------------------------------
427 Signals that a thread has been placed on the run queue, so a worker
428 might need to be woken up to run it.
430 ToDo: should check whether the thread at the front of the queue is
431 bound, and if so wake up the appropriate worker.
432 -------------------------------------------------------------------------- */
434 threadRunnable ( void )
436 #if defined(RTS_SUPPORTS_THREADS)
437 if ( !noCapabilities() && ANY_WORK_TO_DO() && rts_n_waiting_tasks > 0 ) {
438 signalCondition(&thread_ready_cond);
440 startSchedulerTaskIfNecessary();
445 /* ----------------------------------------------------------------------------
448 Wake up... time to die.
449 -------------------------------------------------------------------------- */
453 #if defined(RTS_SUPPORTS_THREADS)
454 signalCondition(&thread_ready_cond);