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/condition.hpp"
35 #include "threads/mutex.hpp"
36 #include "threads/thread.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/exceptions.hpp"
40 #include "vm/global.h"
41 #include "vm/options.h"
44 #include "vm/jit/asmpart.h"
47 /* global variables ***********************************************************/
49 #if defined(ENABLE_THREADS)
50 static Mutex *finalizer_thread_mutex;
51 static Condition *finalizer_thread_cond;
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 finalizer_thread_mutex = Mutex_new();
67 finalizer_thread_cond = Condition_new();
76 /* finalizer_thread ************************************************************
78 This thread waits on an object for a notification and the runs the
79 finalizers (finalizer thread). This is necessary because of a
80 possible deadlock in the GC.
82 *******************************************************************************/
84 #if defined(ENABLE_THREADS)
85 static void finalizer_thread(void)
88 /* get the lock on the finalizer mutex, so we can call wait */
90 Mutex_lock(finalizer_thread_mutex);
92 /* wait forever on that condition till we are signaled */
94 Condition_wait(finalizer_thread_cond, finalizer_thread_mutex);
98 Mutex_unlock(finalizer_thread_mutex);
101 if (opt_DebugFinalizer)
102 log_println("[finalizer thread : status=awake]");
105 /* and call the finalizers */
107 gc_invoke_finalizers();
110 if (opt_DebugFinalizer)
111 log_println("[finalizer thread : status=sleeping]");
118 /* finalizer_start_thread ******************************************************
120 Starts the finalizer thread.
122 *******************************************************************************/
124 #if defined(ENABLE_THREADS)
125 bool finalizer_start_thread(void)
129 name = utf_new_char("Finalizer");
131 if (!threads_thread_start_internal(name, finalizer_thread))
134 /* everything's ok */
141 /* finalizer_notify ************************************************************
143 Notifies the finalizer thread that it should run the
144 gc_invoke_finalizers from the GC.
146 *******************************************************************************/
148 void finalizer_notify(void)
151 if (opt_DebugFinalizer)
152 log_println("[finalizer notified]");
155 #if defined(ENABLE_THREADS)
156 /* get the lock on the finalizer lock object, so we can call wait */
158 Mutex_lock(finalizer_thread_mutex);
160 /* signal the finalizer thread */
162 Condition_signal(finalizer_thread_cond);
166 Mutex_unlock(finalizer_thread_mutex);
168 /* if we don't have threads, just run the finalizers */
170 gc_invoke_finalizers();
175 /* finalizer_run ***************************************************************
177 Actually run the finalizer functions.
179 *******************************************************************************/
181 void finalizer_run(void *o, void *p)
186 h = (java_handle_t *) o;
188 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
189 /* XXX this is only a dirty hack to make Boehm work with handles */
191 h = LLNI_WRAP((java_object_t *) h);
194 LLNI_class_get(h, c);
197 if (opt_DebugFinalizer) {
199 log_print("[finalizer running : o=%p p=%p class=", o, p);
206 /* call the finalizer function */
208 (void) vm_call_method(c->finalizer, h);
211 if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
212 log_println("[finalizer exception]");
213 exceptions_print_stacktrace();
217 /* if we had an exception in the finalizer, ignore it */
219 exceptions_clear_exception();
224 * These are local overrides for various environment variables in Emacs.
225 * Please do not remove this and leave it at the end of the file, where
226 * Emacs will automagically detect them.
227 * ---------------------------------------------------------------------
230 * indent-tabs-mode: t