1 /* src/vm/finalizer.c - finalizer linked list and thread
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "mm/memory.h"
34 #include "threads/lock-common.h"
35 #include "threads/thread.h"
37 #include "vm/builtin.h"
38 #include "vm/exceptions.h"
39 #include "vm/global.h"
40 #include "vm/stringlocal.h"
43 #include "vm/jit/asmpart.h"
45 #include "vmcore/options.h"
48 /* global variables ***********************************************************/
50 #if defined(ENABLE_THREADS)
51 static java_object_t *lock_thread_finalizer;
55 /* finalizer_init **************************************************************
57 Initializes the finalizer global lock and the linked list.
59 *******************************************************************************/
61 bool finalizer_init(void)
63 TRACESUBSYSTEMINITIALIZATION("finalizer_init");
65 #if defined(ENABLE_THREADS)
66 lock_thread_finalizer = NEW(java_object_t);
68 LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
77 /* finalizer_thread ************************************************************
79 This thread waits on an object for a notification and the runs the
80 finalizers (finalizer thread). This is necessary because of a
81 possible deadlock in the GC.
83 *******************************************************************************/
85 #if defined(ENABLE_THREADS)
86 static void finalizer_thread(void)
89 /* get the lock on the finalizer lock object, so we can call wait */
91 LOCK_MONITOR_ENTER(lock_thread_finalizer);
93 /* wait forever on that object till we are signaled */
95 LOCK_WAIT_FOREVER(lock_thread_finalizer);
99 LOCK_MONITOR_EXIT(lock_thread_finalizer);
102 if (opt_DebugFinalizer)
103 log_println("[finalizer thread : status=awake]");
106 /* and call the finalizers */
108 gc_invoke_finalizers();
111 if (opt_DebugFinalizer)
112 log_println("[finalizer thread : status=sleeping]");
119 /* finalizer_start_thread ******************************************************
121 Starts the finalizer thread.
123 *******************************************************************************/
125 #if defined(ENABLE_THREADS)
126 bool finalizer_start_thread(void)
130 name = utf_new_char("Finalizer");
132 if (!threads_thread_start_internal(name, finalizer_thread))
135 /* everything's ok */
142 /* finalizer_notify ************************************************************
144 Notifies the finalizer thread that it should run the
145 gc_invoke_finalizers from the GC.
147 *******************************************************************************/
149 void finalizer_notify(void)
152 if (opt_DebugFinalizer)
153 log_println("[finalizer notified]");
156 #if defined(ENABLE_THREADS)
157 /* get the lock on the finalizer lock object, so we can call wait */
159 LOCK_MONITOR_ENTER(lock_thread_finalizer);
161 /* signal the finalizer thread */
163 LOCK_NOTIFY(lock_thread_finalizer);
167 LOCK_MONITOR_EXIT(lock_thread_finalizer);
169 /* if we don't have threads, just run the finalizers */
171 gc_invoke_finalizers();
176 /* finalizer_run ***************************************************************
178 Actually run the finalizer functions.
180 *******************************************************************************/
182 void finalizer_run(void *o, void *p)
187 h = (java_handle_t *) o;
189 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
190 /* XXX this is only a dirty hack to make Boehm work with handles */
192 h = LLNI_WRAP((java_object_t *) h);
195 LLNI_class_get(h, c);
198 if (opt_DebugFinalizer) {
200 log_print("[finalizer running : o=%p p=%p class=", o, p);
207 /* call the finalizer function */
209 (void) vm_call_method(c->finalizer, h);
212 if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
213 log_println("[finalizer exception]");
214 exceptions_print_stacktrace();
218 /* if we had an exception in the finalizer, ignore it */
220 exceptions_clear_exception();
225 * These are local overrides for various environment variables in Emacs.
226 * Please do not remove this and leave it at the end of the file, where
227 * Emacs will automagically detect them.
228 * ---------------------------------------------------------------------
231 * indent-tabs-mode: t