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