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