7c0dcf096c19ad011ec07a9830c98f7b704b78d1
[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 8304 2007-08-14 19:57:20Z pm $
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/localref.h"
48 #include "native/include/java_lang_Thread.h"
49
50 #include "threads/native/lock.h"
51
52 #include "vm/global.h"
53
54 #include "vm/jit/stacktrace.h"
55
56 #if defined(ENABLE_INTRP)
57 #include "vm/jit/intrp/intrp.h"
58 #endif
59
60 #if defined(__DARWIN__)
61 # include <mach/mach.h>
62
63 typedef struct {
64         pthread_mutex_t mutex;
65         pthread_cond_t cond;
66         int value;
67 } sem_t;
68
69 #else
70 # include <semaphore.h>
71 #endif
72
73
74 /* current threadobject *******************************************************/
75
76 #if defined(HAVE___THREAD)
77
78 #define THREADSPECIFIC    __thread
79 #define THREADOBJECT      threads_current_threadobject
80
81 extern __thread threadobject *threads_current_threadobject;
82
83 #else /* defined(HAVE___THREAD) */
84
85 #define THREADSPECIFIC
86 #define THREADOBJECT \
87         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
88
89 extern pthread_key_t threads_current_threadobject_key;
90
91 #endif /* defined(HAVE___THREAD) */
92
93
94 /* threadobject ****************************************************************
95
96    Struct holding thread local variables.
97
98 *******************************************************************************/
99
100 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
101 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
102 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
103
104
105 struct threadobject {
106         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
107
108         ptrint                thinlock;     /* pre-computed thin lock value       */
109
110         s4                    index;        /* thread index, starting with 1      */
111         u4                    flags;        /* flag field                         */
112         u4                    state;        /* state field                        */
113
114         pthread_t             tid;          /* pthread id                         */
115
116 #if defined(__DARWIN__)
117         mach_port_t           mach_thread;       /* Darwin thread id              */
118 #endif
119
120         /* these are used for the wait/notify implementation                      */
121         pthread_mutex_t       waitmutex;
122         pthread_cond_t        waitcond;
123
124         bool                  interrupted;
125         bool                  signaled;
126         bool                  sleeping;
127
128         u1                   *pc;           /* current PC (used for profiling)    */
129
130         java_object_t        *_exceptionptr;     /* current exception             */
131         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
132         localref_table       *_localref_table;   /* JNI local references          */
133
134 #if defined(ENABLE_INTRP)
135         Cell                 *_global_sp;        /* stack pointer for interpreter */
136 #endif
137
138         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
139
140 #if defined(ENABLE_DEBUG_FILTER)
141         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
142 #endif
143
144 #if !defined(NDEBUG)
145         s4                    tracejavacallindent;
146         u4                    tracejavacallcount;
147 #endif
148
149         listnode_t            linkage;      /* threads-list                       */
150 };
151
152
153 /* exception pointer **********************************************************/
154
155 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
156
157
158 /* stackframeinfo *************************************************************/
159
160 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
161
162 /* counter for verbose call filter ********************************************/
163
164 #if defined(ENABLE_DEBUG_FILTER)
165 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
166 #endif
167
168 /* state for trace java call **************************************************/
169
170 #if !defined(NDEBUG)
171 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
172 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
173 #endif
174
175 /* functions ******************************************************************/
176
177 void threads_sem_init(sem_t *sem, bool shared, int value);
178 void threads_sem_wait(sem_t *sem);
179 void threads_sem_post(sem_t *sem);
180
181 threadobject *threads_get_current_threadobject(void);
182
183 bool threads_init(void);
184
185 void threads_start_thread(threadobject *thread, functionptr function);
186
187 void threads_set_thread_priority(pthread_t tid, int priority);
188
189 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
190 bool threads_detach_thread(threadobject *thread);
191
192 void threads_join_all_threads(void);
193
194 void threads_sleep(s8 millis, s4 nanos);
195
196 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
197
198 void threads_thread_interrupt(threadobject *thread);
199 bool threads_check_if_interrupted_and_reset(void);
200 bool threads_thread_has_been_interrupted(threadobject *thread);
201
202 void threads_cast_stopworld(void);
203 void threads_cast_startworld(void);
204
205 #endif /* _THREADS_H */
206
207
208 /*
209  * These are local overrides for various environment variables in Emacs.
210  * Please do not remove this and leave it at the end of the file, where
211  * Emacs will automagically detect them.
212  * ---------------------------------------------------------------------
213  * Local variables:
214  * mode: c
215  * indent-tabs-mode: t
216  * c-basic-offset: 4
217  * tab-width: 4
218  * End:
219  * vim:noexpandtab:sw=4:ts=4:
220  */