* src/vm/jit/powerpc64/linux/.cvsignore: New file.
[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 4921 2006-05-15 14:24:36Z 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(ENABLE_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(ENABLE_THREADS)
72         lock_finalizer_thread = NEW(java_objectheader);
73
74         lock_init_object_lock(lock_finalizer_thread);
75 #endif
76
77         /* everything's ok */
78
79         return true;
80 }
81
82
83 /* finalizer_thread ************************************************************
84
85    This thread waits on an object for a notification and the runs the
86    finalizers (finalizer thread).  This is necessary because of a
87    possible deadlock in the GC.
88
89 *******************************************************************************/
90
91 #if defined(ENABLE_THREADS)
92 static void finalizer_thread(void)
93 {
94         while (true) {
95                 /* get the lock on the finalizer lock object, so we can call wait */
96
97                 builtin_monitorenter(lock_finalizer_thread);
98
99                 /* wait forever (0, 0) on that object till we are signaled */
100         
101                 lock_wait_for_object(lock_finalizer_thread, 0, 0);
102
103                 /* leave the lock */
104
105                 builtin_monitorexit(lock_finalizer_thread);
106
107                 /* and call the finalizers */
108
109                 gc_invoke_finalizers();
110         }
111 }
112 #endif
113
114
115 /* finalizer_start_thread ******************************************************
116
117    Starts the finalizer thread.
118
119 *******************************************************************************/
120
121 #if defined(ENABLE_THREADS)
122 bool finalizer_start_thread(void)
123 {
124         java_lang_Thread *t;
125
126         /* create the finalizer object */
127
128         finalizer_vmthread =
129                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
130
131         if (!finalizer_vmthread)
132                 return false;
133
134         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
135
136         t->vmThread = finalizer_vmthread;
137         t->name     = javastring_new_from_ascii("Finalizer");
138         t->daemon   = true;
139         t->priority = 5;
140
141         finalizer_vmthread->thread = t;
142
143         /* actually start the finalizer thread */
144
145         threads_start_thread(t, finalizer_thread);
146
147         /* everything's ok */
148
149         return true;
150 }
151 #endif
152
153
154 /* finalizer_notify ************************************************************
155
156    Notifies the finalizer thread that it should run the
157    gc_invoke_finalizers from the GC.
158
159 *******************************************************************************/
160
161 void finalizer_notify(void)
162 {
163 #if defined(ENABLE_THREADS)
164         /* get the lock on the finalizer lock object, so we can call wait */
165
166         builtin_monitorenter(lock_finalizer_thread);
167
168         /* signal the finalizer thread */
169         
170         lock_notify_object(lock_finalizer_thread);
171
172         /* leave the lock */
173
174         builtin_monitorexit(lock_finalizer_thread);
175 #else
176         /* if we don't have threads, just run the finalizers */
177
178         gc_invoke_finalizers();
179 #endif
180 }
181
182
183 /* finalizer_run ***************************************************************
184
185    Actually run the finalizer functions.
186
187 *******************************************************************************/
188
189 void finalizer_run(void *o, void *p)
190 {
191         java_objectheader *ob;
192
193         ob = (java_objectheader *) o;
194
195         /* call the finalizer function */
196
197         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
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  */