1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.34 2004/03/01 14:18:35 simonmar 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
165 , BlockedOnCCall_NoUnblockExc // same as above but don't unblock
166 // async exceptions in resumeThread()
169 #if defined(mingw32_TARGET_OS)
170 /* results from an async I/O request + it's ID. */
182 #if defined(mingw32_TARGET_OS)
183 StgAsyncIOResult* async_result;
189 * TSOs live on the heap, and therefore look just like heap objects.
190 * Large TSOs will live in their own "block group" allocated by the
191 * storage manager, and won't be copied during garbage collection.
195 * ToDo: make this structure sensible on a non-32-bit arch.
198 typedef struct StgTSO_ {
201 struct StgTSO_* link; /* Links threads onto blocking queues */
202 StgMutClosure * mut_link; /* TSO's are mutable of course! */
203 struct StgTSO_* global_link; /* Links all threads together */
205 StgTSOWhatNext what_next : 16;
206 StgTSOBlockReason why_blocked : 16;
207 StgTSOBlockInfo block_info;
208 struct StgTSO_* blocked_exceptions;
211 struct StgMainThread_* main;
213 MAYBE_EMPTY_STRUCT(StgTSOTickyInfo,ticky)
214 MAYBE_EMPTY_STRUCT(StgTSOProfInfo,prof)
215 MAYBE_EMPTY_STRUCT(StgTSOParInfo,par)
216 MAYBE_EMPTY_STRUCT(StgTSOGranInfo,gran)
217 MAYBE_EMPTY_STRUCT(StgTSODistInfo,dist)
219 /* The thread stack... */
220 StgWord stack_size; /* stack size in *words* */
221 StgWord max_stack_size; /* maximum stack size in *words* */
224 StgWord stack[FLEXIBLE_ARRAY];
227 /* -----------------------------------------------------------------------------
230 An active thread has the following properties:
232 tso->stack < tso->sp < tso->stack+tso->stack_size
233 tso->stack_size <= tso->max_stack_size
235 RESERVED_STACK_WORDS is large enough for any heap-check or
238 The size of the TSO struct plus the stack is either
239 (a) smaller than a block, or
240 (b) a multiple of BLOCK_SIZE
242 tso->why_blocked tso->block_info location
243 ----------------------------------------------------------------------
244 NotBlocked NULL runnable_queue, or running
246 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
248 BlockedOnMVar the MVAR the MVAR's queue
250 BlockedOnException the TSO TSO->blocked_exception
252 BlockedOnRead NULL blocked_queue
253 BlockedOnWrite NULL blocked_queue
254 BlockedOnDelay NULL blocked_queue
255 BlockedOnGA closure TSO blocks on BQ of that closure
256 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
258 tso->link == END_TSO_QUEUE, if the thread is currently running.
260 A zombie thread has the following properties:
262 tso->what_next == ThreadComplete or ThreadKilled
263 tso->link == (could be on some queue somewhere)
264 tso->su == tso->stack + tso->stack_size
265 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
266 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
267 exception , if what_next == ThreadKilled
269 (tso->sp is left pointing at the top word on the stack so that
270 the return value or exception will be retained by a GC).
272 tso->blocked_exceptions is either:
274 NULL if async exceptions are unblocked.
276 END_TSO_QUEUE if async exceptions are blocked, but no threads
277 are currently waiting to deliver.
279 (StgTSO *)tso if threads are currently awaiting delivery of
280 exceptions to this thread.
282 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
283 setup only. They mark a TSO that has entered a FETCH_ME or
284 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
285 closure will send a Fetch message.
286 Currently we have no separate code for blocking on an RBH; we use the
287 BlockedOnBlackHole case for that. -- HWL
289 ---------------------------------------------------------------------------- */
291 /* Workaround for a bug/quirk in gcc on certain architectures.
292 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
293 * in other words, gcc pads the structure at the end.
296 extern StgTSO dummy_tso;
298 #define TSO_STRUCT_SIZE \
299 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
301 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))