1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2009
5 * The definitions for Thread State Objects.
7 * ---------------------------------------------------------------------------*/
9 #ifndef RTS_STORAGE_TSO_H
10 #define RTS_STORAGE_TSO_H
13 * PROFILING info in a TSO
16 CostCentreStack *CCCS; /* thread's current CCS */
20 * There is no TICKY info in a TSO at this time.
24 * Thread IDs are 32 bits.
26 typedef StgWord32 StgThreadID;
28 #define tsoDirty(tso) ((tso)->flags & TSO_DIRTY)
29 #define tsoLocked(tso) ((tso)->flags & TSO_LOCKED)
32 * Type returned after running a thread. Values of this type
33 * include HeapOverflow, StackOverflow etc. See Constants.h for the
36 typedef unsigned int StgThreadReturnCode;
38 #if defined(mingw32_HOST_OS)
39 /* results from an async I/O request + its request ID. */
47 /* Reason for thread being blocked. See comment above struct StgTso_. */
51 StgInt fd; /* StgInt instead of int, so that it's the same size as the ptrs */
52 #if defined(mingw32_HOST_OS)
53 StgAsyncIOResult *async_result;
60 * TSOs live on the heap, and therefore look just like heap objects.
61 * Large TSOs will live in their own "block group" allocated by the
62 * storage manager, and won't be copied during garbage collection.
66 * Threads may be blocked for several reasons. A blocked thread will
67 * have the reason in the why_blocked field of the TSO, and some
68 * further info (such as the closure the thread is blocked on, or the
69 * file descriptor if the thread is waiting on I/O) in the block_info
73 typedef struct StgTSO_ {
76 /* The link field, for linking threads together in lists (e.g. the
77 run queue on a Capability.
79 struct StgTSO_* _link;
81 NOTE!!! do not modify _link directly, it is subject to
82 a write barrier for generational GC. Instead use the
83 setTSOLink() function. Exceptions to this rule are:
85 * setting the link field to END_TSO_QUEUE
86 * putting a TSO on the blackhole_queue
87 * setting the link field of the currently running TSO, as it
88 will already be dirty.
91 struct StgTSO_* global_link; /* Links all threads together */
93 StgWord16 what_next; /* Values defined in Constants.h */
94 StgWord16 why_blocked; /* Values defined in Constants.h */
96 StgTSOBlockInfo block_info;
100 struct Capability_* cap;
101 struct StgTRecHeader_ * trec; /* STM transaction record */
104 A list of threads blocked on this TSO waiting to throw
105 exceptions. In order to access this field, the TSO must be
106 locked using lockClosure/unlockClosure (see SMP.h).
108 struct StgTSO_ * blocked_exceptions;
111 /* TICKY-specific stuff would go here. */
116 #ifdef mingw32_HOST_OS
117 StgWord32 saved_winerror;
120 /* The thread stack... */
121 StgWord32 stack_size; /* stack size in *words* */
122 StgWord32 max_stack_size; /* maximum stack size in *words* */
125 StgWord stack[FLEXIBLE_ARRAY];
128 /* -----------------------------------------------------------------------------
130 -------------------------------------------------------------------------- */
132 void dirty_TSO (Capability *cap, StgTSO *tso);
133 void setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target);
135 /* -----------------------------------------------------------------------------
138 An active thread has the following properties:
140 tso->stack < tso->sp < tso->stack+tso->stack_size
141 tso->stack_size <= tso->max_stack_size
143 RESERVED_STACK_WORDS is large enough for any heap-check or
146 The size of the TSO struct plus the stack is either
147 (a) smaller than a block, or
148 (b) a multiple of BLOCK_SIZE
150 tso->why_blocked tso->block_info location
151 ----------------------------------------------------------------------
152 NotBlocked NULL runnable_queue, or running
154 BlockedOnBlackHole the BLACKHOLE blackhole_queue
156 BlockedOnMVar the MVAR the MVAR's queue
158 BlockedOnSTM END_TSO_QUEUE STM wait queue(s)
160 BlockedOnException the TSO TSO->blocked_exception
162 BlockedOnRead NULL blocked_queue
163 BlockedOnWrite NULL blocked_queue
164 BlockedOnDelay NULL blocked_queue
165 BlockedOnGA closure TSO blocks on BQ of that closure
166 BlockedOnGA_NoSend closure TSO blocks on BQ of that closure
168 tso->link == END_TSO_QUEUE, if the thread is currently running.
170 A zombie thread has the following properties:
172 tso->what_next == ThreadComplete or ThreadKilled
173 tso->link == (could be on some queue somewhere)
174 tso->su == tso->stack + tso->stack_size
175 tso->sp == tso->stack + tso->stack_size - 1 (i.e. top stack word)
176 tso->sp[0] == return value of thread, if what_next == ThreadComplete,
177 exception , if what_next == ThreadKilled
179 (tso->sp is left pointing at the top word on the stack so that
180 the return value or exception will be retained by a GC).
182 The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
183 setup only. They mark a TSO that has entered a FETCH_ME or
184 FETCH_ME_BQ closure, respectively; only the first TSO hitting the
185 closure will send a Fetch message.
186 Currently we have no separate code for blocking on an RBH; we use the
187 BlockedOnBlackHole case for that. -- HWL
189 ---------------------------------------------------------------------------- */
191 /* Workaround for a bug/quirk in gcc on certain architectures.
192 * symptom is that (&tso->stack - &tso->header) /= sizeof(StgTSO)
193 * in other words, gcc pads the structure at the end.
196 extern StgTSO dummy_tso;
198 #define TSO_STRUCT_SIZE \
199 ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
201 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
203 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
204 #define END_TSO_QUEUE ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
206 #endif /* RTS_STORAGE_TSO_H */