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 3692 2005-11-17 13:35:49Z twisti $
39 #include "mm/memory.h"
40 #include "native/jni.h"
41 #include "native/include/java_lang_Thread.h"
42 #include "native/include/java_lang_VMThread.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
46 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
50 /* global variables ***********************************************************/
52 #if defined(USE_THREADS)
53 static java_lang_VMThread *finalizer_vmthread;
54 static java_objectheader *lock_finalizer_thread;
58 /* finalizer_init **************************************************************
60 Initializes the finalizer global lock and the linked list.
62 *******************************************************************************/
64 bool finalizer_init(void)
66 #if defined(USE_THREADS)
67 lock_finalizer_thread = NEW(java_objectheader);
69 # if defined(NATIVE_THREADS)
70 initObjectLock(lock_finalizer_thread);
80 /* finalizer_thread ************************************************************
82 This thread waits on an object for a notification and the runs the
83 finalizers (finalizer thread). This is necessary because of a
84 possible deadlock in the GC.
86 *******************************************************************************/
88 #if defined(USE_THREADS)
89 static void finalizer_thread(void)
92 /* get the lock on the finalizer lock object, so we can call wait */
94 builtin_monitorenter(lock_finalizer_thread);
96 /* wait forever (0, 0) on that object till we are signaled */
98 wait_cond_for_object(lock_finalizer_thread, 0, 0);
102 builtin_monitorexit(lock_finalizer_thread);
104 /* and call the finalizers */
106 gc_invoke_finalizers();
112 /* finalizer_start_thread ******************************************************
114 Starts the finalizer thread.
116 *******************************************************************************/
118 #if defined(USE_THREADS)
119 bool finalizer_start_thread(void)
123 /* create the finalizer object */
126 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
128 if (!finalizer_vmthread)
131 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
133 t->vmThread = finalizer_vmthread;
134 t->name = javastring_new_char("Finalizer");
138 finalizer_vmthread->thread = t;
140 /* actually start the finalizer thread */
142 threads_start_thread(t, finalizer_thread);
144 /* everything's ok */
151 /* finalizer_notify ************************************************************
153 Notifies the finalizer thread that it should run the
154 gc_invoke_finalizers from the GC.
156 *******************************************************************************/
158 void finalizer_notify(void)
160 #if defined(USE_THREADS)
161 /* get the lock on the finalizer lock object, so we can call wait */
163 builtin_monitorenter(lock_finalizer_thread);
165 /* signal the finalizer thread */
167 signal_cond_for_object(lock_finalizer_thread);
171 builtin_monitorexit(lock_finalizer_thread);
173 /* if we don't have threads, just run the finalizers */
175 gc_invoke_finalizers();
180 /* finalizer_run ***************************************************************
182 Actually run the finalizer functions.
184 *******************************************************************************/
186 void finalizer_run(void *o, void *p)
188 java_objectheader *ob = (java_objectheader *) o;
190 /* call the finalizer function */
192 asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
194 /* if we had an exception in the finalizer, ignore it */
196 *exceptionptr = NULL;
201 * These are local overrides for various environment variables in Emacs.
202 * Please do not remove this and leave it at the end of the file, where
203 * Emacs will automagically detect them.
204 * ---------------------------------------------------------------------
207 * indent-tabs-mode: t