1 /* -----------------------------------------------------------------------------
2 * $Id: TSO.h,v 1.13 2000/03/17 13:30:23 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * The definitions for Thread State Objects.
8 * ---------------------------------------------------------------------------*/
13 #if defined(GRAN) || defined(PAR)
14 // magic marker for TSOs; debugging only
15 #define TSO_MAGIC 4321
37 #if defined(PROFILING)
39 CostCentreStack *CCCS; /* thread's current CCS */
41 #else /* !PROFILING */
44 #endif /* PROFILING */
47 typedef StgTSOStatBuf StgTSOParInfo;
54 typedef StgTSOStatBuf StgTSOGranInfo;
64 #else /* !TICKY_TICKY */
67 #endif /* TICKY_TICKY */
75 * The what_next field of a TSO indicates how the thread is to be run.
78 ThreadEnterGHC, /* enter top thunk on stack */
79 ThreadRunGHC, /* return to address on top of stack */
80 ThreadEnterHugs, /* enter top thunk on stack (w/ interpreter) */
81 ThreadKilled, /* thread has died, don't run it */
82 ThreadRelocated, /* thread has moved, link points to new locn */
83 ThreadComplete /* thread has finished */
87 * We are completely paranoid and make thread IDs 64 bits to avoid
88 * having to worry about overflow. A little calculation shows that
89 * even doing 10^6 forks per second would take 35 million years to
90 * overflow a 64 bit thread ID :-)
93 typedef StgWord32 StgThreadID;
96 * This type is returned to the scheduler by a thread that has
97 * stopped for one reason or another.
101 HeapOverflow, /* might also be StackOverflow */
106 } StgThreadReturnCode;
109 * Threads may be blocked for several reasons. A blocked thread will
110 * have the reason in the why_blocked field of the TSO, and some
111 * further info (such as the closure the thread is blocked on, or the
112 * file descriptor if the thread is waiting on I/O) in the block_info
125 , BlockedOnGA // blocked on a remote closure represented by a Global Address
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.
145 typedef struct StgTSO_ {
148 struct StgTSO_* link; /* Links threads onto blocking queues */
149 StgMutClosure * mut_link; /* TSO's are mutable of course! */
150 struct StgTSO_* global_link; /* Links all threads together */
152 StgTSOWhatNext what_next;
153 StgTSOBlockReason why_blocked;
154 StgTSOBlockInfo block_info;
155 struct StgTSO_* blocked_exceptions;
157 StgTSOTickyInfo ticky;
162 /* The thread stack... */
163 StgWord stack_size; /* stack size in *words* */
164 StgWord max_stack_size; /* maximum stack size in *words* */
172 /* -----------------------------------------------------------------------------
175 An active thread has the following properties:
177 tso->stack < tso->sp < tso->stack+tso->stack_size
178 tso->stack_size <= tso->max_stack_size
179 tso->splim == tso->stack + RESERVED_STACK_WORDS;
181 RESERVED_STACK_WORDS is large enough for any heap-check or
184 The size of the TSO struct plus the stack is either
185 (a) smaller than a block, or
186 (b) a multiple of BLOCK_SIZE
188 tso->block_reason tso->block_info location
189 ----------------------------------------------------------------------
190 NotBlocked NULL runnable_queue, or running
192 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
194 BlockedOnMVar the MVAR the MVAR's queue
196 BlockedOnException the TSO TSO->blocked_exception
198 BlockedOnRead NULL blocked_queue
199 BlockedOnWrite NULL blocked_queue
200 BlockedOnDelay NULL blocked_queue
202 tso->link == END_TSO_QUEUE, if the thread is currently running.
204 A zombie thread has the following properties:
206 tso->what_next == ThreadComplete or ThreadKilled
207 tso->link == (could be on some queue somewhere)
208 tso->su == tso->stack + tso->stack_size
209 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
210 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
211 exception , if what_next == ThreadKilled
213 (tso->sp is left pointing at the top word on the stack so that
214 the return value or exception will be retained by a GC).
216 tso->blocked_exceptions is either:
218 NULL if async exceptions are unblocked.
220 END_TSO_QUEUE if async exceptions are blocked, but no threads
221 are currently waiting to deliver.
223 (StgTSO *)tso if threads are currently awaiting delivery of
224 exceptions to this thread.
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 ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
238 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))