Changed the makefile system to autoconf/automake.
[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 struct _thread;
44
45 typedef struct _ctx
46 {
47     struct _thread    *thread;
48     bool               free;
49     u1                 status;
50     u1                 priority;
51     u1*                restorePoint;
52     u1*                stackMem;           /* includes guard page */
53     u1*                stackBase;
54     u1*                stackEnd;
55     u1*                usedStackTop;
56     s8                 time;
57     java_objectheader *exceptionptr;
58     struct _thread    *nextlive;
59     u1                 flags;
60 } ctx;
61
62 /*
63 struct _stringClass;
64 struct _object;
65 */
66
67 /* This structure mirrors java.lang.ThreadGroup.h */
68
69 typedef struct _threadGroup
70 {
71     java_objectheader    header;
72     struct _threadGroup* parent;
73     java_objectheader*   name;
74     s4                   maxPrio;
75     s4                   destroyed;
76     s4                   daemon;
77     s4                   nthreads;
78     java_objectheader*   threads;
79     s4                   ngroups;
80     java_objectheader*   groups;
81 } threadGroup;
82
83 /* This structure mirrors java.lang.Thread.h */
84 typedef struct _thread
85 {
86     java_objectheader        header;
87     java_objectheader*       name;
88     s4                       priority;
89     struct _thread*          next;
90     s8                       PrivateInfo;
91     struct java_lang_Object* eetop;           /* ??? */
92     s4                       single_step;
93     s4                       daemon;
94     s4                       stillborn;
95     java_objectheader*       target;
96     s4                       interruptRequested;
97     threadGroup*             group;
98 } thread;
99
100 void initThreads(u1 *stackbottom);
101 void clear_thread_flags(void);
102 void startThread(thread*);
103 void resumeThread(thread*);
104 void iresumeThread(thread*);
105 void suspendThread(thread*);
106 void suspendOnQThread(thread*, thread**);
107 void yieldThread(void);
108 void killThread(thread*);
109 void setPriorityThread(thread*, int);
110
111 void sleepThread(s8);
112 bool aliveThread(thread*);
113 long framesThread(thread*);
114
115 void reschedule(void);
116
117 void checkEvents(bool block);
118
119 extern int blockInts;
120 extern bool needReschedule;
121 extern thread *currentThread;
122 extern thread *mainThread;
123 extern ctx contexts[];
124 extern int threadStackSize;
125
126 extern thread *liveThreads;
127
128 extern thread *threadQhead[MAX_THREAD_PRIO + 1];
129
130 #define CONTEXT(_t)                                                     \
131         (contexts[(_t)->PrivateInfo - 1])
132
133 #if 1
134 #define intsDisable()   blockInts++
135 #define intsRestore()   if (blockInts == 1 && needReschedule) {         \
136                             reschedule();                               \
137                         }                                               \
138                         blockInts--
139 #else
140 #define intsDisable()   do { blockInts++; fprintf(stderr, "++: %d (%s:%d)\n", blockInts, __FILE__, __LINE__); } while (0)
141 #define intsRestore()   do { \
142                             if (blockInts == 1 && needReschedule) {     \
143                                 reschedule();                           \
144                             }                                           \
145                             blockInts--;                                \
146                             fprintf(stderr, "--: %d (%s:%d)\n", blockInts, __FILE__, __LINE__);     \
147                         } while (0)
148 #endif
149
150 #else
151
152 #define intsDisable()
153 #define intsRestore()
154
155 #endif
156
157 #endif