d265cfae04b55c6c9b60639efa33e437c58ffd0f
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: finalizer.c 8295 2007-08-11 17:57:24Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <stdlib.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37
38 #include "threads/lock-common.h"
39 #include "threads/threads-common.h"
40
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/global.h"
44 #include "vm/stringlocal.h"
45 #include "vm/vm.h"
46
47 #include "vm/jit/asmpart.h"
48
49 #include "vmcore/options.h"
50
51
52 /* global variables ***********************************************************/
53
54 #if defined(ENABLE_THREADS)
55 static java_object_t *lock_thread_finalizer;
56 #endif
57
58
59 /* finalizer_init **************************************************************
60
61    Initializes the finalizer global lock and the linked list.
62
63 *******************************************************************************/
64
65 bool finalizer_init(void)
66 {
67 #if defined(ENABLE_THREADS)
68         lock_thread_finalizer = NEW(java_object_t);
69
70         LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
71 #endif
72
73         /* everything's ok */
74
75         return true;
76 }
77
78
79 /* finalizer_thread ************************************************************
80
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.
84
85 *******************************************************************************/
86
87 #if defined(ENABLE_THREADS)
88 static void finalizer_thread(void)
89 {
90         while (true) {
91                 /* get the lock on the finalizer lock object, so we can call wait */
92
93                 lock_monitor_enter(lock_thread_finalizer);
94
95                 /* wait forever (0, 0) on that object till we are signaled */
96         
97                 lock_wait_for_object(lock_thread_finalizer, 0, 0);
98
99                 /* leave the lock */
100
101                 lock_monitor_exit(lock_thread_finalizer);
102
103                 /* and call the finalizers */
104
105                 gc_invoke_finalizers();
106         }
107 }
108 #endif
109
110
111 /* finalizer_start_thread ******************************************************
112
113    Starts the finalizer thread.
114
115 *******************************************************************************/
116
117 #if defined(ENABLE_THREADS)
118 bool finalizer_start_thread(void)
119 {
120         utf *name;
121
122         name = utf_new_char("Finalizer");
123
124         if (!threads_thread_start_internal(name, finalizer_thread))
125                 return false;
126
127         /* everything's ok */
128
129         return true;
130 }
131 #endif
132
133
134 /* finalizer_notify ************************************************************
135
136    Notifies the finalizer thread that it should run the
137    gc_invoke_finalizers from the GC.
138
139 *******************************************************************************/
140
141 void finalizer_notify(void)
142 {
143 #if defined(ENABLE_THREADS)
144         /* get the lock on the finalizer lock object, so we can call wait */
145
146         lock_monitor_enter(lock_thread_finalizer);
147
148         /* signal the finalizer thread */
149         
150         lock_notify_object(lock_thread_finalizer);
151
152         /* leave the lock */
153
154         lock_monitor_exit(lock_thread_finalizer);
155 #else
156         /* if we don't have threads, just run the finalizers */
157
158         gc_invoke_finalizers();
159 #endif
160 }
161
162
163 /* finalizer_run ***************************************************************
164
165    Actually run the finalizer functions.
166
167 *******************************************************************************/
168
169 void finalizer_run(void *o, void *p)
170 {
171         java_object_t *ob;
172
173         ob = (java_object_t *) o;
174
175         /* call the finalizer function */
176
177         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
178
179         /* if we had an exception in the finalizer, ignore it */
180
181         exceptions_clear_exception();
182 }
183
184
185 /*
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  * ---------------------------------------------------------------------
190  * Local variables:
191  * mode: c
192  * indent-tabs-mode: t
193  * c-basic-offset: 4
194  * tab-width: 4
195  * End:
196  */