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 5123 2006-07-12 21:45:34Z 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"
47 #if defined(ENABLE_THREADS)
48 # include "threads/native/lock.h"
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53 #include "vm/global.h"
54 #include "vm/options.h"
55 #include "vm/stringlocal.h"
57 #include "vm/jit/asmpart.h"
60 /* global variables ***********************************************************/
62 #if defined(ENABLE_THREADS)
63 static java_lang_VMThread *finalizer_vmthread;
64 static java_objectheader *lock_finalizer_thread;
68 /* finalizer_init **************************************************************
70 Initializes the finalizer global lock and the linked list.
72 *******************************************************************************/
74 bool finalizer_init(void)
76 #if defined(ENABLE_THREADS)
77 lock_finalizer_thread = NEW(java_objectheader);
79 lock_init_object_lock(lock_finalizer_thread);
88 /* finalizer_thread ************************************************************
90 This thread waits on an object for a notification and the runs the
91 finalizers (finalizer thread). This is necessary because of a
92 possible deadlock in the GC.
94 *******************************************************************************/
96 #if defined(ENABLE_THREADS)
97 static void finalizer_thread(void)
100 /* get the lock on the finalizer lock object, so we can call wait */
102 lock_monitor_enter(lock_finalizer_thread);
104 /* wait forever (0, 0) on that object till we are signaled */
106 lock_wait_for_object(lock_finalizer_thread, 0, 0);
110 lock_monitor_exit(lock_finalizer_thread);
112 /* and call the finalizers */
114 gc_invoke_finalizers();
120 /* finalizer_start_thread ******************************************************
122 Starts the finalizer thread.
124 *******************************************************************************/
126 #if defined(ENABLE_THREADS)
127 bool finalizer_start_thread(void)
131 /* create the finalizer object */
134 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
136 if (!finalizer_vmthread)
139 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
141 t->vmThread = finalizer_vmthread;
142 t->name = javastring_new_from_ascii("Finalizer");
146 finalizer_vmthread->thread = t;
148 /* actually start the finalizer thread */
150 threads_start_thread(t, finalizer_thread);
152 /* everything's ok */
159 /* finalizer_notify ************************************************************
161 Notifies the finalizer thread that it should run the
162 gc_invoke_finalizers from the GC.
164 *******************************************************************************/
166 void finalizer_notify(void)
168 #if defined(ENABLE_THREADS)
169 /* get the lock on the finalizer lock object, so we can call wait */
171 lock_monitor_enter(lock_finalizer_thread);
173 /* signal the finalizer thread */
175 lock_notify_object(lock_finalizer_thread);
179 lock_monitor_exit(lock_finalizer_thread);
181 /* if we don't have threads, just run the finalizers */
183 gc_invoke_finalizers();
188 /* finalizer_run ***************************************************************
190 Actually run the finalizer functions.
192 *******************************************************************************/
194 void finalizer_run(void *o, void *p)
196 java_objectheader *ob;
198 ob = (java_objectheader *) o;
200 /* call the finalizer function */
202 (void) vm_call_method(ob->vftbl->class->finalizer, ob);
204 /* if we had an exception in the finalizer, ignore it */
206 *exceptionptr = NULL;
211 * These are local overrides for various environment variables in Emacs.
212 * Please do not remove this and leave it at the end of the file, where
213 * Emacs will automagically detect them.
214 * ---------------------------------------------------------------------
217 * indent-tabs-mode: t