* src/vm/jit/stack.c (stack_check_invars): Bugfix: Only set flags to
[cacao.git] / src / vm / finalizer.c
index 429197984ee8e01136e5d8c858f56d6b947623f2..e0e69d02c0297e5ef6d67f51dc15a2c63ae81f83 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/finalizer.c - finalizer linked list and thread
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Christian Thalinger
 
    Changes:
 
-   $Id: finalizer.c 3686 2005-11-16 13:29:58Z twisti $
+   $Id: finalizer.c 5123 2006-07-12 21:45:34Z twisti $
 
 */
 
 
 #include "config.h"
+
+#include <stdlib.h>
+
 #include "vm/types.h"
 
 #include "mm/memory.h"
 #include "native/jni.h"
 #include "native/include/java_lang_Thread.h"
 #include "native/include/java_lang_VMThread.h"
+
+#if defined(ENABLE_THREADS)
+# include "threads/native/lock.h"
+#endif
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
+#include "vm/options.h"
 #include "vm/stringlocal.h"
+#include "vm/vm.h"
 #include "vm/jit/asmpart.h"
 
 
-/* local structures ***********************************************************/
-
-typedef struct finalizer_entry finalizer_entry;
-
-struct finalizer_entry {
-       java_objectheader *o;
-       listnode           linkage;
-};
-
-
 /* global variables ***********************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 static java_lang_VMThread *finalizer_vmthread;
 static java_objectheader *lock_finalizer_thread;
-static list *finalizer_list;
 #endif
 
 
@@ -74,17 +73,10 @@ static list *finalizer_list;
 
 bool finalizer_init(void)
 {
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
        lock_finalizer_thread = NEW(java_objectheader);
 
-# if defined(NATIVE_THREADS)
-       initObjectLock(lock_finalizer_thread);
-# endif
-
-       /* initialize the finalizer list */
-
-       finalizer_list = NEW(list);
-       list_init(finalizer_list, OFFSET(finalizer_entry, linkage));
+       lock_init_object_lock(lock_finalizer_thread);
 #endif
 
        /* everything's ok */
@@ -101,79 +93,37 @@ bool finalizer_init(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 static void finalizer_thread(void)
 {
-       finalizer_entry   *fi;
-       java_objectheader *o;
+       while (true) {
+               /* get the lock on the finalizer lock object, so we can call wait */
 
-       /* get the lock on the finalizer lock object, so we can call wait */
+               lock_monitor_enter(lock_finalizer_thread);
 
-       while (true) {
                /* wait forever (0, 0) on that object till we are signaled */
        
-               wait_cond_for_object(lock_finalizer_thread, 0, 0);
-
-               /* now handle all finalizers stored in the list */
-
-               fi = (finalizer_entry *) list_first(finalizer_list);
-
-               /* release the lock so other threads, or the finalizer thread
-                  itself, can add new finalizers to the list */
-
-               builtin_monitorexit(lock_finalizer_thread);
-
-               /* is there actually a finalizer in the list? */
-
-               if (fi) {
-                       do {
-                               /* just for simpler code */
-
-                               o = fi->o;
-
-                               /* call the finalizer function */
+               lock_wait_for_object(lock_finalizer_thread, 0, 0);
 
-                               asm_calljavafunction(o->vftbl->class->finalizer, o,
-                                                                        NULL, NULL, NULL);
+               /* leave the lock */
 
-                               /* if we had an exception in the finalizer, ignore it */
+               lock_monitor_exit(lock_finalizer_thread);
 
-                               *exceptionptr = NULL;
+               /* and call the finalizers */
 
-                               /* enter the monitor again, so we don't get finalizer
-                                  list race conditions */
-
-                               builtin_monitorenter(lock_finalizer_thread);
-
-                               /* remove and clear finalized entry */
-
-                               list_remove(finalizer_list, fi);
-
-                               fi->o = NULL;
-                               FREE(fi, finalizer_entry);
-
-                               /* get next finalizer from the list */
-
-                               fi = list_first(finalizer_list);
-
-                               /* leave the monitor again */
-
-                               builtin_monitorexit(lock_finalizer_thread);
-                       } while (fi != NULL);
-               }
-
-               builtin_monitorenter(lock_finalizer_thread);
+               gc_invoke_finalizers();
        }
 }
 #endif
 
+
 /* finalizer_start_thread ******************************************************
 
    Starts the finalizer thread.
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 bool finalizer_start_thread(void)
 {
        java_lang_Thread *t;
@@ -189,7 +139,7 @@ bool finalizer_start_thread(void)
        t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
        t->vmThread = finalizer_vmthread;
-       t->name     = javastring_new_char("Finalizer");
+       t->name     = javastring_new_from_ascii("Finalizer");
        t->daemon   = true;
        t->priority = 5;
 
@@ -206,51 +156,54 @@ bool finalizer_start_thread(void)
 #endif
 
 
-/* finalizer_add ***************************************************************
+/* finalizer_notify ************************************************************
 
-   Adds a finalizer to be called to the linked list of finalizers.
+   Notifies the finalizer thread that it should run the
+   gc_invoke_finalizers from the GC.
 
 *******************************************************************************/
 
-void finalizer_add(void *o, void *p)
+void finalizer_notify(void)
 {
-#if defined(USE_THREADS)
-       java_objectheader *ob;
-       finalizer_entry   *fi;
+#if defined(ENABLE_THREADS)
+       /* get the lock on the finalizer lock object, so we can call wait */
 
-       ob = (java_objectheader *) o;
+       lock_monitor_enter(lock_finalizer_thread);
 
-       /* wait for the finalizer thread to finish finalizer list operations */
+       /* signal the finalizer thread */
+       
+       lock_notify_object(lock_finalizer_thread);
 
-       builtin_monitorenter(lock_finalizer_thread);
+       /* leave the lock */
 
-       /* create finalizer entry, fill it and add it to the list */
+       lock_monitor_exit(lock_finalizer_thread);
+#else
+       /* if we don't have threads, just run the finalizers */
 
-       fi = NEW(finalizer_entry);
-       fi->o = ob;
+       gc_invoke_finalizers();
+#endif
+}
 
-       list_addlast(finalizer_list, fi);
 
-       /* wakeup the finalizer thread */
+/* finalizer_run ***************************************************************
 
-       signal_cond_for_object(lock_finalizer_thread);
+   Actually run the finalizer functions.
 
-       /* release the lock after the entry is added */
+*******************************************************************************/
 
-       builtin_monitorexit(lock_finalizer_thread);
-#else
+void finalizer_run(void *o, void *p)
+{
        java_objectheader *ob;
 
        ob = (java_objectheader *) o;
 
        /* call the finalizer function */
 
-       asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
+       (void) vm_call_method(ob->vftbl->class->finalizer, ob);
 
        /* if we had an exception in the finalizer, ignore it */
 
        *exceptionptr = NULL;
-#endif
 }