* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / vm / finalizer.c
index 00b0e24ad374e1eebd332240ab77a09c5f0ab87a..d6576eb32fddf721947738d3ddc84a5b222c3156 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/finalizer.c - finalizer linked list and thread
 
-   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
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Christian Thalinger
-
-   Changes:
-
-   $Id: finalizer.c 4908 2006-05-12 16:49:50Z edwin $
-
 */
 
 
 #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"
+
+#include "threads/lock-common.h"
+#include "threads/thread.h"
+
 #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/vm.hpp"
+
 #include "vm/jit/asmpart.h"
 
+#include "vmcore/options.h"
+
 
 /* global variables ***********************************************************/
 
-#if defined(USE_THREADS)
-static java_lang_VMThread *finalizer_vmthread;
-static java_objectheader *lock_finalizer_thread;
+#if defined(ENABLE_THREADS)
+static java_object_t *lock_thread_finalizer;
 #endif
 
 
@@ -68,12 +60,12 @@ static java_objectheader *lock_finalizer_thread;
 
 bool finalizer_init(void)
 {
-#if defined(USE_THREADS)
-       lock_finalizer_thread = NEW(java_objectheader);
+       TRACESUBSYSTEMINITIALIZATION("finalizer_init");
 
-# if defined(NATIVE_THREADS)
-       lock_init_object_lock(lock_finalizer_thread);
-# endif
+#if defined(ENABLE_THREADS)
+       lock_thread_finalizer = NEW(java_object_t);
+
+       LOCK_INIT_OBJECT_LOCK(lock_thread_finalizer);
 #endif
 
        /* everything's ok */
@@ -90,25 +82,35 @@ bool finalizer_init(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 static void finalizer_thread(void)
 {
        while (true) {
                /* get the lock on the finalizer lock object, so we can call wait */
 
-               builtin_monitorenter(lock_finalizer_thread);
+               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_finalizer_thread, 0, 0);
+               LOCK_WAIT_FOREVER(lock_thread_finalizer);
 
                /* leave the lock */
 
-               builtin_monitorexit(lock_finalizer_thread);
+               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
@@ -120,32 +122,16 @@ static void finalizer_thread(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 bool finalizer_start_thread(void)
 {
-       java_lang_Thread *t;
-
-       /* create the finalizer object */
+       utf *name;
 
-       finalizer_vmthread =
-               (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
+       name = utf_new_char("Finalizer");
 
-       if (!finalizer_vmthread)
+       if (!threads_thread_start_internal(name, finalizer_thread))
                return false;
 
-       t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
-
-       t->vmThread = finalizer_vmthread;
-       t->name     = javastring_new_from_ascii("Finalizer");
-       t->daemon   = true;
-       t->priority = 5;
-
-       finalizer_vmthread->thread = t;
-
-       /* actually start the finalizer thread */
-
-       threads_start_thread(t, finalizer_thread);
-
        /* everything's ok */
 
        return true;
@@ -162,18 +148,23 @@ bool finalizer_start_thread(void)
 
 void finalizer_notify(void)
 {
-#if defined(USE_THREADS)
+#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 */
 
-       builtin_monitorenter(lock_finalizer_thread);
+       LOCK_MONITOR_ENTER(lock_thread_finalizer);
 
        /* signal the finalizer thread */
        
-       lock_notify_object(lock_finalizer_thread);
+       LOCK_NOTIFY(lock_thread_finalizer);
 
        /* leave the lock */
 
-       builtin_monitorexit(lock_finalizer_thread);
+       LOCK_MONITOR_EXIT(lock_thread_finalizer);
 #else
        /* if we don't have threads, just run the finalizers */
 
@@ -190,17 +181,43 @@ 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;
 
-       ob = (java_objectheader *) 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);
+
+#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 */
 
-       *exceptionptr = NULL;
+       exceptions_clear_exception();
 }