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
29 $Id: finalizer.c 6228 2006-12-26 19:56:58Z twisti $
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"
45 #if defined(ENABLE_THREADS)
46 # include "threads/native/lock.h"
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"
55 #include "vm/jit/asmpart.h"
58 /* global variables ***********************************************************/
60 #if defined(ENABLE_THREADS)
61 static threadobject *thread_finalizer;
62 static java_objectheader *lock_thread_finalizer;
66 /* finalizer_init **************************************************************
68 Initializes the finalizer global lock and the linked list.
70 *******************************************************************************/
72 bool finalizer_init(void)
74 #if defined(ENABLE_THREADS)
75 lock_thread_finalizer = NEW(java_objectheader);
77 lock_init_object_lock(lock_thread_finalizer);
86 /* finalizer_thread ************************************************************
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.
92 *******************************************************************************/
94 #if defined(ENABLE_THREADS)
95 static void finalizer_thread(void)
98 /* get the lock on the finalizer lock object, so we can call wait */
100 lock_monitor_enter(lock_thread_finalizer);
102 /* wait forever (0, 0) on that object till we are signaled */
104 lock_wait_for_object(lock_thread_finalizer, 0, 0);
108 lock_monitor_exit(lock_thread_finalizer);
110 /* and call the finalizers */
112 gc_invoke_finalizers();
118 /* finalizer_start_thread ******************************************************
120 Starts the finalizer thread.
122 *******************************************************************************/
124 #if defined(ENABLE_THREADS)
125 bool finalizer_start_thread(void)
127 #if defined(WITH_CLASSPATH_GNU)
128 java_lang_VMThread *vmt;
131 /* create the finalizer object */
133 thread_finalizer = (threadobject *) builtin_new(class_java_lang_Thread);
135 if (thread_finalizer == NULL)
138 #if defined(WITH_CLASSPATH_GNU)
139 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
141 vmt->thread = (java_lang_Thread *) thread_finalizer;
143 thread_finalizer->o.vmThread = vmt;
146 thread_finalizer->o.name = javastring_new_from_ascii("Finalizer");
147 thread_finalizer->o.daemon = true;
148 thread_finalizer->o.priority = 5;
150 /* actually start the finalizer thread */
152 threads_start_thread(thread_finalizer, finalizer_thread);
154 /* everything's ok */
161 /* finalizer_notify ************************************************************
163 Notifies the finalizer thread that it should run the
164 gc_invoke_finalizers from the GC.
166 *******************************************************************************/
168 void finalizer_notify(void)
170 #if defined(ENABLE_THREADS)
171 /* get the lock on the finalizer lock object, so we can call wait */
173 lock_monitor_enter(lock_thread_finalizer);
175 /* signal the finalizer thread */
177 lock_notify_object(lock_thread_finalizer);
181 lock_monitor_exit(lock_thread_finalizer);
183 /* if we don't have threads, just run the finalizers */
185 gc_invoke_finalizers();
190 /* finalizer_run ***************************************************************
192 Actually run the finalizer functions.
194 *******************************************************************************/
196 void finalizer_run(void *o, void *p)
198 java_objectheader *ob;
200 ob = (java_objectheader *) o;
202 /* call the finalizer function */
204 (void) vm_call_method(ob->vftbl->class->finalizer, ob);
206 /* if we had an exception in the finalizer, ignore it */
208 *exceptionptr = NULL;
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 * ---------------------------------------------------------------------
219 * indent-tabs-mode: t