Merged trunk and subtype.
[cacao.git] / src / vm / finalizer.c
index 429197984ee8e01136e5d8c858f56d6b947623f2..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 3686 2005-11-16 13:29:58Z 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 "vm/global.h"
-#include "vm/stringlocal.h"
-#include "vm/jit/asmpart.h"
 
+#include "threads/condition.hpp"
+#include "threads/mutex.hpp"
+#include "threads/thread.hpp"
 
-/* local structures ***********************************************************/
-
-typedef struct finalizer_entry finalizer_entry;
+#include "vm/jit/builtin.hpp"
+#include "vm/exceptions.hpp"
+#include "vm/global.h"
+#include "vm/options.h"
+#include "vm/vm.hpp"
 
-struct finalizer_entry {
-       java_objectheader *o;
-       listnode           linkage;
-};
+#include "vm/jit/asmpart.h"
 
 
 /* global variables ***********************************************************/
 
-#if defined(USE_THREADS)
-static java_lang_VMThread *finalizer_vmthread;
-static java_objectheader *lock_finalizer_thread;
-static list *finalizer_list;
+#if defined(ENABLE_THREADS)
+static Mutex     *finalizer_thread_mutex;
+static Condition *finalizer_thread_cond;
 #endif
 
 
@@ -74,17 +60,11 @@ static list *finalizer_list;
 
 bool finalizer_init(void)
 {
-#if defined(USE_THREADS)
-       lock_finalizer_thread = NEW(java_objectheader);
-
-# if defined(NATIVE_THREADS)
-       initObjectLock(lock_finalizer_thread);
-# endif
+       TRACESUBSYSTEMINITIALIZATION("finalizer_init");
 
-       /* initialize the finalizer list */
-
-       finalizer_list = NEW(list);
-       list_init(finalizer_list, OFFSET(finalizer_entry, linkage));
+#if defined(ENABLE_THREADS)
+       finalizer_thread_mutex = Mutex_new();
+       finalizer_thread_cond  = Condition_new();
 #endif
 
        /* everything's ok */
@@ -101,104 +81,56 @@ bool finalizer_init(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
 static void finalizer_thread(void)
 {
-       finalizer_entry   *fi;
-       java_objectheader *o;
-
-       /* get the lock on the finalizer lock object, so we can call wait */
-
        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;
+               /* get the lock on the finalizer mutex, so we can call wait */
 
-                               /* call the finalizer function */
+               Mutex_lock(finalizer_thread_mutex);
 
-                               asm_calljavafunction(o->vftbl->class->finalizer, o,
-                                                                        NULL, NULL, NULL);
-
-                               /* if we had an exception in the finalizer, ignore it */
-
-                               *exceptionptr = NULL;
-
-                               /* 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);
+               /* wait forever on that condition till we are signaled */
+       
+               Condition_wait(finalizer_thread_cond, finalizer_thread_mutex);
 
-                               fi->o = NULL;
-                               FREE(fi, finalizer_entry);
+               /* leave the lock */
 
-                               /* get next finalizer from the list */
+               Mutex_unlock(finalizer_thread_mutex);
 
-                               fi = list_first(finalizer_list);
+#if !defined(NDEBUG)
+               if (opt_DebugFinalizer)
+                       log_println("[finalizer thread    : status=awake]");
+#endif
 
-                               /* leave the monitor again */
+               /* and call the finalizers */
 
-                               builtin_monitorexit(lock_finalizer_thread);
-                       } while (fi != NULL);
-               }
+               gc_invoke_finalizers();
 
-               builtin_monitorenter(lock_finalizer_thread);
+#if !defined(NDEBUG)
+               if (opt_DebugFinalizer)
+                       log_println("[finalizer thread    : status=sleeping]");
+#endif
        }
 }
 #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;
-
-       /* 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_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;
@@ -206,51 +138,85 @@ 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(NDEBUG)
+       if (opt_DebugFinalizer)
+               log_println("[finalizer notified]");
+#endif
 
-       ob = (java_objectheader *) o;
+#if defined(ENABLE_THREADS)
+       /* get the lock on the finalizer lock object, so we can call wait */
 
-       /* wait for the finalizer thread to finish finalizer list operations */
+       Mutex_lock(finalizer_thread_mutex);
 
-       builtin_monitorenter(lock_finalizer_thread);
+       /* signal the finalizer thread */
 
-       /* create finalizer entry, fill it and add it to the list */
+       Condition_signal(finalizer_thread_cond);
 
-       fi = NEW(finalizer_entry);
-       fi->o = ob;
+       /* leave the lock */
 
-       list_addlast(finalizer_list, fi);
+       Mutex_unlock(finalizer_thread_mutex);
+#else
+       /* if we don't have threads, just run the finalizers */
 
-       /* wakeup the finalizer thread */
+       gc_invoke_finalizers();
+#endif
+}
 
-       signal_cond_for_object(lock_finalizer_thread);
 
-       /* release the lock after the entry is added */
+/* finalizer_run ***************************************************************
 
-       builtin_monitorexit(lock_finalizer_thread);
-#else
-       java_objectheader *ob;
+   Actually run the finalizer functions.
 
-       ob = (java_objectheader *) o;
+*******************************************************************************/
+
+void finalizer_run(void *o, void *p)
+{
+       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;
-#endif
+       exceptions_clear_exception();
 }