1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.26 2002/04/10 11:43:44 stolz 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 ThreadEnterGHC, /* enter top thunk on stack */
92 ThreadRunGHC, /* return to address on top of stack */
93 ThreadEnterInterp, /* enter top thunk on stack (w/ interpreter) */
94 ThreadKilled, /* thread has died, don't run it */
95 ThreadRelocated, /* thread has moved, link points to new locn */
96 ThreadComplete /* thread has finished */
100 * Thread IDs are 32 bits.
102 typedef StgWord32 StgThreadID;
105 * This type is returned to the scheduler by a thread that has
106 * stopped for one reason or another.
110 HeapOverflow, /* might also be StackOverflow */
115 } StgThreadReturnCode;
118 * We distinguish between the various classes of threads in the system.
128 * Threads may be blocked for several reasons. A blocked thread will
129 * have the reason in the why_blocked field of the TSO, and some
130 * further info (such as the closure the thread is blocked on, or the
131 * file descriptor if the thread is waiting on I/O) in the block_info
144 , BlockedOnGA // blocked on a remote closure represented by a Global Address
145 , BlockedOnGA_NoSend // same as above but without sending a Fetch message
147 #if defined(RTS_SUPPORTS_THREADS)
160 * TSOs live on the heap, and therefore look just like heap objects.
161 * Large TSOs will live in their own "block group" allocated by the
162 * storage manager, and won't be copied during garbage collection.
166 * ToDo: make this structure sensible on a non-32-bit arch.
169 typedef struct StgTSO_ {
172 struct StgTSO_* link; /* Links threads onto blocking queues */
173 StgMutClosure * mut_link; /* TSO's are mutable of course! */
174 struct StgTSO_* global_link; /* Links all threads together */
176 StgTSOWhatNext what_next : 16;
177 StgTSOBlockReason why_blocked : 16;
178 StgTSOBlockInfo block_info;
179 struct StgTSO_* blocked_exceptions;
185 StgTSOTickyInfo ticky;
191 /* The thread stack... */
192 StgWord stack_size; /* stack size in *words* */
193 StgWord max_stack_size; /* maximum stack size in *words* */
197 StgWord stack[FLEXIBLE_ARRAY];
200 /* -----------------------------------------------------------------------------
203 An active thread has the following properties:
205 tso->stack < tso->sp < tso->stack+tso->stack_size
206 tso->stack_size <= tso->max_stack_size
208 RESERVED_STACK_WORDS is large enough for any heap-check or
211 The size of the TSO struct plus the stack is either
212 (a) smaller than a block, or
213 (b) a multiple of BLOCK_SIZE
215 tso->why_blocked tso->block_info location
216 ----------------------------------------------------------------------
217 NotBlocked NULL runnable_queue, or running
219 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
221 BlockedOnMVar the MVAR the MVAR's queue
223 BlockedOnException the TSO TSO->blocked_exception
225 BlockedOnRead NULL blocked_queue
226 BlockedOnWrite NULL blocked_queue
227 BlockedOnDelay NULL blocked_queue
228 BlockedOnGA closure TSO blocks on BQ of that closure
229 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
231 tso->link == END_TSO_QUEUE, if the thread is currently running.
233 A zombie thread has the following properties:
235 tso->what_next == ThreadComplete or ThreadKilled
236 tso->link == (could be on some queue somewhere)
237 tso->su == tso->stack + tso->stack_size
238 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
239 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
240 exception , if what_next == ThreadKilled
242 (tso->sp is left pointing at the top word on the stack so that
243 the return value or exception will be retained by a GC).
245 tso->blocked_exceptions is either:
247 NULL if async exceptions are unblocked.
249 END_TSO_QUEUE if async exceptions are blocked, but no threads
250 are currently waiting to deliver.
252 (StgTSO *)tso if threads are currently awaiting delivery of
253 exceptions to this thread.
255 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
256 setup only. They mark a TSO that has entered a FETCH_ME or
257 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
258 closure will send a Fetch message.
259 Currently we have no separate code for blocking on an RBH; we use the
260 BlockedOnBlackHole case for that. -- HWL
262 ---------------------------------------------------------------------------- */
264 /* Workaround for a bug/quirk in gcc on certain architectures.
265 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
266 * in other words, gcc pads the structure at the end.
269 extern StgTSO dummy_tso;
271 #define TSO_STRUCT_SIZE \
272 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
274 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))