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
34 #include "mm/memory.h"
36 #include "threads/lock-common.h"
37 #include "threads/threads-common.h"
39 #include "vm/builtin.h"
40 #include "vm/exceptions.h"
41 #include "vm/global.h"
42 #include "vm/stringlocal.h"
45 #include "vm/jit/asmpart.h"
47 #include "vmcore/options.h"
50 /* global variables ***********************************************************/
52 #if defined(ENABLE_THREADS)
53 static java_object_t *lock_thread_finalizer;
57 /* finalizer_init **************************************************************
59 Initializes the finalizer global lock and the linked list.
61 *******************************************************************************/
63 bool finalizer_init(void)
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 (0, 0) on that object till we are signaled */
95 lock_wait_for_object(lock_thread_finalizer, 0, 0);
99 lock_monitor_exit(lock_thread_finalizer);
101 /* and call the finalizers */
103 gc_invoke_finalizers();
109 /* finalizer_start_thread ******************************************************
111 Starts the finalizer thread.
113 *******************************************************************************/
115 #if defined(ENABLE_THREADS)
116 bool finalizer_start_thread(void)
120 name = utf_new_char("Finalizer");
122 if (!threads_thread_start_internal(name, finalizer_thread))
125 /* everything's ok */
132 /* finalizer_notify ************************************************************
134 Notifies the finalizer thread that it should run the
135 gc_invoke_finalizers from the GC.
137 *******************************************************************************/
139 void finalizer_notify(void)
141 #if defined(ENABLE_THREADS)
142 /* get the lock on the finalizer lock object, so we can call wait */
144 lock_monitor_enter(lock_thread_finalizer);
146 /* signal the finalizer thread */
148 lock_notify_object(lock_thread_finalizer);
152 lock_monitor_exit(lock_thread_finalizer);
154 /* if we don't have threads, just run the finalizers */
156 gc_invoke_finalizers();
161 /* finalizer_run ***************************************************************
163 Actually run the finalizer functions.
165 *******************************************************************************/
167 void finalizer_run(void *o, void *p)
171 ob = (java_object_t *) o;
173 /* call the finalizer function */
175 (void) vm_call_method(ob->vftbl->class->finalizer, ob);
177 /* if we had an exception in the finalizer, ignore it */
179 exceptions_clear_exception();
184 * These are local overrides for various environment variables in Emacs.
185 * Please do not remove this and leave it at the end of the file, where
186 * Emacs will automagically detect them.
187 * ---------------------------------------------------------------------
190 * indent-tabs-mode: t