Add a write barrier to the TSO link field (#1589)
[ghc.git] / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-1999
4 *
5 * The definitions for Thread State Objects.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef TSO_H
10 #define TSO_H
11
12 #if DEBUG
13 #define TSO_MAGIC 4321
14 #endif
15
16 typedef struct {
17 StgInt pri;
18 StgInt magic;
19 StgInt sparkname;
20 rtsTime startedat;
21 rtsBool exported;
22 StgInt basicblocks;
23 StgInt allocs;
24 rtsTime exectime;
25 rtsTime fetchtime;
26 rtsTime fetchcount;
27 rtsTime blocktime;
28 StgInt blockcount;
29 rtsTime blockedat;
30 StgInt globalsparks;
31 StgInt localsparks;
32 rtsTime clock;
33 } StgTSOStatBuf;
34
35 /*
36 * GRAN: We distinguish between the various classes of threads in
37 * the system.
38 */
39 typedef enum {
40 AdvisoryPriority,
41 MandatoryPriority,
42 RevalPriority
43 } StgThreadPriority;
44
45 /*
46 * PROFILING info in a TSO
47 */
48 typedef struct {
49 CostCentreStack *CCCS; /* thread's current CCS */
50 } StgTSOProfInfo;
51
52 /*
53 * PAR info in a TSO
54 */
55 typedef StgTSOStatBuf StgTSOParInfo;
56
57 /*
58 * DIST info in a TSO
59 */
60 typedef struct {
61 StgThreadPriority priority;
62 StgInt revalTid; /* ToDo: merge both into 1 word */
63 StgInt revalSlot;
64 } StgTSODistInfo;
65
66 /*
67 * GRAN info in a TSO
68 */
69 typedef StgTSOStatBuf StgTSOGranInfo;
70
71 /*
72 * There is no TICKY info in a TSO at this time.
73 */
74
75 /*
76 * Thread IDs are 32 bits.
77 */
78 typedef StgWord32 StgThreadID;
79
80 #define tsoDirty(tso) ((tso)->flags & TSO_DIRTY)
81 #define tsoLocked(tso) ((tso)->flags & TSO_LOCKED)
82
83 /*
84 * Type returned after running a thread. Values of this type
85 * include HeapOverflow, StackOverflow etc. See Constants.h for the
86 * full list.
87 */
88 typedef unsigned int StgThreadReturnCode;
89
90 #if defined(mingw32_HOST_OS)
91 /* results from an async I/O request + its request ID. */
92 typedef struct {
93 unsigned int reqID;
94 int len;
95 int errCode;
96 } StgAsyncIOResult;
97 #endif
98
99 typedef union {
100 StgClosure *closure;
101 struct StgTSO_ *tso;
102 StgInt fd; /* StgInt instead of int, so that it's the same size as the ptrs */
103 #if defined(mingw32_HOST_OS)
104 StgAsyncIOResult *async_result;
105 #endif
106 StgWord target;
107 } StgTSOBlockInfo;
108
109
110 /*
111 * TSOs live on the heap, and therefore look just like heap objects.
112 * Large TSOs will live in their own "block group" allocated by the
113 * storage manager, and won't be copied during garbage collection.
114 */
115
116 /*
117 * Threads may be blocked for several reasons. A blocked thread will
118 * have the reason in the why_blocked field of the TSO, and some
119 * further info (such as the closure the thread is blocked on, or the
120 * file descriptor if the thread is waiting on I/O) in the block_info
121 * field.
122 */
123
124 typedef struct StgTSO_ {
125 StgHeader header;
126
127 /* The link field, for linking threads together in lists (e.g. the
128 run queue on a Capability.
129 */
130 struct StgTSO_* _link;
131 /*
132 NOTE!!! do not modify _link directly, it is subject to
133 a write barrier for generational GC. Instead use the
134 setTSOLink() function. Exceptions to this rule are:
135
136 * setting the link field to END_TSO_QUEUE
137 * putting a TSO on the blackhole_queue
138 * setting the link field of the currently running TSO, as it
139 will already be dirty.
140 */
141
142 struct StgTSO_* global_link; /* Links all threads together */
143
144 StgWord16 what_next; /* Values defined in Constants.h */
145 StgWord16 why_blocked; /* Values defined in Constants.h */
146 StgWord32 flags;
147 StgTSOBlockInfo block_info;
148 StgThreadID id;
149 int saved_errno;
150 struct Task_* bound;
151 struct Capability_* cap;
152 struct StgTRecHeader_ * trec; /* STM transaction record */
153
154 /*
155 A list of threads blocked on this TSO waiting to throw
156 exceptions. In order to access this field, the TSO must be
157 locked using lockClosure/unlockClosure (see SMP.h).
158 */
159 struct StgTSO_ * blocked_exceptions;
160
161 #ifdef TICKY_TICKY
162 /* TICKY-specific stuff would go here. */
163 #endif
164 #ifdef PROFILING
165 StgTSOProfInfo prof;
166 #endif
167 #ifdef PAR
168 StgTSOParInfo par;
169 #endif
170 #ifdef GRAN
171 StgTSOGranInfo gran;
172 #endif
173 #ifdef DIST
174 StgTSODistInfo dist;
175 #endif
176 #ifdef mingw32_HOST_OS
177 StgWord32 saved_winerror;
178 #endif
179
180 /* The thread stack... */
181 StgWord32 stack_size; /* stack size in *words* */
182 StgWord32 max_stack_size; /* maximum stack size in *words* */
183 StgPtr sp;
184
185 StgWord stack[FLEXIBLE_ARRAY];
186 } StgTSO;
187
188 /* -----------------------------------------------------------------------------
189 functions
190 -------------------------------------------------------------------------- */
191
192 extern void dirty_TSO (Capability *cap, StgTSO *tso);
193 extern void setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target);
194
195 /* -----------------------------------------------------------------------------
196 Invariants:
197
198 An active thread has the following properties:
199
200 tso->stack < tso->sp < tso->stack+tso->stack_size
201 tso->stack_size <= tso->max_stack_size
202
203 RESERVED_STACK_WORDS is large enough for any heap-check or
204 stack-check failure.
205
206 The size of the TSO struct plus the stack is either
207 (a) smaller than a block, or
208 (b) a multiple of BLOCK_SIZE
209
210 tso->why_blocked tso->block_info location
211 ----------------------------------------------------------------------
212 NotBlocked NULL runnable_queue, or running
213
214 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
215
216 BlockedOnMVar the MVAR the MVAR's queue
217
218 BlockedOnSTM END_TSO_QUEUE STM wait queue(s)
219
220 BlockedOnException the TSO TSO->blocked_exception
221
222 BlockedOnRead NULL blocked_queue
223 BlockedOnWrite NULL blocked_queue
224 BlockedOnDelay NULL blocked_queue
225 BlockedOnGA closure TSO blocks on BQ of that closure
226 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
227
228 tso->link == END_TSO_QUEUE, if the thread is currently running.
229
230 A zombie thread has the following properties:
231
232 tso->what_next == ThreadComplete or ThreadKilled
233 tso->link == (could be on some queue somewhere)
234 tso->su == tso->stack + tso->stack_size
235 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
236 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
237 exception , if what_next == ThreadKilled
238
239 (tso->sp is left pointing at the top word on the stack so that
240 the return value or exception will be retained by a GC).
241
242 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
243 setup only. They mark a TSO that has entered a FETCH_ME or
244 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
245 closure will send a Fetch message.
246 Currently we have no separate code for blocking on an RBH; we use the
247 BlockedOnBlackHole case for that. -- HWL
248
249 ---------------------------------------------------------------------------- */
250
251 /* Workaround for a bug/quirk in gcc on certain architectures.
252 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
253 * in other words, gcc pads the structure at the end.
254 */
255
256 extern StgTSO dummy_tso;
257
258 #define TSO_STRUCT_SIZE \
259 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
260
261 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
262
263
264 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
265 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
266
267 #if defined(PAR) || defined(GRAN)
268 /* this is the NIL ptr for a blocking queue */
269 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
270 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
271 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
272 #endif
273 /* ToDo?: different name for end of sleeping queue ? -- HWL */
274
275 #endif /* TSO_H */