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