1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.30 2003/02/21 05:34:15 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 */
46 #endif /* PROFILING */
49 typedef StgTSOStatBuf StgTSOParInfo;
57 StgThreadPriority priority;
58 StgInt revalTid; /* ToDo: merge both into 1 word */
67 typedef StgTSOStatBuf StgTSOGranInfo;
77 #else /* !TICKY_TICKY */
80 #endif /* TICKY_TICKY */
88 * The what_next field of a TSO indicates how the thread is to be run.
91 ThreadRunGHC, /* return to address on top of stack */
92 ThreadInterpret, /* interpret this thread */
93 ThreadKilled, /* thread has died, don't run it */
94 ThreadRelocated, /* thread has moved, link points to new locn */
95 ThreadComplete /* thread has finished */
99 * Thread IDs are 32 bits.
101 typedef StgWord32 StgThreadID;
104 * This type is returned to the scheduler by a thread that has
105 * stopped for one reason or another.
109 HeapOverflow, /* might also be StackOverflow */
114 } StgThreadReturnCode;
117 * We distinguish between the various classes of threads in the system.
127 * Threads may be blocked for several reasons. A blocked thread will
128 * have the reason in the why_blocked field of the TSO, and some
129 * further info (such as the closure the thread is blocked on, or the
130 * file descriptor if the thread is waiting on I/O) in the block_info
143 , BlockedOnGA // blocked on a remote closure represented by a Global Address
144 , BlockedOnGA_NoSend // same as above but without sending a Fetch message
146 #if defined(RTS_SUPPORTS_THREADS)
148 , BlockedOnCCall_NoUnblockExc // same as above but don't unblock async exceptions
153 #ifdef mingw32_TARGET_OS
154 /* results from an async I/O request + it's ID. */
166 #ifdef mingw32_TARGET_OS
167 StgAsyncIOResult* async_result;
173 * TSOs live on the heap, and therefore look just like heap objects.
174 * Large TSOs will live in their own "block group" allocated by the
175 * storage manager, and won't be copied during garbage collection.
179 * ToDo: make this structure sensible on a non-32-bit arch.
182 typedef struct StgTSO_ {
185 struct StgTSO_* link; /* Links threads onto blocking queues */
186 StgMutClosure * mut_link; /* TSO's are mutable of course! */
187 struct StgTSO_* global_link; /* Links all threads together */
189 StgTSOWhatNext what_next : 16;
190 StgTSOBlockReason why_blocked : 16;
191 StgTSOBlockInfo block_info;
192 struct StgTSO_* blocked_exceptions;
195 StgTSOTickyInfo ticky;
201 /* The thread stack... */
202 StgWord stack_size; /* stack size in *words* */
203 StgWord max_stack_size; /* maximum stack size in *words* */
206 StgWord stack[FLEXIBLE_ARRAY];
209 /* -----------------------------------------------------------------------------
212 An active thread has the following properties:
214 tso->stack < tso->sp < tso->stack+tso->stack_size
215 tso->stack_size <= tso->max_stack_size
217 RESERVED_STACK_WORDS is large enough for any heap-check or
220 The size of the TSO struct plus the stack is either
221 (a) smaller than a block, or
222 (b) a multiple of BLOCK_SIZE
224 tso->why_blocked tso->block_info location
225 ----------------------------------------------------------------------
226 NotBlocked NULL runnable_queue, or running
228 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
230 BlockedOnMVar the MVAR the MVAR's queue
232 BlockedOnException the TSO TSO->blocked_exception
234 BlockedOnRead NULL blocked_queue
235 BlockedOnWrite NULL blocked_queue
236 BlockedOnDelay NULL blocked_queue
237 BlockedOnGA closure TSO blocks on BQ of that closure
238 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
240 tso->link == END_TSO_QUEUE, if the thread is currently running.
242 A zombie thread has the following properties:
244 tso->what_next == ThreadComplete or ThreadKilled
245 tso->link == (could be on some queue somewhere)
246 tso->su == tso->stack + tso->stack_size
247 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
248 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
249 exception , if what_next == ThreadKilled
251 (tso->sp is left pointing at the top word on the stack so that
252 the return value or exception will be retained by a GC).
254 tso->blocked_exceptions is either:
256 NULL if async exceptions are unblocked.
258 END_TSO_QUEUE if async exceptions are blocked, but no threads
259 are currently waiting to deliver.
261 (StgTSO *)tso if threads are currently awaiting delivery of
262 exceptions to this thread.
264 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
265 setup only. They mark a TSO that has entered a FETCH_ME or
266 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
267 closure will send a Fetch message.
268 Currently we have no separate code for blocking on an RBH; we use the
269 BlockedOnBlackHole case for that. -- HWL
271 ---------------------------------------------------------------------------- */
273 /* Workaround for a bug/quirk in gcc on certain architectures.
274 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
275 * in other words, gcc pads the structure at the end.
278 extern StgTSO dummy_tso;
280 #define TSO_STRUCT_SIZE \
281 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
283 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))