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. */
70 /* Reason for thread being blocked. See comment above struct StgTso_. */
74 StgInt fd; /* StgInt instead of int, so that it's the same size as the ptrs */
75 #if defined(mingw32_HOST_OS)
76 StgAsyncIOResult *async_result;
83 * TSOs live on the heap, and therefore look just like heap objects.
84 * Large TSOs will live in their own "block group" allocated by the
85 * storage manager, and won't be copied during garbage collection.
89 * Threads may be blocked for several reasons. A blocked thread will
90 * have the reason in the why_blocked field of the TSO, and some
91 * further info (such as the closure the thread is blocked on, or the
92 * file descriptor if the thread is waiting on I/O) in the block_info
96 typedef struct StgTSO_ {
99 /* The link field, for linking threads together in lists (e.g. the
100 run queue on a Capability.
102 struct StgTSO_* _link;
104 NOTE!!! do not modify _link directly, it is subject to
105 a write barrier for generational GC. Instead use the
106 setTSOLink() function. Exceptions to this rule are:
108 * setting the link field to END_TSO_QUEUE
109 * putting a TSO on the blackhole_queue
110 * setting the link field of the currently running TSO, as it
111 will already be dirty.
114 struct StgTSO_* global_link; /* Links all threads together */
116 StgWord16 what_next; /* Values defined in Constants.h */
117 StgWord16 why_blocked; /* Values defined in Constants.h */
119 StgTSOBlockInfo block_info;
123 struct Capability_* cap;
124 struct StgTRecHeader_ * trec; /* STM transaction record */
127 A list of threads blocked on this TSO waiting to throw
128 exceptions. In order to access this field, the TSO must be
129 locked using lockClosure/unlockClosure (see SMP.h).
131 struct StgTSO_ * blocked_exceptions;
134 /* TICKY-specific stuff would go here. */
139 #ifdef mingw32_HOST_OS
140 StgWord32 saved_winerror;
143 /* The thread stack... */
144 StgWord32 stack_size; /* stack size in *words* */
145 StgWord32 max_stack_size; /* maximum stack size in *words* */
148 StgWord stack[FLEXIBLE_ARRAY];
151 /* -----------------------------------------------------------------------------
153 -------------------------------------------------------------------------- */
155 extern void dirty_TSO (Capability *cap, StgTSO *tso);
156 extern void setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target);
158 /* -----------------------------------------------------------------------------
161 An active thread has the following properties:
163 tso->stack < tso->sp < tso->stack+tso->stack_size
164 tso->stack_size <= tso->max_stack_size
166 RESERVED_STACK_WORDS is large enough for any heap-check or
169 The size of the TSO struct plus the stack is either
170 (a) smaller than a block, or
171 (b) a multiple of BLOCK_SIZE
173 tso->why_blocked tso->block_info location
174 ----------------------------------------------------------------------
175 NotBlocked NULL runnable_queue, or running
177 BlockedOnBlackHole the BLACKHOLE_BQ the BLACKHOLE_BQ's queue
179 BlockedOnMVar the MVAR the MVAR's queue
181 BlockedOnSTM END_TSO_QUEUE STM wait queue(s)
183 BlockedOnException the TSO TSO->blocked_exception
185 BlockedOnRead NULL blocked_queue
186 BlockedOnWrite NULL blocked_queue
187 BlockedOnDelay NULL blocked_queue
188 BlockedOnGA closure TSO blocks on BQ of that closure
189 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
191 tso->link == END_TSO_QUEUE, if the thread is currently running.
193 A zombie thread has the following properties:
195 tso->what_next == ThreadComplete or ThreadKilled
196 tso->link == (could be on some queue somewhere)
197 tso->su == tso->stack + tso->stack_size
198 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
199 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
200 exception , if what_next == ThreadKilled
202 (tso->sp is left pointing at the top word on the stack so that
203 the return value or exception will be retained by a GC).
205 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
206 setup only. They mark a TSO that has entered a FETCH_ME or
207 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
208 closure will send a Fetch message.
209 Currently we have no separate code for blocking on an RBH; we use the
210 BlockedOnBlackHole case for that. -- HWL
212 ---------------------------------------------------------------------------- */
214 /* Workaround for a bug/quirk in gcc on certain architectures.
215 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
216 * in other words, gcc pads the structure at the end.
219 extern StgTSO dummy_tso;
221 #define TSO_STRUCT_SIZE \
222 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
224 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
226 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
227 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)