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