* Removed all Id tags.
[cacao.git] / src / threads / native / threads.h
index 0f63e6e5100707690d62c77fc99b23b2bad50a64..417b273d1849ff2236f6a5fe6b4dc10d7cfc5d9c 100644 (file)
@@ -1,9 +1,9 @@
-/* threads/native/threads.h - native threads header
+/* src/threads/native/threads.h - native threads header
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
-   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
-   P. Tomsich, J. Wenninger
+   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
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+*/
 
-   Authors: Stefan Ring
 
-   $Id: threads.h 1621 2004-11-30 13:06:55Z twisti $
+#ifndef _THREADS_H
+#define _THREADS_H
+
+/* forward typedefs ***********************************************************/
+
+typedef struct threadobject threadobject;
 
-*/
 
+#include "config.h"
 
-#ifndef _NATIVETHREAD_H
-#define _NATIVETHREAD_H
+#include <pthread.h>
+#include <ucontext.h>
 
-#include <semaphore.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"
 
-#if defined(__DARWIN__)
-#include <mach/mach.h>
+#include "threads/native/lock.h"
+
+#include "vm/global.h"
+
+#include "vm/jit/stacktrace.h"
 
-/* We need to emulate recursive mutexes. */
-#define MUTEXSIM
+#if defined(ENABLE_INTRP)
+#include "vm/jit/intrp/intrp.h"
 #endif
 
+#if defined(__DARWIN__)
+# include <mach/mach.h>
 
 typedef struct {
-       s4 super_baseval;
-       s4 super_diffval;
-       s4 sub_baseval;
-} castinfo;
-
-struct _threadobject;
-
-
-typedef struct monitorLockRecord monitorLockRecord;
-
-struct monitorLockRecord {
-       struct _threadobject *ownerThread;
-       java_objectheader *o;
-       int                lockCount;
-       monitorLockRecord *nextFree;
-       int                queuers;
-       monitorLockRecord *waiter;
-       monitorLockRecord *incharge;
-       bool               waiting;
-       sem_t              queueSem;
-       pthread_mutex_t    resolveLock;
-       pthread_cond_t     resolveWait;
-};
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       int value;
+} sem_t;
 
+#else
+# include <semaphore.h>
+#endif
 
-struct _lockRecordPool;
 
-typedef struct {
-       struct _lockRecordPool *next;
-       int size;
-} lockRecordPoolHeader; 
+/* current threadobject *******************************************************/
 
-typedef struct _lockRecordPool {
-       lockRecordPoolHeader header;
-       monitorLockRecord lr[1];
-} lockRecordPool;
+#if defined(HAVE___THREAD)
 
-/* Monitor lock implementation */
-typedef struct {
-       monitorLockRecord *firstLR;
-       lockRecordPool *lrpool;
-       int numlr;
-} ExecEnvironment;
+#define THREADSPECIFIC    __thread
+#define THREADOBJECT      threads_current_threadobject
 
-typedef struct {
-       struct _threadobject *next, *prev;
-       java_objectheader *_exceptionptr;
-       methodinfo *_threadrootmethod;
-       void *_stackframeinfo;
-       pthread_t tid;
-#if defined(__DARWIN__)
-       mach_port_t mach_thread;
-#endif
-       pthread_mutex_t joinMutex;
-       pthread_cond_t joinCond;
-} nativethread;
+extern __thread threadobject *threads_current_threadobject;
+
+#else /* defined(HAVE___THREAD) */
+
+#define THREADSPECIFIC
+#define THREADOBJECT \
+       ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
+
+extern pthread_key_t threads_current_threadobject_key;
 
-typedef java_lang_Thread thread;
+#endif /* defined(HAVE___THREAD) */
 
 
 /* threadobject ****************************************************************
 
-   TODO
+   Struct holding thread local variables.
 
 *******************************************************************************/
 
-typedef struct _threadobject {
-       java_lang_VMThread  o;
-       nativethread        info;
-       ExecEnvironment     ee;
+#define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
+#define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
+#define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
 
-       pthread_mutex_t     waitLock;
-       pthread_cond_t      waitCond;
-       bool                interrupted;
-       bool                signaled;
-       bool                isSleeping;
 
-       dumpinfo            dumpinfo;       /* dump memory info structure         */
-} threadobject;
+struct threadobject {
+       java_lang_Thread     *object;       /* link to java.lang.Thread object    */
 
+       ptrint                thinlock;     /* pre-computed thin lock value       */
 
-monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
-bool monitorExit(threadobject *, java_objectheader *);
+       s4                    index;        /* thread index, starting with 1      */
+       u4                    flags;        /* flag field                         */
+       u4                    state;        /* state field                        */
 
-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);
+       pthread_t             tid;          /* pthread id                         */
 
-void initThreadsEarly();
-void initThreads(u1 *stackbottom);
-void initObjectLock(java_objectheader *);
-void initLocks();
-void initThread(java_lang_VMThread *);
-void startThread(thread *t);
-void joinAllThreads();
+#if defined(__DARWIN__)
+       mach_port_t           mach_thread;       /* Darwin thread id              */
+#endif
 
-void sleepThread(s8 millis, s4 nanos);
-void yieldThread();
+       /* these are used for the wait/notify implementation                      */
+       pthread_mutex_t       waitmutex;
+       pthread_cond_t        waitcond;
 
-void setPriorityThread(thread *t, s4 priority);
+       bool                  interrupted;
+       bool                  signaled;
+       bool                  sleeping;
 
-void interruptThread(java_lang_VMThread *);
-bool interruptedThread();
-bool isInterruptedThread(java_lang_VMThread *);
+       u1                   *pc;           /* current PC (used for profiling)    */
 
-#if !defined(HAVE___THREAD)
-extern pthread_key_t tkey_threadinfo;
-#define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
-#define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
-#else
-extern __thread threadobject *threadobj;
-#define THREADOBJECT ((java_lang_VMThread*) threadobj)
-#define THREADINFO (&threadobj->info)
+       java_object_t        *_exceptionptr;     /* current exception             */
+       stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
+       localref_table       *_localref_table;   /* JNI local references          */
+
+#if defined(ENABLE_INTRP)
+       Cell                 *_global_sp;        /* stack pointer for interpreter */
 #endif
 
-/*#include "builtin.h"*/
+       dumpinfo_t            dumpinfo;     /* dump memory info structure         */
 
-/* This must not be changed, it is used in asm_criticalsections */
-typedef struct {
-       u1 *mcodebegin, *mcodeend, *mcoderestart;
-} threadcritnode;
+#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                       */
+};
+
+
+/* exception pointer **********************************************************/
+
+#define exceptionptr      (&(THREADOBJECT->_exceptionptr))
+
+
+/* stackframeinfo *************************************************************/
+
+#define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
+
+/* counter for verbose call filter ********************************************/
+
+#if defined(ENABLE_DEBUG_FILTER)
+#      define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
+#endif
+
+/* state for trace java call **************************************************/
+
+#if !defined(NDEBUG)
+#      define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
+#      define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
+#endif
+
+/* 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 threads_start_thread(threadobject *thread, functionptr function);
+
+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);
+
+void threads_join_all_threads(void);
+
+void threads_sleep(s8 millis, s4 nanos);
 
-void thread_registercritical(threadcritnode *);
-u1 *thread_checkcritical(u1*);
+bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
 
-extern volatile int stopworldwhere;
+void threads_thread_interrupt(threadobject *thread);
+bool threads_check_if_interrupted_and_reset(void);
+bool threads_thread_has_been_interrupted(threadobject *thread);
 
-void cast_stopworld();
-void cast_startworld();
+void threads_cast_stopworld(void);
+void threads_cast_startworld(void);
 
-#endif /* _NATIVETHREAD_H */
+#endif /* _THREADS_H */
 
 
 /*
@@ -196,4 +214,5 @@ void cast_startworld();
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */