PR162: Make class init protection aware of multiple threads.
[cacao.git] / src / vm / initialize.cpp
1 /* src/vm/initialize.cpp - static class initializer functions
2
3    Copyright (C) 1996-2011
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 #include "config.h"
27
28 #include <string.h>
29
30 #include "vm/types.h"
31
32 #include "threads/lock.hpp"
33
34 #include "vm/jit/builtin.hpp"
35 #include "vm/class.hpp"
36 #include "vm/exceptions.hpp"
37 #include "vm/global.h"
38 #include "vm/globals.hpp"
39 #include "vm/initialize.hpp"
40 #include "vm/loader.hpp"
41 #include "vm/options.h"
42 #include "vm/vm.hpp"
43
44 #if defined(ENABLE_STATISTICS)
45 # include "vm/statistics.h"
46 #endif
47
48 #include "vm/jit/asmpart.h"
49
50
51 /* private functions **********************************************************/
52
53 static bool initialize_class_intern(classinfo *c);
54
55
56 /* initialize_init *************************************************************
57
58    Initialize important system classes.
59
60 *******************************************************************************/
61
62 void initialize_init(void)
63 {
64         TRACESUBSYSTEMINITIALIZATION("initialize_init");
65
66 #if defined(ENABLE_JAVASE)
67 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
68
69         /* Nothing. */
70
71 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
72
73         if (!initialize_class(class_java_lang_String))
74                 vm_abort("initialize_init: Initialization failed: java.lang.String");
75
76         if (!initialize_class(class_java_lang_System))
77                 vm_abort("initialize_init: Initialization failed: java.lang.System");
78
79         if (!initialize_class(class_java_lang_ThreadGroup))
80                 vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
81
82         if (!initialize_class(class_java_lang_Thread))
83                 vm_abort("initialize_init: Initialization failed: java.lang.Thread");
84
85 # else
86 #  error unknown classpath configuration
87 # endif
88
89 #elif defined(ENABLE_JAVAME_CLDC1_1)
90
91         /* Nothing. */
92
93 #else
94 # error unknown Java configuration
95 #endif
96 }
97
98 /* initialize_class ************************************************************
99
100    In Java, every class can have a static initialization
101    function. This function has to be called BEFORE calling other
102    methods or accessing static variables.
103
104 *******************************************************************************/
105
106 bool initialize_class(classinfo *c)
107 {
108         bool r;
109
110         if (!makeinitializations)
111                 return true;
112
113         LOCK_MONITOR_ENTER(c);
114
115         /* maybe the class is already initalized or the current thread, which can
116            pass the monitor, is currently initalizing this class */
117
118         if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
119                 LOCK_MONITOR_EXIT(c);
120
121                 return true;
122         }
123
124         /* if <clinit> throw an Error before, the class was marked with an
125        error and we have to throw a NoClassDefFoundError */
126
127         if (c->state & CLASS_ERROR) {
128                 exceptions_throw_noclassdeffounderror(c->name);
129
130                 LOCK_MONITOR_EXIT(c);
131
132                 /* ...but return true, this is ok (mauve test) */
133
134                 return true;
135         }
136
137         /* this initalizing run begins NOW */
138
139         c->initializing_thread = thread_get_current();
140         c->state |= CLASS_INITIALIZING;
141
142         /* call the internal function */
143
144         r = initialize_class_intern(c);
145
146         /* if return value is not NULL everything was ok and the class is
147            initialized */
148
149         if (r) {
150         // Let's make sure that everything is flushed out to memory before
151         // marking the class as initialized.
152         Atomic::write_memory_barrier();
153
154         c->state |= CLASS_INITIALIZED;
155     }
156
157         /* this initalizing run is done */
158
159         c->state &= ~CLASS_INITIALIZING;
160
161         LOCK_MONITOR_EXIT(c);
162
163         return r;
164 }
165
166
167 /* initialize_class_intern *****************************************************
168
169    This function MUST NOT be called directly, because of thread
170    <clinit> race conditions.
171
172 *******************************************************************************/
173
174 static bool initialize_class_intern(classinfo *c)
175 {
176         methodinfo    *m;
177         java_handle_t *cause;
178         classinfo     *clazz;
179
180         /* maybe the class is not already linked */
181
182         if (!(c->state & CLASS_LINKED))
183                 if (!link_class(c))
184                         return false;
185
186 #if defined(ENABLE_STATISTICS)
187         if (opt_stat)
188                 count_class_inits++;
189 #endif
190
191         /* Initialize super class. */
192
193         if (c->super != NULL) {
194                 if (!(c->super->state & CLASS_INITIALIZED)) {
195 #if !defined(NDEBUG)
196                         if (initverbose)
197                                 log_message_class_message_class("Initialize super class ",
198                                                                                                 c->super,
199                                                                                                 " from ",
200                                                                                                 c);
201 #endif
202
203                         if (!initialize_class(c->super))
204                                 return false;
205                 }
206         }
207
208         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
209
210         m = class_findmethod(c, utf_clinit, utf_void__void);
211
212         if (m == NULL) {
213 #if !defined(NDEBUG)
214                 if (initverbose)
215                         log_message_class("Class has no static class initializer: ", c);
216 #endif
217
218                 return true;
219         }
220
221         /* Sun's and IBM's JVM don't care about the static flag */
222 /*      if (!(m->flags & ACC_STATIC)) { */
223 /*              log_text("Class initializer is not static!"); */
224
225 #if !defined(NDEBUG)
226         if (initverbose)
227                 log_message_class("Starting static class initializer for class: ", c);
228 #endif
229
230         /* now call the initializer */
231
232         (void) vm_call_method(m, NULL);
233
234         /* we have an exception or error */
235
236         cause = exceptions_get_exception();
237
238         if (cause != NULL) {
239                 /* class is NOT initialized and is marked with error */
240
241                 c->state |= CLASS_ERROR;
242
243                 /* Load java/lang/Exception for the instanceof check. */
244
245                 clazz = load_class_bootstrap(utf_java_lang_Exception);
246
247                 if (clazz == NULL)
248                         return false;
249
250                 /* Is this an exception?  Yes, than wrap it. */
251
252                 if (builtin_instanceof(cause, clazz)) {
253                         /* clear exception, because we are calling jit code again */
254
255                         exceptions_clear_exception();
256
257                         /* wrap the exception */
258
259                         exceptions_throw_exceptionininitializererror(cause);
260                 }
261
262                 return false;
263         }
264
265 #if !defined(NDEBUG)
266         if (initverbose)
267                 log_message_class("Finished static class initializer for class: ", c);
268 #endif
269
270         return true;
271 }
272
273
274 /*
275  * These are local overrides for various environment variables in Emacs.
276  * Please do not remove this and leave it at the end of the file, where
277  * Emacs will automagically detect them.
278  * ---------------------------------------------------------------------
279  * Local variables:
280  * mode: c++
281  * indent-tabs-mode: t
282  * c-basic-offset: 4
283  * tab-width: 4
284  * vim:noexpandtab:sw=4:ts=4:
285  * End:
286  */