* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: finalizer.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <stdlib.h>
39
40 #include "vm/types.h"
41
42 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/include/java_lang_Thread.h"
45 #include "native/include/java_lang_VMThread.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/options.h"
50 #include "vm/stringlocal.h"
51 #include "vm/jit/asmpart.h"
52
53
54 /* global variables ***********************************************************/
55
56 #if defined(USE_THREADS)
57 static java_lang_VMThread *finalizer_vmthread;
58 static java_objectheader *lock_finalizer_thread;
59 #endif
60
61
62 /* finalizer_init **************************************************************
63
64    Initializes the finalizer global lock and the linked list.
65
66 *******************************************************************************/
67
68 bool finalizer_init(void)
69 {
70 #if defined(USE_THREADS)
71         lock_finalizer_thread = NEW(java_objectheader);
72
73 # if defined(NATIVE_THREADS)
74         initObjectLock(lock_finalizer_thread);
75 # endif
76 #endif
77
78         /* everything's ok */
79
80         return true;
81 }
82
83
84 /* finalizer_thread ************************************************************
85
86    This thread waits on an object for a notification and the runs the
87    finalizers (finalizer thread).  This is necessary because of a
88    possible deadlock in the GC.
89
90 *******************************************************************************/
91
92 #if defined(USE_THREADS)
93 static void finalizer_thread(void)
94 {
95         while (true) {
96                 /* get the lock on the finalizer lock object, so we can call wait */
97
98                 builtin_monitorenter(lock_finalizer_thread);
99
100                 /* wait forever (0, 0) on that object till we are signaled */
101         
102                 wait_cond_for_object(lock_finalizer_thread, 0, 0);
103
104                 /* leave the lock */
105
106                 builtin_monitorexit(lock_finalizer_thread);
107
108                 /* and call the finalizers */
109
110                 gc_invoke_finalizers();
111         }
112 }
113 #endif
114
115
116 /* finalizer_start_thread ******************************************************
117
118    Starts the finalizer thread.
119
120 *******************************************************************************/
121
122 #if defined(USE_THREADS)
123 bool finalizer_start_thread(void)
124 {
125         java_lang_Thread *t;
126
127         /* create the finalizer object */
128
129         finalizer_vmthread =
130                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
131
132         if (!finalizer_vmthread)
133                 return false;
134
135         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
136
137         t->vmThread = finalizer_vmthread;
138         t->name     = javastring_new_char("Finalizer");
139         t->daemon   = true;
140         t->priority = 5;
141
142         finalizer_vmthread->thread = t;
143
144         /* actually start the finalizer thread */
145
146         threads_start_thread(t, finalizer_thread);
147
148         /* everything's ok */
149
150         return true;
151 }
152 #endif
153
154
155 /* finalizer_notify ************************************************************
156
157    Notifies the finalizer thread that it should run the
158    gc_invoke_finalizers from the GC.
159
160 *******************************************************************************/
161
162 void finalizer_notify(void)
163 {
164 #if defined(USE_THREADS)
165         /* get the lock on the finalizer lock object, so we can call wait */
166
167         builtin_monitorenter(lock_finalizer_thread);
168
169         /* signal the finalizer thread */
170         
171         signal_cond_for_object(lock_finalizer_thread);
172
173         /* leave the lock */
174
175         builtin_monitorexit(lock_finalizer_thread);
176 #else
177         /* if we don't have threads, just run the finalizers */
178
179         gc_invoke_finalizers();
180 #endif
181 }
182
183
184 /* finalizer_run ***************************************************************
185
186    Actually run the finalizer functions.
187
188 *******************************************************************************/
189
190 void finalizer_run(void *o, void *p)
191 {
192         java_objectheader *ob = (java_objectheader *) o;
193
194         /* call the finalizer function */
195
196         ASM_CALLJAVAFUNCTION(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
197
198         /* if we had an exception in the finalizer, ignore it */
199
200         *exceptionptr = NULL;
201 }
202
203
204 /*
205  * These are local overrides for various environment variables in Emacs.
206  * Please do not remove this and leave it at the end of the file, where
207  * Emacs will automagically detect them.
208  * ---------------------------------------------------------------------
209  * Local variables:
210  * mode: c
211  * indent-tabs-mode: t
212  * c-basic-offset: 4
213  * tab-width: 4
214  * End:
215  */