1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.19 2000/12/14 15:19:47 sewardj Exp $
4 * (c) The GHC Team, 1998-1999
6 * The definitions for Thread State Objects.
8 * ---------------------------------------------------------------------------*/
13 #if defined(GRAN) || defined(PAR)
15 #if DEBUG // && PARANOIA_LEVEL>999
16 // magic marker for TSOs; debugging only
17 #define TSO_MAGIC 4321
40 #if defined(PROFILING)
42 CostCentreStack *CCCS; /* thread's current CCS */
44 #else /* !PROFILING */
47 #endif /* PROFILING */
50 typedef StgTSOStatBuf StgTSOParInfo;
57 typedef StgTSOStatBuf StgTSOGranInfo;
67 #else /* !TICKY_TICKY */
70 #endif /* TICKY_TICKY */
78 * The what_next field of a TSO indicates how the thread is to be run.
81 ThreadEnterGHC, /* enter top thunk on stack */
82 ThreadRunGHC, /* return to address on top of stack */
83 ThreadEnterInterp, /* enter top thunk on stack (w/ interpreter) */
84 ThreadKilled, /* thread has died, don't run it */
85 ThreadRelocated, /* thread has moved, link points to new locn */
86 ThreadComplete /* thread has finished */
90 * We are completely paranoid and make thread IDs 64 bits to avoid
91 * having to worry about overflow. A little calculation shows that
92 * even doing 10^6 forks per second would take 35 million years to
93 * overflow a 64 bit thread ID :-)
96 typedef StgWord32 StgThreadID;
99 * This type is returned to the scheduler by a thread that has
100 * stopped for one reason or another.
104 HeapOverflow, /* might also be StackOverflow */
109 } StgThreadReturnCode;
112 * Threads may be blocked for several reasons. A blocked thread will
113 * have the reason in the why_blocked field of the TSO, and some
114 * further info (such as the closure the thread is blocked on, or the
115 * file descriptor if the thread is waiting on I/O) in the block_info
128 , BlockedOnGA // blocked on a remote closure represented by a Global Address
129 , BlockedOnGA_NoSend // same as above but without sending a Fetch message
141 * TSOs live on the heap, and therefore look just like heap objects.
142 * Large TSOs will live in their own "block group" allocated by the
143 * storage manager, and won't be copied during garbage collection.
147 * ToDo: make this structure sensible on a non-32-bit arch.
150 typedef struct StgTSO_ {
153 struct StgTSO_* link; /* Links threads onto blocking queues */
154 StgMutClosure * mut_link; /* TSO's are mutable of course! */
155 struct StgTSO_* global_link; /* Links all threads together */
157 StgTSOWhatNext what_next : 16;
158 StgTSOBlockReason why_blocked : 16;
159 StgTSOBlockInfo block_info;
160 struct StgTSO_* blocked_exceptions;
163 StgTSOTickyInfo ticky;
168 /* The thread stack... */
169 StgWord stack_size; /* stack size in *words* */
170 StgWord max_stack_size; /* maximum stack size in *words* */
177 /* -----------------------------------------------------------------------------
180 An active thread has the following properties:
182 tso->stack < tso->sp < tso->stack+tso->stack_size
183 tso->stack_size <= tso->max_stack_size
185 RESERVED_STACK_WORDS is large enough for any heap-check or
188 The size of the TSO struct plus the stack is either
189 (a) smaller than a block, or
190 (b) a multiple of BLOCK_SIZE
192 tso->why_blocked tso->block_info location
193 ----------------------------------------------------------------------
194 NotBlocked NULL runnable_queue, or running
196 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
198 BlockedOnMVar the MVAR the MVAR's queue
200 BlockedOnException the TSO TSO->blocked_exception
202 BlockedOnRead NULL blocked_queue
203 BlockedOnWrite NULL blocked_queue
204 BlockedOnDelay NULL blocked_queue
205 BlockedOnGA closure TSO blocks on BQ of that closure
206 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
208 tso->link == END_TSO_QUEUE, if the thread is currently running.
210 A zombie thread has the following properties:
212 tso->what_next == ThreadComplete or ThreadKilled
213 tso->link == (could be on some queue somewhere)
214 tso->su == tso->stack + tso->stack_size
215 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
216 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
217 exception , if what_next == ThreadKilled
219 (tso->sp is left pointing at the top word on the stack so that
220 the return value or exception will be retained by a GC).
222 tso->blocked_exceptions is either:
224 NULL if async exceptions are unblocked.
226 END_TSO_QUEUE if async exceptions are blocked, but no threads
227 are currently waiting to deliver.
229 (StgTSO *)tso if threads are currently awaiting delivery of
230 exceptions to this thread.
232 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
233 setup only. They mark a TSO that has entered a FETCH_ME or
234 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
235 closure will send a Fetch message.
236 Currently we have no separate code for blocking on an RBH; we use the
237 BlockedOnBlackHole case for that. -- HWL
239 ---------------------------------------------------------------------------- */
241 /* Workaround for a bug/quirk in gcc on certain architectures.
242 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
243 * in other words, gcc pads the structure at the end.
246 extern StgTSO dummy_tso;
248 #define TSO_STRUCT_SIZE \
249 ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
251 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))