* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdlib.h>
29
30 #include "vm/types.h"
31
32 #include "mm/memory.h"
33
34 #include "threads/lock-common.h"
35 #include "threads/thread.h"
36
37 #include "vm/builtin.h"
38 #include "vm/exceptions.h"
39 #include "vm/global.h"
40 #include "vm/stringlocal.h"
41 #include "vm/vm.hpp"
42
43 #include "vm/jit/asmpart.h"
44
45 #include "vmcore/options.h"
46
47
48 /* global variables ***********************************************************/
49
50 #if defined(ENABLE_THREADS)
51 static java_object_t *lock_thread_finalizer;
52 #endif
53
54
55 /* finalizer_init **************************************************************
56
57    Initializes the finalizer global lock and the linked list.
58
59 *******************************************************************************/
60
61 bool finalizer_init(void)
62 {
63         TRACESUBSYSTEMINITIALIZATION("finalizer_init");
64
65 #if defined(ENABLE_THREADS)
66         lock_thread_finalizer = NEW(java_object_t);
67
68         LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
69 #endif
70
71         /* everything's ok */
72
73         return true;
74 }
75
76
77 /* finalizer_thread ************************************************************
78
79    This thread waits on an object for a notification and the runs the
80    finalizers (finalizer thread).  This is necessary because of a
81    possible deadlock in the GC.
82
83 *******************************************************************************/
84
85 #if defined(ENABLE_THREADS)
86 static void finalizer_thread(void)
87 {
88         while (true) {
89                 /* get the lock on the finalizer lock object, so we can call wait */
90
91                 LOCK_MONITOR_ENTER(lock_thread_finalizer);
92
93                 /* wait forever on that object till we are signaled */
94         
95                 LOCK_WAIT_FOREVER(lock_thread_finalizer);
96
97                 /* leave the lock */
98
99                 LOCK_MONITOR_EXIT(lock_thread_finalizer);
100
101 #if !defined(NDEBUG)
102                 if (opt_DebugFinalizer)
103                         log_println("[finalizer thread    : status=awake]");
104 #endif
105
106                 /* and call the finalizers */
107
108                 gc_invoke_finalizers();
109
110 #if !defined(NDEBUG)
111                 if (opt_DebugFinalizer)
112                         log_println("[finalizer thread    : status=sleeping]");
113 #endif
114         }
115 }
116 #endif
117
118
119 /* finalizer_start_thread ******************************************************
120
121    Starts the finalizer thread.
122
123 *******************************************************************************/
124
125 #if defined(ENABLE_THREADS)
126 bool finalizer_start_thread(void)
127 {
128         utf *name;
129
130         name = utf_new_char("Finalizer");
131
132         if (!threads_thread_start_internal(name, finalizer_thread))
133                 return false;
134
135         /* everything's ok */
136
137         return true;
138 }
139 #endif
140
141
142 /* finalizer_notify ************************************************************
143
144    Notifies the finalizer thread that it should run the
145    gc_invoke_finalizers from the GC.
146
147 *******************************************************************************/
148
149 void finalizer_notify(void)
150 {
151 #if !defined(NDEBUG)
152         if (opt_DebugFinalizer)
153                 log_println("[finalizer notified]");
154 #endif
155
156 #if defined(ENABLE_THREADS)
157         /* get the lock on the finalizer lock object, so we can call wait */
158
159         LOCK_MONITOR_ENTER(lock_thread_finalizer);
160
161         /* signal the finalizer thread */
162         
163         LOCK_NOTIFY(lock_thread_finalizer);
164
165         /* leave the lock */
166
167         LOCK_MONITOR_EXIT(lock_thread_finalizer);
168 #else
169         /* if we don't have threads, just run the finalizers */
170
171         gc_invoke_finalizers();
172 #endif
173 }
174
175
176 /* finalizer_run ***************************************************************
177
178    Actually run the finalizer functions.
179
180 *******************************************************************************/
181
182 void finalizer_run(void *o, void *p)
183 {
184         java_handle_t *h;
185         classinfo     *c;
186
187         h = (java_handle_t *) o;
188
189 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
190         /* XXX this is only a dirty hack to make Boehm work with handles */
191
192         h = LLNI_WRAP((java_object_t *) h);
193 #endif
194
195         LLNI_class_get(h, c);
196
197 #if !defined(NDEBUG)
198         if (opt_DebugFinalizer) {
199                 log_start();
200                 log_print("[finalizer running   : o=%p p=%p class=", o, p);
201                 class_print(c);
202                 log_print("]");
203                 log_finish();
204         }
205 #endif
206
207         /* call the finalizer function */
208
209         (void) vm_call_method(c->finalizer, h);
210
211 #if !defined(NDEBUG)
212         if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
213                 log_println("[finalizer exception]");
214                 exceptions_print_stacktrace();
215         }
216 #endif
217
218         /* if we had an exception in the finalizer, ignore it */
219
220         exceptions_clear_exception();
221 }
222
223
224 /*
225  * These are local overrides for various environment variables in Emacs.
226  * Please do not remove this and leave it at the end of the file, where
227  * Emacs will automagically detect them.
228  * ---------------------------------------------------------------------
229  * Local variables:
230  * mode: c
231  * indent-tabs-mode: t
232  * c-basic-offset: 4
233  * tab-width: 4
234  * End:
235  */