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"
37 #include "threads/lock.hpp"
39 #include "vm/jit/builtin.hpp"
40 #include "vm/exceptions.hpp"
41 #include "vm/global.h"
42 #include "vm/options.h"
45 #include "vm/jit/asmpart.h"
48 /* global variables ***********************************************************/
50 #if defined(ENABLE_THREADS)
51 static Mutex *finalizer_thread_mutex;
52 static Condition *finalizer_thread_cond;
55 #if defined(__cplusplus)
59 /* finalizer_init **************************************************************
61 Initializes the finalizer global lock and the linked list.
63 *******************************************************************************/
65 bool finalizer_init(void)
67 TRACESUBSYSTEMINITIALIZATION("finalizer_init");
69 #if defined(ENABLE_THREADS)
70 finalizer_thread_mutex = new Mutex();
71 finalizer_thread_cond = new Condition();
80 /* finalizer_thread ************************************************************
82 This thread waits on an object for a notification and the runs the
83 finalizers (finalizer thread). This is necessary because of a
84 possible deadlock in the GC.
86 *******************************************************************************/
88 #if defined(ENABLE_THREADS)
89 static void finalizer_thread(void)
92 /* get the lock on the finalizer mutex, so we can call wait */
94 finalizer_thread_mutex->lock();
96 /* wait forever on that condition till we are signaled */
98 finalizer_thread_cond->wait(finalizer_thread_mutex);
102 finalizer_thread_mutex->unlock();
105 if (opt_DebugFinalizer)
106 log_println("[finalizer thread : status=awake]");
109 /* and call the finalizers */
111 gc_invoke_finalizers();
114 if (opt_DebugFinalizer)
115 log_println("[finalizer thread : status=sleeping]");
122 /* finalizer_start_thread ******************************************************
124 Starts the finalizer thread.
126 *******************************************************************************/
128 #if defined(ENABLE_THREADS)
129 bool finalizer_start_thread(void)
133 name = utf_new_char("Finalizer");
135 if (!threads_thread_start_internal(name, finalizer_thread))
138 /* everything's ok */
145 /* finalizer_notify ************************************************************
147 Notifies the finalizer thread that it should run the
148 gc_invoke_finalizers from the GC.
150 *******************************************************************************/
152 void finalizer_notify(void)
155 if (opt_DebugFinalizer)
156 log_println("[finalizer notified]");
159 #if defined(ENABLE_THREADS)
160 /* get the lock on the finalizer lock object, so we can call wait */
162 finalizer_thread_mutex->lock();
164 /* signal the finalizer thread */
166 finalizer_thread_cond->signal();
170 finalizer_thread_mutex->unlock();
172 /* if we don't have threads, just run the finalizers */
174 gc_invoke_finalizers();
179 /* finalizer_run ***************************************************************
181 Actually run the finalizer functions.
183 *******************************************************************************/
185 void finalizer_run(void *o, void *p)
190 h = (java_handle_t *) o;
192 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
193 /* XXX this is only a dirty hack to make Boehm work with handles */
195 h = LLNI_WRAP((java_object_t *) h);
198 LLNI_class_get(h, c);
201 if (opt_DebugFinalizer) {
203 log_print("[finalizer running : o=%p p=%p class=", o, p);
210 /* call the finalizer function */
212 (void) vm_call_method(c->finalizer, h);
215 if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
216 log_println("[finalizer exception]");
217 exceptions_print_stacktrace();
221 /* if we had an exception in the finalizer, ignore it */
223 exceptions_clear_exception();
225 #if defined(ENABLE_GC_BOEHM)
226 lock_schedule_lockrecord_removal(h);
230 #if defined(__cplusplus)
235 * These are local overrides for various environment variables in Emacs.
236 * Please do not remove this and leave it at the end of the file, where
237 * Emacs will automagically detect them.
238 * ---------------------------------------------------------------------
241 * indent-tabs-mode: t