* Removed all Id tags.
[cacao.git] / src / threads / native / threads.h
index 7a7f592d6e0e82771f4618ddabd5ac0c7076c0a8..417b273d1849ff2236f6a5fe6b4dc10d7cfc5d9c 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
-                       Edwin Steiner
-
-   $Id: threads.h 4908 2006-05-12 16:49:50Z 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/localref.h"
 #include "native/include/java_lang_Thread.h"
-#include "native/include/java_lang_VMThread.h"
-#include "vm/global.h"
 
 #include "threads/native/lock.h"
 
+#include "vm/global.h"
+
+#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>
+
+typedef struct {
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       int value;
+} sem_t;
 
-/* We need to emulate recursive mutexes. */
-#define MUTEXSIM
+#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 ****************************************************************
+
+   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                      */
+
+
+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                         */
 
-typedef struct nativethread nativethread;
-typedef struct threadobject threadobject;
-typedef java_lang_Thread thread;
-
-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;
+       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 ****************************************************************
+       bool                  interrupted;
+       bool                  signaled;
+       bool                  sleeping;
 
-   Every java.lang.VMThread object is actually an instance of this
-   structure.
+       u1                   *pc;           /* current PC (used for profiling)    */
 
-*******************************************************************************/
+       java_object_t        *_exceptionptr;     /* current exception             */
+       stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
+       localref_table       *_localref_table;   /* JNI local references          */
 
-struct threadobject {
-       java_lang_VMThread  o;
-       nativethread        info;           /* some general pthreads stuff        */
-       lock_execution_env_t     ee;             /* contains our lock record pool      */
+#if defined(ENABLE_INTRP)
+       Cell                 *_global_sp;        /* stack pointer for interpreter */
+#endif
 
-       /* these are used for the wait/notify implementation                      */
-       pthread_mutex_t     waitLock;
-       pthread_cond_t      waitCond;
-       bool                interrupted;
-       bool                signaled;
-       bool                isSleeping;
+       dumpinfo_t            dumpinfo;     /* dump memory info structure         */
 
-       dumpinfo            dumpinfo;       /* dump memory info structure         */
-};
+#if defined(ENABLE_DEBUG_FILTER)
+       u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
+#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);
+#if !defined(NDEBUG)
+       s4                    tracejavacallindent;
+       u4                    tracejavacallcount;
+#endif
 
-void *thread_getself(void);
+       listnode_t            linkage;      /* threads-list                       */
+};
 
-void threads_preinit(void);
-bool threads_init(u1 *stackbottom);
 
-void lock_init();
-void initThread(java_lang_VMThread *);
+/* exception pointer **********************************************************/
 
-/* start a thread */
-void threads_start_thread(thread *t, functionptr function);
+#define exceptionptr      (&(THREADOBJECT->_exceptionptr))
 
-void joinAllThreads();
 
-void thread_sleep(s8 millis, s4 nanos);
-void yieldThread();
+/* stackframeinfo *************************************************************/
 
-bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
+#define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
 
-void setPriorityThread(thread *t, s4 priority);
+/* counter for verbose call filter ********************************************/
 
-void interruptThread(java_lang_VMThread *);
-bool interruptedThread();
-bool isInterruptedThread(java_lang_VMThread *);
+#if defined(ENABLE_DEBUG_FILTER)
+#      define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
+#endif
 
-#if defined(ENABLE_JVMTI)
-void setthreadobject(threadobject *thread);
+/* state for trace java call **************************************************/
+
+#if !defined(NDEBUG)
+#      define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
+#      define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
 #endif
 
-extern threadobject *mainthreadobj;
+/* functions ******************************************************************/
 
-void cast_stopworld();
-void cast_startworld();
+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);
 
-/* dumps all threads */
-void threads_dump(void);
+threadobject *threads_get_current_threadobject(void);
 
+bool threads_init(void);
 
-/******************************************************************************/
-/* Recursive Mutex Implementation for Darwin                                  */
-/******************************************************************************/
+void threads_start_thread(threadobject *thread, functionptr function);
 
-#if defined(MUTEXSIM)
+void threads_set_thread_priority(pthread_t tid, int priority);
 
-/* We need this for older MacOSX (10.1.x) */
+bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
+bool threads_detach_thread(threadobject *thread);
 
-typedef struct {
-       pthread_mutex_t mutex;
-       pthread_t owner;
-       int count;
-} pthread_mutex_rec_t;
+void threads_join_all_threads(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_sleep(s8 millis, s4 nanos);
 
-#else /* !defined(MUTEXSIM) */
+bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
 
-#define pthread_mutex_lock_rec pthread_mutex_lock
-#define pthread_mutex_unlock_rec pthread_mutex_unlock
-#define pthread_mutex_rec_t pthread_mutex_t
+void threads_thread_interrupt(threadobject *thread);
+bool threads_check_if_interrupted_and_reset(void);
+bool threads_thread_has_been_interrupted(threadobject *thread);
 
-#endif /* defined(MUTEXSIM) */
+void threads_cast_stopworld(void);
+void threads_cast_startworld(void);
 
 #endif /* _THREADS_H */