major rework of jvmti. now we have three processes in jvmti mode. there are still...
[cacao.git] / src / threads / native / threads.h
index 0f63e6e5100707690d62c77fc99b23b2bad50a64..4f1afcbb7f93944092ad7168af9d66abc0dfe26a 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 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
+   Contact: cacao@cacaojvm.org
 
    Authors: Stefan Ring
 
-   $Id: threads.h 1621 2004-11-30 13:06:55Z twisti $
+   Changes: Christian Thalinger
+
+   $Id: threads.h 4661 2006-03-21 00:04:59Z motse $
 
 */
 
 
-#ifndef _NATIVETHREAD_H
-#define _NATIVETHREAD_H
+#ifndef _THREADS_H
+#define _THREADS_H
+
+#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"
 
 #if defined(__DARWIN__)
 #include <mach/mach.h>
 #endif
 
 
-typedef struct {
-       s4 super_baseval;
-       s4 super_diffval;
-       s4 sub_baseval;
-} castinfo;
+#if defined(HAVE___THREAD)
 
-struct _threadobject;
+#define THREADSPECIFIC    __thread
+#define THREADOBJECT      threadobj
+#define THREADINFO        (&threadobj->info)
 
+extern __thread threadobject *threadobj;
 
-typedef struct monitorLockRecord monitorLockRecord;
+#else /* defined(HAVE___THREAD) */
 
-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;
-};
+#define THREADSPECIFIC
+#define THREADOBJECT      pthread_getspecific(tkey_threadinfo)
+#define THREADINFO        (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
 
+extern pthread_key_t tkey_threadinfo;
 
-struct _lockRecordPool;
+#endif /* defined(HAVE___THREAD) */
 
-typedef struct {
-       struct _lockRecordPool *next;
-       int size;
-} lockRecordPoolHeader; 
 
-typedef struct _lockRecordPool {
-       lockRecordPoolHeader header;
-       monitorLockRecord lr[1];
-} lockRecordPool;
+/* typedefs *******************************************************************/
 
-/* Monitor lock implementation */
-typedef struct {
+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;
+
+
+/* ExecEnvironment *************************************************************
+
+   Monitor lock implementation
+
+*******************************************************************************/
+
+struct ExecEnvironment {
        monitorLockRecord *firstLR;
-       lockRecordPool *lrpool;
-       int numlr;
-} ExecEnvironment;
+       lockRecordPool    *lrpool;
+       int                numlr;
+};
 
-typedef struct {
-       struct _threadobject *next, *prev;
+
+struct nativethread {
+       threadobject      *next;
+       threadobject      *prev;
        java_objectheader *_exceptionptr;
-       methodinfo *_threadrootmethod;
-       void *_stackframeinfo;
-       pthread_t tid;
+       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;
 #endif
-       pthread_mutex_t joinMutex;
-       pthread_cond_t joinCond;
-} nativethread;
-
-typedef java_lang_Thread thread;
+       pthread_mutex_t    joinMutex;
+       pthread_cond_t     joinCond;
+};
 
 
 /* threadobject ****************************************************************
 
-   TODO
+   Every java.lang.VMThread object is actually an instance of this
+   structure.
 
 *******************************************************************************/
 
-typedef struct _threadobject {
+struct threadobject {
        java_lang_VMThread  o;
-       nativethread        info;
-       ExecEnvironment     ee;
+       nativethread        info;           /* some general pthreads stuff        */
+       ExecEnvironment     ee;             /* contains our lock record pool      */
 
+       /* these are used for the wait/notify implementation                      */
        pthread_mutex_t     waitLock;
        pthread_cond_t      waitCond;
        bool                interrupted;
@@ -129,26 +143,66 @@ typedef struct _threadobject {
        bool                isSleeping;
 
        dumpinfo            dumpinfo;       /* dump memory info structure         */
-} threadobject;
+};
+
+/* monitorLockRecord ***********************************************************
+
+   This is the really interesting stuff.
+   See handbook for a detailed description.
+
+*******************************************************************************/
+
+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;
+};
+
+
+struct lockRecordPoolHeader {
+       lockRecordPool *next;
+       int             size;
+}; 
+
+struct lockRecordPool {
+       lockRecordPoolHeader header;
+       monitorLockRecord    lr[1];
+};
 
 
 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
 bool monitorExit(threadobject *, java_objectheader *);
 
 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);
 
-void initThreadsEarly();
-void initThreads(u1 *stackbottom);
+void wait_cond_for_object(java_objectheader *o, s8 millis, s4 nanos);
+void signal_cond_for_object(java_objectheader *o);
+void broadcast_cond_for_object(java_objectheader *o);
+
+void *thread_getself(void);
+
+void threads_preinit(void);
+bool threads_init(u1 *stackbottom);
+
 void initObjectLock(java_objectheader *);
+monitorLockRecord *get_dummyLR(void);
 void initLocks();
 void initThread(java_lang_VMThread *);
-void startThread(thread *t);
+
+/* start a thread */
+void threads_start_thread(thread *t, functionptr function);
+
 void joinAllThreads();
 
-void sleepThread(s8 millis, s4 nanos);
+void thread_sleep(s8 millis, s4 nanos);
 void yieldThread();
 
 void setPriorityThread(thread *t, s4 priority);
@@ -157,32 +211,38 @@ void interruptThread(java_lang_VMThread *);
 bool interruptedThread();
 bool isInterruptedThread(java_lang_VMThread *);
 
-#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)
+#if defined(ENABLE_JVMTI)
+void setthreadobject(threadobject *thread);
 #endif
 
-/*#include "builtin.h"*/
 
 /* This must not be changed, it is used in asm_criticalsections */
 typedef struct {
-       u1 *mcodebegin, *mcodeend, *mcoderestart;
+       u1 *mcodebegin;
+       u1 *mcodeend;
+       u1 *mcoderestart;
 } threadcritnode;
 
 void thread_registercritical(threadcritnode *);
 u1 *thread_checkcritical(u1*);
 
 extern volatile int stopworldwhere;
+extern threadobject *mainthreadobj;
+
+extern pthread_mutex_t pool_lock;
+extern lockRecordPool *global_pool;
+
 
 void cast_stopworld();
 void cast_startworld();
 
-#endif /* _NATIVETHREAD_H */
+/* dumps all threads */
+void threads_dump(void);
+
+/* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
+void thread_restartcriticalsection(ucontext_t *);
+
+#endif /* _THREADS_H */
 
 
 /*