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