[project @ 2000-08-15 14:18:43 by simonmar]
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  * $Id: TSO.h,v 1.17 2000/08/15 14:18:43 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 #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 /* 
151  * ToDo: make this structure sensible on a non-32-bit arch.
152  */
153
154 typedef struct StgTSO_ {
155   StgHeader          header;
156
157   struct StgTSO_*    link;           /* Links threads onto blocking queues */
158   StgMutClosure *    mut_link;       /* TSO's are mutable of course! */
159   struct StgTSO_*    global_link;    /* Links all threads together */
160   
161   StgTSOWhatNext     what_next   : 16;
162   StgTSOBlockReason  why_blocked : 16;
163   StgTSOBlockInfo    block_info;
164   struct StgTSO_*    blocked_exceptions;
165   StgThreadID        id;
166
167   StgTSOTickyInfo    ticky; 
168   StgTSOProfInfo     prof;
169   StgTSOParInfo      par;
170   StgTSOGranInfo     gran;
171
172   /* The thread stack... */
173   StgWord            stack_size;     /* stack size in *words* */
174   StgWord            max_stack_size; /* maximum stack size in *words* */
175   StgPtr             sp;
176   StgUpdateFrame*    su;
177   
178   StgWord            stack[0];
179 } StgTSO;
180
181 /* -----------------------------------------------------------------------------
182    Invariants:
183
184    An active thread has the following properties:
185
186       tso->stack < tso->sp < tso->stack+tso->stack_size
187       tso->stack_size <= tso->max_stack_size
188       
189       RESERVED_STACK_WORDS is large enough for any heap-check or
190       stack-check failure.
191
192       The size of the TSO struct plus the stack is either
193         (a) smaller than a block, or
194         (b) a multiple of BLOCK_SIZE
195
196         tso->why_blocked       tso->block_info      location
197         ----------------------------------------------------------------------
198         NotBlocked             NULL                 runnable_queue, or running
199         
200         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
201         
202         BlockedOnMVar          the MVAR             the MVAR's queue
203         
204         BlockedOnException     the TSO              TSO->blocked_exception
205
206         BlockedOnRead          NULL                 blocked_queue
207         BlockedOnWrite         NULL                 blocked_queue
208         BlockedOnDelay         NULL                 blocked_queue
209         BlockedOnGA            closure TSO blocks on   BQ of that closure
210         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
211
212       tso->link == END_TSO_QUEUE, if the thread is currently running.
213
214    A zombie thread has the following properties:
215       
216       tso->what_next == ThreadComplete or ThreadKilled
217       tso->link     ==  (could be on some queue somewhere)
218       tso->su       ==  tso->stack + tso->stack_size
219       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
220       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
221                         exception             , if what_next == ThreadKilled
222
223       (tso->sp is left pointing at the top word on the stack so that
224       the return value or exception will be retained by a GC).
225
226    tso->blocked_exceptions is either:
227
228       NULL             if async exceptions are unblocked.
229
230       END_TSO_QUEUE    if async exceptions are blocked, but no threads
231                        are currently waiting to deliver.
232
233       (StgTSO *)tso    if threads are currently awaiting delivery of
234                        exceptions to this thread.
235
236    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
237    setup only. They mark a TSO that has entered a FETCH_ME or
238    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
239    closure will send a Fetch message.
240    Currently we have no separate code for blocking on an RBH; we use the
241    BlockedOnBlackHole case for that.   -- HWL
242
243  ---------------------------------------------------------------------------- */
244
245 /* Workaround for a bug/quirk in gcc on certain architectures.
246  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
247  * in other words, gcc pads the structure at the end.
248  */
249
250 extern StgTSO dummy_tso;
251
252 #define TSO_STRUCT_SIZE \
253    ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
254
255 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
256
257 #endif /* TSO_H */