* src/threads/thread.cpp: Use a finalizer to remove dead threads.
[cacao.git] / src / threads / posix / thread-posix.hpp
index 89c6d1671ee46eb6dd5b78262b7f40d24704f5d1..b8e63635f769497cf3e9764a0ef0874069dc4797 100644 (file)
@@ -1,6 +1,6 @@
 /* src/threads/posix/thread-posix.hpp - POSIX thread functions
 
-   Copyright (C) 1996-2005, 2006, 2007, 2008
+   Copyright (C) 1996-2011
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
@@ -43,6 +43,8 @@
 #include "threads/condition.hpp"
 #include "threads/mutex.hpp"
 
+#include "vm/global.h"
+
 
 /* threadobject ****************************************************************
 
 #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      */
+#define SUSPEND_REASON_NONE      0      /* no reason to suspend               */
+#define SUSPEND_REASON_JAVA      1      /* suspended from java.lang.Thread    */
+#define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-the-world      */
+#define SUSPEND_REASON_DUMP      3      /* suspended from threadlist dumping  */
+#define SUSPEND_REASON_JVMTI     4      /* suspended from JVMTI agent         */
 
 
 typedef struct threadobject threadobject;
@@ -69,6 +74,7 @@ struct threadobject {
        s4                    index;        /* thread index, starting with 1      */
        u4                    flags;        /* flag field                         */
        u4                    state;        /* state field                        */
+       bool                  is_in_active_list; /* for debugging only            */
 
        pthread_t             tid;          /* pthread id                         */
 
@@ -79,6 +85,7 @@ struct threadobject {
        /* for the sable tasuki lock extension */
        bool                  flc_bit;
        struct threadobject  *flc_list;     /* FLC list head for this thread      */
+       struct threadobject  *flc_tail;     /* tail pointer for FLC list          */
        struct threadobject  *flc_next;     /* next pointer for FLC list          */
        java_handle_t        *flc_object;
        Mutex*                flc_lock;     /* controlling access to these fields */
@@ -93,6 +100,7 @@ struct threadobject {
 
        bool                  interrupted;
        bool                  signaled;
+       bool                  park_permit;
 
        bool                  suspended;    /* is this thread suspended?          */
        s4                    suspend_reason; /* reason for suspending            */
@@ -132,9 +140,6 @@ struct threadobject {
 #if defined(ENABLE_ESCAPE_REASON)
        void *escape_reasons;
 #endif
-
-       listnode_t            linkage;      /* threads-list                       */
-       listnode_t            linkage_free; /* free-list                          */
 };
 
 
@@ -192,11 +197,11 @@ inline static threadobject* thread_get_current(void);
 
 
 // Includes.
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
-#include "native/localref.h"
+#include "native/localref.hpp"
 
-#include "threads/posix/lock.h"
+#include "threads/lock.hpp"
 
 #include "vm/global.h"
 #include "vm/vm.hpp"
@@ -206,25 +211,10 @@ inline static threadobject* thread_get_current(void);
 # include "vm/jit/replace.hpp"
 #endif
 
-#include "vm/jit/stacktrace.hpp"
-
 #if defined(ENABLE_INTRP)
 #include "vm/jit/intrp/intrp.h"
 #endif
 
-#if defined(__DARWIN__)
-# include <mach/mach.h>
-
-typedef struct {
-       Mutex* mutex;
-       Condition* cond;
-       int value;
-} sem_t;
-
-#else
-# include <semaphore.h>
-#endif
-
 
 // FIXME
 #ifdef __cplusplus
@@ -267,17 +257,18 @@ inline static void thread_set_current(threadobject* t)
        result = pthread_setspecific(thread_current_key, t);
 
        if (result != 0)
-               vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
+               //os::abort_errnum(result, "thread_set_current: pthread_setspecific failed");
+               vm_abort("thread_set_current: pthread_setspecific failed");
 #endif
 }
 
 
-inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
+inline static struct stackframeinfo_t* threads_get_current_stackframeinfo(void)
 {
        return THREADOBJECT->_stackframeinfo;
 }
 
-inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
+inline static void threads_set_current_stackframeinfo(struct stackframeinfo_t* sfi)
 {
        THREADOBJECT->_stackframeinfo = sfi;
 }
@@ -285,19 +276,13 @@ inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
 
 /* functions ******************************************************************/
 
-void threads_sem_init(sem_t *sem, bool shared, int value);
-void threads_sem_wait(sem_t *sem);
-void threads_sem_post(sem_t *sem);
-
 void threads_start_thread(threadobject *thread, functionptr function);
 
 void threads_set_thread_priority(pthread_t tid, int priority);
 
-#if defined(ENABLE_GC_CACAO)
-bool threads_suspend_thread(threadobject *thread, s4 reason);
-void threads_suspend_ack(u1* pc, u1* sp);
-bool threads_resume_thread(threadobject *thread);
-#endif
+bool threads_suspend_thread(threadobject *thread, int32_t reason);
+bool threads_resume_thread(threadobject *thread, int32_t reason);
+void threads_suspend_ack();
 
 void threads_join_all_threads(void);
 
@@ -307,6 +292,9 @@ void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
 
 void threads_thread_interrupt(threadobject *thread);
 
+void threads_park(bool absolute, int64_t nanos);
+void threads_unpark(threadobject *thread);
+
 #if defined(ENABLE_TLH)
 void threads_tlh_add_frame();
 void threads_tlh_remove_frame();