1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.17 2000/08/15 14:18:43 simonmar 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 ThreadEnterHugs, /* 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
137 #if defined(HAVE_SETITIMER) || defined(mingw32_TARGET_OS)
145 * TSOs live on the heap, and therefore look just like heap objects.
146 * Large TSOs will live in their own "block group" allocated by the
147 * storage manager, and won't be copied during garbage collection.
151 * ToDo: make this structure sensible on a non-32-bit arch.
154 typedef struct StgTSO_ {
157 struct StgTSO_* link; /* Links threads onto blocking queues */
158 StgMutClosure * mut_link; /* TSO's are mutable of course! */
159 struct StgTSO_* global_link; /* Links all threads together */
161 StgTSOWhatNext what_next : 16;
162 StgTSOBlockReason why_blocked : 16;
163 StgTSOBlockInfo block_info;
164 struct StgTSO_* blocked_exceptions;
167 StgTSOTickyInfo ticky;
172 /* The thread stack... */
173 StgWord stack_size; /* stack size in *words* */
174 StgWord max_stack_size; /* maximum stack size in *words* */
181 /* -----------------------------------------------------------------------------
184 An active thread has the following properties:
186 tso->stack < tso->sp < tso->stack+tso->stack_size
187 tso->stack_size <= tso->max_stack_size
189 RESERVED_STACK_WORDS is large enough for any heap-check or
192 The size of the TSO struct plus the stack is either
193 (a) smaller than a block, or
194 (b) a multiple of BLOCK_SIZE
196 tso->why_blocked tso->block_info location
197 ----------------------------------------------------------------------
198 NotBlocked NULL runnable_queue, or running
200 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
202 BlockedOnMVar the MVAR the MVAR's queue
204 BlockedOnException the TSO TSO->blocked_exception
206 BlockedOnRead NULL blocked_queue
207 BlockedOnWrite NULL blocked_queue
208 BlockedOnDelay NULL blocked_queue
209 BlockedOnGA closure TSO blocks on BQ of that closure
210 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
212 tso->link == END_TSO_QUEUE, if the thread is currently running.
214 A zombie thread has the following properties:
216 tso->what_next == ThreadComplete or ThreadKilled
217 tso->link == (could be on some queue somewhere)
218 tso->su == tso->stack + tso->stack_size
219 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
220 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
221 exception , if what_next == ThreadKilled
223 (tso->sp is left pointing at the top word on the stack so that
224 the return value or exception will be retained by a GC).
226 tso->blocked_exceptions is either:
228 NULL if async exceptions are unblocked.
230 END_TSO_QUEUE if async exceptions are blocked, but no threads
231 are currently waiting to deliver.
233 (StgTSO *)tso if threads are currently awaiting delivery of
234 exceptions to this thread.
236 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
237 setup only. They mark a TSO that has entered a FETCH_ME or
238 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
239 closure will send a Fetch message.
240 Currently we have no separate code for blocking on an RBH; we use the
241 BlockedOnBlackHole case for that. -- HWL
243 ---------------------------------------------------------------------------- */
245 /* Workaround for a bug/quirk in gcc on certain architectures.
246 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
247 * in other words, gcc pads the structure at the end.
250 extern StgTSO dummy_tso;
252 #define TSO_STRUCT_SIZE \
253 ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
255 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))