Merged revisions 7797-7917 via svnmerge from
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: threads.h 7918 2007-05-20 20:42:18Z michi $
26
27 */
28
29
30 #ifndef _THREADS_H
31 #define _THREADS_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct threadobject threadobject;
36
37
38 #include "config.h"
39
40 #include <pthread.h>
41 #include <ucontext.h>
42
43 #include "vm/types.h"
44
45 #include "mm/memory.h"
46 #include "native/jni.h"
47 #include "native/include/java_lang_Thread.h"
48
49 #include "threads/native/lock.h"
50
51 #include "vm/global.h"
52
53 #if defined(ENABLE_GC_CACAO)
54 # include "vm/jit/replace.h"
55 #endif
56
57 #include "vm/jit/stacktrace.h"
58
59 #if defined(ENABLE_INTRP)
60 #include "vm/jit/intrp/intrp.h"
61 #endif
62
63 #if defined(__DARWIN__)
64 # include <mach/mach.h>
65
66 typedef struct {
67         pthread_mutex_t mutex;
68         pthread_cond_t cond;
69         int value;
70 } sem_t;
71
72 #else
73 # include <semaphore.h>
74 #endif
75
76
77 /* current threadobject *******************************************************/
78
79 #if defined(HAVE___THREAD)
80
81 #define THREADSPECIFIC    __thread
82 #define THREADOBJECT      threads_current_threadobject
83
84 extern __thread threadobject *threads_current_threadobject;
85
86 #else /* defined(HAVE___THREAD) */
87
88 #define THREADSPECIFIC
89 #define THREADOBJECT \
90         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
91
92 extern pthread_key_t threads_current_threadobject_key;
93
94 #endif /* defined(HAVE___THREAD) */
95
96
97 /* threadobject ****************************************************************
98
99    Struct holding thread local variables.
100
101 *******************************************************************************/
102
103 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
104 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
105 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
106 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
107
108 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
109 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
110
111
112 struct threadobject {
113         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
114
115         ptrint                thinlock;     /* pre-computed thin lock value       */
116
117         s4                    index;        /* thread index, starting with 1      */
118         u4                    flags;        /* flag field                         */
119         u4                    state;        /* state field                        */
120
121         pthread_t             tid;          /* pthread id                         */
122
123 #if defined(__DARWIN__)
124         mach_port_t           mach_thread;       /* Darwin thread id              */
125 #endif
126
127         /* these are used for the wait/notify implementation                      */
128         pthread_mutex_t       waitmutex;
129         pthread_cond_t        waitcond;
130
131         pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
132         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
133
134         bool                  interrupted;
135         bool                  signaled;
136         bool                  sleeping;
137
138         bool                  suspended;    /* is this thread suspended?          */
139         s4                    suspend_reason; /* reason for suspending            */
140
141         u1                   *pc;           /* current PC (used for profiling)    */
142
143         java_objectheader    *_exceptionptr;     /* current exception             */
144         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
145         localref_table       *_localref_table;   /* JNI local references          */
146
147 #if defined(ENABLE_INTRP)
148         Cell                 *_global_sp;        /* stack pointer for interpreter */
149 #endif
150
151 #if defined(ENABLE_GC_CACAO)
152         bool                  gc_critical;  /* indicates a critical section       */
153
154         sourcestate_t        *ss;
155         executionstate_t     *es;
156 #endif
157
158         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
159 };
160
161
162 /* exception pointer **********************************************************/
163
164 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
165
166
167 /* stackframeinfo *************************************************************/
168
169 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
170
171
172 /* functions ******************************************************************/
173
174 void threads_sem_init(sem_t *sem, bool shared, int value);
175 void threads_sem_wait(sem_t *sem);
176 void threads_sem_post(sem_t *sem);
177
178 threadobject *threads_get_current_threadobject(void);
179
180 bool threads_init(void);
181
182 void threads_start_thread(threadobject *thread, functionptr function);
183
184 void threads_set_thread_priority(pthread_t tid, int priority);
185
186 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
187 bool threads_detach_thread(threadobject *thread);
188
189 bool threads_suspend_thread(threadobject *thread, s4 reason);
190 void threads_suspend_ack(u1* pc, u1* sp);
191 bool threads_resume_thread(threadobject *thread);
192
193 void threads_join_all_threads(void);
194
195 void threads_sleep(s8 millis, s4 nanos);
196 void threads_yield(void);
197
198 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
199
200 void threads_thread_interrupt(threadobject *thread);
201 bool threads_check_if_interrupted_and_reset(void);
202 bool threads_thread_has_been_interrupted(threadobject *thread);
203
204 #if !defined(DISABLE_GC)
205 void threads_stopworld(void);
206 void threads_startworld(void);
207 #endif
208
209 #endif /* _THREADS_H */
210
211
212 /*
213  * These are local overrides for various environment variables in Emacs.
214  * Please do not remove this and leave it at the end of the file, where
215  * Emacs will automagically detect them.
216  * ---------------------------------------------------------------------
217  * Local variables:
218  * mode: c
219  * indent-tabs-mode: t
220  * c-basic-offset: 4
221  * tab-width: 4
222  * End:
223  * vim:noexpandtab:sw=4:ts=4:
224  */