* src/threads/posix/threads.c: Renamed from here...
[cacao.git] / src / native / llni.h
1 /* src/native/llni.h - low level native interfarce (LLNI)
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _LLNI_H
27 #define _LLNI_H
28
29 #include "config.h"
30
31 /* forward defines ************************************************************/
32
33 /* LLNI wrapping / unwrapping macros *******************************************
34
35    ATTENTION: Only use these macros inside a LLNI critical section!
36    Once the ciritical section ends, all pointers onto the GC heap
37    retrieved through these macros are void!
38
39 *******************************************************************************/
40
41 #if defined(ENABLE_HANDLES)
42 # define LLNI_WRAP(obj)      ((obj) == NULL ? NULL : localref_add(obj))
43 # define LLNI_UNWRAP(hdl)    ((hdl) == NULL ? NULL : (hdl)->heap_object)
44 # define LLNI_QUICKWRAP(obj) ((obj) == NULL ? NULL : &(obj))
45 # define LLNI_DIRECT(hdl)    ((hdl)->heap_object)
46 #else
47 # define LLNI_WRAP(obj)      (obj)
48 # define LLNI_UNWRAP(hdl)    (hdl)
49 # define LLNI_QUICKWRAP(obj) (obj)
50 # define LLNI_DIRECT(hdl)    (hdl)
51 #endif
52
53
54 #include "native/localref.h"
55
56 #include "threads/thread.h"
57
58
59 /* LLNI macros *****************************************************************
60
61    The following macros should be used whenever a Java Object is
62    accessed in native code without the use of an JNI function.
63
64    LLNI_field_set_val, LLNI_field_get_val:
65      Deal with primitive values like integer and float values. Do
66      not use these macros to access pointers or references!
67
68    LLNI_field_set_ref, LLNI_field_get_ref:
69      Deal with references to other objects.
70
71    LLNI_field_set_cls, LLNI_field_get_cls:
72      Deal with references to Java Classes which are internally
73      represented by classinfo or java_lang_Class.
74
75 *******************************************************************************/
76
77 #define LLNI_field_set_val(obj, field, value) \
78         LLNI_field_direct(obj, field) = (value)
79
80 #define LLNI_field_set_ref(obj, field, reference) \
81         LLNI_field_direct(obj, field) = LLNI_UNWRAP(reference)
82
83 #define LLNI_field_set_cls(obj, field, value) \
84         LLNI_field_direct(obj, field) = (java_lang_Class *) (value)
85
86 #define LLNI_field_get_val(obj, field, variable) \
87         (variable) = LLNI_field_direct(obj, field)
88
89 #define LLNI_field_get_ref(obj, field, variable) \
90         (variable) = LLNI_WRAP(LLNI_field_direct(obj, field))
91
92 #define LLNI_field_get_cls(obj, field, variable) \
93         (variable) = (classinfo *) LLNI_field_direct(obj, field)
94
95 #define LLNI_class_get(obj, variable) \
96         (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->class)
97
98
99 /* LLNI_equals ****************************************************************
100  
101    Test if two java_handle_t* point to the same java_object_t*.
102
103 ******************************************************************************/
104
105 #define LLNI_equals(obj1, obj2, result) \
106         LLNI_CRITICAL_START; \
107         (result) = LLNI_UNWRAP(obj1) == LLNI_UNWRAP(obj2); \
108         LLNI_CRITICAL_END
109         
110
111 /* LLNI_classinfo_field_get ***************************************************
112  
113    Get a field from classinfo that is a java object.
114
115 ******************************************************************************/
116
117 #define LLNI_classinfo_field_get(cls, field, variable) \
118         LLNI_CRITICAL_START; \
119         (variable) = LLNI_WRAP((cls)->field); \
120         LLNI_CRITICAL_END
121
122
123 /* LLNI_classinfo_field_set ***************************************************
124  
125    Set a field from classinfo that is a java object.
126
127 ******************************************************************************/
128
129 #define LLNI_classinfo_field_set(cls, field, variable) \
130         LLNI_CRITICAL_START; \
131         (cls)->field = LLNI_UNWRAP(variable); \
132         LLNI_CRITICAL_END
133
134
135 /* LLNI classinfo wrapping / unwrapping macros *********************************
136
137    The following macros are used to wrap or unwrap a classinfo from
138    or into a handle (typically java_lang_Class). No critical sections
139    are needed here, because classinfos are not placed on the GC heap.
140
141    XXX This might change once we implement Class Unloading!
142
143 *******************************************************************************/
144
145 #define LLNI_classinfo_wrap(classinfo) \
146         ((java_lang_Class *) LLNI_WRAP(classinfo))
147
148 #define LLNI_classinfo_unwrap(clazz) \
149         ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
150
151
152 /* XXX the direct macros have to be used inside a critical section!!! */
153
154 #define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
155 #define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
156 #define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
157 #define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
158 #define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
159
160
161 /* LLNI critical sections ******************************************************
162
163    These macros handle the LLNI critical sections. While a critical
164    section is active, the absolute position of objects on the GC heap
165    is not allowed to change (no moving Garbage Collection).
166
167    ATTENTION: Use a critical section whenever you have a direct
168    pointer onto the GC heap!
169
170 *******************************************************************************/
171
172 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
173 # define LLNI_CRITICAL_START           llni_critical_start()
174 # define LLNI_CRITICAL_END             llni_critical_end()
175 # define LLNI_CRITICAL_START_THREAD(t) llni_critical_start_thread(t)
176 # define LLNI_CRITICAL_END_THREAD(t)   llni_critical_end_thread(t)
177 #else
178 # define LLNI_CRITICAL_START
179 # define LLNI_CRITICAL_END
180 # define LLNI_CRITICAL_START_THREAD(t)
181 # define LLNI_CRITICAL_END_THREAD(t)
182 #endif
183
184 void llni_critical_start();
185 void llni_critical_end();
186 void llni_critical_start_thread(threadobject *t);
187 void llni_critical_end_thread(threadobject *t);
188
189
190 #endif /* _LLNI_H */
191
192
193 /*
194  * These are local overrides for various environment variables in Emacs.
195  * Please do not remove this and leave it at the end of the file, where
196  * Emacs will automagically detect them.
197  * ---------------------------------------------------------------------
198  * Local variables:
199  * mode: c
200  * indent-tabs-mode: t
201  * c-basic-offset: 4
202  * tab-width: 4
203  * End:
204  */