fecda2fc20506003351880bb1c1e2f07df69ccae
[cacao.git] / src / threads / thread-openjdk.cpp
1 /* src/threads/thread-openjdk.cpp - thread functions specific to the OpenJDK library
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 "thread-openjdk.hpp"
27
28 #include "vm/global.h"
29 #include "vm/globals.hpp"
30
31 #include "mm/gc.hpp"
32 #include "vm/globals.hpp"
33 #include "vm/javaobjects.hpp"
34 #include "vm/exceptions.hpp"
35
36 #include "threadlist.hpp"
37
38 #if defined(ENABLE_THREADS) && defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
39
40 classinfo *ThreadRuntimeOpenjdk::get_thread_class_from_object(java_handle_t *object) {
41         classinfo *c;
42         LLNI_class_get(object, c);
43         return c;
44 }
45
46 java_handle_t *ThreadRuntimeOpenjdk::get_vmthread_handle(const java_lang_Thread &jlt) {
47         return jlt.get_handle();
48 }
49
50 java_handle_t *ThreadRuntimeOpenjdk::get_thread_exception_handler(const java_lang_Thread &jlt)
51 {
52         return jlt.get_uncaughtExceptionHandler();
53 }
54
55 methodinfo *ThreadRuntimeOpenjdk::get_threadgroup_remove_method(classinfo *c)
56 {
57         return class_resolveclassmethod(c,
58                                                                         utf_remove,
59                                                                         utf_java_lang_Thread__V,
60                                                                         class_java_lang_ThreadGroup,
61                                                                         true);
62 }
63
64 methodinfo *ThreadRuntimeOpenjdk::get_thread_init_method()
65 {
66         return class_resolveclassmethod(class_java_lang_Thread,
67                                                                         utf_init,
68                                                                         utf_new_char("(Ljava/lang/ThreadGroup;Ljava/lang/String;)V"),
69                                                                         class_java_lang_Thread,
70                                                                         true);
71 }
72
73 void ThreadRuntimeOpenjdk::setup_thread_vmdata(const java_lang_Thread& jlt, threadobject *t)
74 {
75         // Nothing to do.
76 }
77
78 void ThreadRuntimeOpenjdk::print_thread_name(const java_lang_Thread& jlt, FILE *stream)
79 {
80         /* FIXME: In OpenJDK and CLDC the name is a char[]. */
81         //java_chararray_t *name;
82
83         /* FIXME This prints to stdout. */
84         utf_display_printable_ascii(utf_null);
85 }
86
87 void ThreadRuntimeOpenjdk::set_javathread_state(threadobject *t, int state)
88 {
89         // Set the state of the java.lang.Thread object.
90         java_lang_Thread thread(LLNI_WRAP(t->object));
91         assert(thread.is_non_null());
92         thread.set_threadStatus(state);
93 }
94
95 threadobject *ThreadRuntimeOpenjdk::get_threadobject_from_thread(java_handle_t *h)
96 {
97         /* XXX This is just a quick hack. */
98         return ThreadList::get_thread_from_java_object(h);
99 }
100
101 void ThreadRuntimeOpenjdk::thread_create_initial_threadgroups(java_handle_t **threadgroup_system, java_handle_t **threadgroup_main)
102 {
103         java_handle_t *name;
104         methodinfo    *m;
105
106         /* Allocate and initialize the system thread group. */
107
108         *threadgroup_system = native_new_and_init(class_java_lang_ThreadGroup);
109
110         if (*threadgroup_system == NULL)
111                 vm_abort("thread_create_initial_threadgroups: failed to allocate system threadgroup");
112
113         /* Allocate and initialize the main thread group. */
114
115         *threadgroup_main = builtin_new(class_java_lang_ThreadGroup);
116
117         if (*threadgroup_main == NULL)
118                 vm_abort("thread_create_initial_threadgroups: failed to allocate main threadgroup");
119
120         name = javastring_new(utf_main);
121
122         m = class_resolveclassmethod(class_java_lang_ThreadGroup,
123                                                                  utf_init,
124                                                                  utf_Ljava_lang_ThreadGroup_Ljava_lang_String__V,
125                                                                  class_java_lang_ThreadGroup,
126                                                                  true);
127
128         if (m == NULL)
129                 vm_abort("thread_create_initial_threadgroups: failed to resolve threadgroup init method");
130
131         (void) vm_call_method(m, *threadgroup_main, *threadgroup_system, name);
132
133         if (exceptions_get_exception())
134                 vm_abort("thread_create_initial_threadgroups: exception while initializing main threadgroup");
135
136 }
137
138 bool ThreadRuntimeOpenjdk::invoke_thread_initializer(java_lang_Thread& jlt, threadobject *t, methodinfo *thread_method_init, java_handle_t *name, java_handle_t *group)
139 {
140         /* Set the priority.  java.lang.Thread.<init> requires it because
141            it sets the priority of the current thread to the parent's one
142            (which is the current thread in this case). */
143         jlt.set_priority(NORM_PRIORITY);
144
145         // Call: java.lang.Thread.<init>(Ljava/lang/ThreadGroup;Ljava/lang/String;)V
146
147         (void) vm_call_method(thread_method_init, jlt.get_handle(), group, name);
148
149         if (exceptions_get_exception())
150                 return false;
151
152         return true;
153 }
154
155 #endif /* ENABLE_THREADS && WITH_JAVA_RUNTIME_LIBRARY_OPENJDK */
156
157
158 /*
159  * These are local overrides for various environment variables in Emacs.
160  * Please do not remove this and leave it at the end of the file, where
161  * Emacs will automagically detect them.
162  * ---------------------------------------------------------------------
163  * Local variables:
164  * mode: c++
165  * indent-tabs-mode: t
166  * c-basic-offset: 4
167  * tab-width: 4
168  * End:
169  * vim:noexpandtab:sw=4:ts=4:
170  */