1 /* src/vm/finalizer.c - finalizer linked list and thread
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
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
31 $Id: finalizer.c 4357 2006-01-22 23:33:38Z twisti $
42 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/include/java_lang_Thread.h"
45 #include "native/include/java_lang_VMThread.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/options.h"
50 #include "vm/stringlocal.h"
51 #include "vm/jit/asmpart.h"
54 /* global variables ***********************************************************/
56 #if defined(USE_THREADS)
57 static java_lang_VMThread *finalizer_vmthread;
58 static java_objectheader *lock_finalizer_thread;
62 /* finalizer_init **************************************************************
64 Initializes the finalizer global lock and the linked list.
66 *******************************************************************************/
68 bool finalizer_init(void)
70 #if defined(USE_THREADS)
71 lock_finalizer_thread = NEW(java_objectheader);
73 # if defined(NATIVE_THREADS)
74 initObjectLock(lock_finalizer_thread);
84 /* finalizer_thread ************************************************************
86 This thread waits on an object for a notification and the runs the
87 finalizers (finalizer thread). This is necessary because of a
88 possible deadlock in the GC.
90 *******************************************************************************/
92 #if defined(USE_THREADS)
93 static void finalizer_thread(void)
96 /* get the lock on the finalizer lock object, so we can call wait */
98 builtin_monitorenter(lock_finalizer_thread);
100 /* wait forever (0, 0) on that object till we are signaled */
102 wait_cond_for_object(lock_finalizer_thread, 0, 0);
106 builtin_monitorexit(lock_finalizer_thread);
108 /* and call the finalizers */
110 gc_invoke_finalizers();
116 /* finalizer_start_thread ******************************************************
118 Starts the finalizer thread.
120 *******************************************************************************/
122 #if defined(USE_THREADS)
123 bool finalizer_start_thread(void)
127 /* create the finalizer object */
130 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
132 if (!finalizer_vmthread)
135 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
137 t->vmThread = finalizer_vmthread;
138 t->name = javastring_new_char("Finalizer");
142 finalizer_vmthread->thread = t;
144 /* actually start the finalizer thread */
146 threads_start_thread(t, finalizer_thread);
148 /* everything's ok */
155 /* finalizer_notify ************************************************************
157 Notifies the finalizer thread that it should run the
158 gc_invoke_finalizers from the GC.
160 *******************************************************************************/
162 void finalizer_notify(void)
164 #if defined(USE_THREADS)
165 /* get the lock on the finalizer lock object, so we can call wait */
167 builtin_monitorenter(lock_finalizer_thread);
169 /* signal the finalizer thread */
171 signal_cond_for_object(lock_finalizer_thread);
175 builtin_monitorexit(lock_finalizer_thread);
177 /* if we don't have threads, just run the finalizers */
179 gc_invoke_finalizers();
184 /* finalizer_run ***************************************************************
186 Actually run the finalizer functions.
188 *******************************************************************************/
190 void finalizer_run(void *o, void *p)
192 java_objectheader *ob = (java_objectheader *) o;
194 /* call the finalizer function */
196 ASM_CALLJAVAFUNCTION(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
198 /* if we had an exception in the finalizer, ignore it */
200 *exceptionptr = NULL;
205 * These are local overrides for various environment variables in Emacs.
206 * Please do not remove this and leave it at the end of the file, where
207 * Emacs will automagically detect them.
208 * ---------------------------------------------------------------------
211 * indent-tabs-mode: t