Add ASSERTs to all calls of nameModule
[ghc-hetmet.git] / rts / Capability.h
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001-2006
4  *
5  * Capabilities
6  *
7  * The notion of a capability is used when operating in multi-threaded
8  * environments (which the THREADED_RTS build of the RTS does), to
9  * hold all the state an OS thread/task needs to run Haskell code:
10  * its STG registers, a pointer to its  TSO, a nursery etc. During
11  * STG execution, a pointer to the capabilitity is kept in a 
12  * register (BaseReg).
13  *
14  * Only in an THREADED_RTS build will there be multiple capabilities,
15  * in the non-threaded builds there is one global capability, namely
16  * MainCapability.
17  *
18  * This header file contains the functions for working with capabilities.
19  * (the main, and only, consumer of this interface is the scheduler).
20  * 
21  * --------------------------------------------------------------------------*/
22
23 #ifndef CAPABILITY_H
24 #define CAPABILITY_H
25
26 #include "RtsFlags.h"
27 #include "Task.h"
28
29 struct Capability_ {
30     // State required by the STG virtual machine when running Haskell
31     // code.  During STG execution, the BaseReg register always points
32     // to the StgRegTable of the current Capability (&cap->r).
33     StgFunTable f;
34     StgRegTable r;
35
36     nat no;  // capability number.
37
38     // The Task currently holding this Capability.  This task has
39     // exclusive access to the contents of this Capability (apart from
40     // returning_tasks_hd/returning_tasks_tl).
41     // Locks required: cap->lock.
42     Task *running_task;
43
44     // true if this Capability is running Haskell code, used for
45     // catching unsafe call-ins.
46     rtsBool in_haskell;
47
48     // The run queue.  The Task owning this Capability has exclusive
49     // access to its run queue, so can wake up threads without
50     // taking a lock, and the common path through the scheduler is
51     // also lock-free.
52     StgTSO *run_queue_hd;
53     StgTSO *run_queue_tl;
54
55     // Tasks currently making safe foreign calls.  Doubly-linked.
56     // When returning, a task first acquires the Capability before
57     // removing itself from this list, so that the GC can find all
58     // the suspended TSOs easily.  Hence, when migrating a Task from
59     // the returning_tasks list, we must also migrate its entry from
60     // this list.
61     Task *suspended_ccalling_tasks;
62
63     // One mutable list per generation, so we don't need to take any
64     // locks when updating an old-generation thunk.  These
65     // mini-mut-lists are moved onto the respective gen->mut_list at
66     // each GC.
67     bdescr **mut_lists;
68
69     // Context switch flag. We used to have one global flag, now one 
70     // per capability. Locks required  : none (conflicts are harmless)
71     int context_switch;
72
73 #if defined(THREADED_RTS)
74     // Worker Tasks waiting in the wings.  Singly-linked.
75     Task *spare_workers;
76
77     // This lock protects running_task, returning_tasks_{hd,tl}, wakeup_queue.
78     Mutex lock;
79
80     // Tasks waiting to return from a foreign call, or waiting to make
81     // a new call-in using this Capability (NULL if empty).
82     // NB. this field needs to be modified by tasks other than the
83     // running_task, so it requires cap->lock to modify.  A task can
84     // check whether it is NULL without taking the lock, however.
85     Task *returning_tasks_hd; // Singly-linked, with head/tail
86     Task *returning_tasks_tl;
87
88     // A list of threads to append to this Capability's run queue at
89     // the earliest opportunity.  These are threads that have been
90     // woken up by another Capability.
91     StgTSO *wakeup_queue_hd;
92     StgTSO *wakeup_queue_tl;
93 #endif
94
95     // Per-capability STM-related data
96     StgTVarWatchQueue *free_tvar_watch_queues;
97     StgInvariantCheckQueue *free_invariant_check_queues;
98     StgTRecChunk *free_trec_chunks;
99     StgTRecHeader *free_trec_headers;
100     nat transaction_tokens;
101 }; // typedef Capability, defined in RtsAPI.h
102
103
104 #if defined(THREADED_RTS)
105 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
106 #else
107 #define ASSERT_TASK_ID(task) /*empty*/
108 #endif
109
110 // These properties should be true when a Task is holding a Capability
111 #define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task)                     \
112   ASSERT(cap->running_task != NULL && cap->running_task == task);       \
113   ASSERT(task->cap == cap);                                             \
114   ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
115
116 // Sometimes a Task holds a Capability, but the Task is not associated
117 // with that Capability (ie. task->cap != cap).  This happens when
118 // (a) a Task holds multiple Capabilities, and (b) when the current
119 // Task is bound, its thread has just blocked, and it may have been
120 // moved to another Capability.
121 #define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)  \
122   ASSERT(cap->run_queue_hd == END_TSO_QUEUE ?           \
123             cap->run_queue_tl == END_TSO_QUEUE : 1);    \
124   ASSERT(myTask() == task);                             \
125   ASSERT_TASK_ID(task);
126
127 // Converts a *StgRegTable into a *Capability.
128 //
129 INLINE_HEADER Capability *
130 regTableToCapability (StgRegTable *reg)
131 {
132     return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
133 }
134
135 // Initialise the available capabilities.
136 //
137 void initCapabilities (void);
138
139 // Release a capability.  This is called by a Task that is exiting
140 // Haskell to make a foreign call, or in various other cases when we
141 // want to relinquish a Capability that we currently hold.
142 //
143 // ASSUMES: cap->running_task is the current Task.
144 //
145 #if defined(THREADED_RTS)
146 void releaseCapability  (Capability* cap);
147 void releaseCapability_ (Capability* cap); // assumes cap->lock is held
148 #else
149 // releaseCapability() is empty in non-threaded RTS
150 INLINE_HEADER void releaseCapability  (Capability* cap STG_UNUSED) {};
151 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED) {};
152 #endif
153
154 #if !IN_STG_CODE
155 // one global capability
156 extern Capability MainCapability; 
157 #endif
158
159 // Array of all the capabilities
160 //
161 extern nat n_capabilities;
162 extern Capability *capabilities;
163
164 // The Capability that was last free.  Used as a good guess for where
165 // to assign new threads.
166 //
167 extern Capability *last_free_capability;
168
169 // GC indicator, in scope for the scheduler
170 extern volatile StgWord waiting_for_gc;
171
172 // Acquires a capability at a return point.  If *cap is non-NULL, then
173 // this is taken as a preference for the Capability we wish to
174 // acquire.
175 //
176 // OS threads waiting in this function get priority over those waiting
177 // in waitForCapability().
178 //
179 // On return, *cap is non-NULL, and points to the Capability acquired.
180 //
181 void waitForReturnCapability (Capability **cap/*in/out*/, Task *task);
182
183 INLINE_HEADER void recordMutableCap (StgClosure *p, Capability *cap, nat gen);
184
185 #if defined(THREADED_RTS)
186
187 // Gives up the current capability IFF there is a higher-priority
188 // thread waiting for it.  This happens in one of two ways:
189 //
190 //   (a) we are passing the capability to another OS thread, so
191 //       that it can run a bound Haskell thread, or
192 //
193 //   (b) there is an OS thread waiting to return from a foreign call
194 //
195 // On return: *pCap is NULL if the capability was released.  The
196 // current task should then re-acquire it using waitForCapability().
197 //
198 void yieldCapability (Capability** pCap, Task *task);
199
200 // Acquires a capability for doing some work.
201 //
202 // On return: pCap points to the capability.
203 //
204 void waitForCapability (Task *task, Mutex *mutex, Capability **pCap);
205
206 // Wakes up a thread on a Capability (probably a different Capability
207 // from the one held by the current Task).
208 //
209 void wakeupThreadOnCapability (Capability *my_cap, Capability *other_cap,
210                                StgTSO *tso);
211
212 // Wakes up a worker thread on just one Capability, used when we
213 // need to service some global event.
214 //
215 void prodOneCapability (void);
216
217 // Similar to prodOneCapability(), but prods all of them.
218 //
219 void prodAllCapabilities (void);
220
221 // Waits for a capability to drain of runnable threads and workers,
222 // and then acquires it.  Used at shutdown time.
223 //
224 void shutdownCapability (Capability *cap, Task *task, rtsBool wait_foreign);
225
226 // Attempt to gain control of a Capability if it is free.
227 //
228 rtsBool tryGrabCapability (Capability *cap, Task *task);
229
230 #else // !THREADED_RTS
231
232 // Grab a capability.  (Only in the non-threaded RTS; in the threaded
233 // RTS one of the waitFor*Capability() functions must be used).
234 //
235 extern void grabCapability (Capability **pCap);
236
237 #endif /* !THREADED_RTS */
238
239 // cause all capabilities to context switch as soon as possible.
240 void setContextSwitches(void);
241
242 // Free a capability on exit
243 void freeCapability (Capability *cap);
244
245 // FOr the GC:
246 void markSomeCapabilities (evac_fn evac, void *user, nat i0, nat delta);
247 void markCapabilities (evac_fn evac, void *user);
248 void traverseSparkQueues (evac_fn evac, void *user);
249
250 /* -----------------------------------------------------------------------------
251  * INLINE functions... private below here
252  * -------------------------------------------------------------------------- */
253
254 INLINE_HEADER void
255 recordMutableCap (StgClosure *p, Capability *cap, nat gen)
256 {
257     bdescr *bd;
258
259     // We must own this Capability in order to modify its mutable list.
260     ASSERT(cap->running_task == myTask());
261     bd = cap->mut_lists[gen];
262     if (bd->free >= bd->start + BLOCK_SIZE_W) {
263         bdescr *new_bd;
264         new_bd = allocBlock_lock();
265         new_bd->link = bd;
266         bd = new_bd;
267         cap->mut_lists[gen] = bd;
268     }
269     *bd->free++ = (StgWord)p;
270 }
271
272 #endif /* CAPABILITY_H */