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.hpp"
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;
54 #if defined(__cplusplus)
58 /* finalizer_init **************************************************************
60 Initializes the finalizer global lock and the linked list.
62 *******************************************************************************/
64 bool finalizer_init(void)
66 TRACESUBSYSTEMINITIALIZATION("finalizer_init");
68 #if defined(ENABLE_THREADS)
69 finalizer_thread_mutex = new Mutex();
70 finalizer_thread_cond = new Condition();
79 /* finalizer_thread ************************************************************
81 This thread waits on an object for a notification and the runs the
82 finalizers (finalizer thread). This is necessary because of a
83 possible deadlock in the GC.
85 *******************************************************************************/
87 #if defined(ENABLE_THREADS)
88 static void finalizer_thread(void)
91 /* get the lock on the finalizer mutex, so we can call wait */
93 finalizer_thread_mutex->lock();
95 /* wait forever on that condition till we are signaled */
97 finalizer_thread_cond->wait(finalizer_thread_mutex);
101 finalizer_thread_mutex->unlock();
104 if (opt_DebugFinalizer)
105 log_println("[finalizer thread : status=awake]");
108 /* and call the finalizers */
110 gc_invoke_finalizers();
113 if (opt_DebugFinalizer)
114 log_println("[finalizer thread : status=sleeping]");
121 /* finalizer_start_thread ******************************************************
123 Starts the finalizer thread.
125 *******************************************************************************/
127 #if defined(ENABLE_THREADS)
128 bool finalizer_start_thread(void)
132 name = utf_new_char("Finalizer");
134 if (!threads_thread_start_internal(name, finalizer_thread))
137 /* everything's ok */
144 /* finalizer_notify ************************************************************
146 Notifies the finalizer thread that it should run the
147 gc_invoke_finalizers from the GC.
149 *******************************************************************************/
151 void finalizer_notify(void)
154 if (opt_DebugFinalizer)
155 log_println("[finalizer notified]");
158 #if defined(ENABLE_THREADS)
159 /* get the lock on the finalizer lock object, so we can call wait */
161 finalizer_thread_mutex->lock();
163 /* signal the finalizer thread */
165 finalizer_thread_cond->signal();
169 finalizer_thread_mutex->unlock();
171 /* if we don't have threads, just run the finalizers */
173 gc_invoke_finalizers();
178 /* finalizer_run ***************************************************************
180 Actually run the finalizer functions.
182 *******************************************************************************/
184 void finalizer_run(void *o, void *p)
189 h = (java_handle_t *) o;
191 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
192 /* XXX this is only a dirty hack to make Boehm work with handles */
194 h = LLNI_WRAP((java_object_t *) h);
197 LLNI_class_get(h, c);
200 if (opt_DebugFinalizer) {
202 log_print("[finalizer running : o=%p p=%p class=", o, p);
209 /* call the finalizer function */
211 (void) vm_call_method(c->finalizer, h);
214 if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
215 log_println("[finalizer exception]");
216 exceptions_print_stacktrace();
220 /* if we had an exception in the finalizer, ignore it */
222 exceptions_clear_exception();
225 #if defined(__cplusplus)
230 * These are local overrides for various environment variables in Emacs.
231 * Please do not remove this and leave it at the end of the file, where
232 * Emacs will automagically detect them.
233 * ---------------------------------------------------------------------
236 * indent-tabs-mode: t