3b9a48d8a45f6461cbb132dfb5af8674652aefad
[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 #include "builtin.h"
23
24 #define MAXTHREADS              256          /* schani */
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 #if 1
45 #define DBG(s)
46 #define SDBG(s)
47 #else
48 #define DBG(s)                 s
49 #define SDBG(s)                s
50 #endif
51
52 struct _thread;
53
54 typedef struct _ctx
55 {
56     struct _thread    *thread;
57     bool               free;
58     u1                 status;
59     u1                 priority;
60     u1*                restorePoint;
61     u1*                stackMem;           /* includes guard page */
62     u1*                stackBase;
63     u1*                stackEnd;
64     u1*                usedStackTop;
65     s8                 time;
66     java_objectheader *texceptionptr;
67     struct _thread    *nextlive;
68     u1                 flags;
69 } ctx;
70
71 /*
72 struct _stringClass;
73 struct _object;
74 */
75
76 /* This structure mirrors java.lang.ThreadGroup.h */
77
78 typedef struct _threadGroup {
79    java_objectheader header;
80    struct _threadGroup* parent;
81    struct java_objectheader* name;
82    struct java_objectheader* threads;
83    struct java_objectheader* groups;
84    s4 daemon_flag;
85    s4 maxpri;
86 } threadGroup;
87
88
89 /* This structure mirrors java.lang.Thread.h */
90 typedef struct _thread {
91    java_objectheader header;
92    struct _threadGroup* group;
93    struct java_objectheader* toRun;
94    struct java_objectheader* name;
95    s8 PrivateInfo;
96    struct _thread* next;
97    s4 daemon;
98    s4 priority;
99    struct java_objectheader* contextClassLoader;
100 } thread;
101
102 void initThreads (u1 *stackbottom);
103 void clear_thread_flags (void);
104 void startThread (thread*);
105 void resumeThread (thread*);
106 void iresumeThread (thread*);
107 void suspendThread (thread*);
108 void suspendOnQThread (thread*, thread**);
109 void yieldThread (void);
110 void killThread (thread*);
111 void setPriorityThread (thread*, int);
112
113 s8 currentTime (void);
114 void sleepThread (s8);
115 bool aliveThread (thread*);
116 long framesThread (thread*);
117
118 void reschedule (void);
119
120 void checkEvents (bool block);
121
122 extern int blockInts;
123 extern bool needReschedule;
124 extern thread *currentThread;
125 extern thread *mainThread;
126 extern ctx contexts[];
127 extern int threadStackSize;
128
129 extern thread *liveThreads;
130 extern thread *sleepThreads;
131
132 extern thread *threadQhead[MAX_THREAD_PRIO + 1];
133
134
135 #define CONTEXT(_t)     (contexts[(_t)->PrivateInfo - 1])
136
137 #define intsDisable()   blockInts++
138
139 #define intsRestore()   if (blockInts == 1 && needReschedule) { \
140                             reschedule(); \
141                         } \
142                         blockInts--
143
144
145 /* access macros */
146
147 #define THREADSTACKSIZE         (32 * 1024)
148
149 #define THREADSWITCH(to, from) \
150     do { \
151         asm_perform_threadswitch(&(from)->restorePoint,\
152                                  &(to)->restorePoint, &(from)->usedStackTop); \
153     } while (0)
154
155
156 #define THREADINIT(to, func) \
157     do { \
158         (to)->restorePoint = asm_initialize_thread_stack((u1*)(func), \
159                                                          (to)->stackEnd); \
160     } while (0)
161
162
163 #define THREADINFO(e) \
164     do { \
165         (e)->restorePoint = 0; \
166         (e)->flags = THREAD_FLAGS_NOSTACKALLOC; \
167     } while(0)
168
169
170 /* function prototypes */
171 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop);
172 u1*  asm_initialize_thread_stack(void *func, u1 *stack);
173
174 typedef struct {
175         java_objectheader *_exceptionptr;
176 } nativethread;
177
178 #if defined(NATIVE_THREADS) && !defined(HAVE___THREAD)
179 extern pthread_key_t tkey_threadinfo;
180 #endif
181
182 #else
183
184 #define intsDisable()
185 #define intsRestore()
186
187 #endif /* USE_THREADS */
188
189 #endif /* _THREAD_H */
190