1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * The definitions for Thread State Objects.
7 * ---------------------------------------------------------------------------*/
13 #define TSO_MAGIC 4321
36 * PROFILING info in a TSO
39 CostCentreStack *CCCS; /* thread's current CCS */
43 * There is no TICKY info in a TSO at this time.
47 * Thread IDs are 32 bits.
49 typedef StgWord32 StgThreadID;
51 #define tsoDirty(tso) ((tso)->flags & TSO_DIRTY)
52 #define tsoLocked(tso) ((tso)->flags & TSO_LOCKED)
55 * Type returned after running a thread. Values of this type
56 * include HeapOverflow, StackOverflow etc. See Constants.h for the
59 typedef unsigned int StgThreadReturnCode;
61 #if defined(mingw32_HOST_OS)
62 /* results from an async I/O request + its request ID. */
73 StgInt fd; /* StgInt instead of int, so that it's the same size as the ptrs */
74 #if defined(mingw32_HOST_OS)
75 StgAsyncIOResult *async_result;
82 * TSOs live on the heap, and therefore look just like heap objects.
83 * Large TSOs will live in their own "block group" allocated by the
84 * storage manager, and won't be copied during garbage collection.
88 * Threads may be blocked for several reasons. A blocked thread will
89 * have the reason in the why_blocked field of the TSO, and some
90 * further info (such as the closure the thread is blocked on, or the
91 * file descriptor if the thread is waiting on I/O) in the block_info
95 typedef struct StgTSO_ {
98 /* The link field, for linking threads together in lists (e.g. the
99 run queue on a Capability.
101 struct StgTSO_* _link;
103 NOTE!!! do not modify _link directly, it is subject to
104 a write barrier for generational GC. Instead use the
105 setTSOLink() function. Exceptions to this rule are:
107 * setting the link field to END_TSO_QUEUE
108 * putting a TSO on the blackhole_queue
109 * setting the link field of the currently running TSO, as it
110 will already be dirty.
113 struct StgTSO_* global_link; /* Links all threads together */
115 StgWord16 what_next; /* Values defined in Constants.h */
116 StgWord16 why_blocked; /* Values defined in Constants.h */
118 StgTSOBlockInfo block_info;
122 struct Capability_* cap;
123 struct StgTRecHeader_ * trec; /* STM transaction record */
126 A list of threads blocked on this TSO waiting to throw
127 exceptions. In order to access this field, the TSO must be
128 locked using lockClosure/unlockClosure (see SMP.h).
130 struct StgTSO_ * blocked_exceptions;
133 /* TICKY-specific stuff would go here. */
138 #ifdef mingw32_HOST_OS
139 StgWord32 saved_winerror;
142 /* The thread stack... */
143 StgWord32 stack_size; /* stack size in *words* */
144 StgWord32 max_stack_size; /* maximum stack size in *words* */
147 StgWord stack[FLEXIBLE_ARRAY];
150 /* -----------------------------------------------------------------------------
152 -------------------------------------------------------------------------- */
154 extern void dirty_TSO (Capability *cap, StgTSO *tso);
155 extern void setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target);
157 /* -----------------------------------------------------------------------------
160 An active thread has the following properties:
162 tso->stack < tso->sp < tso->stack+tso->stack_size
163 tso->stack_size <= tso->max_stack_size
165 RESERVED_STACK_WORDS is large enough for any heap-check or
168 The size of the TSO struct plus the stack is either
169 (a) smaller than a block, or
170 (b) a multiple of BLOCK_SIZE
172 tso->why_blocked tso->block_info location
173 ----------------------------------------------------------------------
174 NotBlocked NULL runnable_queue, or running
176 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
178 BlockedOnMVar the MVAR the MVAR's queue
180 BlockedOnSTM END_TSO_QUEUE STM wait queue(s)
182 BlockedOnException the TSO TSO->blocked_exception
184 BlockedOnRead NULL blocked_queue
185 BlockedOnWrite NULL blocked_queue
186 BlockedOnDelay NULL blocked_queue
187 BlockedOnGA closure TSO blocks on BQ of that closure
188 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
190 tso->link == END_TSO_QUEUE, if the thread is currently running.
192 A zombie thread has the following properties:
194 tso->what_next == ThreadComplete or ThreadKilled
195 tso->link == (could be on some queue somewhere)
196 tso->su == tso->stack + tso->stack_size
197 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
198 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
199 exception , if what_next == ThreadKilled
201 (tso->sp is left pointing at the top word on the stack so that
202 the return value or exception will be retained by a GC).
204 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
205 setup only. They mark a TSO that has entered a FETCH_ME or
206 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
207 closure will send a Fetch message.
208 Currently we have no separate code for blocking on an RBH; we use the
209 BlockedOnBlackHole case for that. -- HWL
211 ---------------------------------------------------------------------------- */
213 /* Workaround for a bug/quirk in gcc on certain architectures.
214 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
215 * in other words, gcc pads the structure at the end.
218 extern StgTSO dummy_tso;
220 #define TSO_STRUCT_SIZE \
221 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
223 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
225 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
226 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)