* Removed all Id tags.
[cacao.git] / src / vm / finalizer.c
1 /* src/vm/finalizer.c - finalizer linked list and thread
2
3    Copyright (C) 1996-2005, 2006, 2007 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 */
26
27
28 #include "config.h"
29
30 #include <stdlib.h>
31
32 #include "vm/types.h"
33
34 #include "mm/memory.h"
35
36 #include "threads/lock-common.h"
37 #include "threads/threads-common.h"
38
39 #include "vm/builtin.h"
40 #include "vm/exceptions.h"
41 #include "vm/global.h"
42 #include "vm/stringlocal.h"
43 #include "vm/vm.h"
44
45 #include "vm/jit/asmpart.h"
46
47 #include "vmcore/options.h"
48
49
50 /* global variables ***********************************************************/
51
52 #if defined(ENABLE_THREADS)
53 static java_object_t *lock_thread_finalizer;
54 #endif
55
56
57 /* finalizer_init **************************************************************
58
59    Initializes the finalizer global lock and the linked list.
60
61 *******************************************************************************/
62
63 bool finalizer_init(void)
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 (0, 0) on that object till we are signaled */
94         
95                 lock_wait_for_object(lock_thread_finalizer, 0, 0);
96
97                 /* leave the lock */
98
99                 lock_monitor_exit(lock_thread_finalizer);
100
101                 /* and call the finalizers */
102
103                 gc_invoke_finalizers();
104         }
105 }
106 #endif
107
108
109 /* finalizer_start_thread ******************************************************
110
111    Starts the finalizer thread.
112
113 *******************************************************************************/
114
115 #if defined(ENABLE_THREADS)
116 bool finalizer_start_thread(void)
117 {
118         utf *name;
119
120         name = utf_new_char("Finalizer");
121
122         if (!threads_thread_start_internal(name, finalizer_thread))
123                 return false;
124
125         /* everything's ok */
126
127         return true;
128 }
129 #endif
130
131
132 /* finalizer_notify ************************************************************
133
134    Notifies the finalizer thread that it should run the
135    gc_invoke_finalizers from the GC.
136
137 *******************************************************************************/
138
139 void finalizer_notify(void)
140 {
141 #if defined(ENABLE_THREADS)
142         /* get the lock on the finalizer lock object, so we can call wait */
143
144         lock_monitor_enter(lock_thread_finalizer);
145
146         /* signal the finalizer thread */
147         
148         lock_notify_object(lock_thread_finalizer);
149
150         /* leave the lock */
151
152         lock_monitor_exit(lock_thread_finalizer);
153 #else
154         /* if we don't have threads, just run the finalizers */
155
156         gc_invoke_finalizers();
157 #endif
158 }
159
160
161 /* finalizer_run ***************************************************************
162
163    Actually run the finalizer functions.
164
165 *******************************************************************************/
166
167 void finalizer_run(void *o, void *p)
168 {
169         java_object_t *ob;
170
171         ob = (java_object_t *) o;
172
173         /* call the finalizer function */
174
175         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
176
177         /* if we had an exception in the finalizer, ignore it */
178
179         exceptions_clear_exception();
180 }
181
182
183 /*
184  * These are local overrides for various environment variables in Emacs.
185  * Please do not remove this and leave it at the end of the file, where
186  * Emacs will automagically detect them.
187  * ---------------------------------------------------------------------
188  * Local variables:
189  * mode: c
190  * indent-tabs-mode: t
191  * c-basic-offset: 4
192  * tab-width: 4
193  * End:
194  */