Merge -> trunk
[cacao.git] / src / threads / native / threads.h
index 0cb2fcf9d67e6475f436646ba6667c3f4595c628..cf7312b713a83f2bb8cea7eb54acd95c1a2b9f54 100644 (file)
@@ -1,9 +1,7 @@
 /* src/threads/native/threads.h - native threads header
 
-   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: threads.h 7338 2007-02-13 00:17:22Z twisti $
-
 */
 
 
@@ -32,9 +28,7 @@
 
 /* forward typedefs ***********************************************************/
 
-typedef struct threadobject          threadobject;
-typedef union  threads_table_entry_t threads_table_entry_t;
-typedef struct threads_table_t       threads_table_t;
+typedef struct threadobject threadobject;
 
 
 #include "config.h"
@@ -46,12 +40,16 @@ typedef struct threads_table_t       threads_table_t;
 
 #include "mm/memory.h"
 #include "native/jni.h"
-#include "native/include/java_lang_Thread.h"
+#include "native/localref.h"
 
 #include "threads/native/lock.h"
 
 #include "vm/global.h"
 
+#if defined(ENABLE_GC_CACAO)
+# include "vm/jit/replace.h"
+#endif
+
 #include "vm/jit/stacktrace.h"
 
 #if defined(ENABLE_INTRP)
@@ -92,56 +90,29 @@ extern pthread_key_t threads_current_threadobject_key;
 #endif /* defined(HAVE___THREAD) */
 
 
-/* threads_table_entry_t *******************************************************
-
-   An entry in the global threads table.
-
-*******************************************************************************/
-
-union threads_table_entry_t {
-       threadobject       *thread;        /* an existing thread                  */
-       ptrint              nextfree;      /* next free index                     */
-};
-
-
-/* threads_table_t *************************************************************
-
-   Struct for the global threads table.
-
-*******************************************************************************/
-
-struct threads_table_t {
-       threads_table_entry_t *table;      /* the table, threads[0] is the head   */
-                                          /* of the free list. Real entries      */
-                                                                          /* start at threads[1].                */
-       s4                     size;       /* current size of the table           */
-};
-
-
 /* threadobject ****************************************************************
 
-   Every java.lang.Thread object is actually an instance of this
-   structure.
+   Struct holding thread local variables.
 
 *******************************************************************************/
 
 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
+#define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
 
+#define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
+#define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
 
-struct threadobject {
-       java_lang_Thread      o;            /* the java.lang.Thread object        */
-
-       lock_execution_env_t  ee;           /* data for the lock implementation   */
 
-       threadobject         *next;         /* next thread in list, or self       */
-       threadobject         *prev;         /* prev thread in list, or self       */
+struct threadobject {
+       java_object_t        *object;       /* link to java.lang.Thread object    */
 
        ptrint                thinlock;     /* pre-computed thin lock value       */
 
        s4                    index;        /* thread index, starting with 1      */
        u4                    flags;        /* flag field                         */
+       u4                    state;        /* state field                        */
 
        pthread_t             tid;          /* pthread id                         */
 
@@ -149,44 +120,97 @@ struct threadobject {
        mach_port_t           mach_thread;       /* Darwin thread id              */
 #endif
 
-       pthread_mutex_t       joinmutex;
-       pthread_cond_t        joincond;
+       /* for the sable tasuki lock extension */
+       bool                  flc_bit;
+       struct threadobject  *flc_list;     /* FLC list head for this thread      */
+       struct threadobject  *flc_next;     /* next pointer for FLC list          */
+       java_handle_t        *flc_object;
+       pthread_mutex_t       flc_lock;     /* controlling access to these fields */
+       pthread_cond_t        flc_cond;
 
        /* these are used for the wait/notify implementation                      */
        pthread_mutex_t       waitmutex;
        pthread_cond_t        waitcond;
 
+       pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
+       pthread_cond_t        suspendcond;  /* notify to resume this thread       */
+
        bool                  interrupted;
        bool                  signaled;
        bool                  sleeping;
 
+       bool                  suspended;    /* is this thread suspended?          */
+       s4                    suspend_reason; /* reason for suspending            */
+
        u1                   *pc;           /* current PC (used for profiling)    */
 
-       java_objectheader    *_exceptionptr;     /* current exception             */
-       stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
+       java_object_t        *_exceptionptr;     /* current exception             */
+       stackframeinfo_t     *_stackframeinfo;   /* current native stackframeinfo */
        localref_table       *_localref_table;   /* JNI local references          */
 
 #if defined(ENABLE_INTRP)
        Cell                 *_global_sp;        /* stack pointer for interpreter */
 #endif
 
+#if defined(ENABLE_GC_CACAO)
+       bool                  gc_critical;  /* indicates a critical section       */
+
+       sourcestate_t        *ss;
+       executionstate_t     *es;
+#endif
+
        dumpinfo_t            dumpinfo;     /* dump memory info structure         */
+
+#if defined(ENABLE_DEBUG_FILTER)
+       u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
+#endif
+
+#if !defined(NDEBUG)
+       s4                    tracejavacallindent;
+       u4                    tracejavacallcount;
+#endif
+
+       listnode_t            linkage;      /* threads-list                       */
+       listnode_t            linkage_free; /* free-list                          */
 };
 
 
-/* exception pointer **********************************************************/
+/* native-world flags *********************************************************/
+
+#if defined(ENABLE_GC_CACAO)
+# define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
+# define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
+#else
+# define THREAD_NATIVEWORLD_ENTER /*nop*/
+# define THREAD_NATIVEWORLD_EXIT  /*nop*/
+#endif
+
 
-#define exceptionptr      (&(THREADOBJECT->_exceptionptr))
+/* counter for verbose call filter ********************************************/
+
+#if defined(ENABLE_DEBUG_FILTER)
+#      define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
+#endif
 
+/* state for trace java call **************************************************/
 
-/* stackframeinfo *************************************************************/
+#if !defined(NDEBUG)
+#      define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
+#      define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
+#endif
 
-#define STACKFRAMEINFO    (&(THREADOBJECT->_stackframeinfo))
 
+/* inline functions ***********************************************************/
 
-/* variables ******************************************************************/
+inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
+{
+       return THREADOBJECT->_stackframeinfo;
+}
 
-extern threadobject *mainthreadobj;
+inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
+{
+       THREADOBJECT->_stackframeinfo = sfi;
+}
 
 
 /* functions ******************************************************************/
@@ -197,9 +221,6 @@ void threads_sem_post(sem_t *sem);
 
 threadobject *threads_get_current_threadobject(void);
 
-void threads_preinit(void);
-bool threads_init(void);
-
 void threads_start_thread(threadobject *thread, functionptr function);
 
 void threads_set_thread_priority(pthread_t tid, int priority);
@@ -207,21 +228,24 @@ void threads_set_thread_priority(pthread_t tid, int priority);
 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
 bool threads_detach_thread(threadobject *thread);
 
+bool threads_suspend_thread(threadobject *thread, s4 reason);
+void threads_suspend_ack(u1* pc, u1* sp);
+bool threads_resume_thread(threadobject *thread);
+
 void threads_join_all_threads(void);
 
 void threads_sleep(s8 millis, s4 nanos);
-void threads_yield(void);
 
-bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
+void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
 
 void threads_thread_interrupt(threadobject *thread);
 bool threads_check_if_interrupted_and_reset(void);
 bool threads_thread_has_been_interrupted(threadobject *thread);
 
-void threads_cast_stopworld(void);
-void threads_cast_startworld(void);
-
-void threads_dump(void);
+#if !defined(DISABLE_GC)
+void threads_stopworld(void);
+void threads_startworld(void);
+#endif
 
 #endif /* _THREADS_H */