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