remove old comment
[ghc-hetmet.git] / ghc / includes / TSO.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-1999
4  *
5  * The definitions for Thread State Objects.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef TSO_H
10 #define TSO_H
11
12 #if DEBUG
13 #define TSO_MAGIC 4321
14 #endif
15
16 typedef struct {
17   StgInt   pri;
18   StgInt   magic;
19   StgInt   sparkname;
20   rtsTime  startedat;
21   rtsBool  exported;
22   StgInt   basicblocks;
23   StgInt   allocs;
24   rtsTime  exectime;
25   rtsTime  fetchtime;
26   rtsTime  fetchcount;
27   rtsTime  blocktime;
28   StgInt   blockcount;
29   rtsTime  blockedat;
30   StgInt   globalsparks;
31   StgInt   localsparks;
32   rtsTime  clock;
33 } StgTSOStatBuf;
34
35 /*
36  * GRAN: We distinguish between the various classes of threads in 
37  * the system.
38  */
39 typedef enum {
40   AdvisoryPriority,
41   MandatoryPriority,
42   RevalPriority
43 } StgThreadPriority;
44
45 /*
46  * PROFILING info in a TSO
47  */
48 typedef struct {
49   CostCentreStack *CCCS;        /* thread's current CCS */
50 } StgTSOProfInfo;
51
52 /*
53  * PAR info in a TSO
54  */
55 typedef StgTSOStatBuf StgTSOParInfo;
56
57 /*
58  * DIST info in a TSO
59  */
60 typedef struct {
61   StgThreadPriority  priority;   
62   StgInt             revalTid;   /* ToDo: merge both into 1 word */
63   StgInt             revalSlot;
64 } StgTSODistInfo;
65
66 /*
67  * GRAN info in a TSO
68  */
69 typedef StgTSOStatBuf StgTSOGranInfo;
70
71 /*
72  * There is no TICKY info in a TSO at this time.
73  */
74
75 /*
76  * Thread IDs are 32 bits.
77  */
78 typedef StgWord32 StgThreadID;
79
80 /*
81  * Type returned after running a thread.  Values of this type
82  * include HeapOverflow, StackOverflow etc.  See Constants.h for the
83  * full list.
84  */
85 typedef unsigned int StgThreadReturnCode;
86
87 #if defined(mingw32_HOST_OS)
88 /* results from an async I/O request + its request ID. */
89 typedef struct {
90   unsigned int reqID;
91   int          len;
92   int          errCode;
93 } StgAsyncIOResult;
94 #endif
95
96 typedef union {
97   StgClosure *closure;
98   struct StgTSO_ *tso;
99   StgInt fd;    /* StgInt instead of int, so that it's the same size as the ptrs */
100 #if defined(mingw32_HOST_OS)
101   StgAsyncIOResult *async_result;
102 #endif
103   StgWord target;
104 } StgTSOBlockInfo;
105
106 /*
107  * TSOs live on the heap, and therefore look just like heap objects.
108  * Large TSOs will live in their own "block group" allocated by the
109  * storage manager, and won't be copied during garbage collection.
110  */
111
112 /* 
113  * Threads may be blocked for several reasons.  A blocked thread will
114  * have the reason in the why_blocked field of the TSO, and some
115  * further info (such as the closure the thread is blocked on, or the
116  * file descriptor if the thread is waiting on I/O) in the block_info
117  * field.
118  */
119
120 typedef struct StgTSO_ {
121   StgHeader          header;
122
123   struct StgTSO_*    link;           /* Links threads onto blocking queues */
124   struct StgTSO_*    global_link;    /* Links all threads together */
125   
126   StgWord16           what_next;  /* Values defined in Constants.h */
127   StgWord16           why_blocked;  /* Values defined in Constants.h */
128   StgTSOBlockInfo    block_info;
129   struct StgTSO_*    blocked_exceptions;
130   StgThreadID        id;
131   int                saved_errno;
132   struct Task_*      bound;          // non-NULL for a bound thread
133   struct StgTRecHeader_ *trec;       /* STM transaction record */
134   
135 #ifdef TICKY_TICKY
136   /* TICKY-specific stuff would go here. */
137 #endif
138 #ifdef PROFILING
139    StgTSOProfInfo prof;
140 #endif
141 #ifdef PAR
142    StgTSOParInfo par;
143 #endif
144 #ifdef GRAN
145    StgTSOGranInfo gran;
146 #endif
147 #ifdef DIST
148    StgTSODistInfo dist;
149 #endif
150
151   /* The thread stack... */
152   StgWord            stack_size;     /* stack size in *words* */
153   StgWord            max_stack_size; /* maximum stack size in *words* */
154   StgPtr             sp;
155   
156   StgWord            stack[FLEXIBLE_ARRAY];
157 } StgTSO;
158
159 /* -----------------------------------------------------------------------------
160    Invariants:
161
162    An active thread has the following properties:
163
164       tso->stack < tso->sp < tso->stack+tso->stack_size
165       tso->stack_size <= tso->max_stack_size
166       
167       RESERVED_STACK_WORDS is large enough for any heap-check or
168       stack-check failure.
169
170       The size of the TSO struct plus the stack is either
171         (a) smaller than a block, or
172         (b) a multiple of BLOCK_SIZE
173
174         tso->why_blocked       tso->block_info      location
175         ----------------------------------------------------------------------
176         NotBlocked             NULL                 runnable_queue, or running
177         
178         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
179         
180         BlockedOnMVar          the MVAR             the MVAR's queue
181
182         BlockedOnSTM           END_TSO_QUEUE        STM wait queue(s)
183         
184         BlockedOnException     the TSO              TSO->blocked_exception
185
186         BlockedOnRead          NULL                 blocked_queue
187         BlockedOnWrite         NULL                 blocked_queue
188         BlockedOnDelay         NULL                 blocked_queue
189         BlockedOnGA            closure TSO blocks on   BQ of that closure
190         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
191
192       tso->link == END_TSO_QUEUE, if the thread is currently running.
193
194    A zombie thread has the following properties:
195       
196       tso->what_next == ThreadComplete or ThreadKilled
197       tso->link     ==  (could be on some queue somewhere)
198       tso->su       ==  tso->stack + tso->stack_size
199       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
200       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
201                         exception             , if what_next == ThreadKilled
202
203       (tso->sp is left pointing at the top word on the stack so that
204       the return value or exception will be retained by a GC).
205
206    tso->blocked_exceptions is either:
207
208       NULL             if async exceptions are unblocked.
209
210       END_TSO_QUEUE    if async exceptions are blocked, but no threads
211                        are currently waiting to deliver.
212
213       (StgTSO *)tso    if threads are currently awaiting delivery of
214                        exceptions to this thread.
215
216    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
217    setup only. They mark a TSO that has entered a FETCH_ME or
218    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
219    closure will send a Fetch message.
220    Currently we have no separate code for blocking on an RBH; we use the
221    BlockedOnBlackHole case for that.   -- HWL
222
223  ---------------------------------------------------------------------------- */
224
225 /* Workaround for a bug/quirk in gcc on certain architectures.
226  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
227  * in other words, gcc pads the structure at the end.
228  */
229
230 extern StgTSO dummy_tso;
231
232 #define TSO_STRUCT_SIZE \
233    ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
234
235 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
236
237
238 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
239 #if IN_STG_CODE
240 #define END_TSO_QUEUE  (stg_END_TSO_QUEUE_closure)
241 #else
242 #define END_TSO_QUEUE  ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
243 #endif
244
245 #if defined(PAR) || defined(GRAN)
246 /* this is the NIL ptr for a blocking queue */
247 # define END_BQ_QUEUE  ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
248 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
249 # define END_BF_QUEUE  ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
250 #endif
251 /* ToDo?: different name for end of sleeping queue ? -- HWL */
252
253 #endif /* TSO_H */