[project @ 2004-02-26 16:31:44 by simonmar]
[ghc-hetmet.git] / ghc / rts / Capability.c
1 /* ---------------------------------------------------------------------------
2  * (c) The GHC Team, 2003
3  *
4  * Capabilities
5  *
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
10  * register (BaseReg).
11  *
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.
15  * 
16  * --------------------------------------------------------------------------*/
17
18 #include "PosixSource.h"
19 #include "Rts.h"
20 #include "RtsUtils.h"
21 #include "RtsFlags.h"
22 #include "OSThreads.h"
23 #include "Capability.h"
24 #include "Schedule.h"  /* to get at EMPTY_RUN_QUEUE() */
25 #include "Signals.h" /* to get at handleSignalsInThisThread() */
26
27 #if !defined(SMP)
28 Capability MainCapability;     /* for non-SMP, we have one global capability */
29 #endif
30
31 #if defined(RTS_SUPPORTS_THREADS)
32
33 nat rts_n_free_capabilities;
34
35 /* returning_worker_cond: when a worker thread returns from executing an
36  * external call, it needs to wait for an RTS Capability before passing
37  * on the result of the call to the Haskell thread that made it.
38  * 
39  * returning_worker_cond is signalled in Capability.releaseCapability().
40  *
41  */
42 Condition returning_worker_cond = INIT_COND_VAR;
43
44 /*
45  * To avoid starvation of threads blocked on worker_thread_cond,
46  * the task(s) that enter the Scheduler will check to see whether
47  * there are one or more worker threads blocked waiting on
48  * returning_worker_cond.
49  */
50 nat rts_n_waiting_workers = 0;
51
52 /* thread_ready_cond: when signalled, a thread has become runnable for a
53  * task to execute.
54  *
55  * In the non-SMP case, it also implies that the thread that is woken up has
56  * exclusive access to the RTS and all its data structures (that are not
57  * locked by the Scheduler's mutex).
58  *
59  * thread_ready_cond is signalled whenever noCapabilities doesn't hold.
60  *
61  */
62 Condition thread_ready_cond = INIT_COND_VAR;
63
64 /*
65  * To be able to make an informed decision about whether or not 
66  * to create a new task when making an external call, keep track of
67  * the number of tasks currently blocked waiting on thread_ready_cond.
68  * (if > 0 => no need for a new task, just unblock an existing one).
69  *
70  * waitForWorkCapability() takes care of keeping it up-to-date;
71  * Task.startTask() uses its current value.
72  */
73 nat rts_n_waiting_tasks = 0;
74
75 static Condition *passTarget = NULL;
76 static rtsBool passingCapability = rtsFalse;
77 #endif
78
79 #ifdef SMP
80 #define UNUSED_IF_NOT_SMP
81 #else
82 #define UNUSED_IF_NOT_SMP STG_UNUSED
83 #endif
84
85 /* ----------------------------------------------------------------------------
86    Initialisation
87    ------------------------------------------------------------------------- */
88
89 static void
90 initCapability( Capability *cap )
91 {
92     cap->f.stgGCEnter1     = (F_)__stg_gc_enter_1;
93     cap->f.stgGCFun        = (F_)__stg_gc_fun;
94 }
95
96 #if defined(SMP)
97 static void initCapabilities_(nat n);
98 #endif
99
100 /* ---------------------------------------------------------------------------
101  * Function:  initCapabilities()
102  *
103  * Purpose:   set up the Capability handling. For the SMP build,
104  *            we keep a table of them, the size of which is
105  *            controlled by the user via the RTS flag RtsFlags.ParFlags.nNodes
106  *
107  * ------------------------------------------------------------------------- */
108 void
109 initCapabilities( void )
110 {
111 #if defined(SMP)
112   initCapabilities_(RtsFlags.ParFlags.nNodes);
113 #else
114   initCapability(&MainCapability);
115 #endif
116
117 #if defined(RTS_SUPPORTS_THREADS)
118   initCondition(&returning_worker_cond);
119   initCondition(&thread_ready_cond);
120   rts_n_free_capabilities = 1;
121 #endif
122
123   return;
124 }
125
126 #if defined(SMP)
127 /* Free capability list. */
128 static Capability *free_capabilities; /* Available capabilities for running threads */
129 static Capability *returning_capabilities; 
130         /* Capabilities being passed to returning worker threads */
131 #endif
132
133 /* ----------------------------------------------------------------------------
134    grabCapability( Capability** )
135
136    (only externally visible when !RTS_SUPPORTS_THREADS.  In the
137    threaded RTS, clients must use waitFor*Capability()).
138    ------------------------------------------------------------------------- */
139
140 void
141 grabCapability( Capability** cap )
142 {
143 #if !defined(SMP)
144 #if defined(RTS_SUPPORTS_THREADS)
145   ASSERT(rts_n_free_capabilities == 1);
146   rts_n_free_capabilities = 0;
147 #endif
148   *cap = &MainCapability;
149   handleSignalsInThisThread();
150 #else
151   *cap = free_capabilities;
152   free_capabilities = (*cap)->link;
153   rts_n_free_capabilities--;
154 #endif
155   IF_DEBUG(scheduler, sched_belch("worker: got capability"));
156 }
157
158 /* ----------------------------------------------------------------------------
159  * Function:  releaseCapability(Capability*)
160  *
161  * Purpose:   Letting go of a capability. Causes a
162  *            'returning worker' thread or a 'waiting worker'
163  *            to wake up, in that order.
164  * ------------------------------------------------------------------------- */
165
166 void
167 releaseCapability( Capability* cap UNUSED_IF_NOT_SMP )
168 {
169     // Precondition: sched_mutex is held.
170 #if defined(RTS_SUPPORTS_THREADS)
171 #ifndef SMP
172     ASSERT(rts_n_free_capabilities == 0);
173 #endif
174     // Check to see whether a worker thread can be given
175     // the go-ahead to return the result of an external call..
176     if (rts_n_waiting_workers > 0) {
177         // Decrement the counter here to avoid livelock where the
178         // thread that is yielding its capability will repeatedly
179         // signal returning_worker_cond.
180
181 #if defined(SMP)
182         // SMP variant untested
183         cap->link = returning_capabilities;
184         returning_capabilities = cap;
185 #endif
186
187         rts_n_waiting_workers--;
188         signalCondition(&returning_worker_cond);
189         IF_DEBUG(scheduler, sched_belch("worker: released capability to returning worker"));
190     } else if (passingCapability) {
191         if (passTarget == NULL) {
192             signalCondition(&thread_ready_cond);
193             startSchedulerTaskIfNecessary();
194         } else {
195             signalCondition(passTarget);
196         }
197         rts_n_free_capabilities = 1;
198         IF_DEBUG(scheduler, sched_belch("worker: released capability, passing it"));
199
200     } else {
201 #if defined(SMP)
202         cap->link = free_capabilities;
203         free_capabilities = cap;
204         rts_n_free_capabilities++;
205 #else
206         rts_n_free_capabilities = 1;
207 #endif
208         // Signal that a capability is available
209         if (rts_n_waiting_tasks > 0) {
210             signalCondition(&thread_ready_cond);
211         }
212         startSchedulerTaskIfNecessary();
213         IF_DEBUG(scheduler, sched_belch("worker: released capability"));
214     }
215 #endif
216     return;
217 }
218
219 #if defined(RTS_SUPPORTS_THREADS)
220 /*
221  * When a native thread has completed the execution of an external
222  * call, it needs to communicate the result back. This is done
223  * as follows:
224  *
225  *  - in resumeThread(), the thread calls waitForReturnCapability().
226  *  - If no capabilities are readily available, waitForReturnCapability()
227  *    increments a counter rts_n_waiting_workers, and blocks
228  *    waiting for the condition returning_worker_cond to become
229  *    signalled.
230  *  - upon entry to the Scheduler, a worker thread checks the
231  *    value of rts_n_waiting_workers. If > 0, the worker thread
232  *    will yield its capability to let a returning worker thread
233  *    proceed with returning its result -- this is done via
234  *    yieldToReturningWorker().
235  *  - the worker thread that yielded its capability then tries
236  *    to re-grab a capability and re-enter the Scheduler.
237  */
238
239 /* ----------------------------------------------------------------------------
240  * waitForReturnCapability( Mutext *pMutex, Capability** )
241  *
242  * Purpose:  when an OS thread returns from an external call,
243  * it calls grabReturnCapability() (via Schedule.resumeThread())
244  * to wait for permissions to enter the RTS & communicate the
245  * result of the external call back to the Haskell thread that
246  * made it.
247  *
248  * ------------------------------------------------------------------------- */
249
250 void
251 waitForReturnCapability( Mutex* pMutex, Capability** pCap )
252 {
253     // Pre-condition: pMutex is held.
254
255     IF_DEBUG(scheduler, 
256              sched_belch("worker: returning; workers waiting: %d",
257                          rts_n_waiting_workers));
258
259     if ( noCapabilities() || passingCapability ) {
260         rts_n_waiting_workers++;
261         wakeBlockedWorkerThread();
262         context_switch = 1;     // make sure it's our turn soon
263         waitCondition(&returning_worker_cond, pMutex);
264 #if defined(SMP)
265         *pCap = returning_capabilities;
266         returning_capabilities = (*pCap)->link;
267 #else
268         *pCap = &MainCapability;
269         ASSERT(rts_n_free_capabilities == 0);
270         handleSignalsInThisThread();
271 #endif
272     } else {
273         grabCapability(pCap);
274     }
275
276     // Post-condition: pMutex is held, pCap points to a capability
277     // which is now held by the current thread.
278     return;
279 }
280
281
282 /* ----------------------------------------------------------------------------
283  * yieldCapability( Mutex* pMutex, Capability** pCap )
284  * ------------------------------------------------------------------------- */
285
286 void
287 yieldCapability( Capability** pCap )
288 {
289     // Pre-condition:  pMutex is assumed held, the current thread
290     // holds the capability pointed to by pCap.
291
292     if ( rts_n_waiting_workers > 0 || passingCapability ) {
293         IF_DEBUG(scheduler, sched_belch("worker: giving up capability"));
294         releaseCapability(*pCap);
295         *pCap = NULL;
296     }
297
298     // Post-condition:  pMutex is assumed held, and either:
299     //
300     //  1. *pCap is NULL, in which case the current thread does not
301     //     hold a capability now, or
302     //  2. *pCap is not NULL, in which case the current thread still
303     //     holds the capability.
304     //
305     return;
306 }
307
308
309 /* ----------------------------------------------------------------------------
310  * waitForCapability( Mutex*, Capability**, Condition* )
311  *
312  * Purpose:  wait for a Capability to become available. In
313  *           the process of doing so, updates the number
314  *           of tasks currently blocked waiting for a capability/more
315  *           work. That counter is used when deciding whether or
316  *           not to create a new worker thread when an external
317  *           call is made.
318  *           If pThreadCond is not NULL, a capability can be specifically
319  *           passed to this thread using passCapability.
320  * ------------------------------------------------------------------------- */
321  
322 void 
323 waitForCapability( Mutex* pMutex, Capability** pCap, Condition* pThreadCond )
324 {
325     // Pre-condition: pMutex is held.
326
327     while ( noCapabilities() || 
328             (passingCapability && passTarget != pThreadCond)) {
329         IF_DEBUG(scheduler,
330                  sched_belch("worker: wait for capability (cond: %p)",
331                              pThreadCond));
332
333         if (pThreadCond != NULL) {
334             waitCondition(pThreadCond, pMutex);
335             IF_DEBUG(scheduler, sched_belch("worker: get passed capability"));
336         } else {
337             rts_n_waiting_tasks++;
338             waitCondition(&thread_ready_cond, pMutex);
339             rts_n_waiting_tasks--;
340             IF_DEBUG(scheduler, sched_belch("worker: get normal capability"));
341         }
342     }
343     passingCapability = rtsFalse;
344     grabCapability(pCap);
345
346     // Post-condition: pMutex is held and *pCap is held by the current thread
347     return;
348 }
349
350 /* ----------------------------------------------------------------------------
351    passCapability, passCapabilityToWorker
352    ------------------------------------------------------------------------- */
353
354 void
355 passCapability( Condition *pTargetThreadCond )
356 {
357     // Pre-condition: pMutex is held and cap is held by the current thread
358
359     passTarget = pTargetThreadCond;
360     passingCapability = rtsTrue;
361     IF_DEBUG(scheduler, sched_belch("worker: passCapability"));
362
363     // Post-condition: pMutex is held; cap is still held, but will be
364     //                 passed to the target thread when next released.
365 }
366
367 void
368 passCapabilityToWorker( void )
369 {
370     // Pre-condition: pMutex is held and cap is held by the current thread
371
372     passTarget = NULL;
373     passingCapability = rtsTrue;
374     IF_DEBUG(scheduler, sched_belch("worker: passCapabilityToWorker"));
375
376     // Post-condition: pMutex is held; cap is still held, but will be
377     //                 passed to a worker thread when next released.
378 }
379
380 #endif /* RTS_SUPPORTS_THREADS */
381
382 /* ------------------------------------------------------------------------- */
383
384 #if defined(SMP)
385 /*
386  * Function: initCapabilities_(nat)
387  *
388  * Purpose:  upon startup, allocate and fill in table
389  *           holding 'n' Capabilities. Only for SMP, since
390  *           it is the only build that supports multiple
391  *           capabilities within the RTS.
392  */
393 static void
394 initCapabilities_(nat n)
395 {
396   nat i;
397   Capability *cap, *prev;
398   cap  = NULL;
399   prev = NULL;
400   for (i = 0; i < n; i++) {
401     cap = stgMallocBytes(sizeof(Capability), "initCapabilities");
402     initCapability(cap);
403     cap->link = prev;
404     prev = cap;
405   }
406   free_capabilities = cap;
407   rts_n_free_capabilities = n;
408   returning_capabilities = NULL;
409   IF_DEBUG(scheduler,
410            sched_belch("allocated %d capabilities", n_free_capabilities));
411 }
412 #endif /* SMP */
413