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