7a4ecd4711e86c50f9beb68d519a9c8a9e506ff2
[cacao.git] / src / vm / finalizer.cpp
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.hpp"
33
34 #include "threads/condition.hpp"
35 #include "threads/mutex.hpp"
36 #include "threads/thread.hpp"
37 #include "threads/lock.hpp"
38
39 #include "vm/jit/builtin.hpp"
40 #include "vm/exceptions.hpp"
41 #include "vm/global.h"
42 #include "vm/options.h"
43 #include "vm/vm.hpp"
44
45 #include "vm/jit/asmpart.h"
46
47
48 /* global variables ***********************************************************/
49
50 #if defined(ENABLE_THREADS)
51 static Mutex     *finalizer_thread_mutex;
52 static Condition *finalizer_thread_cond;
53 #endif
54
55 #if defined(__cplusplus)
56 extern "C" {
57 #endif
58
59 /* finalizer_init **************************************************************
60
61    Initializes the finalizer global lock and the linked list.
62
63 *******************************************************************************/
64
65 bool finalizer_init(void)
66 {
67         TRACESUBSYSTEMINITIALIZATION("finalizer_init");
68
69 #if defined(ENABLE_THREADS)
70         finalizer_thread_mutex = new Mutex();
71         finalizer_thread_cond  = new Condition();
72 #endif
73
74         /* everything's ok */
75
76         return true;
77 }
78
79
80 /* finalizer_thread ************************************************************
81
82    This thread waits on an object for a notification and the runs the
83    finalizers (finalizer thread).  This is necessary because of a
84    possible deadlock in the GC.
85
86 *******************************************************************************/
87
88 #if defined(ENABLE_THREADS)
89 static void finalizer_thread(void)
90 {
91         while (true) {
92                 /* get the lock on the finalizer mutex, so we can call wait */
93
94                 finalizer_thread_mutex->lock();
95
96                 /* wait forever on that condition till we are signaled */
97         
98                 finalizer_thread_cond->wait(finalizer_thread_mutex);
99
100                 /* leave the lock */
101
102                 finalizer_thread_mutex->unlock();
103
104 #if !defined(NDEBUG)
105                 if (opt_DebugFinalizer)
106                         log_println("[finalizer thread    : status=awake]");
107 #endif
108
109                 /* and call the finalizers */
110
111                 gc_invoke_finalizers();
112
113 #if !defined(NDEBUG)
114                 if (opt_DebugFinalizer)
115                         log_println("[finalizer thread    : status=sleeping]");
116 #endif
117         }
118 }
119 #endif
120
121
122 /* finalizer_start_thread ******************************************************
123
124    Starts the finalizer thread.
125
126 *******************************************************************************/
127
128 #if defined(ENABLE_THREADS)
129 bool finalizer_start_thread(void)
130 {
131         utf *name;
132
133         name = utf_new_char("Finalizer");
134
135         if (!threads_thread_start_internal(name, finalizer_thread))
136                 return false;
137
138         /* everything's ok */
139
140         return true;
141 }
142 #endif
143
144
145 /* finalizer_notify ************************************************************
146
147    Notifies the finalizer thread that it should run the
148    gc_invoke_finalizers from the GC.
149
150 *******************************************************************************/
151
152 void finalizer_notify(void)
153 {
154 #if !defined(NDEBUG)
155         if (opt_DebugFinalizer)
156                 log_println("[finalizer notified]");
157 #endif
158
159 #if defined(ENABLE_THREADS)
160         /* get the lock on the finalizer lock object, so we can call wait */
161
162         finalizer_thread_mutex->lock();
163
164         /* signal the finalizer thread */
165
166         finalizer_thread_cond->signal();
167
168         /* leave the lock */
169
170         finalizer_thread_mutex->unlock();
171 #else
172         /* if we don't have threads, just run the finalizers */
173
174         gc_invoke_finalizers();
175 #endif
176 }
177
178
179 /* finalizer_run ***************************************************************
180
181    Actually run the finalizer functions.
182
183 *******************************************************************************/
184
185 void finalizer_run(void *o, void *p)
186 {
187         java_handle_t *h;
188         classinfo     *c;
189
190         h = (java_handle_t *) o;
191
192 #if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
193         /* XXX this is only a dirty hack to make Boehm work with handles */
194
195         h = LLNI_WRAP((java_object_t *) h);
196 #endif
197
198         LLNI_class_get(h, c);
199
200 #if !defined(NDEBUG)
201         if (opt_DebugFinalizer) {
202                 log_start();
203                 log_print("[finalizer running   : o=%p p=%p class=", o, p);
204                 class_print(c);
205                 log_print("]");
206                 log_finish();
207         }
208 #endif
209
210         /* call the finalizer function */
211
212         (void) vm_call_method(c->finalizer, h);
213
214 #if !defined(NDEBUG)
215         if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
216                 log_println("[finalizer exception]");
217                 exceptions_print_stacktrace();
218         }
219 #endif
220
221         /* if we had an exception in the finalizer, ignore it */
222
223         exceptions_clear_exception();
224
225 #if defined(ENABLE_GC_BOEHM)
226     lock_schedule_lockrecord_removal(h);
227 #endif
228 }
229
230 #if defined(__cplusplus)
231 }
232 #endif
233
234 /*
235  * These are local overrides for various environment variables in Emacs.
236  * Please do not remove this and leave it at the end of the file, where
237  * Emacs will automagically detect them.
238  * ---------------------------------------------------------------------
239  * Local variables:
240  * mode: c
241  * indent-tabs-mode: t
242  * c-basic-offset: 4
243  * tab-width: 4
244  * End:
245  */