175b05cbc8a5b0733808b05b610935fdf205b17f
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    $Id: finalizer.c 6228 2006-12-26 19:56:58Z twisti $
30
31 */
32
33
34 #include "config.h"
35
36 #include <stdlib.h>
37
38 #include "vm/types.h"
39
40 #include "mm/memory.h"
41 #include "native/jni.h"
42 #include "native/include/java_lang_Thread.h"
43 #include "native/include/java_lang_VMThread.h"
44
45 #if defined(ENABLE_THREADS)
46 # include "threads/native/lock.h"
47 #endif
48
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/global.h"
52 #include "vm/options.h"
53 #include "vm/stringlocal.h"
54 #include "vm/vm.h"
55 #include "vm/jit/asmpart.h"
56
57
58 /* global variables ***********************************************************/
59
60 #if defined(ENABLE_THREADS)
61 static threadobject      *thread_finalizer;
62 static java_objectheader *lock_thread_finalizer;
63 #endif
64
65
66 /* finalizer_init **************************************************************
67
68    Initializes the finalizer global lock and the linked list.
69
70 *******************************************************************************/
71
72 bool finalizer_init(void)
73 {
74 #if defined(ENABLE_THREADS)
75         lock_thread_finalizer = NEW(java_objectheader);
76
77         lock_init_object_lock(lock_thread_finalizer);
78 #endif
79
80         /* everything's ok */
81
82         return true;
83 }
84
85
86 /* finalizer_thread ************************************************************
87
88    This thread waits on an object for a notification and the runs the
89    finalizers (finalizer thread).  This is necessary because of a
90    possible deadlock in the GC.
91
92 *******************************************************************************/
93
94 #if defined(ENABLE_THREADS)
95 static void finalizer_thread(void)
96 {
97         while (true) {
98                 /* get the lock on the finalizer lock object, so we can call wait */
99
100                 lock_monitor_enter(lock_thread_finalizer);
101
102                 /* wait forever (0, 0) on that object till we are signaled */
103         
104                 lock_wait_for_object(lock_thread_finalizer, 0, 0);
105
106                 /* leave the lock */
107
108                 lock_monitor_exit(lock_thread_finalizer);
109
110                 /* and call the finalizers */
111
112                 gc_invoke_finalizers();
113         }
114 }
115 #endif
116
117
118 /* finalizer_start_thread ******************************************************
119
120    Starts the finalizer thread.
121
122 *******************************************************************************/
123
124 #if defined(ENABLE_THREADS)
125 bool finalizer_start_thread(void)
126 {
127 #if defined(WITH_CLASSPATH_GNU)
128         java_lang_VMThread *vmt;
129 #endif
130
131         /* create the finalizer object */
132
133         thread_finalizer = (threadobject *) builtin_new(class_java_lang_Thread);
134
135         if (thread_finalizer == NULL)
136                 return false;
137
138 #if defined(WITH_CLASSPATH_GNU)
139         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
140
141         vmt->thread = (java_lang_Thread *) thread_finalizer;
142
143         thread_finalizer->o.vmThread = vmt;
144 #endif
145
146         thread_finalizer->o.name     = javastring_new_from_ascii("Finalizer");
147         thread_finalizer->o.daemon   = true;
148         thread_finalizer->o.priority = 5;
149
150         /* actually start the finalizer thread */
151
152         threads_start_thread(thread_finalizer, finalizer_thread);
153
154         /* everything's ok */
155
156         return true;
157 }
158 #endif
159
160
161 /* finalizer_notify ************************************************************
162
163    Notifies the finalizer thread that it should run the
164    gc_invoke_finalizers from the GC.
165
166 *******************************************************************************/
167
168 void finalizer_notify(void)
169 {
170 #if defined(ENABLE_THREADS)
171         /* get the lock on the finalizer lock object, so we can call wait */
172
173         lock_monitor_enter(lock_thread_finalizer);
174
175         /* signal the finalizer thread */
176         
177         lock_notify_object(lock_thread_finalizer);
178
179         /* leave the lock */
180
181         lock_monitor_exit(lock_thread_finalizer);
182 #else
183         /* if we don't have threads, just run the finalizers */
184
185         gc_invoke_finalizers();
186 #endif
187 }
188
189
190 /* finalizer_run ***************************************************************
191
192    Actually run the finalizer functions.
193
194 *******************************************************************************/
195
196 void finalizer_run(void *o, void *p)
197 {
198         java_objectheader *ob;
199
200         ob = (java_objectheader *) o;
201
202         /* call the finalizer function */
203
204         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
205
206         /* if we had an exception in the finalizer, ignore it */
207
208         *exceptionptr = NULL;
209 }
210
211
212 /*
213  * These are local overrides for various environment variables in Emacs.
214  * Please do not remove this and leave it at the end of the file, where
215  * Emacs will automagically detect them.
216  * ---------------------------------------------------------------------
217  * Local variables:
218  * mode: c
219  * indent-tabs-mode: t
220  * c-basic-offset: 4
221  * tab-width: 4
222  * End:
223  */