1 /* src/vm/finalizer.c - finalizer linked list and thread
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: finalizer.c 8295 2007-08-11 17:57:24Z michi $
36 #include "mm/memory.h"
38 #include "threads/lock-common.h"
39 #include "threads/threads-common.h"
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/global.h"
44 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
49 #include "vmcore/options.h"
52 /* global variables ***********************************************************/
54 #if defined(ENABLE_THREADS)
55 static java_object_t *lock_thread_finalizer;
59 /* finalizer_init **************************************************************
61 Initializes the finalizer global lock and the linked list.
63 *******************************************************************************/
65 bool finalizer_init(void)
67 #if defined(ENABLE_THREADS)
68 lock_thread_finalizer = NEW(java_object_t);
70 LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
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 lock object, so we can call wait */
93 lock_monitor_enter(lock_thread_finalizer);
95 /* wait forever (0, 0) on that object till we are signaled */
97 lock_wait_for_object(lock_thread_finalizer, 0, 0);
101 lock_monitor_exit(lock_thread_finalizer);
103 /* and call the finalizers */
105 gc_invoke_finalizers();
111 /* finalizer_start_thread ******************************************************
113 Starts the finalizer thread.
115 *******************************************************************************/
117 #if defined(ENABLE_THREADS)
118 bool finalizer_start_thread(void)
122 name = utf_new_char("Finalizer");
124 if (!threads_thread_start_internal(name, finalizer_thread))
127 /* everything's ok */
134 /* finalizer_notify ************************************************************
136 Notifies the finalizer thread that it should run the
137 gc_invoke_finalizers from the GC.
139 *******************************************************************************/
141 void finalizer_notify(void)
143 #if defined(ENABLE_THREADS)
144 /* get the lock on the finalizer lock object, so we can call wait */
146 lock_monitor_enter(lock_thread_finalizer);
148 /* signal the finalizer thread */
150 lock_notify_object(lock_thread_finalizer);
154 lock_monitor_exit(lock_thread_finalizer);
156 /* if we don't have threads, just run the finalizers */
158 gc_invoke_finalizers();
163 /* finalizer_run ***************************************************************
165 Actually run the finalizer functions.
167 *******************************************************************************/
169 void finalizer_run(void *o, void *p)
173 ob = (java_object_t *) o;
175 /* call the finalizer function */
177 (void) vm_call_method(ob->vftbl->class->finalizer, ob);
179 /* if we had an exception in the finalizer, ignore it */
181 exceptions_clear_exception();
186 * These are local overrides for various environment variables in Emacs.
187 * Please do not remove this and leave it at the end of the file, where
188 * Emacs will automagically detect them.
189 * ---------------------------------------------------------------------
192 * indent-tabs-mode: t