Merged with tip.
[cacao.git] / src / vm / finalizer.c
index 5e683a9a2c99bd85af826d60ca802112b9f5496d..833efbfb127d02449d36c7d5c342e06e867df1d5 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/finalizer.c - finalizer linked list and thread
 
-   Copyright (C) 1996-2005, 2006, 2007 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, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -22,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: finalizer.c 7280 2007-02-03 19:34:10Z twisti $
-
 */
 
 
 #include "vm/types.h"
 
 #include "mm/memory.h"
-#include "native/jni.h"
-#include "native/include/java_lang_Thread.h"
-
-#if defined(WITH_CLASSPATH_GNU)
-# include "native/include/java_lang_VMThread.h"
-#endif
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-# include "threads/native/lock.h"
-#endif
+#include "threads/lock-common.h"
+#include "threads/thread.h"
 
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
@@ -60,8 +48,7 @@
 /* global variables ***********************************************************/
 
 #if defined(ENABLE_THREADS)
-static threadobject      *thread_finalizer;
-static java_objectheader *lock_thread_finalizer;
+static java_object_t *lock_thread_finalizer;
 #endif
 
 
@@ -73,10 +60,12 @@ static java_objectheader *lock_thread_finalizer;
 
 bool finalizer_init(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("finalizer_init");
+
 #if defined(ENABLE_THREADS)
-       lock_thread_finalizer = NEW(java_objectheader);
+       lock_thread_finalizer = NEW(java_object_t);
 
-       lock_init_object_lock(lock_thread_finalizer);
+       LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
 #endif
 
        /* everything's ok */
@@ -99,19 +88,29 @@ static void finalizer_thread(void)
        while (true) {
                /* get the lock on the finalizer lock object, so we can call wait */
 
-               lock_monitor_enter(lock_thread_finalizer);
+               LOCK_MONITOR_ENTER(lock_thread_finalizer);
 
-               /* wait forever (0, 0) on that object till we are signaled */
+               /* wait forever on that object till we are signaled */
        
-               lock_wait_for_object(lock_thread_finalizer, 0, 0);
+               LOCK_WAIT_FOREVER(lock_thread_finalizer);
 
                /* leave the lock */
 
-               lock_monitor_exit(lock_thread_finalizer);
+               LOCK_MONITOR_EXIT(lock_thread_finalizer);
+
+#if !defined(NDEBUG)
+               if (opt_DebugFinalizer)
+                       log_println("[finalizer thread    : status=awake]");
+#endif
 
                /* and call the finalizers */
 
                gc_invoke_finalizers();
+
+#if !defined(NDEBUG)
+               if (opt_DebugFinalizer)
+                       log_println("[finalizer thread    : status=sleeping]");
+#endif
        }
 }
 #endif
@@ -126,37 +125,13 @@ static void finalizer_thread(void)
 #if defined(ENABLE_THREADS)
 bool finalizer_start_thread(void)
 {
-#if defined(WITH_CLASSPATH_GNU)
-       java_lang_VMThread *vmt;
-#endif
+       utf *name;
 
-       /* create the finalizer object */
+       name = utf_new_char("Finalizer");
 
-       thread_finalizer = (threadobject *) builtin_new(class_java_lang_Thread);
-
-       if (thread_finalizer == NULL)
+       if (!threads_thread_start_internal(name, finalizer_thread))
                return false;
 
-#if defined(WITH_CLASSPATH_GNU)
-       vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
-
-       vmt->thread = (java_lang_Thread *) thread_finalizer;
-
-       thread_finalizer->o.vmThread = vmt;
-#endif
-
-       thread_finalizer->flags      = THREAD_FLAG_DAEMON;
-
-       thread_finalizer->o.name     = javastring_new_from_ascii("Finalizer");
-#if defined(ENABLE_JAVASE)
-       thread_finalizer->o.daemon   = true;
-#endif
-       thread_finalizer->o.priority = 5;
-
-       /* actually start the finalizer thread */
-
-       threads_start_thread(thread_finalizer, finalizer_thread);
-
        /* everything's ok */
 
        return true;
@@ -173,18 +148,23 @@ bool finalizer_start_thread(void)
 
 void finalizer_notify(void)
 {
+#if !defined(NDEBUG)
+       if (opt_DebugFinalizer)
+               log_println("[finalizer notified]");
+#endif
+
 #if defined(ENABLE_THREADS)
        /* get the lock on the finalizer lock object, so we can call wait */
 
-       lock_monitor_enter(lock_thread_finalizer);
+       LOCK_MONITOR_ENTER(lock_thread_finalizer);
 
        /* signal the finalizer thread */
        
-       lock_notify_object(lock_thread_finalizer);
+       LOCK_NOTIFY(lock_thread_finalizer);
 
        /* leave the lock */
 
-       lock_monitor_exit(lock_thread_finalizer);
+       LOCK_MONITOR_EXIT(lock_thread_finalizer);
 #else
        /* if we don't have threads, just run the finalizers */
 
@@ -201,13 +181,39 @@ void finalizer_notify(void)
 
 void finalizer_run(void *o, void *p)
 {
-       java_objectheader *ob;
+       java_handle_t *h;
+       classinfo     *c;
+
+       h = (java_handle_t *) o;
+
+#if !defined(ENABLE_GC_CACAO) && defined(ENABLE_HANDLES)
+       /* XXX this is only a dirty hack to make Boehm work with handles */
 
-       ob = (java_objectheader *) o;
+       h = LLNI_WRAP((java_object_t *) h);
+#endif
+
+       LLNI_class_get(h, c);
+
+#if !defined(NDEBUG)
+       if (opt_DebugFinalizer) {
+               log_start();
+               log_print("[finalizer running   : o=%p p=%p class=", o, p);
+               class_print(c);
+               log_print("]");
+               log_finish();
+       }
+#endif
 
        /* call the finalizer function */
 
-       (void) vm_call_method(ob->vftbl->class->finalizer, ob);
+       (void) vm_call_method(c->finalizer, h);
+
+#if !defined(NDEBUG)
+       if (opt_DebugFinalizer && (exceptions_get_exception() != NULL)) {
+               log_println("[finalizer exception]");
+               exceptions_print_stacktrace();
+       }
+#endif
 
        /* if we had an exception in the finalizer, ignore it */