[project @ 2000-01-13 14:33:57 by hwloidl]
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  * $Id: TSO.h,v 1.10 2000/01/13 14:34:01 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 // 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 typedef enum {
75   ThreadEnterGHC,
76   ThreadRunGHC,
77   ThreadEnterHugs,
78   ThreadKilled,
79   ThreadComplete
80 } StgTSOWhatNext;
81
82 /*
83  * We are completely paranoid and make thread IDs 64 bits to avoid
84  * having to worry about overflow.  A little calculation shows that
85  * even doing 10^6 forks per second would take 35 million years to
86  * overflow a 64 bit thread ID :-)
87  *
88  */
89 typedef StgWord32 StgThreadID;
90
91 /*
92  * This type is returned to the scheduler by a thread that has
93  * stopped for one reason or another.
94  */
95
96 typedef enum {
97   HeapOverflow,                 /* might also be StackOverflow */
98   StackOverflow,
99   ThreadYielding,
100   ThreadBlocked,
101   ThreadFinished
102 } StgThreadReturnCode;
103
104 /* 
105  * Threads may be blocked for several reasons.  A blocked thread will
106  * have the reason in the why_blocked field of the TSO, and some
107  * further info (such as the closure the thread is blocked on, or the
108  * file descriptor if the thread is waiting on I/O) in the block_info
109  * field.
110  */
111
112 typedef enum {
113   NotBlocked,
114   BlockedOnMVar,
115   BlockedOnBlackHole,
116   BlockedOnException,
117   BlockedOnRead,
118   BlockedOnWrite,
119   BlockedOnDelay
120 #if defined(PAR)
121   , BlockedOnGA    // blocked on a remote closure represented by a Global Address
122 #endif
123 } StgTSOBlockReason;
124
125 typedef union {
126   StgClosure *closure;
127   struct StgTSO_ *tso;
128   int fd;
129   unsigned int delay;
130 #if defined(PAR)
131   globalAddr ga;
132 #endif
133 } StgTSOBlockInfo;
134
135 /*
136  * TSOs live on the heap, and therefore look just like heap objects.
137  * Large TSOs will live in their own "block group" allocated by the
138  * storage manager, and won't be copied during garbage collection.
139  */
140
141 typedef struct StgTSO_ {
142   StgHeader          header;
143   struct StgTSO_*    link;
144   /* SDM and HWL agree that it would be cool to have a list of all TSOs */
145   StgMutClosure *    mut_link;  /* TSO's are mutable of course! */
146   StgTSOWhatNext     whatNext;
147   StgTSOBlockReason  why_blocked;
148   StgTSOBlockInfo    block_info;
149   struct StgTSO_*    blocked_exceptions;
150   StgThreadID        id;
151   StgTSOTickyInfo    ticky; 
152   StgTSOProfInfo     prof;
153   StgTSOParInfo      par;
154   StgTSOGranInfo     gran;
155
156   /* The thread stack... */
157   StgWord            stack_size;     /* stack size in *words* */
158   StgWord            max_stack_size; /* maximum stack size in *words* */
159   StgPtr             sp;
160   StgUpdateFrame*    su;
161   StgPtr             splim;
162   
163   StgWord            stack[0];
164 } StgTSO;
165
166 /* -----------------------------------------------------------------------------
167    Invariants:
168
169    An active thread has the following properties:
170
171       tso->stack < tso->sp < tso->stack+tso->stack_size
172       tso->stack_size <= tso->max_stack_size
173       tso->splim == tso->stack + RESERVED_STACK_WORDS;
174       
175       RESERVED_STACK_WORDS is large enough for any heap-check or
176       stack-check failure.
177
178       The size of the TSO struct plus the stack is either
179         (a) smaller than a block, or
180         (b) a multiple of BLOCK_SIZE
181
182         tso->block_reason      tso->block_info      location
183         ----------------------------------------------------------------------
184         NotBlocked             NULL                 runnable_queue, or running
185         
186         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
187         
188         BlockedOnMVar          the MVAR             the MVAR's queue
189         
190         BlockedOnException     the TSO              TSO->blocked_exception
191
192         BlockedOnRead          NULL                 blocked_queue
193         BlockedOnWrite         NULL                 blocked_queue
194         BlockedOnDelay         NULL                 blocked_queue
195
196       tso->link == END_TSO_QUEUE, if the thread is currently running.
197
198    A zombie thread has the following properties:
199       
200       tso->whatNext == ThreadComplete or ThreadKilled
201       tso->link     ==  (could be on some queue somewhere)
202       tso->su       ==  tso->stack + tso->stack_size
203       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
204       tso->sp[0]    ==  return value of thread, if whatNext == ThreadComplete,
205                         exception             , if whatNext == ThreadKilled
206
207       (tso->sp is left pointing at the top word on the stack so that
208       the return value or exception will be retained by a GC).
209
210    tso->blocked_exceptions is either:
211
212       NULL             if async exceptions are unblocked.
213
214       END_TSO_QUEUE    if async exceptions are blocked, but no threads
215                        are currently waiting to deliver.
216
217       (StgTSO *)tso    if threads are currently awaiting delivery of
218                        exceptions to this thread.
219
220  ---------------------------------------------------------------------------- */
221
222 /* Workaround for a bug/quirk in gcc on certain architectures.
223  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
224  * in other words, gcc pads the structure at the end.
225  */
226
227 extern StgTSO dummy_tso;
228
229 #define TSO_STRUCT_SIZE \
230    ((int)&(dummy_tso).stack - (int)&(dummy_tso).header)
231
232 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
233
234 #endif /* TSO_H */