implement clean/dirty TSOs
[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  * Flags for the tso->flags field.
82  *
83  * The TSO_DIRTY flag indicates that this TSO's stack should be
84  * scanned during garbage collection.  The link field of a TSO is
85  * always scanned, so we don't have to dirty a TSO just for linking
86  * it on a different list.
87  *
88  * TSO_DIRTY is set by 
89  *    - schedule(), just before running a thread,
90  *    - raiseAsync(), because it modifies a thread's stack
91  *    - resumeThread(), just before running the thread again
92  * and unset by the garbage collector (only).
93  */
94 #define TSO_DIRTY   1
95
96 /*
97  * Type returned after running a thread.  Values of this type
98  * include HeapOverflow, StackOverflow etc.  See Constants.h for the
99  * full list.
100  */
101 typedef unsigned int StgThreadReturnCode;
102
103 #if defined(mingw32_HOST_OS)
104 /* results from an async I/O request + its request ID. */
105 typedef struct {
106   unsigned int reqID;
107   int          len;
108   int          errCode;
109 } StgAsyncIOResult;
110 #endif
111
112 typedef union {
113   StgClosure *closure;
114   struct StgTSO_ *tso;
115   StgInt fd;    /* StgInt instead of int, so that it's the same size as the ptrs */
116 #if defined(mingw32_HOST_OS)
117   StgAsyncIOResult *async_result;
118 #endif
119   StgWord target;
120 } StgTSOBlockInfo;
121
122 /*
123  * TSOs live on the heap, and therefore look just like heap objects.
124  * Large TSOs will live in their own "block group" allocated by the
125  * storage manager, and won't be copied during garbage collection.
126  */
127
128 /* 
129  * Threads may be blocked for several reasons.  A blocked thread will
130  * have the reason in the why_blocked field of the TSO, and some
131  * further info (such as the closure the thread is blocked on, or the
132  * file descriptor if the thread is waiting on I/O) in the block_info
133  * field.
134  */
135
136 typedef struct StgTSO_ {
137   StgHeader          header;
138
139   struct StgTSO_*    link;           /* Links threads onto blocking queues */
140   struct StgTSO_*    global_link;    /* Links all threads together */
141   
142   StgWord16          what_next;      /* Values defined in Constants.h */
143   StgWord16          why_blocked;    /* Values defined in Constants.h */
144   StgWord32          flags;
145   StgTSOBlockInfo    block_info;
146   struct StgTSO_*    blocked_exceptions;
147   StgThreadID        id;
148   int                saved_errno;
149   struct Task_*      bound;          // non-NULL for a bound thread
150   struct StgTRecHeader_ *trec;       /* STM transaction record */
151   
152 #ifdef TICKY_TICKY
153   /* TICKY-specific stuff would go here. */
154 #endif
155 #ifdef PROFILING
156    StgTSOProfInfo prof;
157 #endif
158 #ifdef PAR
159    StgTSOParInfo par;
160 #endif
161 #ifdef GRAN
162    StgTSOGranInfo gran;
163 #endif
164 #ifdef DIST
165    StgTSODistInfo dist;
166 #endif
167
168   /* The thread stack... */
169   StgWord            stack_size;     /* stack size in *words* */
170   StgWord            max_stack_size; /* maximum stack size in *words* */
171   StgPtr             sp;
172   
173   StgWord            stack[FLEXIBLE_ARRAY];
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       
184       RESERVED_STACK_WORDS is large enough for any heap-check or
185       stack-check failure.
186
187       The size of the TSO struct plus the stack is either
188         (a) smaller than a block, or
189         (b) a multiple of BLOCK_SIZE
190
191         tso->why_blocked       tso->block_info      location
192         ----------------------------------------------------------------------
193         NotBlocked             NULL                 runnable_queue, or running
194         
195         BlockedOnBlackHole     the BLACKHOLE_BQ     the BLACKHOLE_BQ's queue
196         
197         BlockedOnMVar          the MVAR             the MVAR's queue
198
199         BlockedOnSTM           END_TSO_QUEUE        STM wait queue(s)
200         
201         BlockedOnException     the TSO              TSO->blocked_exception
202
203         BlockedOnRead          NULL                 blocked_queue
204         BlockedOnWrite         NULL                 blocked_queue
205         BlockedOnDelay         NULL                 blocked_queue
206         BlockedOnGA            closure TSO blocks on   BQ of that closure
207         BlockedOnGA_NoSend     closure TSO blocks on   BQ of that closure
208
209       tso->link == END_TSO_QUEUE, if the thread is currently running.
210
211    A zombie thread has the following properties:
212       
213       tso->what_next == ThreadComplete or ThreadKilled
214       tso->link     ==  (could be on some queue somewhere)
215       tso->su       ==  tso->stack + tso->stack_size
216       tso->sp       ==  tso->stack + tso->stack_size - 1 (i.e. top stack word)
217       tso->sp[0]    ==  return value of thread, if what_next == ThreadComplete,
218                         exception             , if what_next == ThreadKilled
219
220       (tso->sp is left pointing at the top word on the stack so that
221       the return value or exception will be retained by a GC).
222
223    tso->blocked_exceptions is either:
224
225       NULL             if async exceptions are unblocked.
226
227       END_TSO_QUEUE    if async exceptions are blocked, but no threads
228                        are currently waiting to deliver.
229
230       (StgTSO *)tso    if threads are currently awaiting delivery of
231                        exceptions to this thread.
232
233    The 2 cases BlockedOnGA and BlockedOnGA_NoSend are needed in a GUM
234    setup only. They mark a TSO that has entered a FETCH_ME or
235    FETCH_ME_BQ closure, respectively; only the first TSO hitting the 
236    closure will send a Fetch message.
237    Currently we have no separate code for blocking on an RBH; we use the
238    BlockedOnBlackHole case for that.   -- HWL
239
240  ---------------------------------------------------------------------------- */
241
242 /* Workaround for a bug/quirk in gcc on certain architectures.
243  * symptom is that (&tso->stack - &tso->header) /=  sizeof(StgTSO)
244  * in other words, gcc pads the structure at the end.
245  */
246
247 extern StgTSO dummy_tso;
248
249 #define TSO_STRUCT_SIZE \
250    ((char *)&dummy_tso.stack - (char *)&dummy_tso.header)
251
252 #define TSO_STRUCT_SIZEW (TSO_STRUCT_SIZE / sizeof(W_))
253
254
255 /* this is the NIL ptr for a TSO queue (e.g. runnable queue) */
256 #if IN_STG_CODE
257 #define END_TSO_QUEUE  (stg_END_TSO_QUEUE_closure)
258 #else
259 #define END_TSO_QUEUE  ((StgTSO *)(void*)&stg_END_TSO_QUEUE_closure)
260 #endif
261
262 #if defined(PAR) || defined(GRAN)
263 /* this is the NIL ptr for a blocking queue */
264 # define END_BQ_QUEUE  ((StgBlockingQueueElement *)(void*)&stg_END_TSO_QUEUE_closure)
265 /* this is the NIL ptr for a blocked fetch queue (as in PendingFetches in GUM) */
266 # define END_BF_QUEUE  ((StgBlockedFetch *)(void*)&stg_END_TSO_QUEUE_closure)
267 #endif
268 /* ToDo?: different name for end of sleeping queue ? -- HWL */
269
270 #endif /* TSO_H */