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