* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / threads / native / threads.h
index e6857f10c9783c0e371279a9fea5e23c829c78ab..8de392338d988032dc19833ec8b5c156b809ac75 100644 (file)
@@ -1,6 +1,6 @@
 /* src/threads/native/threads.h - native threads header
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   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
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Stefan Ring
-
-   Changes: Christian Thalinger
-
-   $Id: threads.h 4444 2006-02-05 13:52:26Z stefan $
-
 */
 
 
 #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/localref.h"
 #include "native/include/java_lang_Thread.h"
-#include "native/include/java_lang_VMThread.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)
+#include "vm/jit/intrp/intrp.h"
+#endif
+
 #if defined(__DARWIN__)
-#include <mach/mach.h>
+# include <mach/mach.h>
 
-/* We need to emulate recursive mutexes. */
-#define MUTEXSIM
+typedef struct {
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       int value;
+} sem_t;
+
+#else
+# include <semaphore.h>
 #endif
 
 
+/* current threadobject *******************************************************/
+
 #if defined(HAVE___THREAD)
 
 #define THREADSPECIFIC    __thread
-#define THREADOBJECT      threadobj
-#define THREADINFO        (&threadobj->info)
+#define THREADOBJECT      threads_current_threadobject
 
-extern __thread threadobject *threadobj;
+extern __thread threadobject *threads_current_threadobject;
 
 #else /* defined(HAVE___THREAD) */
 
 #define THREADSPECIFIC
-#define THREADOBJECT      pthread_getspecific(tkey_threadinfo)
-#define THREADINFO        (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
+#define THREADOBJECT \
+       ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
 
-extern pthread_key_t tkey_threadinfo;
+extern pthread_key_t threads_current_threadobject_key;
 
 #endif /* defined(HAVE___THREAD) */
 
 
-/* typedefs *******************************************************************/
+/* threadobject ****************************************************************
 
-typedef struct ExecEnvironment ExecEnvironment;
-typedef struct nativethread nativethread;
-typedef struct threadobject threadobject;
-typedef struct monitorLockRecord monitorLockRecord;
-typedef struct lockRecordPoolHeader lockRecordPoolHeader;
-typedef struct lockRecordPool lockRecordPool;
-typedef java_lang_Thread thread;
+   Struct holding thread local variables.
 
+*******************************************************************************/
 
-/* ExecEnvironment *************************************************************
+#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    */
 
-   Monitor lock implementation
+#define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
+#define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
 
-*******************************************************************************/
 
-struct ExecEnvironment {
-       monitorLockRecord *firstLR;
-       lockRecordPool    *lrpool;
-       int                numlr;
-};
+struct threadobject {
+       java_lang_Thread     *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                         */
 
-struct nativethread {
-       threadobject      *next;
-       threadobject      *prev;
-       java_objectheader *_exceptionptr;
-       void              *_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;
+       mach_port_t           mach_thread;       /* Darwin thread id              */
 #endif
-       pthread_mutex_t    joinMutex;
-       pthread_cond_t     joinCond;
-};
 
+       /* these are used for the wait/notify implementation                      */
+       pthread_mutex_t       waitmutex;
+       pthread_cond_t        waitcond;
 
-/* threadobject ****************************************************************
+       pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
+       pthread_cond_t        suspendcond;  /* notify to resume this thread       */
 
-   Every java.lang.VMThread object is actually an instance of this
-   structure.
+       bool                  interrupted;
+       bool                  signaled;
+       bool                  sleeping;
 
-*******************************************************************************/
+       bool                  suspended;    /* is this thread suspended?          */
+       s4                    suspend_reason; /* reason for suspending            */
 
-struct threadobject {
-       java_lang_VMThread  o;
-       nativethread        info;           /* some general pthreads stuff        */
-       ExecEnvironment     ee;             /* contains our lock record pool      */
+       u1                   *pc;           /* current PC (used for profiling)    */
 
-       /* these are used for the wait/notify implementation                      */
-       pthread_mutex_t     waitLock;
-       pthread_cond_t      waitCond;
-       bool                interrupted;
-       bool                signaled;
-       bool                isSleeping;
+       java_object_t        *_exceptionptr;     /* current exception             */
+       stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
+       localref_table       *_localref_table;   /* JNI local references          */
 
-       dumpinfo            dumpinfo;       /* dump memory info structure         */
-};
+#if defined(ENABLE_INTRP)
+       Cell                 *_global_sp;        /* stack pointer for interpreter */
+#endif
 
-/* monitorLockRecord ***********************************************************
+#if defined(ENABLE_GC_CACAO)
+       bool                  gc_critical;  /* indicates a critical section       */
 
-   This is the really interesting stuff.
-   See handbook for a detailed description.
+       sourcestate_t        *ss;
+       executionstate_t     *es;
+#endif
 
-*******************************************************************************/
+       dumpinfo_t            dumpinfo;     /* dump memory info structure         */
 
-struct monitorLockRecord {
-       threadobject      *ownerThread;
-       java_objectheader *o;
-       s4                 lockCount;
-       monitorLockRecord *nextFree;
-       s4                 queuers;
-       monitorLockRecord *waiter;
-       monitorLockRecord *incharge;
-       java_objectheader *waiting;
-       sem_t              queueSem;
-       pthread_mutex_t    resolveLock;
-       pthread_cond_t     resolveWait;
+#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                       */
 };
 
 
-struct lockRecordPoolHeader {
-       lockRecordPool *next;
-       int             size;
-}; 
+/* exception pointer **********************************************************/
 
-struct lockRecordPool {
-       lockRecordPoolHeader header;
-       monitorLockRecord    lr[1];
-};
+#define exceptionptr      (&(THREADOBJECT->_exceptionptr))
+
+
+/* stackframeinfo *************************************************************/
+
+#define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
 
 
-monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
-bool monitorExit(threadobject *, java_objectheader *);
+/* native-world flags *********************************************************/
 
-bool threadHoldsLock(threadobject *t, java_objectheader *o);
-void signal_cond_for_object(java_objectheader *obj);
-void broadcast_cond_for_object(java_objectheader *obj);
-void wait_cond_for_object(java_objectheader *obj, s8 time, s4 nanos);
+#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
 
-void *thread_getself(void);
 
-void threads_preinit(void);
-bool threads_init(u1 *stackbottom);
+/* counter for verbose call filter ********************************************/
 
-void initObjectLock(java_objectheader *);
-monitorLockRecord *get_dummyLR(void);
-void initLocks();
-void initThread(java_lang_VMThread *);
+#if defined(ENABLE_DEBUG_FILTER)
+#      define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
+#endif
 
-/* start a thread */
-void threads_start_thread(thread *t, functionptr function);
+/* state for trace java call **************************************************/
 
-void joinAllThreads();
+#if !defined(NDEBUG)
+#      define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
+#      define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
+#endif
 
-void sleepThread(s8 millis, s4 nanos);
-void yieldThread();
+/* functions ******************************************************************/
 
-void setPriorityThread(thread *t, s4 priority);
+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 interruptThread(java_lang_VMThread *);
-bool interruptedThread();
-bool isInterruptedThread(java_lang_VMThread *);
+threadobject *threads_get_current_threadobject(void);
 
-/* This must not be changed, it is used in asm_criticalsections */
-typedef struct {
-       u1 *mcodebegin;
-       u1 *mcodeend;
-       u1 *mcoderestart;
-} threadcritnode;
+bool threads_init(void);
 
-void thread_registercritical(threadcritnode *);
-u1 *thread_checkcritical(u1*);
+void threads_start_thread(threadobject *thread, functionptr function);
 
-extern volatile int stopworldwhere;
-extern threadobject *mainthreadobj;
+void threads_set_thread_priority(pthread_t tid, int priority);
 
-extern pthread_mutex_t pool_lock;
-extern lockRecordPool *global_pool;
+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 cast_stopworld();
-void cast_startworld();
+void threads_join_all_threads(void);
 
-/* dumps all threads */
-void threads_dump(void);
+void threads_sleep(s8 millis, s4 nanos);
 
-/* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
-void thread_restartcriticalsection(ucontext_t *);
+bool 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 */
 
@@ -250,4 +253,5 @@ void thread_restartcriticalsection(ucontext_t *);
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */