55a657bd5a2114f29cc2282eabeb6e93f7f5f76e
[cacao.git] / src / threads / green / threads.h
1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4 -*- */
2 /*
3  * thread.h
4  * Thread support.
5  *
6  * Copyright (c) 1996 T. J. Wilkinson & Associates, London, UK.
7  *
8  * See the file "license.terms" for information on usage and redistribution
9  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10  *
11  * Written by Tim Wilkinson <tim@tjwassoc.demon.co.uk>, 1996.
12  */
13
14
15 #ifndef _THREADS_H
16 #define _THREADS_H
17
18 #include "config.h"
19 #include "mm/memory.h"
20 #include "vm/global.h"                                 /* for native includes */
21 #include "native/include/java_lang_ClassLoader.h"
22 #include "native/include/java_lang_String.h"
23 #include "native/include/java_lang_Throwable.h"
24
25
26 #define THREADCLASS         "java/lang/Thread"
27 #define THREADGROUPCLASS    "java/lang/ThreadGroup"
28 #define THREADDEATHCLASS    "java/lang/ThreadDeath"
29
30 #define MIN_THREAD_PRIO         1
31 #define NORM_THREAD_PRIO        5
32 #define MAX_THREAD_PRIO         10
33
34 #define THREAD_SUSPENDED        0
35 #define THREAD_RUNNING          1
36 #define THREAD_DEAD             2
37 #define THREAD_KILL             3
38
39 #define THREAD_FLAGS_GENERAL            0
40 #define THREAD_FLAGS_NOSTACKALLOC       1
41 #define THREAD_FLAGS_USER_SUSPEND       2  /* Flag explicit suspend() call */
42 #define THREAD_FLAGS_KILLED             4
43
44 #define THREADSPECIFIC
45
46 #define MAXTHREADS              256          /* schani */
47
48 #if 1
49 #define DBG(s)
50 #define SDBG(s)
51 #else
52 #define DBG(s)                 s
53 #define SDBG(s)                s
54 #endif
55
56 struct _thread;
57
58 #if 0
59 typedef struct _ctx
60 {
61     struct _thread    *thread;
62     bool               free;
63     u1                 status;
64     u1                 priority;
65     u1*                restorePoint;
66     u1*                stackMem;           /* includes guard page */
67     u1*                stackBase;
68     u1*                stackEnd;
69     u1*                usedStackTop;
70     s8                 time;
71     java_objectheader *texceptionptr;
72     struct _thread    *nextlive;
73     u1                 flags;
74 } ctx;
75 #endif
76
77 /*
78 struct _stringClass;
79 struct _object;
80 */
81
82 /* This structure mirrors java.lang.ThreadGroup.h */
83
84 typedef struct _threadGroup {
85    java_objectheader header;
86    struct _threadGroup* parent;
87    struct java_objectheader* name;
88    struct java_objectheader* threads;
89    struct java_objectheader* groups;
90    s4 daemon_flag;
91    s4 maxpri;
92 } threadGroup;
93
94
95 typedef struct thread thread;
96 typedef struct vmthread vmthread;
97
98 /* This structure mirrors java.lang.Thread.h */
99
100 struct thread {
101    java_objectheader      header;
102    vmthread              *vmThread;
103    threadGroup           *group;
104    struct java_lang_Runnable* runnable;
105    java_lang_String      *name;
106    s4                     daemon;
107    s4                     priority;
108    s8                     stacksize;
109    java_lang_Throwable   *stillborn;
110    java_lang_ClassLoader *contextClassLoader;
111 };
112
113
114 /* This structure mirrors java.lang.VMThread.h */
115
116 struct vmthread {
117         java_objectheader  header;
118         thread            *thread;
119         s4                 running;
120         s4                 status;
121         s4                 priority;
122         void              *restorePoint;
123         void              *stackMem;
124         void              *stackBase;
125         void              *stackEnd;
126         void              *usedStackTop;
127         s8                 time;
128         java_objectheader *texceptionptr;
129         struct _thread    *nextlive;
130         struct _thread    *next;
131         s4                 flags;
132
133 #if 0
134         dumpinfo          *dumpinfo;        /* dump memory info structure         */
135 #endif
136 };
137
138
139 extern int blockInts;
140 extern bool needReschedule;
141 extern thread *currentThread;
142 extern thread *mainThread;
143 /*extern ctx contexts[];*/
144 extern int threadStackSize;
145
146 extern thread *liveThreads;
147 extern thread *sleepThreads;
148
149 extern thread *threadQhead[MAX_THREAD_PRIO + 1];
150
151
152 /*#define CONTEXT(_t)     (contexts[(_t)->PrivateInfo - 1])*/
153 #define CONTEXT(_t)     (*(_t->vmThread))
154
155 #define intsDisable()   blockInts++
156
157 #define intsRestore()   if (blockInts == 1 && needReschedule) { \
158                             reschedule(); \
159                         } \
160                         blockInts--
161
162
163 /* access macros */
164
165 #define THREADSTACKSIZE         (32 * 1024)
166
167 #define THREADSWITCH(to, from) \
168     do { \
169         void *from1; \
170         void *from2; \
171         asm_perform_threadswitch((from?&(from)->restorePoint:&from1),\
172                                  &(to)->restorePoint, (from?&(from)->usedStackTop:&from2)); \
173     } while (0)
174
175
176 #define THREADINIT(to, func) \
177     do { \
178         (to)->restorePoint = asm_initialize_thread_stack((u1*)(func), \
179                                                          (to)->stackEnd); \
180     } while (0)
181
182
183 #define THREADINFO(e) \
184     do { \
185         (e)->restorePoint = 0; \
186         (e)->flags = THREAD_FLAGS_NOSTACKALLOC; \
187     } while(0)
188
189
190 /* function prototypes ********************************************************/
191
192 void initThreads (u1 *stackbottom);
193 void clear_thread_flags (void);
194 void startThread (thread*);
195 void resumeThread (thread*);
196 void iresumeThread (thread*);
197 void suspendThread (thread*);
198 void suspendOnQThread (thread*, thread**);
199 void yieldThread (void);
200 void killThread (thread*);
201 void setPriorityThread (thread*, int);
202
203 s8 currentTime (void);
204 void sleepThread(s8 millis, s4 nanos);
205 bool aliveThread (thread*);
206 long framesThread (thread*);
207
208 void reschedule (void);
209
210 void checkEvents (bool block);
211
212 #endif /* _THREADS_H */
213
214
215 /*
216  * These are local overrides for various environment variables in Emacs.
217  * Please do not remove this and leave it at the end of the file, where
218  * Emacs will automagically detect them.
219  * ---------------------------------------------------------------------
220  * Local variables:
221  * mode: c
222  * indent-tabs-mode: t
223  * c-basic-offset: 4
224  * tab-width: 4
225  * End:
226  */