* src/threads/thread.cpp: Break a reference cycle.
[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         jlvmt.set_vmdata(t);
81 }
82
83 void ThreadRuntimeClasspath::print_thread_name(const java_lang_Thread& jlt, FILE *stream)
84 {
85         java_handle_t* name = jlt.get_name();
86         javastring_fprint(name, stream);
87 }
88
89 void ThreadRuntimeClasspath::set_javathread_state(threadobject *t, int state)
90 {
91 }
92
93 threadobject *ThreadRuntimeClasspath::get_threadobject_from_thread(java_handle_t *h)
94 {
95         java_lang_VMThread jlvmt(h);
96         return jlvmt.get_vmdata();
97 }
98
99 void ThreadRuntimeClasspath::thread_create_initial_threadgroups(java_handle_t **threadgroup_system, java_handle_t **threadgroup_main)
100 {
101         /* Allocate and initialize the main thread group. */
102
103         *threadgroup_main = native_new_and_init(class_java_lang_ThreadGroup);
104
105         if (*threadgroup_main == NULL)
106                 vm_abort("thread_create_initial_threadgroups: failed to allocate main threadgroup");
107
108         /* Use the same threadgroup for system as for main. */
109
110         *threadgroup_system = *threadgroup_main;
111 }
112
113 bool ThreadRuntimeClasspath::invoke_thread_initializer(java_lang_Thread& jlt, threadobject *t, methodinfo *thread_method_init, java_handle_t *name, java_handle_t *group)
114 {
115         java_handle_t *h = builtin_new(class_java_lang_VMThread);
116
117         if (h == NULL)
118                 return false;
119
120         // Create and initialize a java.lang.VMThread object.
121         java_lang_VMThread jlvmt(h, jlt.get_handle(), t);
122
123         /* Call:
124            java.lang.Thread.<init>(Ljava/lang/VMThread;Ljava/lang/String;IZ)V */
125
126         bool isdaemon = thread_is_daemon(t);
127
128         (void) vm_call_method(thread_method_init, jlt.get_handle(), jlvmt.get_handle(),
129                                                   name, NORM_PRIORITY, isdaemon);
130
131         if (exceptions_get_exception())
132                 return false;
133
134         // Set the ThreadGroup in the Java thread object.
135         jlt.set_group(group);
136
137         /* Add thread to the threadgroup. */
138
139         classinfo* c;
140         LLNI_class_get(group, c);
141
142         methodinfo* m = class_resolveclassmethod(c,
143                                                                                          utf_addThread,
144                                                                                          utf_java_lang_Thread__V,
145                                                                                          class_java_lang_ThreadGroup,
146                                                                                          true);
147
148         if (m == NULL)
149                 return false;
150
151         (void) vm_call_method(m, group, jlt.get_handle());
152
153         if (exceptions_get_exception())
154                 return false;
155
156         return true;
157 }
158
159 void ThreadRuntimeClasspath::clear_heap_reference(java_lang_Thread& jlt)
160 {
161         // Nothing to do.
162 }
163
164 #endif /* ENABLE_THREADS && WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH */
165
166
167 /*
168  * These are local overrides for various environment variables in Emacs.
169  * Please do not remove this and leave it at the end of the file, where
170  * Emacs will automagically detect them.
171  * ---------------------------------------------------------------------
172  * Local variables:
173  * mode: c++
174  * indent-tabs-mode: t
175  * c-basic-offset: 4
176  * tab-width: 4
177  * End:
178  * vim:noexpandtab:sw=4:ts=4:
179  */