1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.35 2004/08/13 13:09:40 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
40 * PROFILING info in a TSO
43 CostCentreStack *CCCS; /* thread's current CCS */
50 typedef StgTSOStatBuf StgTSOParInfo;
52 #ifdef SUPPORTS_EMPTY_STRUCTS
64 StgThreadPriority priority;
65 StgInt revalTid; /* ToDo: merge both into 1 word */
69 #ifdef SUPPORTS_EMPTY_STRUCTS
80 typedef StgTSOStatBuf StgTSOGranInfo;
82 #ifdef SUPPORTS_EMPTY_STRUCTS
90 * TICKY_TICKY info in a TSO
92 #ifdef SUPPORTS_EMPTY_STRUCTS
99 * Thread IDs are 32 bits.
101 typedef StgWord32 StgThreadID;
104 * Type returned after running a thread. Values of this type
105 * include HeapOverflow, StackOverflow etc. See Constants.h for the
108 typedef unsigned int StgThreadReturnCode;
111 * We distinguish between the various classes of threads in the system.
120 #if defined(mingw32_TARGET_OS)
121 /* results from an async I/O request + it's ID. */
133 #if defined(mingw32_TARGET_OS)
134 StgAsyncIOResult* async_result;
140 * TSOs live on the heap, and therefore look just like heap objects.
141 * Large TSOs will live in their own "block group" allocated by the
142 * storage manager, and won't be copied during garbage collection.
146 * Threads may be blocked for several reasons. A blocked thread will
147 * have the reason in the why_blocked field of the TSO, and some
148 * further info (such as the closure the thread is blocked on, or the
149 * file descriptor if the thread is waiting on I/O) in the block_info
154 * ToDo: make this structure sensible on a non-32-bit arch.
157 typedef struct StgTSO_ {
160 struct StgTSO_* link; // Links threads onto blocking queues */
161 StgMutClosure * mut_link; // TSO's are mutable of course! */
162 struct StgTSO_* global_link; // Links all threads together */
164 StgWord16 what_next; // Values defined in Constants.h
165 StgWord16 why_blocked; // Values defined in Constants.h
166 StgTSOBlockInfo block_info;
167 struct StgTSO_* blocked_exceptions;
172 MAYBE_EMPTY_STRUCT(StgTSOTickyInfo,ticky)
178 MAYBE_EMPTY_STRUCT(StgTSOParInfo,par);
179 MAYBE_EMPTY_STRUCT(StgTSOGranInfo,gran);
180 MAYBE_EMPTY_STRUCT(StgTSODistInfo,dist);
182 /* The thread stack... */
183 StgWord stack_size; /* stack size in *words* */
184 StgWord max_stack_size; /* maximum stack size in *words* */
187 StgWord stack[FLEXIBLE_ARRAY];
190 /* -----------------------------------------------------------------------------
193 An active thread has the following properties:
195 tso->stack < tso->sp < tso->stack+tso->stack_size
196 tso->stack_size <= tso->max_stack_size
198 RESERVED_STACK_WORDS is large enough for any heap-check or
201 The size of the TSO struct plus the stack is either
202 (a) smaller than a block, or
203 (b) a multiple of BLOCK_SIZE
205 tso->why_blocked tso->block_info location
206 ----------------------------------------------------------------------
207 NotBlocked NULL runnable_queue, or running
209 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
211 BlockedOnMVar the MVAR the MVAR's queue
213 BlockedOnException the TSO TSO->blocked_exception
215 BlockedOnRead NULL blocked_queue
216 BlockedOnWrite NULL blocked_queue
217 BlockedOnDelay NULL blocked_queue
218 BlockedOnGA closure TSO blocks on BQ of that closure
219 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
221 tso->link == END_TSO_QUEUE, if the thread is currently running.
223 A zombie thread has the following properties:
225 tso->what_next == ThreadComplete or ThreadKilled
226 tso->link == (could be on some queue somewhere)
227 tso->su == tso->stack + tso->stack_size
228 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
229 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
230 exception , if what_next == ThreadKilled
232 (tso->sp is left pointing at the top word on the stack so that
233 the return value or exception will be retained by a GC).
235 tso->blocked_exceptions is either:
237 NULL if async exceptions are unblocked.
239 END_TSO_QUEUE if async exceptions are blocked, but no threads
240 are currently waiting to deliver.
242 (StgTSO *)tso if threads are currently awaiting delivery of
243 exceptions to this thread.
245 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
246 setup only. They mark a TSO that has entered a FETCH_ME or
247 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
248 closure will send a Fetch message.
249 Currently we have no separate code for blocking on an RBH; we use the
250 BlockedOnBlackHole case for that. -- HWL
252 ---------------------------------------------------------------------------- */
254 /* Workaround for a bug/quirk in gcc on certain architectures.
255 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
256 * in other words, gcc pads the structure at the end.
259 extern StgTSO dummy_tso;
261 #define TSO_STRUCT_SIZE \
262 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
264 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
267 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
268 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
270 #if defined(PAR) || defined(GRAN)
271 /* this is the NIL ptr for a blocking queue */
272 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
273 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
274 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
276 /* ToDo?: different name for end of sleeping queue ? -- HWL */