509e55f7841c1dd977017f13382b466a58116e69
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  * $Id: TSO.h,v 1.18 2000/08/25 13:12:07 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 // && 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   unsigned int target;
138 } StgTSOBlockInfo;
139
140 /*
141  * TSOs live on the heap, and therefore look just like heap objects.
142  * Large TSOs will live in their own "block group" allocated by the
143  * storage manager, and won't be copied during garbage collection.
144  */
145
146 /* 
147  * ToDo: make this structure sensible on a non-32-bit arch.
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   : 16;
158   StgTSOBlockReason  why_blocked : 16;
159   StgTSOBlockInfo    block_info;
160   struct StgTSO_*    blocked_exceptions;
161   StgThreadID        id;
162
163   StgTSOTickyInfo    ticky; 
164   StgTSOProfInfo     prof;
165   StgTSOParInfo      par;
166   StgTSOGranInfo     gran;
167
168   /* The thread stack... */
169   StgWord            stack_size;     /* stack size in *words* */
170   StgWord            max_stack_size; /* maximum stack size in *words* */
171   StgPtr             sp;
172   StgUpdateFrame*    su;
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       
185       RESERVED_STACK_WORDS is large enough for any heap-check or
186       stack-check failure.
187
188       The size of the TSO struct plus the stack is either
189         (a) smaller than a block, or
190         (b) a multiple of BLOCK_SIZE
191
192         tso->why_blocked       tso->block_info      location
193         ----------------------------------------------------------------------
194         NotBlocked             NULL                 runnable_queue, or running
195         
196         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
197         
198         BlockedOnMVar          the MVAR             the MVAR's queue
199         
200         BlockedOnException     the TSO              TSO->blocked_exception
201
202         BlockedOnRead          NULL                 blocked_queue
203         BlockedOnWrite         NULL                 blocked_queue
204         BlockedOnDelay         NULL                 blocked_queue
205         BlockedOnGA            closure TSO blocks on   BQ of that closure
206         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
207
208       tso->link == END_TSO_QUEUE, if the thread is currently running.
209
210    A zombie thread has the following properties:
211       
212       tso->what_next == ThreadComplete or ThreadKilled
213       tso->link     ==  (could be on some queue somewhere)
214       tso->su       ==  tso->stack + tso->stack_size
215       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
216       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
217                         exception             , if what_next == ThreadKilled
218
219       (tso->sp is left pointing at the top word on the stack so that
220       the return value or exception will be retained by a GC).
221
222    tso->blocked_exceptions is either:
223
224       NULL             if async exceptions are unblocked.
225
226       END_TSO_QUEUE    if async exceptions are blocked, but no threads
227                        are currently waiting to deliver.
228
229       (StgTSO *)tso    if threads are currently awaiting delivery of
230                        exceptions to this thread.
231
232    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
233    setup only. They mark a TSO that has entered a FETCH_ME or
234    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
235    closure will send a Fetch message.
236    Currently we have no separate code for blocking on an RBH; we use the
237    BlockedOnBlackHole case for that.   -- HWL
238
239  ---------------------------------------------------------------------------- */
240
241 /* Workaround for a bug/quirk in gcc on certain architectures.
242  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
243  * in other words, gcc pads the structure at the end.
244  */
245
246 extern StgTSO dummy_tso;
247
248 #define TSO_STRUCT_SIZE \
249    ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
250
251 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
252
253 #endif /* TSO_H */