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