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