67689c47a6623a497577d97296fc4ec4a7569a22
[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    $Id: finalizer.c 7343 2007-02-13 02:36:29Z ajordan $
26
27 */
28
29
30 #include "config.h"
31
32 #include <stdlib.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37
38 #if defined(ENABLE_THREADS)
39 # include "threads/threads-common.h"
40
41 # include "threads/native/threads.h"
42 # include "threads/native/lock.h"
43 #else
44 # include "threads/none/threads.h"
45 # include "threads/none/lock.h"
46 #endif
47
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/stringlocal.h"
52 #include "vm/vm.h"
53
54 #include "vm/jit/asmpart.h"
55
56 #include "vmcore/options.h"
57
58
59 /* global variables ***********************************************************/
60
61 #if defined(ENABLE_THREADS)
62 static threadobject      *thread_finalizer;
63 static java_objectheader *lock_thread_finalizer;
64 #endif
65
66
67 /* finalizer_init **************************************************************
68
69    Initializes the finalizer global lock and the linked list.
70
71 *******************************************************************************/
72
73 bool finalizer_init(void)
74 {
75 #if defined(ENABLE_THREADS)
76         lock_thread_finalizer = NEW(java_objectheader);
77
78         lock_init_object_lock(lock_thread_finalizer);
79 #endif
80
81         /* everything's ok */
82
83         return true;
84 }
85
86
87 /* finalizer_thread ************************************************************
88
89    This thread waits on an object for a notification and the runs the
90    finalizers (finalizer thread).  This is necessary because of a
91    possible deadlock in the GC.
92
93 *******************************************************************************/
94
95 #if defined(ENABLE_THREADS)
96 static void finalizer_thread(void)
97 {
98         while (true) {
99                 /* get the lock on the finalizer lock object, so we can call wait */
100
101                 lock_monitor_enter(lock_thread_finalizer);
102
103                 /* wait forever (0, 0) on that object till we are signaled */
104         
105                 lock_wait_for_object(lock_thread_finalizer, 0, 0);
106
107                 /* leave the lock */
108
109                 lock_monitor_exit(lock_thread_finalizer);
110
111                 /* and call the finalizers */
112
113                 gc_invoke_finalizers();
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         thread_finalizer = threads_create_thread(name);
133
134         if (thread_finalizer == NULL)
135                 return false;
136
137         /* actually start the finalizer thread */
138
139         threads_start_thread(thread_finalizer, finalizer_thread);
140
141         /* everything's ok */
142
143         return true;
144 }
145 #endif
146
147
148 /* finalizer_notify ************************************************************
149
150    Notifies the finalizer thread that it should run the
151    gc_invoke_finalizers from the GC.
152
153 *******************************************************************************/
154
155 void finalizer_notify(void)
156 {
157 #if defined(ENABLE_THREADS)
158         /* get the lock on the finalizer lock object, so we can call wait */
159
160         lock_monitor_enter(lock_thread_finalizer);
161
162         /* signal the finalizer thread */
163         
164         lock_notify_object(lock_thread_finalizer);
165
166         /* leave the lock */
167
168         lock_monitor_exit(lock_thread_finalizer);
169 #else
170         /* if we don't have threads, just run the finalizers */
171
172         gc_invoke_finalizers();
173 #endif
174 }
175
176
177 /* finalizer_run ***************************************************************
178
179    Actually run the finalizer functions.
180
181 *******************************************************************************/
182
183 void finalizer_run(void *o, void *p)
184 {
185         java_objectheader *ob;
186
187         ob = (java_objectheader *) o;
188
189         /* call the finalizer function */
190
191         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
192
193         /* if we had an exception in the finalizer, ignore it */
194
195         exceptions_clear_exception();
196 }
197
198
199 /*
200  * These are local overrides for various environment variables in Emacs.
201  * Please do not remove this and leave it at the end of the file, where
202  * Emacs will automagically detect them.
203  * ---------------------------------------------------------------------
204  * Local variables:
205  * mode: c
206  * indent-tabs-mode: t
207  * c-basic-offset: 4
208  * tab-width: 4
209  * End:
210  */