RTS tidyup sweep, first phase
[ghc-hetmet.git] / includes / rts / storage / TSO.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2009
4  *
5  * The definitions for Thread State Objects.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef RTS_STORAGE_TSO_H
10 #define RTS_STORAGE_TSO_H
11
12 /*
13  * PROFILING info in a TSO
14  */
15 typedef struct {
16   CostCentreStack *CCCS;        /* thread's current CCS */
17 } StgTSOProfInfo;
18
19 /*
20  * There is no TICKY info in a TSO at this time.
21  */
22
23 /*
24  * Thread IDs are 32 bits.
25  */
26 typedef StgWord32 StgThreadID;
27
28 #define tsoDirty(tso)  ((tso)->flags & TSO_DIRTY)
29 #define tsoLocked(tso) ((tso)->flags & TSO_LOCKED)
30
31 /*
32  * Type returned after running a thread.  Values of this type
33  * include HeapOverflow, StackOverflow etc.  See Constants.h for the
34  * full list.
35  */
36 typedef unsigned int StgThreadReturnCode;
37
38 #if defined(mingw32_HOST_OS)
39 /* results from an async I/O request + its request ID. */
40 typedef struct {
41   unsigned int reqID;
42   int          len;
43   int          errCode;
44 } StgAsyncIOResult;
45 #endif
46
47 /* Reason for thread being blocked. See comment above struct StgTso_. */
48 typedef union {
49   StgClosure *closure;
50   struct StgTSO_ *tso;
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;
54 #endif
55   StgWord target;
56 } StgTSOBlockInfo;
57
58
59 /*
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.
63  */
64
65 /*
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
70  * field.
71  */
72
73 typedef struct StgTSO_ {
74     StgHeader               header;
75
76     /* The link field, for linking threads together in lists (e.g. the
77        run queue on a Capability.
78     */
79     struct StgTSO_*         _link;
80     /*
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:
84
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.
89     */
90
91     struct StgTSO_*         global_link;    /* Links all threads together */
92     
93     StgWord16               what_next;      /* Values defined in Constants.h */
94     StgWord16               why_blocked;    /* Values defined in Constants.h */
95     StgWord32               flags;
96     StgTSOBlockInfo         block_info;
97     StgThreadID             id;
98     int                     saved_errno;
99     struct Task_*           bound;
100     struct Capability_*     cap;
101     struct StgTRecHeader_ * trec;       /* STM transaction record */
102
103     /*
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).
107     */
108     struct StgTSO_ *        blocked_exceptions;
109
110 #ifdef TICKY_TICKY
111     /* TICKY-specific stuff would go here. */
112 #endif
113 #ifdef PROFILING
114     StgTSOProfInfo prof;
115 #endif
116 #ifdef mingw32_HOST_OS
117     StgWord32 saved_winerror;
118 #endif
119
120     /* The thread stack... */
121     StgWord32          stack_size;     /* stack size in *words* */
122     StgWord32          max_stack_size; /* maximum stack size in *words* */
123     StgPtr             sp;
124     
125     StgWord            stack[FLEXIBLE_ARRAY];
126 } *StgTSOPtr;
127
128 /* -----------------------------------------------------------------------------
129    functions
130    -------------------------------------------------------------------------- */
131
132 void dirty_TSO  (Capability *cap, StgTSO *tso);
133 void setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target);
134
135 /* -----------------------------------------------------------------------------
136    Invariants:
137
138    An active thread has the following properties:
139
140       tso->stack < tso->sp < tso->stack+tso->stack_size
141       tso->stack_size <= tso->max_stack_size
142       
143       RESERVED_STACK_WORDS is large enough for any heap-check or
144       stack-check failure.
145
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
149
150         tso->why_blocked       tso->block_info      location
151         ----------------------------------------------------------------------
152         NotBlocked             NULL                 runnable_queue, or running
153         
154         BlockedOnBlackHole     the BLACKHOLE        blackhole_queue
155         
156         BlockedOnMVar          the MVAR             the MVAR's queue
157
158         BlockedOnSTM           END_TSO_QUEUE        STM wait queue(s)
159         
160         BlockedOnException     the TSO              TSO->blocked_exception
161
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
167
168       tso->link == END_TSO_QUEUE, if the thread is currently running.
169
170    A zombie thread has the following properties:
171       
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
178
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).
181
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
188
189  ---------------------------------------------------------------------------- */
190
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.
194  */
195
196 extern StgTSO dummy_tso;
197
198 #define TSO_STRUCT_SIZE \
199    ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
200
201 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
202
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)
205
206 #endif /* RTS_STORAGE_TSO_H */