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