[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  * $Id: TSO.h,v 1.33 2003/11/12 17:27:05 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 # 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 #if defined(RTS_SUPPORTS_THREADS)
165   , BlockedOnCCall
166   , BlockedOnCCall_NoUnblockExc // same as above but don't unblock async exceptions
167                                 // in resumeThread()
168 #endif
169 } StgTSOBlockReason;
170
171 #if defined(mingw32_TARGET_OS)
172 /* results from an async I/O request + it's ID. */
173 typedef struct {
174   unsigned int reqID;
175   int          len;
176   int          errCode;
177 } StgAsyncIOResult;
178 #endif
179
180 typedef union {
181   StgClosure *closure;
182   struct StgTSO_ *tso;
183   int fd;
184 #if defined(mingw32_TARGET_OS)
185   StgAsyncIOResult* async_result;
186 #endif
187   unsigned int target;
188 } StgTSOBlockInfo;
189
190 /*
191  * TSOs live on the heap, and therefore look just like heap objects.
192  * Large TSOs will live in their own "block group" allocated by the
193  * storage manager, and won't be copied during garbage collection.
194  */
195
196 /* 
197  * ToDo: make this structure sensible on a non-32-bit arch.
198  */
199
200 typedef struct StgTSO_ {
201   StgHeader          header;
202
203   struct StgTSO_*    link;           /* Links threads onto blocking queues */
204   StgMutClosure *    mut_link;       /* TSO's are mutable of course! */
205   struct StgTSO_*    global_link;    /* Links all threads together */
206   
207   StgTSOWhatNext     what_next   : 16;
208   StgTSOBlockReason  why_blocked : 16;
209   StgTSOBlockInfo    block_info;
210   struct StgTSO_*    blocked_exceptions;
211   StgThreadID        id;
212   int                saved_errno;
213   
214   MAYBE_EMPTY_STRUCT(StgTSOTickyInfo,ticky)
215   MAYBE_EMPTY_STRUCT(StgTSOProfInfo,prof)
216   MAYBE_EMPTY_STRUCT(StgTSOParInfo,par)
217   MAYBE_EMPTY_STRUCT(StgTSOGranInfo,gran)
218   MAYBE_EMPTY_STRUCT(StgTSODistInfo,dist)
219     
220   /* The thread stack... */
221   StgWord            stack_size;     /* stack size in *words* */
222   StgWord            max_stack_size; /* maximum stack size in *words* */
223   StgPtr             sp;
224   
225   StgWord            stack[FLEXIBLE_ARRAY];
226 } StgTSO;
227
228 /* -----------------------------------------------------------------------------
229    Invariants:
230
231    An active thread has the following properties:
232
233       tso->stack < tso->sp < tso->stack+tso->stack_size
234       tso->stack_size <= tso->max_stack_size
235       
236       RESERVED_STACK_WORDS is large enough for any heap-check or
237       stack-check failure.
238
239       The size of the TSO struct plus the stack is either
240         (a) smaller than a block, or
241         (b) a multiple of BLOCK_SIZE
242
243         tso->why_blocked       tso->block_info      location
244         ----------------------------------------------------------------------
245         NotBlocked             NULL                 runnable_queue, or running
246         
247         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
248         
249         BlockedOnMVar          the MVAR             the MVAR's queue
250         
251         BlockedOnException     the TSO              TSO->blocked_exception
252
253         BlockedOnRead          NULL                 blocked_queue
254         BlockedOnWrite         NULL                 blocked_queue
255         BlockedOnDelay         NULL                 blocked_queue
256         BlockedOnGA            closure TSO blocks on   BQ of that closure
257         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
258
259       tso->link == END_TSO_QUEUE, if the thread is currently running.
260
261    A zombie thread has the following properties:
262       
263       tso->what_next == ThreadComplete or ThreadKilled
264       tso->link     ==  (could be on some queue somewhere)
265       tso->su       ==  tso->stack + tso->stack_size
266       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
267       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
268                         exception             , if what_next == ThreadKilled
269
270       (tso->sp is left pointing at the top word on the stack so that
271       the return value or exception will be retained by a GC).
272
273    tso->blocked_exceptions is either:
274
275       NULL             if async exceptions are unblocked.
276
277       END_TSO_QUEUE    if async exceptions are blocked, but no threads
278                        are currently waiting to deliver.
279
280       (StgTSO *)tso    if threads are currently awaiting delivery of
281                        exceptions to this thread.
282
283    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
284    setup only. They mark a TSO that has entered a FETCH_ME or
285    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
286    closure will send a Fetch message.
287    Currently we have no separate code for blocking on an RBH; we use the
288    BlockedOnBlackHole case for that.   -- HWL
289
290  ---------------------------------------------------------------------------- */
291
292 /* Workaround for a bug/quirk in gcc on certain architectures.
293  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
294  * in other words, gcc pads the structure at the end.
295  */
296
297 extern StgTSO dummy_tso;
298
299 #define TSO_STRUCT_SIZE \
300    ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
301
302 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
303
304 #endif /* TSO_H */