b0d29818cb86ee3daf12e98582ca615f2a15fcd0
[cacao.git] / src / threads / native / threads.h
1 #ifndef _NATIVETHREAD_H
2 #define _NATIVETHREAD_H
3
4 #include <semaphore.h>
5
6 #include "jni.h"
7 #include "nat/java_lang_Object.h"
8 #include "nat/java_lang_Throwable.h"
9 #include "nat/java_lang_Thread.h"
10 #include "nat/java_lang_VMThread.h"
11 #include "toolbox/memory.h"
12
13 #if defined(__DARWIN__)
14 #include <mach/mach.h>
15
16 /* We need to emulate recursive mutexes. */
17 #define MUTEXSIM
18 #endif
19
20 struct _threadobject;
21
22
23 typedef struct monitorLockRecord monitorLockRecord;
24
25 struct monitorLockRecord {
26         struct _threadobject *ownerThread;
27         java_objectheader *o;
28         int                lockCount;
29         monitorLockRecord *nextFree;
30         int                queuers;
31         monitorLockRecord *waiter;
32         monitorLockRecord *incharge;
33         bool               waiting;
34         sem_t              queueSem;
35         pthread_mutex_t    resolveLock;
36         pthread_cond_t     resolveWait;
37 };
38
39
40 struct _lockRecordPool;
41
42 typedef struct {
43         struct _lockRecordPool *next;
44         int size;
45 } lockRecordPoolHeader; 
46
47 typedef struct _lockRecordPool {
48         lockRecordPoolHeader header;
49         monitorLockRecord lr[1];
50 } lockRecordPool;
51
52 /* Monitor lock implementation */
53 typedef struct {
54         monitorLockRecord *firstLR;
55         lockRecordPool *lrpool;
56         int numlr;
57 } ExecEnvironment;
58
59 typedef struct {
60         struct _threadobject *next, *prev;
61         java_objectheader *_exceptionptr;
62         methodinfo *_threadrootmethod;
63         void *_stackframeinfo;
64         pthread_t tid;
65 #if defined(__DARWIN__)
66         mach_port_t mach_thread;
67 #endif
68         pthread_mutex_t joinMutex;
69         pthread_cond_t joinCond;
70 } nativethread;
71
72 typedef java_lang_Thread thread;
73
74
75 /* threadobject ****************************************************************
76
77    TODO
78
79 *******************************************************************************/
80
81 typedef struct _threadobject {
82         java_lang_VMThread  o;
83         nativethread        info;
84         ExecEnvironment     ee;
85
86         pthread_mutex_t     waitLock;
87         pthread_cond_t      waitCond;
88         bool                interrupted;
89         bool                signaled;
90         bool                isSleeping;
91
92         dumpinfo            dumpinfo;       /* dump memory info structure         */
93 } threadobject;
94
95
96 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
97 bool monitorExit(threadobject *, java_objectheader *);
98
99 bool threadHoldsLock(threadobject *t, java_objectheader *o);
100 void signal_cond_for_object (java_objectheader *obj);
101 void broadcast_cond_for_object (java_objectheader *obj);
102 void wait_cond_for_object (java_objectheader *obj, s8 time, s4 nanos);
103
104 void initThreadsEarly();
105 void initThreads(u1 *stackbottom);
106 void initObjectLock(java_objectheader *);
107 void initLocks();
108 void initThread(java_lang_VMThread *);
109 void startThread(thread *t);
110 void joinAllThreads();
111
112 void sleepThread(s8 millis, s4 nanos);
113 void yieldThread();
114
115 void setPriorityThread(thread *t, s4 priority);
116
117 void interruptThread(java_lang_VMThread *);
118 bool interruptedThread();
119 bool isInterruptedThread(java_lang_VMThread *);
120
121 #if !defined(HAVE___THREAD)
122 extern pthread_key_t tkey_threadinfo;
123 #define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
124 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
125 #else
126 extern __thread threadobject *threadobj;
127 #define THREADOBJECT ((java_lang_VMThread*) threadobj)
128 #define THREADINFO (&threadobj->info)
129 #endif
130
131 #include "builtin.h"
132
133 /* This must not be changed, it is used in asm_criticalsections */
134 typedef struct {
135         u1 *mcodebegin, *mcodeend, *mcoderestart;
136 } threadcritnode;
137
138 void thread_registercritical(threadcritnode *);
139 u1 *thread_checkcritical(u1*);
140
141 extern volatile int stopworldwhere;
142
143 void cast_stopworld();
144 void cast_startworld();
145
146 #endif /* _NATIVETHREAD_H */
147
148
149 /*
150  * These are local overrides for various environment variables in Emacs.
151  * Please do not remove this and leave it at the end of the file, where
152  * Emacs will automagically detect them.
153  * ---------------------------------------------------------------------
154  * Local variables:
155  * mode: c
156  * indent-tabs-mode: t
157  * c-basic-offset: 4
158  * tab-width: 4
159  * End:
160  */