1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * The definitions for Thread State Objects.
7 * ---------------------------------------------------------------------------*/
13 #define TSO_MAGIC 4321
36 * GRAN: We distinguish between the various classes of threads in
46 * PROFILING info in a TSO
49 CostCentreStack *CCCS; /* thread's current CCS */
55 typedef StgTSOStatBuf StgTSOParInfo;
61 StgThreadPriority priority;
62 StgInt revalTid; /* ToDo: merge both into 1 word */
69 typedef StgTSOStatBuf StgTSOGranInfo;
72 * There is no TICKY info in a TSO at this time.
76 * Thread IDs are 32 bits.
78 typedef StgWord32 StgThreadID;
81 * Type returned after running a thread. Values of this type
82 * include HeapOverflow, StackOverflow etc. See Constants.h for the
85 typedef unsigned int StgThreadReturnCode;
87 #if defined(mingw32_HOST_OS)
88 /* results from an async I/O request + its request ID. */
99 StgInt fd; /* StgInt instead of int, so that it's the same size as the ptrs */
100 #if defined(mingw32_HOST_OS)
101 StgAsyncIOResult *async_result;
107 * TSOs live on the heap, and therefore look just like heap objects.
108 * Large TSOs will live in their own "block group" allocated by the
109 * storage manager, and won't be copied during garbage collection.
113 * Threads may be blocked for several reasons. A blocked thread will
114 * have the reason in the why_blocked field of the TSO, and some
115 * further info (such as the closure the thread is blocked on, or the
116 * file descriptor if the thread is waiting on I/O) in the block_info
121 * ToDo: make this structure sensible on a non-32-bit arch.
124 typedef struct StgTSO_ {
127 struct StgTSO_* link; /* Links threads onto blocking queues */
128 struct StgTSO_* global_link; /* Links all threads together */
130 StgWord16 what_next; /* Values defined in Constants.h */
131 StgWord16 why_blocked; /* Values defined in Constants.h */
132 StgTSOBlockInfo block_info;
133 struct StgTSO_* blocked_exceptions;
136 struct Task_* bound; // non-NULL for a bound thread
137 struct StgTRecHeader_ *trec; /* STM transaction record */
140 /* TICKY-specific stuff would go here. */
155 /* The thread stack... */
156 StgWord stack_size; /* stack size in *words* */
157 StgWord max_stack_size; /* maximum stack size in *words* */
160 StgWord stack[FLEXIBLE_ARRAY];
163 /* -----------------------------------------------------------------------------
166 An active thread has the following properties:
168 tso->stack < tso->sp < tso->stack+tso->stack_size
169 tso->stack_size <= tso->max_stack_size
171 RESERVED_STACK_WORDS is large enough for any heap-check or
174 The size of the TSO struct plus the stack is either
175 (a) smaller than a block, or
176 (b) a multiple of BLOCK_SIZE
178 tso->why_blocked tso->block_info location
179 ----------------------------------------------------------------------
180 NotBlocked NULL runnable_queue, or running
182 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
184 BlockedOnMVar the MVAR the MVAR's queue
186 BlockedOnSTM END_TSO_QUEUE STM wait queue(s)
188 BlockedOnException the TSO TSO->blocked_exception
190 BlockedOnRead NULL blocked_queue
191 BlockedOnWrite NULL blocked_queue
192 BlockedOnDelay NULL blocked_queue
193 BlockedOnGA closure TSO blocks on BQ of that closure
194 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
196 tso->link == END_TSO_QUEUE, if the thread is currently running.
198 A zombie thread has the following properties:
200 tso->what_next == ThreadComplete or ThreadKilled
201 tso->link == (could be on some queue somewhere)
202 tso->su == tso->stack + tso->stack_size
203 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
204 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
205 exception , if what_next == ThreadKilled
207 (tso->sp is left pointing at the top word on the stack so that
208 the return value or exception will be retained by a GC).
210 tso->blocked_exceptions is either:
212 NULL if async exceptions are unblocked.
214 END_TSO_QUEUE if async exceptions are blocked, but no threads
215 are currently waiting to deliver.
217 (StgTSO *)tso if threads are currently awaiting delivery of
218 exceptions to this thread.
220 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
221 setup only. They mark a TSO that has entered a FETCH_ME or
222 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
223 closure will send a Fetch message.
224 Currently we have no separate code for blocking on an RBH; we use the
225 BlockedOnBlackHole case for that. -- HWL
227 ---------------------------------------------------------------------------- */
229 /* Workaround for a bug/quirk in gcc on certain architectures.
230 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
231 * in other words, gcc pads the structure at the end.
234 extern StgTSO dummy_tso;
236 #define TSO_STRUCT_SIZE \
237 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
239 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
242 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
244 #define END_TSO_QUEUE (stg_END_TSO_QUEUE_closure)
246 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
249 #if defined(PAR) || defined(GRAN)
250 /* this is the NIL ptr for a blocking queue */
251 # define END_BQ_QUEUE ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
252 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
253 # define END_BF_QUEUE ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
255 /* ToDo?: different name for end of sleeping queue ? -- HWL */