* src/threads/posix/thread-posix.cpp, src/threads/thread.cpp,
[cacao.git] / src / threads / thread-classpath.cpp
1 /* src/threads/thread-classpath.cpp - thread functions specific to the GNU classpath 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-classpath.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_GNU_CLASSPATH)
39
40 classinfo *ThreadRuntimeClasspath::get_thread_class_from_object(java_handle_t *object) {
41         return class_java_lang_VMThread;
42 }
43
44 java_handle_t *ThreadRuntimeClasspath::get_vmthread_handle(const java_lang_Thread &jlt) {
45         java_lang_VMThread jlvmt(jlt.get_vmThread());
46         return jlvmt.get_handle();
47 }
48
49 java_handle_t *ThreadRuntimeClasspath::get_thread_exception_handler(const java_lang_Thread &jlt)
50 {
51         return jlt.get_exceptionHandler();
52 }
53
54 methodinfo *ThreadRuntimeClasspath::get_threadgroup_remove_method(classinfo *c)
55 {
56         return class_resolveclassmethod(c,
57                                                                         utf_removeThread,
58                                                                         utf_java_lang_Thread__V,
59                                                                         class_java_lang_ThreadGroup,
60                                                                         true);
61 }
62
63 methodinfo *ThreadRuntimeClasspath::get_thread_init_method()
64 {
65         return class_resolveclassmethod(class_java_lang_Thread,
66                                                                         utf_init,
67                                                                         utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
68                                                                         class_java_lang_Thread,
69                                                                         true);
70 }
71
72 void ThreadRuntimeClasspath::setup_thread_vmdata(const java_lang_Thread& jlt, threadobject *t)
73 {
74         /* Get the java.lang.VMThread object and do some sanity checks. */
75         java_lang_VMThread jlvmt(jlt.get_vmThread());
76
77         assert(jlvmt.get_handle() != NULL);
78         assert(jlvmt.get_vmdata() == NULL);
79
80         ThreadList::lock();
81         jlvmt.set_vmdata(t);
82         ThreadList::unlock();
83 }
84
85 void ThreadRuntimeClasspath::print_thread_name(const java_lang_Thread& jlt, FILE *stream)
86 {
87         java_handle_t* name = jlt.get_name();
88         javastring_fprint(name, stream);
89 }
90
91 void ThreadRuntimeClasspath::set_javathread_state(threadobject *t, int state)
92 {
93 }
94
95 threadobject *ThreadRuntimeClasspath::get_threadobject_from_thread(java_handle_t *h)
96 {
97         java_lang_VMThread jlvmt(h);
98         return jlvmt.get_vmdata();
99 }
100
101 void ThreadRuntimeClasspath::thread_create_initial_threadgroups(java_handle_t **threadgroup_system, java_handle_t **threadgroup_main)
102 {
103         /* Allocate and initialize the main thread group. */
104
105         *threadgroup_main = native_new_and_init(class_java_lang_ThreadGroup);
106
107         if (*threadgroup_main == NULL)
108                 vm_abort("thread_create_initial_threadgroups: failed to allocate main threadgroup");
109
110         /* Use the same threadgroup for system as for main. */
111
112         *threadgroup_system = *threadgroup_main;
113 }
114
115 bool ThreadRuntimeClasspath::invoke_thread_initializer(java_lang_Thread& jlt, threadobject *t, methodinfo *thread_method_init, java_handle_t *name, java_handle_t *group)
116 {
117         java_handle_t *h = builtin_new(class_java_lang_VMThread);
118
119         if (h == NULL)
120                 return false;
121
122         // Create and initialize a java.lang.VMThread object.
123         java_lang_VMThread jlvmt(h, jlt.get_handle(), t);
124
125         /* Call:
126            java.lang.Thread.<init>(Ljava/lang/VMThread;Ljava/lang/String;IZ)V */
127
128         bool isdaemon = thread_is_daemon(t);
129
130         (void) vm_call_method(thread_method_init, jlt.get_handle(), jlvmt.get_handle(),
131                                                   name, NORM_PRIORITY, isdaemon);
132
133         if (exceptions_get_exception())
134                 return false;
135
136         // Set the ThreadGroup in the Java thread object.
137         jlt.set_group(group);
138
139         /* Add thread to the threadgroup. */
140
141         classinfo* c;
142         LLNI_class_get(group, c);
143
144         methodinfo* m = class_resolveclassmethod(c,
145                                                                                          utf_addThread,
146                                                                                          utf_java_lang_Thread__V,
147                                                                                          class_java_lang_ThreadGroup,
148                                                                                          true);
149
150         if (m == NULL)
151                 return false;
152
153         (void) vm_call_method(m, group, jlt.get_handle());
154
155         if (exceptions_get_exception())
156                 return false;
157
158         return true;
159 }
160
161 #endif /* ENABLE_THREADS && WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH */
162
163
164 /*
165  * These are local overrides for various environment variables in Emacs.
166  * Please do not remove this and leave it at the end of the file, where
167  * Emacs will automagically detect them.
168  * ---------------------------------------------------------------------
169  * Local variables:
170  * mode: c++
171  * indent-tabs-mode: t
172  * c-basic-offset: 4
173  * tab-width: 4
174  * End:
175  * vim:noexpandtab:sw=4:ts=4:
176  */