Merge the smp and threaded RTS ways
[ghc-hetmet.git] / ghc / 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 #if defined(THREADED_RTS)
70     // Worker Tasks waiting in the wings.  Singly-linked.
71     Task *spare_workers;
72
73     // This lock protects running_task and returning_tasks_{hd,tl}.
74     Mutex lock;
75
76     // Tasks waiting to return from a foreign call, or waiting to make
77     // a new call-in using this Capability (NULL if empty).
78     // NB. this field needs to be modified by tasks other than the
79     // running_task, so it requires cap->lock to modify.  A task can
80     // check whether it is NULL without taking the lock, however.
81     Task *returning_tasks_hd; // Singly-linked, with head/tail
82     Task *returning_tasks_tl;
83 #endif
84
85     // Per-capability STM-related data
86     StgTVarWaitQueue *free_tvar_wait_queues;
87     StgTRecChunk *free_trec_chunks;
88     StgTRecHeader *free_trec_headers;
89     nat transaction_tokens;
90 }; // typedef Capability, defined in RtsAPI.h
91
92
93 #if defined(THREADED_RTS)
94 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
95 #else
96 #define ASSERT_TASK_ID(task) /*empty*/
97 #endif
98
99 // These properties should be true when a Task is holding a Capability
100 #define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task)                     \
101   ASSERT(cap->running_task != NULL && cap->running_task == task);       \
102   ASSERT(task->cap == cap);                                             \
103   ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
104
105 // Sometimes a Task holds a Capability, but the Task is not associated
106 // with that Capability (ie. task->cap != cap).  This happens when
107 // (a) a Task holds multiple Capabilities, and (b) when the current
108 // Task is bound, its thread has just blocked, and it may have been
109 // moved to another Capability.
110 #define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)  \
111   ASSERT(cap->run_queue_hd == END_TSO_QUEUE ?           \
112             cap->run_queue_tl == END_TSO_QUEUE : 1);    \
113   ASSERT(myTask() == task);                             \
114   ASSERT_TASK_ID(task);
115
116 // Converts a *StgRegTable into a *Capability.
117 //
118 INLINE_HEADER Capability *
119 regTableToCapability (StgRegTable *reg)
120 {
121     return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
122 }
123
124 // Initialise the available capabilities.
125 //
126 void initCapabilities (void);
127
128 // Release a capability.  This is called by a Task that is exiting
129 // Haskell to make a foreign call, or in various other cases when we
130 // want to relinquish a Capability that we currently hold.
131 //
132 // ASSUMES: cap->running_task is the current Task.
133 //
134 #if defined(THREADED_RTS)
135 void releaseCapability  (Capability* cap);
136 void releaseCapability_ (Capability* cap); // assumes cap->lock is held
137 #else
138 // releaseCapability() is empty in non-threaded RTS
139 INLINE_HEADER void releaseCapability  (Capability* cap STG_UNUSED) {};
140 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED) {};
141 #endif
142
143 #if !IN_STG_CODE
144 // one global capability
145 extern Capability MainCapability; 
146 #endif
147
148 // Array of all the capabilities
149 //
150 extern nat n_capabilities;
151 extern Capability *capabilities;
152
153 // The Capability that was last free.  Used as a good guess for where
154 // to assign new threads.
155 //
156 extern Capability *last_free_capability;
157
158 // Acquires a capability at a return point.  If *cap is non-NULL, then
159 // this is taken as a preference for the Capability we wish to
160 // acquire.
161 //
162 // OS threads waiting in this function get priority over those waiting
163 // in waitForCapability().
164 //
165 // On return, *cap is non-NULL, and points to the Capability acquired.
166 //
167 void waitForReturnCapability (Capability **cap/*in/out*/, Task *task);
168
169 INLINE_HEADER void recordMutableCap (StgClosure *p, Capability *cap, nat gen);
170
171 #if defined(THREADED_RTS)
172
173 // Gives up the current capability IFF there is a higher-priority
174 // thread waiting for it.  This happens in one of two ways:
175 //
176 //   (a) we are passing the capability to another OS thread, so
177 //       that it can run a bound Haskell thread, or
178 //
179 //   (b) there is an OS thread waiting to return from a foreign call
180 //
181 // On return: *pCap is NULL if the capability was released.  The
182 // current task should then re-acquire it using waitForCapability().
183 //
184 void yieldCapability (Capability** pCap, Task *task);
185
186 // Acquires a capability for doing some work.
187 //
188 // On return: pCap points to the capability.
189 //
190 void waitForCapability (Task *task, Mutex *mutex, Capability **pCap);
191
192 // Wakes up a worker thread on just one Capability, used when we
193 // need to service some global event.
194 //
195 void prodOneCapability (void);
196
197 // Similar to prodOneCapability(), but prods all of them.
198 //
199 void prodAllCapabilities (void);
200
201 // Waits for a capability to drain of runnable threads and workers,
202 // and then acquires it.  Used at shutdown time.
203 //
204 void shutdownCapability (Capability *cap, Task *task);
205
206 // Attempt to gain control of a Capability if it is free.
207 //
208 rtsBool tryGrabCapability (Capability *cap, Task *task);
209
210 #else // !THREADED_RTS
211
212 // Grab a capability.  (Only in the non-threaded RTS; in the threaded
213 // RTS one of the waitFor*Capability() functions must be used).
214 //
215 extern void grabCapability (Capability **pCap);
216
217 #endif /* !THREADED_RTS */
218
219 /* -----------------------------------------------------------------------------
220  * INLINE functions... private below here
221  * -------------------------------------------------------------------------- */
222
223 INLINE_HEADER void
224 recordMutableCap (StgClosure *p, Capability *cap, nat gen)
225 {
226     bdescr *bd;
227
228     bd = cap->mut_lists[gen];
229     if (bd->free >= bd->start + BLOCK_SIZE_W) {
230         bdescr *new_bd;
231         new_bd = allocBlock_lock();
232         new_bd->link = bd;
233         bd = new_bd;
234         cap->mut_lists[gen] = bd;
235     }
236     *bd->free++ = (StgWord)p;
237 }
238
239 #endif /* CAPABILITY_H */