Merged trunk and subtype.
[cacao.git] / src / vm / finalizer.c
index c5cd52a32c846b9dfea090bacdc2ed7ddce1e896..ea331ec96e9c12b7a51d2298d362f8c15ba3af0e 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 7338 2007-02-13 00:17:22Z twisti $
-
 */
 
 
 
 #include "mm/memory.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/threads-common.h"
-
-# include "threads/native/threads.h"
-# include "threads/native/lock.h"
-#endif
+#include "threads/condition.hpp"
+#include "threads/mutex.hpp"
+#include "threads/thread.hpp"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/exceptions.hpp"
 #include "vm/global.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/options.h"
+#include "vm/vm.hpp"
 
 #include "vm/jit/asmpart.h"
 
-#include "vmcore/options.h"
-
 
 /* global variables ***********************************************************/
 
 #if defined(ENABLE_THREADS)
-static threadobject      *thread_finalizer;
-static java_objectheader *lock_thread_finalizer;
+static Mutex     *finalizer_thread_mutex;
+static Condition *finalizer_thread_cond;
 #endif
 
 
@@ -69,10 +60,11 @@ static java_objectheader *lock_thread_finalizer;
 
 bool finalizer_init(void)
 {
-#if defined(ENABLE_THREADS)
-       lock_thread_finalizer = NEW(java_objectheader);
+       TRACESUBSYSTEMINITIALIZATION("finalizer_init");
 
-       lock_init_object_lock(lock_thread_finalizer);
+#if defined(ENABLE_THREADS)
+       finalizer_thread_mutex = Mutex_new();
+       finalizer_thread_cond  = Condition_new();
 #endif
 
        /* everything's ok */
@@ -93,21 +85,31 @@ bool finalizer_init(void)
 static void finalizer_thread(void)
 {
        while (true) {
-               /* get the lock on the finalizer lock object, so we can call wait */
+               /* get the lock on the finalizer mutex, so we can call wait */
 
-               lock_monitor_enter(lock_thread_finalizer);
+               Mutex_lock(finalizer_thread_mutex);
 
-               /* wait forever (0, 0) on that object till we are signaled */
+               /* wait forever on that condition till we are signaled */
        
-               lock_wait_for_object(lock_thread_finalizer, 0, 0);
+               Condition_wait(finalizer_thread_cond, finalizer_thread_mutex);
 
                /* leave the lock */
 
-               lock_monitor_exit(lock_thread_finalizer);
+               Mutex_unlock(finalizer_thread_mutex);
+
+#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,15 +128,9 @@ bool finalizer_start_thread(void)
 
        name = utf_new_char("Finalizer");
 
-       thread_finalizer = threads_create_thread(name);
-
-       if (thread_finalizer == NULL)
+       if (!threads_thread_start_internal(name, finalizer_thread))
                return false;
 
-       /* actually start the finalizer thread */
-
-       threads_start_thread(thread_finalizer, finalizer_thread);
-
        /* everything's ok */
 
        return true;
@@ -151,18 +147,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);
+       Mutex_lock(finalizer_thread_mutex);
 
        /* signal the finalizer thread */
-       
-       lock_notify_object(lock_thread_finalizer);
+
+       Condition_signal(finalizer_thread_cond);
 
        /* leave the lock */
 
-       lock_monitor_exit(lock_thread_finalizer);
+       Mutex_unlock(finalizer_thread_mutex);
 #else
        /* if we don't have threads, just run the finalizers */
 
@@ -179,13 +180,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 */
+
+       h = LLNI_WRAP((java_object_t *) h);
+#endif
+
+       LLNI_class_get(h, c);
 
-       ob = (java_objectheader *) o;
+#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 */