* Only added some debug output for mips and powerpc, this should be remove
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: finalizer.c 3545 2005-11-03 20:36:59Z twisti $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "mm/memory.h"
40 #include "native/jni.h"
41 #include "native/include/java_lang_Thread.h"
42 #include "native/include/gnu_classpath_Pointer.h"
43 #include "native/include/java_lang_VMThread.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/stringlocal.h"
48 #include "vm/jit/asmpart.h"
49
50
51 /* local structures ***********************************************************/
52
53 typedef struct finalizer_entry finalizer_entry;
54
55 struct finalizer_entry {
56         java_objectheader *o;
57         listnode           linkage;
58 };
59
60
61 /* global variables ***********************************************************/
62
63 #if defined(USE_THREADS)
64 static java_lang_VMThread *finalizer_vmthread;
65 static java_objectheader *lock_finalizer_thread;
66 static list *finalizer_list;
67 #endif
68
69
70 /* finalizer_init **************************************************************
71
72    Initializes the finalizer global lock and the linked list.
73
74 *******************************************************************************/
75
76 bool finalizer_init(void)
77 {
78 #if defined(USE_THREADS)
79         lock_finalizer_thread = NEW(java_objectheader);
80
81 # if defined(NATIVE_THREADS)
82         initObjectLock(lock_finalizer_thread);
83 # endif
84
85         /* initialize the finalizer list */
86
87         finalizer_list = NEW(list);
88         list_init(finalizer_list, OFFSET(finalizer_entry, linkage));
89 #endif
90
91         /* everything's ok */
92
93         return true;
94 }
95
96
97 /* finalizer_thread ************************************************************
98
99    This thread waits on an object for a notification and the runs the
100    finalizers (finalizer thread).  This is necessary because of a
101    possible deadlock in the GC.
102
103 *******************************************************************************/
104
105 #if defined(USE_THREADS)
106 static void finalizer_thread(void)
107 {
108         finalizer_entry   *fi;
109         java_objectheader *o;
110
111         /* get the lock on the finalizer lock object, so we can call wait */
112
113         while (true) {
114                 /* wait forever (0, 0) on that object till we are signaled */
115         
116                 wait_cond_for_object(lock_finalizer_thread, 0, 0);
117
118                 /* now handle all finalizers stored in the list */
119
120                 fi = (finalizer_entry *) list_first(finalizer_list);
121
122                 /* release the lock so other threads, or the finalizer thread
123                    itself, can add new finalizers to the list */
124
125                 builtin_monitorexit(lock_finalizer_thread);
126
127                 /* is there actually a finalizer in the list? */
128
129                 if (fi) {
130                         do {
131                                 /* just for simpler code */
132
133                                 o = fi->o;
134
135                                 /* call the finalizer function */
136
137                                 asm_calljavafunction(o->vftbl->class->finalizer, o,
138                                                                          NULL, NULL, NULL);
139
140                                 /* if we had an exception in the finalizer, ignore it */
141
142                                 *exceptionptr = NULL;
143
144                                 /* enter the monitor again, so we don't get finalizer
145                                    list race conditions */
146
147                                 builtin_monitorenter(lock_finalizer_thread);
148
149                                 /* remove and clear finalized entry */
150
151                                 list_remove(finalizer_list, fi);
152
153                                 fi->o = NULL;
154                                 FREE(fi, finalizer_entry);
155
156                                 /* get next finalizer from the list */
157
158                                 fi = list_first(finalizer_list);
159
160                                 /* leave the monitor again */
161
162                                 builtin_monitorexit(lock_finalizer_thread);
163                         } while (fi != NULL);
164                 }
165
166                 builtin_monitorenter(lock_finalizer_thread);
167         }
168 }
169 #endif
170
171 /* finalizer_start_thread ******************************************************
172
173    Starts the finalizer thread.
174
175 *******************************************************************************/
176
177 #if defined(USE_THREADS)
178 bool finalizer_start_thread(void)
179 {
180         java_lang_Thread *t;
181
182         /* create the finalizer object */
183
184         finalizer_vmthread =
185                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
186
187         if (!finalizer_vmthread)
188                 return false;
189
190         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
191
192         t->vmThread = finalizer_vmthread;
193         t->name     = javastring_new_char("Finalizer");
194         t->daemon   = true;
195         t->priority = 5;
196
197         finalizer_vmthread->thread = t;
198
199         /* actually start the finalizer thread */
200
201         threads_start_thread(t, finalizer_thread);
202
203         /* everything's ok */
204
205         return true;
206 }
207 #endif
208
209
210 /* finalizer_add ***************************************************************
211
212    Adds a finalizer to be called to the linked list of finalizers.
213
214 *******************************************************************************/
215
216 void finalizer_add(void *o, void *p)
217 {
218 #if defined(USE_THREADS)
219         java_objectheader *ob;
220         finalizer_entry   *fi;
221
222         ob = (java_objectheader *) o;
223
224         /* wait for the finalizer thread to finish finalizer list operations */
225
226         builtin_monitorenter(lock_finalizer_thread);
227
228         /* create finalizer entry, fill it and add it to the list */
229
230         fi = NEW(finalizer_entry);
231         fi->o = ob;
232
233         list_addlast(finalizer_list, fi);
234
235         /* wakeup the finalizer thread */
236
237         signal_cond_for_object(lock_finalizer_thread);
238
239         /* release the lock after the entry is added */
240
241         builtin_monitorexit(lock_finalizer_thread);
242 #else
243         java_objectheader *ob;
244
245         ob = (java_objectheader *) o;
246
247         /* call the finalizer function */
248
249         asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
250
251         /* if we had an exception in the finalizer, ignore it */
252
253         *exceptionptr = NULL;
254 #endif
255 }
256
257
258 /*
259  * These are local overrides for various environment variables in Emacs.
260  * Please do not remove this and leave it at the end of the file, where
261  * Emacs will automagically detect them.
262  * ---------------------------------------------------------------------
263  * Local variables:
264  * mode: c
265  * indent-tabs-mode: t
266  * c-basic-offset: 4
267  * tab-width: 4
268  * End:
269  */