1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.38 2004/11/10 02:13:12 wolfgang Exp $
4 * (c) The GHC Team, 1998-1999
6 * The definitions for Thread State Objects.
8 * ---------------------------------------------------------------------------*/
14 #define TSO_MAGIC 4321
37 * GRAN: We distinguish between the various classes of threads in
47 * PROFILING info in a TSO
50 CostCentreStack *CCCS; /* thread's current CCS */
56 typedef StgTSOStatBuf StgTSOParInfo;
62 StgThreadPriority priority;
63 StgInt revalTid; /* ToDo: merge both into 1 word */
70 typedef StgTSOStatBuf StgTSOGranInfo;
73 * There is no TICKY info in a TSO at this time.
77 * Thread IDs are 32 bits.
79 typedef StgWord32 StgThreadID;
82 * Type returned after running a thread. Values of this type
83 * include HeapOverflow, StackOverflow etc. See Constants.h for the
86 typedef unsigned int StgThreadReturnCode;
88 #if defined(mingw32_TARGET_OS)
89 /* results from an async I/O request + it's ID. */
100 StgInt fd; // StgInt instead of int, so that it's the same size as the ptrs
101 #if defined(mingw32_TARGET_OS)
102 StgAsyncIOResult* async_result;
108 * TSOs live on the heap, and therefore look just like heap objects.
109 * Large TSOs will live in their own "block group" allocated by the
110 * storage manager, and won't be copied during garbage collection.
114 * Threads may be blocked for several reasons. A blocked thread will
115 * have the reason in the why_blocked field of the TSO, and some
116 * further info (such as the closure the thread is blocked on, or the
117 * file descriptor if the thread is waiting on I/O) in the block_info
122 * ToDo: make this structure sensible on a non-32-bit arch.
125 typedef struct StgTSO_ {
128 struct StgTSO_* link; // Links threads onto blocking queues */
129 StgMutClosure * mut_link; // TSO's are mutable of course! */
130 struct StgTSO_* global_link; // Links all threads together */
132 StgWord16 what_next; // Values defined in Constants.h
133 StgWord16 why_blocked; // Values defined in Constants.h
134 StgTSOBlockInfo block_info;
135 struct StgTSO_* blocked_exceptions;
138 struct StgMainThread_* main;
141 // TICKY-specific stuff would go here.
156 /* The thread stack... */
157 StgWord stack_size; /* stack size in *words* */
158 StgWord max_stack_size; /* maximum stack size in *words* */
161 StgWord stack[FLEXIBLE_ARRAY];
164 /* -----------------------------------------------------------------------------
167 An active thread has the following properties:
169 tso->stack < tso->sp < tso->stack+tso->stack_size
170 tso->stack_size <= tso->max_stack_size
172 RESERVED_STACK_WORDS is large enough for any heap-check or
175 The size of the TSO struct plus the stack is either
176 (a) smaller than a block, or
177 (b) a multiple of BLOCK_SIZE
179 tso->why_blocked tso->block_info location
180 ----------------------------------------------------------------------
181 NotBlocked NULL runnable_queue, or running
183 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
185 BlockedOnMVar the MVAR the MVAR's queue
187 BlockedOnException the TSO TSO->blocked_exception
189 BlockedOnRead NULL blocked_queue
190 BlockedOnWrite NULL blocked_queue
191 BlockedOnDelay NULL blocked_queue
192 BlockedOnGA closure TSO blocks on BQ of that closure
193 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
195 tso->link == END_TSO_QUEUE, if the thread is currently running.
197 A zombie thread has the following properties:
199 tso->what_next == ThreadComplete or ThreadKilled
200 tso->link == (could be on some queue somewhere)
201 tso->su == tso->stack + tso->stack_size
202 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
203 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
204 exception , if what_next == ThreadKilled
206 (tso->sp is left pointing at the top word on the stack so that
207 the return value or exception will be retained by a GC).
209 tso->blocked_exceptions is either:
211 NULL if async exceptions are unblocked.
213 END_TSO_QUEUE if async exceptions are blocked, but no threads
214 are currently waiting to deliver.
216 (StgTSO *)tso if threads are currently awaiting delivery of
217 exceptions to this thread.
219 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
220 setup only. They mark a TSO that has entered a FETCH_ME or
221 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
222 closure will send a Fetch message.
223 Currently we have no separate code for blocking on an RBH; we use the
224 BlockedOnBlackHole case for that. -- HWL
226 ---------------------------------------------------------------------------- */
228 /* Workaround for a bug/quirk in gcc on certain architectures.
229 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
230 * in other words, gcc pads the structure at the end.
233 extern StgTSO dummy_tso;
235 #define TSO_STRUCT_SIZE \
236 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
238 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
241 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
242 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
244 #if defined(PAR) || defined(GRAN)
245 /* this is the NIL ptr for a blocking queue */
246 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
247 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
248 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
250 /* ToDo?: different name for end of sleeping queue ? -- HWL */