Merged trunk and subtype.
[cacao.git] / src / vm / finalizer.c
index a9ecada15b4417cc42456906771334ffc7ff62db..ea331ec96e9c12b7a51d2298d362f8c15ba3af0e 100644 (file)
@@ -1,9 +1,7 @@
 /* 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, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    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.
-
-   Contact: cacao@complang.tuwien.ac.at
-
-   Authors: Christian Thalinger
-
-   Changes:
-
-   $Id: finalizer.c 3692 2005-11-17 13:35:49Z twisti $
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
 */
 
 
 #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"
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+
+#include "threads/condition.hpp"
+#include "threads/mutex.hpp"
+#include "threads/thread.hpp"
+
+#include "vm/jit/builtin.hpp"
+#include "vm/exceptions.hpp"
 #include "vm/global.h"
-#include "vm/stringlocal.h"
+#include "vm/options.h"
+#include "vm/vm.hpp"
+
 #include "vm/jit/asmpart.h"
 
 
 /* global variables ***********************************************************/
 
-#if defined(USE_THREADS)
-static java_lang_VMThread *finalizer_vmthread;
-static java_objectheader *lock_finalizer_thread;
+#if defined(ENABLE_THREADS)
+static Mutex     *finalizer_thread_mutex;
+static Condition *finalizer_thread_cond;
 #endif
 
 
@@ -63,12 +60,11 @@ 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)
-       initObjectLock(lock_finalizer_thread);
-# endif
+#if defined(ENABLE_THREADS)
+       finalizer_thread_mutex = Mutex_new();
+       finalizer_thread_cond  = Condition_new();
 #endif
 
        /* everything's ok */
@@ -85,25 +81,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 */
+               /* get the lock on the finalizer mutex, so we can call wait */
 
-               builtin_monitorenter(lock_finalizer_thread);
+               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 */
        
-               wait_cond_for_object(lock_finalizer_thread, 0, 0);
+               Condition_wait(finalizer_thread_cond, finalizer_thread_mutex);
 
                /* leave the lock */
 
-               builtin_monitorexit(lock_finalizer_thread);
+               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
@@ -115,32 +121,16 @@ static void finalizer_thread(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 bool finalizer_start_thread(void)
 {
-       java_lang_Thread *t;
+       utf *name;
 
-       /* create the finalizer object */
+       name = utf_new_char("Finalizer");
 
-       finalizer_vmthread =
-               (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
-
-       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_char("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;
@@ -157,18 +147,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);
+       Mutex_lock(finalizer_thread_mutex);
 
        /* signal the finalizer thread */
-       
-       signal_cond_for_object(lock_finalizer_thread);
+
+       Condition_signal(finalizer_thread_cond);
 
        /* leave the lock */
 
-       builtin_monitorexit(lock_finalizer_thread);
+       Mutex_unlock(finalizer_thread_mutex);
 #else
        /* if we don't have threads, just run the finalizers */
 
@@ -185,15 +180,43 @@ void finalizer_notify(void)
 
 void finalizer_run(void *o, void *p)
 {
-       java_objectheader *ob = (java_objectheader *) o;
+       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);
+
+#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 */
 
-       asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
+       (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();
 }