1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.33 2003/11/12 17:27:05 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * The definitions for Thread State Objects.
8 * ---------------------------------------------------------------------------*/
13 #if defined(GRAN) || defined(PAR)
16 #define TSO_MAGIC 4321
39 #if defined(PROFILING)
41 CostCentreStack *CCCS; /* thread's current CCS */
43 #else /* !PROFILING */
44 # if defined(SUPPORTS_EMPTY_STRUCTS)
49 #endif /* PROFILING */
52 typedef StgTSOStatBuf StgTSOParInfo;
54 # if defined(SUPPORTS_EMPTY_STRUCTS)
63 StgThreadPriority priority;
64 StgInt revalTid; /* ToDo: merge both into 1 word */
68 # if defined(SUPPORTS_EMPTY_STRUCTS)
76 typedef StgTSOStatBuf StgTSOGranInfo;
78 # if defined(SUPPORTS_EMPTY_STRUCTS)
89 #else /* !TICKY_TICKY */
90 # if defined(SUPPORTS_EMPTY_STRUCTS)
95 #endif /* TICKY_TICKY */
103 * The what_next field of a TSO indicates how the thread is to be run.
106 ThreadRunGHC, /* return to address on top of stack */
107 ThreadInterpret, /* interpret this thread */
108 ThreadKilled, /* thread has died, don't run it */
109 ThreadRelocated, /* thread has moved, link points to new locn */
110 ThreadComplete /* thread has finished */
114 * Thread IDs are 32 bits.
116 typedef StgWord32 StgThreadID;
119 * This type is returned to the scheduler by a thread that has
120 * stopped for one reason or another.
124 HeapOverflow, /* might also be StackOverflow */
129 } StgThreadReturnCode;
132 * We distinguish between the various classes of threads in the system.
142 * Threads may be blocked for several reasons. A blocked thread will
143 * have the reason in the why_blocked field of the TSO, and some
144 * further info (such as the closure the thread is blocked on, or the
145 * file descriptor if the thread is waiting on I/O) in the block_info
157 #if defined(mingw32_TARGET_OS)
161 , BlockedOnGA // blocked on a remote closure represented by a Global Address
162 , BlockedOnGA_NoSend // same as above but without sending a Fetch message
164 #if defined(RTS_SUPPORTS_THREADS)
166 , BlockedOnCCall_NoUnblockExc // same as above but don't unblock async exceptions
171 #if defined(mingw32_TARGET_OS)
172 /* results from an async I/O request + it's ID. */
184 #if defined(mingw32_TARGET_OS)
185 StgAsyncIOResult* async_result;
191 * TSOs live on the heap, and therefore look just like heap objects.
192 * Large TSOs will live in their own "block group" allocated by the
193 * storage manager, and won't be copied during garbage collection.
197 * ToDo: make this structure sensible on a non-32-bit arch.
200 typedef struct StgTSO_ {
203 struct StgTSO_* link; /* Links threads onto blocking queues */
204 StgMutClosure * mut_link; /* TSO's are mutable of course! */
205 struct StgTSO_* global_link; /* Links all threads together */
207 StgTSOWhatNext what_next : 16;
208 StgTSOBlockReason why_blocked : 16;
209 StgTSOBlockInfo block_info;
210 struct StgTSO_* blocked_exceptions;
214 MAYBE_EMPTY_STRUCT(StgTSOTickyInfo,ticky)
215 MAYBE_EMPTY_STRUCT(StgTSOProfInfo,prof)
216 MAYBE_EMPTY_STRUCT(StgTSOParInfo,par)
217 MAYBE_EMPTY_STRUCT(StgTSOGranInfo,gran)
218 MAYBE_EMPTY_STRUCT(StgTSODistInfo,dist)
220 /* The thread stack... */
221 StgWord stack_size; /* stack size in *words* */
222 StgWord max_stack_size; /* maximum stack size in *words* */
225 StgWord stack[FLEXIBLE_ARRAY];
228 /* -----------------------------------------------------------------------------
231 An active thread has the following properties:
233 tso->stack < tso->sp < tso->stack+tso->stack_size
234 tso->stack_size <= tso->max_stack_size
236 RESERVED_STACK_WORDS is large enough for any heap-check or
239 The size of the TSO struct plus the stack is either
240 (a) smaller than a block, or
241 (b) a multiple of BLOCK_SIZE
243 tso->why_blocked tso->block_info location
244 ----------------------------------------------------------------------
245 NotBlocked NULL runnable_queue, or running
247 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
249 BlockedOnMVar the MVAR the MVAR's queue
251 BlockedOnException the TSO TSO->blocked_exception
253 BlockedOnRead NULL blocked_queue
254 BlockedOnWrite NULL blocked_queue
255 BlockedOnDelay NULL blocked_queue
256 BlockedOnGA closure TSO blocks on BQ of that closure
257 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
259 tso->link == END_TSO_QUEUE, if the thread is currently running.
261 A zombie thread has the following properties:
263 tso->what_next == ThreadComplete or ThreadKilled
264 tso->link == (could be on some queue somewhere)
265 tso->su == tso->stack + tso->stack_size
266 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
267 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
268 exception , if what_next == ThreadKilled
270 (tso->sp is left pointing at the top word on the stack so that
271 the return value or exception will be retained by a GC).
273 tso->blocked_exceptions is either:
275 NULL if async exceptions are unblocked.
277 END_TSO_QUEUE if async exceptions are blocked, but no threads
278 are currently waiting to deliver.
280 (StgTSO *)tso if threads are currently awaiting delivery of
281 exceptions to this thread.
283 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
284 setup only. They mark a TSO that has entered a FETCH_ME or
285 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
286 closure will send a Fetch message.
287 Currently we have no separate code for blocking on an RBH; we use the
288 BlockedOnBlackHole case for that. -- HWL
290 ---------------------------------------------------------------------------- */
292 /* Workaround for a bug/quirk in gcc on certain architectures.
293 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
294 * in other words, gcc pads the structure at the end.
297 extern StgTSO dummy_tso;
299 #define TSO_STRUCT_SIZE \
300 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
302 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))