Removed gc function definitions.
[cacao.git] / threads / thread.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 #ifndef _THREAD_H
15 #define _THREAD_H
16
17 #include "config.h"
18
19 typedef struct {
20         int super_baseval, super_diffval, sub_baseval;
21 } castinfo;
22
23 #ifdef USE_THREADS
24
25 #include "global.h"
26
27 #define THREADCLASS         "java/lang/Thread"
28 #define THREADGROUPCLASS    "java/lang/ThreadGroup"
29 #define THREADDEATHCLASS    "java/lang/ThreadDeath"
30
31 #define MIN_THREAD_PRIO         1
32 #define NORM_THREAD_PRIO        5
33 #define MAX_THREAD_PRIO         10
34
35 #define THREAD_SUSPENDED        0
36 #define THREAD_RUNNING          1
37 #define THREAD_DEAD             2
38 #define THREAD_KILL             3
39
40 #define THREAD_FLAGS_GENERAL            0
41 #define THREAD_FLAGS_NOSTACKALLOC       1
42 #define THREAD_FLAGS_USER_SUSPEND       2  /* Flag explicit suspend() call */
43 #define THREAD_FLAGS_KILLED             4
44
45 #if !defined(NATIVE_THREADS)
46
47 #define MAXTHREADS              256          /* schani */
48
49 #if 1
50 #define DBG(s)
51 #define SDBG(s)
52 #else
53 #define DBG(s)                 s
54 #define SDBG(s)                s
55 #endif
56
57 struct _thread;
58
59 #if 0
60 typedef struct _ctx
61 {
62     struct _thread    *thread;
63     bool               free;
64     u1                 status;
65     u1                 priority;
66     u1*                restorePoint;
67     u1*                stackMem;           /* includes guard page */
68     u1*                stackBase;
69     u1*                stackEnd;
70     u1*                usedStackTop;
71     s8                 time;
72     java_objectheader *texceptionptr;
73     struct _thread    *nextlive;
74     u1                 flags;
75 } ctx;
76 #endif
77
78 /*
79 struct _stringClass;
80 struct _object;
81 */
82
83 /* This structure mirrors java.lang.ThreadGroup.h */
84
85 typedef struct _threadGroup {
86    java_objectheader header;
87    struct _threadGroup* parent;
88    struct java_objectheader* name;
89    struct java_objectheader* threads;
90    struct java_objectheader* groups;
91    s4 daemon_flag;
92    s4 maxpri;
93 } threadGroup;
94
95
96
97 /* This structure mirrors java.lang.VMThread.h */
98 typedef struct vmthread {
99    java_objectheader header;
100    struct _thread* thread;
101    s4 running;
102    s4 status;
103    s4 priority;
104    void* restorePoint;
105    void* stackMem;
106    void* stackBase;
107    void* stackEnd;
108    void* usedStackTop;
109    s8 time;
110    java_objectheader *texceptionptr;
111    struct _thread* nextlive;
112    struct _thread* next;
113    s4 flags;
114 } vmthread;
115
116 /* This structure mirrors java.lang.Thread.h */
117 typedef struct _thread {
118    java_objectheader header;
119    vmthread* vmThread;
120    threadGroup* group;
121    struct java_lang_Runnable* runnable;
122    struct java_lang_String* name;
123    s4 daemon;
124    s4 priority;
125    s8 stacksize;
126    struct java_lang_Throwable* stillborn;
127    struct java_lang_ClassLoader* contextClassLoader;
128 } thread;
129
130 void initThreads (u1 *stackbottom);
131 void clear_thread_flags (void);
132 void startThread (thread*);
133 void resumeThread (thread*);
134 void iresumeThread (thread*);
135 void suspendThread (thread*);
136 void suspendOnQThread (thread*, thread**);
137 void yieldThread (void);
138 void killThread (thread*);
139 void setPriorityThread (thread*, int);
140
141 s8 currentTime (void);
142 void sleepThread(s8 millis, s4 nanos);
143 bool aliveThread (thread*);
144 long framesThread (thread*);
145
146 void reschedule (void);
147
148 void checkEvents (bool block);
149
150 extern int blockInts;
151 extern bool needReschedule;
152 extern thread *currentThread;
153 extern thread *mainThread;
154 /*extern ctx contexts[];*/
155 extern int threadStackSize;
156
157 extern thread *liveThreads;
158 extern thread *sleepThreads;
159
160 extern thread *threadQhead[MAX_THREAD_PRIO + 1];
161
162
163 /*#define CONTEXT(_t)     (contexts[(_t)->PrivateInfo - 1])*/
164 #define CONTEXT(_t)     (*(_t->vmThread))
165
166 #define intsDisable()   blockInts++
167
168 #define intsRestore()   if (blockInts == 1 && needReschedule) { \
169                             reschedule(); \
170                         } \
171                         blockInts--
172
173
174 /* access macros */
175
176 #define THREADSTACKSIZE         (32 * 1024)
177
178 #define THREADSWITCH(to, from) \
179     do { \
180         void *from1; \
181         void *from2; \
182         asm_perform_threadswitch((from?&(from)->restorePoint:&from1),\
183                                  &(to)->restorePoint, (from?&(from)->usedStackTop:&from2)); \
184     } while (0)
185
186
187 #define THREADINIT(to, func) \
188     do { \
189         (to)->restorePoint = asm_initialize_thread_stack((u1*)(func), \
190                                                          (to)->stackEnd); \
191     } while (0)
192
193
194 #define THREADINFO(e) \
195     do { \
196         (e)->restorePoint = 0; \
197         (e)->flags = THREAD_FLAGS_NOSTACKALLOC; \
198     } while(0)
199
200
201 /* function prototypes */
202 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop);
203 u1*  asm_initialize_thread_stack(void *func, u1 *stack);
204
205 #else /* NATIVE_THREADS */
206 #include "nativethread.h"
207 #endif
208
209 #else
210
211 #define intsDisable()
212 #define intsRestore()
213
214 #endif /* USE_THREADS */
215
216 #endif /* _THREAD_H */
217