14c47abed4ea4739c993b06273459bc5caa74d2a
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-1999
4  *
5  * The definitions for Thread State Objects.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef TSO_H
10 #define TSO_H
11
12 #if DEBUG
13 #define TSO_MAGIC 4321
14 #endif
15
16 typedef struct {
17   StgInt   pri;
18   StgInt   magic;
19   StgInt   sparkname;
20   rtsTime  startedat;
21   rtsBool  exported;
22   StgInt   basicblocks;
23   StgInt   allocs;
24   rtsTime  exectime;
25   rtsTime  fetchtime;
26   rtsTime  fetchcount;
27   rtsTime  blocktime;
28   StgInt   blockcount;
29   rtsTime  blockedat;
30   StgInt   globalsparks;
31   StgInt   localsparks;
32   rtsTime  clock;
33 } StgTSOStatBuf;
34
35 /*
36  * GRAN: We distinguish between the various classes of threads in 
37  * the system.
38  */
39 typedef enum {
40   AdvisoryPriority,
41   MandatoryPriority,
42   RevalPriority
43 } StgThreadPriority;
44
45 /*
46  * PROFILING info in a TSO
47  */
48 typedef struct {
49   CostCentreStack *CCCS;        /* thread's current CCS */
50 } StgTSOProfInfo;
51
52 /*
53  * PAR info in a TSO
54  */
55 typedef StgTSOStatBuf StgTSOParInfo;
56
57 /*
58  * DIST info in a TSO
59  */
60 typedef struct {
61   StgThreadPriority  priority;   
62   StgInt             revalTid;   /* ToDo: merge both into 1 word */
63   StgInt             revalSlot;
64 } StgTSODistInfo;
65
66 /*
67  * GRAN info in a TSO
68  */
69 typedef StgTSOStatBuf StgTSOGranInfo;
70
71 /*
72  * There is no TICKY info in a TSO at this time.
73  */
74
75 /*
76  * Thread IDs are 32 bits.
77  */
78 typedef StgWord32 StgThreadID;
79
80 /* 
81  * Flags for the tso->flags field.
82  *
83  * The TSO_DIRTY flag indicates that this TSO's stack should be
84  * scanned during garbage collection.  The link field of a TSO is
85  * always scanned, so we don't have to dirty a TSO just for linking
86  * it on a different list.
87  *
88  * TSO_DIRTY is set by 
89  *    - schedule(), just before running a thread,
90  *    - raiseAsync(), because it modifies a thread's stack
91  *    - resumeThread(), just before running the thread again
92  * and unset by the garbage collector (only).
93  */
94 #define TSO_DIRTY   1
95
96 #define tsoDirty(tso) ((tso)->flags & TSO_DIRTY)
97
98 /*
99  * Type returned after running a thread.  Values of this type
100  * include HeapOverflow, StackOverflow etc.  See Constants.h for the
101  * full list.
102  */
103 typedef unsigned int StgThreadReturnCode;
104
105 #if defined(mingw32_HOST_OS)
106 /* results from an async I/O request + its request ID. */
107 typedef struct {
108   unsigned int reqID;
109   int          len;
110   int          errCode;
111 } StgAsyncIOResult;
112 #endif
113
114 typedef union {
115   StgClosure *closure;
116   struct StgTSO_ *tso;
117   StgInt fd;    /* StgInt instead of int, so that it's the same size as the ptrs */
118 #if defined(mingw32_HOST_OS)
119   StgAsyncIOResult *async_result;
120 #endif
121   StgWord target;
122 } StgTSOBlockInfo;
123
124 /*
125  * TSOs live on the heap, and therefore look just like heap objects.
126  * Large TSOs will live in their own "block group" allocated by the
127  * storage manager, and won't be copied during garbage collection.
128  */
129
130 /* 
131  * Threads may be blocked for several reasons.  A blocked thread will
132  * have the reason in the why_blocked field of the TSO, and some
133  * further info (such as the closure the thread is blocked on, or the
134  * file descriptor if the thread is waiting on I/O) in the block_info
135  * field.
136  */
137
138 typedef struct StgTSO_ {
139     StgHeader               header;
140
141     struct StgTSO_*         link;       /* Links threads onto blocking queues */
142     struct StgTSO_*         global_link;    /* Links all threads together */
143     
144     StgWord16               what_next;      /* Values defined in Constants.h */
145     StgWord16               why_blocked;    /* Values defined in Constants.h */
146     StgWord32               flags;
147     StgTSOBlockInfo         block_info;
148     struct StgTSO_*         blocked_exceptions;
149     StgThreadID             id;
150     int                     saved_errno;
151     struct Task_*           bound;
152     struct Capability_*     cap;
153     struct StgTRecHeader_ * trec;       /* STM transaction record */
154
155 #ifdef TICKY_TICKY
156     /* TICKY-specific stuff would go here. */
157 #endif
158 #ifdef PROFILING
159     StgTSOProfInfo prof;
160 #endif
161 #ifdef PAR
162     StgTSOParInfo par;
163 #endif
164 #ifdef GRAN
165     StgTSOGranInfo gran;
166 #endif
167 #ifdef DIST
168     StgTSODistInfo dist;
169 #endif
170
171     /* The thread stack... */
172     StgWord32          stack_size;     /* stack size in *words* */
173     StgWord32          max_stack_size; /* maximum stack size in *words* */
174     StgPtr             sp;
175     
176     StgWord            stack[FLEXIBLE_ARRAY];
177 } StgTSO;
178
179 /* -----------------------------------------------------------------------------
180    Invariants:
181
182    An active thread has the following properties:
183
184       tso->stack < tso->sp < tso->stack+tso->stack_size
185       tso->stack_size <= tso->max_stack_size
186       
187       RESERVED_STACK_WORDS is large enough for any heap-check or
188       stack-check failure.
189
190       The size of the TSO struct plus the stack is either
191         (a) smaller than a block, or
192         (b) a multiple of BLOCK_SIZE
193
194         tso->why_blocked       tso->block_info      location
195         ----------------------------------------------------------------------
196         NotBlocked             NULL                 runnable_queue, or running
197         
198         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
199         
200         BlockedOnMVar          the MVAR             the MVAR's queue
201
202         BlockedOnSTM           END_TSO_QUEUE        STM wait queue(s)
203         
204         BlockedOnException     the TSO              TSO->blocked_exception
205
206         BlockedOnRead          NULL                 blocked_queue
207         BlockedOnWrite         NULL                 blocked_queue
208         BlockedOnDelay         NULL                 blocked_queue
209         BlockedOnGA            closure TSO blocks on   BQ of that closure
210         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
211
212       tso->link == END_TSO_QUEUE, if the thread is currently running.
213
214    A zombie thread has the following properties:
215       
216       tso->what_next == ThreadComplete or ThreadKilled
217       tso->link     ==  (could be on some queue somewhere)
218       tso->su       ==  tso->stack + tso->stack_size
219       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
220       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
221                         exception             , if what_next == ThreadKilled
222
223       (tso->sp is left pointing at the top word on the stack so that
224       the return value or exception will be retained by a GC).
225
226    tso->blocked_exceptions is either:
227
228       NULL             if async exceptions are unblocked.
229
230       END_TSO_QUEUE    if async exceptions are blocked, but no threads
231                        are currently waiting to deliver.
232
233       (StgTSO *)tso    if threads are currently awaiting delivery of
234                        exceptions to this thread.
235
236    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
237    setup only. They mark a TSO that has entered a FETCH_ME or
238    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
239    closure will send a Fetch message.
240    Currently we have no separate code for blocking on an RBH; we use the
241    BlockedOnBlackHole case for that.   -- HWL
242
243  ---------------------------------------------------------------------------- */
244
245 /* Workaround for a bug/quirk in gcc on certain architectures.
246  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
247  * in other words, gcc pads the structure at the end.
248  */
249
250 extern StgTSO dummy_tso;
251
252 #define TSO_STRUCT_SIZE \
253    ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
254
255 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
256
257
258 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
259 #if IN_STG_CODE
260 #define END_TSO_QUEUE  (stg_END_TSO_QUEUE_closure)
261 #else
262 #define END_TSO_QUEUE  ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
263 #endif
264
265 #if defined(PAR) || defined(GRAN)
266 /* this is the NIL ptr for a blocking queue */
267 # define END_BQ_QUEUE  ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
268 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
269 # define END_BF_QUEUE  ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
270 #endif
271 /* ToDo?: different name for end of sleeping queue ? -- HWL */
272
273 #endif /* TSO_H */