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 7343 2007-02-13 02:36:29Z ajordan $
36 #include "mm/memory.h"
38 #if defined(ENABLE_THREADS)
39 # include "threads/threads-common.h"
41 # include "threads/native/threads.h"
42 # include "threads/native/lock.h"
44 # include "threads/none/threads.h"
45 # include "threads/none/lock.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/stringlocal.h"
54 #include "vm/jit/asmpart.h"
56 #include "vmcore/options.h"
59 /* global variables ***********************************************************/
61 #if defined(ENABLE_THREADS)
62 static threadobject *thread_finalizer;
63 static java_objectheader *lock_thread_finalizer;
67 /* finalizer_init **************************************************************
69 Initializes the finalizer global lock and the linked list.
71 *******************************************************************************/
73 bool finalizer_init(void)
75 #if defined(ENABLE_THREADS)
76 lock_thread_finalizer = NEW(java_objectheader);
78 lock_init_object_lock(lock_thread_finalizer);
87 /* finalizer_thread ************************************************************
89 This thread waits on an object for a notification and the runs the
90 finalizers (finalizer thread). This is necessary because of a
91 possible deadlock in the GC.
93 *******************************************************************************/
95 #if defined(ENABLE_THREADS)
96 static void finalizer_thread(void)
99 /* get the lock on the finalizer lock object, so we can call wait */
101 lock_monitor_enter(lock_thread_finalizer);
103 /* wait forever (0, 0) on that object till we are signaled */
105 lock_wait_for_object(lock_thread_finalizer, 0, 0);
109 lock_monitor_exit(lock_thread_finalizer);
111 /* and call the finalizers */
113 gc_invoke_finalizers();
119 /* finalizer_start_thread ******************************************************
121 Starts the finalizer thread.
123 *******************************************************************************/
125 #if defined(ENABLE_THREADS)
126 bool finalizer_start_thread(void)
130 name = utf_new_char("Finalizer");
132 thread_finalizer = threads_create_thread(name);
134 if (thread_finalizer == NULL)
137 /* actually start the finalizer thread */
139 threads_start_thread(thread_finalizer, finalizer_thread);
141 /* everything's ok */
148 /* finalizer_notify ************************************************************
150 Notifies the finalizer thread that it should run the
151 gc_invoke_finalizers from the GC.
153 *******************************************************************************/
155 void finalizer_notify(void)
157 #if defined(ENABLE_THREADS)
158 /* get the lock on the finalizer lock object, so we can call wait */
160 lock_monitor_enter(lock_thread_finalizer);
162 /* signal the finalizer thread */
164 lock_notify_object(lock_thread_finalizer);
168 lock_monitor_exit(lock_thread_finalizer);
170 /* if we don't have threads, just run the finalizers */
172 gc_invoke_finalizers();
177 /* finalizer_run ***************************************************************
179 Actually run the finalizer functions.
181 *******************************************************************************/
183 void finalizer_run(void *o, void *p)
185 java_objectheader *ob;
187 ob = (java_objectheader *) o;
189 /* call the finalizer function */
191 (void) vm_call_method(ob->vftbl->class->finalizer, ob);
193 /* if we had an exception in the finalizer, ignore it */
195 exceptions_clear_exception();
200 * These are local overrides for various environment variables in Emacs.
201 * Please do not remove this and leave it at the end of the file, where
202 * Emacs will automagically detect them.
203 * ---------------------------------------------------------------------
206 * indent-tabs-mode: t