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