* src/threads/lock-common.h: New file.
[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 7813 2007-04-25 19:20:13Z twisti $
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 #include "threads/lock-common.h"
39 #include "threads/threads-common.h"
40
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/global.h"
44 #include "vm/stringlocal.h"
45 #include "vm/vm.h"
46
47 #include "vm/jit/asmpart.h"
48
49 #include "vmcore/options.h"
50
51
52 /* global variables ***********************************************************/
53
54 #if defined(ENABLE_THREADS)
55 static threadobject      *thread_finalizer;
56 static java_objectheader *lock_thread_finalizer;
57 #endif
58
59
60 /* finalizer_init **************************************************************
61
62    Initializes the finalizer global lock and the linked list.
63
64 *******************************************************************************/
65
66 bool finalizer_init(void)
67 {
68 #if defined(ENABLE_THREADS)
69         lock_thread_finalizer = NEW(java_objectheader);
70
71         LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
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 lock object, so we can call wait */
93
94                 lock_monitor_enter(lock_thread_finalizer);
95
96                 /* wait forever (0, 0) on that object till we are signaled */
97         
98                 lock_wait_for_object(lock_thread_finalizer, 0, 0);
99
100                 /* leave the lock */
101
102                 lock_monitor_exit(lock_thread_finalizer);
103
104                 /* and call the finalizers */
105
106                 gc_invoke_finalizers();
107         }
108 }
109 #endif
110
111
112 /* finalizer_start_thread ******************************************************
113
114    Starts the finalizer thread.
115
116 *******************************************************************************/
117
118 #if defined(ENABLE_THREADS)
119 bool finalizer_start_thread(void)
120 {
121         utf *name;
122
123         name = utf_new_char("Finalizer");
124
125         thread_finalizer = threads_thread_create_internal(name);
126
127         if (thread_finalizer == NULL)
128                 return false;
129
130         /* actually start the finalizer thread */
131
132         threads_start_thread(thread_finalizer, finalizer_thread);
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(ENABLE_THREADS)
151         /* get the lock on the finalizer lock object, so we can call wait */
152
153         lock_monitor_enter(lock_thread_finalizer);
154
155         /* signal the finalizer thread */
156         
157         lock_notify_object(lock_thread_finalizer);
158
159         /* leave the lock */
160
161         lock_monitor_exit(lock_thread_finalizer);
162 #else
163         /* if we don't have threads, just run the finalizers */
164
165         gc_invoke_finalizers();
166 #endif
167 }
168
169
170 /* finalizer_run ***************************************************************
171
172    Actually run the finalizer functions.
173
174 *******************************************************************************/
175
176 void finalizer_run(void *o, void *p)
177 {
178         java_objectheader *ob;
179
180         ob = (java_objectheader *) o;
181
182         /* call the finalizer function */
183
184         (void) vm_call_method(ob->vftbl->class->finalizer, ob);
185
186         /* if we had an exception in the finalizer, ignore it */
187
188         exceptions_clear_exception();
189 }
190
191
192 /*
193  * These are local overrides for various environment variables in Emacs.
194  * Please do not remove this and leave it at the end of the file, where
195  * Emacs will automagically detect them.
196  * ---------------------------------------------------------------------
197  * Local variables:
198  * mode: c
199  * indent-tabs-mode: t
200  * c-basic-offset: 4
201  * tab-width: 4
202  * End:
203  */