1 /* src/vm/finalizer.c - finalizer linked list and thread
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Thalinger
31 $Id: finalizer.c 3545 2005-11-03 20:36:59Z twisti $
39 #include "mm/memory.h"
40 #include "native/jni.h"
41 #include "native/include/java_lang_Thread.h"
42 #include "native/include/gnu_classpath_Pointer.h"
43 #include "native/include/java_lang_VMThread.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/stringlocal.h"
48 #include "vm/jit/asmpart.h"
51 /* local structures ***********************************************************/
53 typedef struct finalizer_entry finalizer_entry;
55 struct finalizer_entry {
61 /* global variables ***********************************************************/
63 #if defined(USE_THREADS)
64 static java_lang_VMThread *finalizer_vmthread;
65 static java_objectheader *lock_finalizer_thread;
66 static list *finalizer_list;
70 /* finalizer_init **************************************************************
72 Initializes the finalizer global lock and the linked list.
74 *******************************************************************************/
76 bool finalizer_init(void)
78 #if defined(USE_THREADS)
79 lock_finalizer_thread = NEW(java_objectheader);
81 # if defined(NATIVE_THREADS)
82 initObjectLock(lock_finalizer_thread);
85 /* initialize the finalizer list */
87 finalizer_list = NEW(list);
88 list_init(finalizer_list, OFFSET(finalizer_entry, linkage));
97 /* finalizer_thread ************************************************************
99 This thread waits on an object for a notification and the runs the
100 finalizers (finalizer thread). This is necessary because of a
101 possible deadlock in the GC.
103 *******************************************************************************/
105 #if defined(USE_THREADS)
106 static void finalizer_thread(void)
109 java_objectheader *o;
111 /* get the lock on the finalizer lock object, so we can call wait */
114 /* wait forever (0, 0) on that object till we are signaled */
116 wait_cond_for_object(lock_finalizer_thread, 0, 0);
118 /* now handle all finalizers stored in the list */
120 fi = (finalizer_entry *) list_first(finalizer_list);
122 /* release the lock so other threads, or the finalizer thread
123 itself, can add new finalizers to the list */
125 builtin_monitorexit(lock_finalizer_thread);
127 /* is there actually a finalizer in the list? */
131 /* just for simpler code */
135 /* call the finalizer function */
137 asm_calljavafunction(o->vftbl->class->finalizer, o,
140 /* if we had an exception in the finalizer, ignore it */
142 *exceptionptr = NULL;
144 /* enter the monitor again, so we don't get finalizer
145 list race conditions */
147 builtin_monitorenter(lock_finalizer_thread);
149 /* remove and clear finalized entry */
151 list_remove(finalizer_list, fi);
154 FREE(fi, finalizer_entry);
156 /* get next finalizer from the list */
158 fi = list_first(finalizer_list);
160 /* leave the monitor again */
162 builtin_monitorexit(lock_finalizer_thread);
163 } while (fi != NULL);
166 builtin_monitorenter(lock_finalizer_thread);
171 /* finalizer_start_thread ******************************************************
173 Starts the finalizer thread.
175 *******************************************************************************/
177 #if defined(USE_THREADS)
178 bool finalizer_start_thread(void)
182 /* create the finalizer object */
185 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
187 if (!finalizer_vmthread)
190 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
192 t->vmThread = finalizer_vmthread;
193 t->name = javastring_new_char("Finalizer");
197 finalizer_vmthread->thread = t;
199 /* actually start the finalizer thread */
201 threads_start_thread(t, finalizer_thread);
203 /* everything's ok */
210 /* finalizer_add ***************************************************************
212 Adds a finalizer to be called to the linked list of finalizers.
214 *******************************************************************************/
216 void finalizer_add(void *o, void *p)
218 #if defined(USE_THREADS)
219 java_objectheader *ob;
222 ob = (java_objectheader *) o;
224 /* wait for the finalizer thread to finish finalizer list operations */
226 builtin_monitorenter(lock_finalizer_thread);
228 /* create finalizer entry, fill it and add it to the list */
230 fi = NEW(finalizer_entry);
233 list_addlast(finalizer_list, fi);
235 /* wakeup the finalizer thread */
237 signal_cond_for_object(lock_finalizer_thread);
239 /* release the lock after the entry is added */
241 builtin_monitorexit(lock_finalizer_thread);
243 java_objectheader *ob;
245 ob = (java_objectheader *) o;
247 /* call the finalizer function */
249 asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
251 /* if we had an exception in the finalizer, ignore it */
253 *exceptionptr = NULL;
259 * These are local overrides for various environment variables in Emacs.
260 * Please do not remove this and leave it at the end of the file, where
261 * Emacs will automagically detect them.
262 * ---------------------------------------------------------------------
265 * indent-tabs-mode: t