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