interrupt() / notify() fix.
[cacao.git] / src / threads / native / threads.h
index b66e1be2a0dd53757ef624546d76ed8eabc0f181..f02f38871e9b8a64c9961927611322fedd6654d3 100644 (file)
@@ -1,9 +1,7 @@
 /* src/threads/native/threads.h - native threads header
 
-   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: Stefan Ring
-
-   Changes: Christian Thalinger
-                       Edwin Steiner
-
-   $Id: threads.h 4909 2006-05-13 23:10:21Z edwin $
-
 */
 
 
 #ifndef _THREADS_H
 #define _THREADS_H
 
+/* forward typedefs ***********************************************************/
+
+typedef struct threadobject threadobject;
+
+
 #include "config.h"
 
 #include <pthread.h>
-#include <semaphore.h>
 #include <ucontext.h>
 
 #include "vm/types.h"
 
-#include "config.h"
-#include "vm/types.h"
-
 #include "mm/memory.h"
 #include "native/jni.h"
-#include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
-#include "native/include/java_lang_Thread.h"
-#include "native/include/java_lang_VMThread.h"
-#include "vm/global.h"
+#include "native/localref.h"
 
 #include "threads/native/lock.h"
 
-#if defined(__DARWIN__)
-#include <mach/mach.h>
+#include "vm/global.h"
 
-/* We need to emulate recursive mutexes. */
-#define MUTEXSIM
+#if defined(ENABLE_GC_CACAO)
+# include "vm/jit/replace.h"
 #endif
 
+#include "vm/jit/stacktrace.h"
 
-/* forward typedefs ***********************************************************/
+#if defined(ENABLE_INTRP)
+#include "vm/jit/intrp/intrp.h"
+#endif
 
-typedef struct nativethread nativethread;
-typedef struct threadobject threadobject;
+#if defined(__DARWIN__)
+# include <mach/mach.h>
+
+typedef struct {
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       int value;
+} sem_t;
+
+#else
+# include <semaphore.h>
+#endif
 
 
 /* current threadobject *******************************************************/
@@ -77,137 +76,178 @@ typedef struct threadobject threadobject;
 
 #define THREADSPECIFIC    __thread
 #define THREADOBJECT      threads_current_threadobject
-#define THREADINFO        (&threads_current_threadobject->info)
 
 extern __thread threadobject *threads_current_threadobject;
 
 #else /* defined(HAVE___THREAD) */
 
 #define THREADSPECIFIC
-#define THREADOBJECT      pthread_getspecific(threads_current_threadobject_key)
-#define THREADINFO        (&((threadobject*) pthread_getspecific(threads_current_threadobject_key))->info)
+#define THREADOBJECT \
+       ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
 
 extern pthread_key_t threads_current_threadobject_key;
 
 #endif /* defined(HAVE___THREAD) */
 
 
-/* nativethread ****************************************************************
+/* threadobject ****************************************************************
 
-   XXX
+   Struct holding thread local variables.
 
 *******************************************************************************/
 
-struct nativethread {
-       threadobject      *next;
-       threadobject      *prev;
-       java_objectheader *_exceptionptr;
-       stackframeinfo    *_stackframeinfo;
-       localref_table    *_localref_table; /* JNI local references               */
-#if defined(ENABLE_INTRP)
-       void              *_global_sp;
-#endif
-       pthread_t          tid;
-#if defined(__DARWIN__)
-       mach_port_t        mach_thread;
-#endif
-       pthread_mutex_t    joinMutex;
-       pthread_cond_t     joinCond;
-};
+#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      */
 
-/* threadobject ****************************************************************
 
-   Every java.lang.VMThread object is actually an instance of this
-   structure.
+struct threadobject {
+       java_object_t        *object;       /* link to java.lang.Thread object    */
 
-*******************************************************************************/
+       ptrint                thinlock;     /* pre-computed thin lock value       */
 
-struct threadobject {
-       java_lang_VMThread    o;
-       nativethread          info;         /* some general pthreads stuff        */
-       lock_execution_env_t  ee;           /* contains our lock record pool      */
+       s4                    index;        /* thread index, starting with 1      */
+       u4                    flags;        /* flag field                         */
+       u4                    state;        /* state field                        */
+
+       pthread_t             tid;          /* pthread id                         */
+
+#if defined(__DARWIN__)
+       mach_port_t           mach_thread;       /* Darwin thread id              */
+#endif
+
+       /* 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       waitLock;
-       pthread_cond_t        waitCond;
+       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                  isSleeping;
+       bool                  sleeping;
 
-       dumpinfo              dumpinfo;     /* dump memory info structure         */
-};
+       bool                  suspended;    /* is this thread suspended?          */
+       s4                    suspend_reason; /* reason for suspending            */
 
+       u1                   *pc;           /* current PC (used for profiling)    */
 
-/* variables ******************************************************************/
+       java_object_t        *_exceptionptr;     /* current exception             */
+       stackframeinfo_t     *_stackframeinfo;   /* current native stackframeinfo */
+       localref_table       *_localref_table;   /* JNI local references          */
 
-extern threadobject *mainthreadobj;
+#if defined(ENABLE_INTRP)
+       Cell                 *_global_sp;        /* stack pointer for interpreter */
+#endif
 
+#if defined(ENABLE_GC_CACAO)
+       bool                  gc_critical;  /* indicates a critical section       */
 
-/* functions ******************************************************************/
+       sourcestate_t        *ss;
+       executionstate_t     *es;
+#endif
 
-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);
+       dumpinfo_t            dumpinfo;     /* dump memory info structure         */
 
-threadobject *threads_get_current_threadobject(void);
+#if defined(ENABLE_DEBUG_FILTER)
+       u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
+#endif
 
-void threads_preinit(void);
-bool threads_init(u1 *stackbottom);
+#if !defined(NDEBUG)
+       s4                    tracejavacallindent;
+       u4                    tracejavacallcount;
+#endif
 
-void threads_init_threadobject(java_lang_VMThread *);
+       listnode_t            linkage;      /* threads-list                       */
+       listnode_t            linkage_free; /* free-list                          */
+};
 
-void threads_start_thread(java_lang_Thread *t, functionptr function);
 
-void threads_join_all_threads(void);
+/* native-world flags *********************************************************/
 
-void threads_sleep(s8 millis, s4 nanos);
-void threads_yield(void);
+#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
 
-bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
 
-void threads_interrupt_thread(java_lang_VMThread *);
-bool threads_check_if_interrupted_and_reset(void);
-bool threads_thread_has_been_interrupted(java_lang_VMThread *);
+/* counter for verbose call filter ********************************************/
 
-#if defined(ENABLE_JVMTI)
-void threads_set_current_threadobject(threadobject *thread);
+#if defined(ENABLE_DEBUG_FILTER)
+#      define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
 #endif
 
-void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
+/* state for trace java call **************************************************/
 
-void threads_cast_stopworld(void);
-void threads_cast_startworld(void);
+#if !defined(NDEBUG)
+#      define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
+#      define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
+#endif
 
-void threads_dump(void);
 
+/* inline functions ***********************************************************/
 
-/******************************************************************************/
-/* Recursive Mutex Implementation for Darwin                                  */
-/******************************************************************************/
+inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
+{
+       return THREADOBJECT->_stackframeinfo;
+}
 
-#if defined(MUTEXSIM)
+inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
+{
+       THREADOBJECT->_stackframeinfo = sfi;
+}
 
-/* We need this for older MacOSX (10.1.x) */
 
-typedef struct {
-       pthread_mutex_t mutex;
-       pthread_t owner;
-       int count;
-} pthread_mutex_rec_t;
+/* 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);
+
+threadobject *threads_get_current_threadobject(void);
+
+bool threads_init(void);
 
-void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
-void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
-void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
-void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
+void threads_start_thread(threadobject *thread, functionptr function);
 
-#else /* !defined(MUTEXSIM) */
+void threads_set_thread_priority(pthread_t tid, int priority);
 
-#define pthread_mutex_lock_rec pthread_mutex_lock
-#define pthread_mutex_unlock_rec pthread_mutex_unlock
-#define pthread_mutex_rec_t pthread_mutex_t
+bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
+bool threads_detach_thread(threadobject *thread);
 
-#endif /* defined(MUTEXSIM) */
+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_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);
+
+#if !defined(DISABLE_GC)
+void threads_stopworld(void);
+void threads_startworld(void);
+#endif
 
 #endif /* _THREADS_H */