* Merged with tip.
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Wed, 2 Apr 2008 08:24:24 +0000 (10:24 +0200)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Wed, 2 Apr 2008 08:24:24 +0000 (10:24 +0200)
16 files changed:
src/cacao/cacao.c
src/mm/Makefile.am
src/native/jvmti/cacaodbg.c
src/native/jvmti/cacaodbg.h
src/native/jvmti/jvmti.c
src/threads/Makefile.am
src/threads/mutex.h [new file with mode: 0644]
src/threads/posix/Makefile.am
src/threads/posix/generic-primitives.h
src/threads/posix/lock.c
src/threads/posix/lock.h
src/threads/posix/mutex-posix.h [new file with mode: 0644]
src/threads/posix/thread-posix.c
src/threads/posix/thread-posix.h
src/threads/posix/threadlist-posix.c
src/vm/vm.c

index 8ab94aaedf97396bfe5a04fd18b4478cd337ebf5..2e0de464189fd8e6e784c3733d922f6fd8df7b87 100644 (file)
@@ -49,6 +49,7 @@
 #if defined(ENABLE_JVMTI)
 # include "native/jvmti/jvmti.h"
 # include "native/jvmti/cacaodbg.h"
+# include "threads/mutex.h"
 #endif
 
 #include "vm/vm.h"
@@ -162,7 +163,7 @@ int main(int argc, char **argv)
        (void) vm_createjvm(&vm, (void *) &env, vm_args);
 
 #if defined(ENABLE_JVMTI)
-       pthread_mutex_init(&dbgcomlock,NULL);
+       mutex_init(&dbgcomlock);
        if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
 #endif
 
index 4d4fbb2eeb7825ad5a59cfeed99301cf0b0425bd..22c39cec2ed77c989a22004cbf4b07976c250b7f 100644 (file)
@@ -1,9 +1,7 @@
 ## src/mm/Makefile.am
 ##
-## 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
+## Copyright (C) 1996-2005, 2006, 2007, 2008
+## CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 ##
 ## This file is part of CACAO.
 ##
index cb319e78cd5d2d6634e063e781eae15a81ce82f1..8db874a8fb8d8102250be0fe2d80aeb16f0ffbf1 100644 (file)
@@ -33,6 +33,7 @@
 #include "vm/jit/asmpart.h"
 #include "vm/stringlocal.h"
 #include "toolbox/logging.h"
+#include "threads/mutex.h"
 #include "threads/thread.h"
 
 #include <sys/types.h>
@@ -133,7 +134,7 @@ static void breakpointtable_creator() {
 void jvmti_set_system_breakpoint(int sysbrk, bool mode) {      
        struct brkpts *jvmtibrkpt;
 
-       pthread_mutex_lock(&dbgcomlock);
+       mutex_lock(&dbgcomlock);
        jvmtibrkpt = &dbgcom->jvmtibrkpt;
 
        assert (sysbrk < BEGINUSERBRK); 
@@ -144,7 +145,7 @@ void jvmti_set_system_breakpoint(int sysbrk, bool mode) {
                /* add breakpoint*/
                if (jvmtibrkpt->brk[sysbrk].count > 0) {
                        jvmtibrkpt->brk[sysbrk].count++;
-                       pthread_mutex_unlock(&dbgcomlock);
+                       mutex_unlock(&dbgcomlock);
                        return;
                }
                dbgcom->addbrkpt = true;
@@ -159,11 +160,11 @@ void jvmti_set_system_breakpoint(int sysbrk, bool mode) {
                } else {
                        /* avoid negative counter values */
                        if (jvmtibrkpt->brk[sysbrk].count > 0) jvmtibrkpt->brk[sysbrk].count--;
-                       pthread_mutex_unlock(&dbgcomlock);
+                       mutex_unlock(&dbgcomlock);
                        return;
                }
        }
-       pthread_mutex_unlock(&dbgcomlock);
+       mutex_unlock(&dbgcomlock);
        /* call cacaodbgserver */
        __asm__ ("setsysbrkpt:");
        TRAP; 
@@ -179,7 +180,7 @@ void jvmti_set_system_breakpoint(int sysbrk, bool mode) {
 void jvmti_add_breakpoint(void* addr, jmethodID method, jlocation location) {
        struct brkpts *jvmtibrkpt;
 
-       pthread_mutex_lock(&dbgcomlock);
+       mutex_lock(&dbgcomlock);
        jvmtibrkpt = &dbgcom->jvmtibrkpt;;
 
        if (jvmtibrkpt->size == jvmtibrkpt->num)
@@ -194,7 +195,7 @@ void jvmti_add_breakpoint(void* addr, jmethodID method, jlocation location) {
        /* todo: set breakpoint */
 /*     jvmtibrkpt.brk[jvmtibrkpt.num].orig = */
        jvmtibrkpt->num++;
-       pthread_mutex_unlock(&dbgcomlock);
+       mutex_unlock(&dbgcomlock);
 
        fprintf (stderr,"add brk done\n");
 }
@@ -208,7 +209,7 @@ void jvmti_add_breakpoint(void* addr, jmethodID method, jlocation location) {
 
 *******************************************************************************/
 void jvmti_cacaodbgserver_quit(){
-       pthread_mutex_lock(&dbgcomlock);
+       mutex_lock(&dbgcomlock);
        dbgcom->running--;
        if (dbgcom->running  == 0) {
                __asm__ ("cacaodbgserver_quit:");
@@ -217,7 +218,7 @@ void jvmti_cacaodbgserver_quit(){
                wait(NULL);
                dbgcom = NULL;
        }
-       pthread_mutex_unlock(&dbgcomlock);
+       mutex_unlock(&dbgcomlock);
 }
 
 
@@ -272,7 +273,7 @@ void jvmti_cacao_debug_init() {
        pid_t dbgserver;        
 
        /* start new cacaodbgserver if needed*/
-       pthread_mutex_lock(&dbgcomlock);
+       mutex_lock(&dbgcomlock);
        if (dbgcom == NULL) {
                dbgcom = heap_allocate(sizeof(cacaodbgcommunication),true,NULL);                
                dbgcom->running = 1;
@@ -297,12 +298,12 @@ void jvmti_cacao_debug_init() {
                                }
                        }
                }
-               pthread_mutex_unlock(&dbgcomlock);
+               mutex_unlock(&dbgcomlock);
                /* let cacaodbgserver get ready */
                sleep(1);
        } else {
                dbgcom->running++;
-               pthread_mutex_unlock(&dbgcomlock);
+               mutex_unlock(&dbgcomlock);
        }
 }
 
index 3d6bf6138c313b1ed91d6b4be65accd70ca59903..4b4a219eb7025cd7e2b4dc57d8f111821369e188 100644 (file)
@@ -25,6 +25,7 @@
 #ifndef _CACAODBG_H
 #define _CACAODBG_H
 
+#include "threads/mutex.h"
 #include "threads/thread.h"
 #include "native/jvmti/jvmti.h"
 #include "native/include/java_lang_String.h"
@@ -112,7 +113,7 @@ cacaodbgcommunication *dbgcom;
 
 bool jvmti;                 /* jvmti agent  */
 
-extern pthread_mutex_t dbgcomlock;
+extern mutex_t dbgcomlock;
 
 jvmtiEnv* jvmti_new_environment();
 void jvmti_set_phase(jvmtiPhase p);
index 9573aa7aadd14781666d49e1aed0eda9f5ec4b13..b80dae48d18ee8f3658d0990544d205cc2ddde94 100644 (file)
@@ -52,6 +52,7 @@
 #include "vm/options.h"
 #include "vm/stringlocal.h"
 #include "mm/memory.h"
+#include "threads/mutex.h"
 #include "threads/thread.h"
 #include "threads/lock-common.h"
 #include "vm/exceptions.h"
@@ -76,7 +77,7 @@
 
 typedef struct _environment environment;
 static environment *envs=NULL;
-pthread_mutex_t dbgcomlock;
+mutex_t dbgcomlock;
 
 extern const struct JNIInvokeInterface _Jv_JNIInvokeInterface;
 
@@ -734,7 +735,7 @@ GetOwnedMonitorInfo (jvmtiEnv * env, jthread thread,
 
        om=MNEW(java_objectheader*,size);
 
-       pthread_mutex_lock(&lock_global_pool_lock);
+       mutex_lock(&lock_global_pool_lock);
        lrp=lock_global_pool;
 
        /* iterate over all lock record pools */
@@ -755,7 +756,7 @@ GetOwnedMonitorInfo (jvmtiEnv * env, jthread thread,
                lrp=lrp->header.next;
        }
 
-       pthread_mutex_unlock(&lock_global_pool_lock);
+       mutex_unlock(&lock_global_pool_lock);
 
        *owned_monitors_ptr     = 
                heap_allocate(sizeof(java_objectheader*) * i, true, NULL);
@@ -804,7 +805,7 @@ GetCurrentContendedMonitor (jvmtiEnv * env, jthread thread,
 
 #if defined(ENABLE_THREADS)
 
-       pthread_mutex_lock(&lock_global_pool_lock);
+       mutex_lock(&lock_global_pool_lock);
 
        lrp=lock_global_pool;
 
@@ -825,7 +826,7 @@ GetCurrentContendedMonitor (jvmtiEnv * env, jthread thread,
                lrp=lrp->header.next;
        }
 
-       pthread_mutex_unlock(&lock_global_pool_lock);
+       mutex_unlock(&lock_global_pool_lock);
 
 
 #endif
index 844235966110061902f6d272695685714c9cd173..a592f72de6526ea97807698e39a06d3df1d1bf18 100644 (file)
@@ -42,6 +42,7 @@ libthreads_la_SOURCES = \
        critical.c \
        critical.h \
        lock-common.h \
+       mutex.h \
        threadlist.c \
        threadlist.h \
        thread.c \
diff --git a/src/threads/mutex.h b/src/threads/mutex.h
new file mode 100644 (file)
index 0000000..7458b0d
--- /dev/null
@@ -0,0 +1,51 @@
+/* src/threads/mutex.h - machine independent mutual exclusion functions
+
+   Copyright (C) 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+
+   This file is part of CACAO.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   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., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+*/
+
+
+#ifndef _MUTEX_H
+#define _MUTEX_H
+
+#include "config.h"
+
+#if defined(ENABLE_THREADS)
+# include "threads/posix/mutex-posix.h"
+#endif
+
+
+#endif /* _MUTEX_H */
+
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ * vim:noexpandtab:sw=4:ts=4:
+ */
index 1ea176c9a1780b54e325f37a92df34d4d1535898..e9fd1b7d1713c0858e842a660ec91a0d58fcdb34 100644 (file)
@@ -31,6 +31,7 @@ noinst_LTLIBRARIES = \
 libposix_la_SOURCES = \
        lock.c \
        lock.h \
+       mutex-posix.h \
        threadlist-posix.c \
        thread-posix.c \
        thread-posix.h
@@ -42,4 +43,4 @@ libposix_la_SOURCES = \
 ## c-basic-offset: 4
 ## tab-width: 8
 ## compile-command: "automake --add-missing"
-## End:
\ No newline at end of file
+## End:
index 5541a447b2aea26af07745c35b8f5a213ba42d78..2ab03a155d37a00313bd25dbb7a357b7296ffedd 100644 (file)
 
 #include <pthread.h>
 
-extern pthread_mutex_t _cas_lock;
-extern pthread_mutex_t _mb_lock;
+#include "threads/mutex.h"
+
+
+extern mutex_t _cas_lock;
+extern mutex_t _mb_lock;
 
 
 static inline long compare_and_swap(volatile long *p, long oldval, long newval)
 {
   long ret;
 
-  pthread_mutex_lock(&_cas_lock);
+  mutex_lock(&_cas_lock);
 
   /* do the compare-and-swap */
 
@@ -56,14 +59,14 @@ static inline long compare_and_swap(volatile long *p, long oldval, long newval)
   if (oldval == ret)
     *p = newval;
 
-  pthread_mutex_unlock(&_cas_lock);
+  mutex_unlock(&_cas_lock);
 
   return ret;
 }
 
 
-#define MEMORY_BARRIER()                  (pthread_mutex_lock(&_mb_lock), \
-                                           pthread_mutex_unlock(&_mb_lock))
+#define MEMORY_BARRIER()                  (mutex_lock(&_mb_lock), \
+                                           mutex_unlock(&_mb_lock))
 #define STORE_ORDER_BARRIER()             MEMORY_BARRIER()
 #define MEMORY_BARRIER_AFTER_ATOMIC()     /* nothing */
 
index 9dfd4571219445fc463051661755a6e3a4196912..f237127a919b7547eaca5097b99be3c62df87669 100644 (file)
@@ -39,6 +39,7 @@
 #include "native/llni.h"
 
 #include "threads/lock-common.h"
+#include "threads/mutex.h"
 #include "threads/threadlist.h"
 #include "threads/thread.h"
 
@@ -246,7 +247,6 @@ ptrint lock_pre_compute_thinlock(s4 index)
 
 static lock_record_t *lock_record_new(void)
 {
-       int result;
        lock_record_t *lr;
 
        /* allocate the data structure on the C heap */
@@ -273,9 +273,7 @@ static lock_record_t *lock_record_new(void)
 
        /* initialize the mutex */
 
-       result = pthread_mutex_init(&(lr->mutex), NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "lock_record_new: pthread_mutex_init failed");
+       mutex_init(&(lr->mutex));
 
        DEBUGLOCKS(("[lock_record_new   : lr=%p]", (void *) lr));
 
@@ -294,15 +292,11 @@ static lock_record_t *lock_record_new(void)
 
 static void lock_record_free(lock_record_t *lr)
 {
-       int result;
-
        DEBUGLOCKS(("[lock_record_free  : lr=%p]", (void *) lr));
 
        /* Destroy the mutex. */
 
-       result = pthread_mutex_destroy(&(lr->mutex));
-       if (result != 0)
-               vm_abort_errnum(result, "lock_record_free: pthread_mutex_destroy failed");
+       mutex_destroy(&(lr->mutex));
 
 #if defined(ENABLE_GC_CACAO)
        /* unregister the lock object reference with the GC */
@@ -337,7 +331,7 @@ static void lock_record_free(lock_record_t *lr)
 
 static void lock_hashtable_init(void)
 {
-       pthread_mutex_init(&(lock_hashtable.mutex), NULL);
+       mutex_init(&(lock_hashtable.mutex));
 
        lock_hashtable.size    = LOCK_INITIAL_HASHTABLE_SIZE;
        lock_hashtable.entries = 0;
@@ -440,7 +434,7 @@ void lock_hashtable_cleanup(void)
 
        /* lock the hashtable */
 
-       pthread_mutex_lock(&(lock_hashtable.mutex));
+       mutex_lock(&(lock_hashtable.mutex));
 
        /* search the hashtable for cleared references */
 
@@ -476,7 +470,7 @@ void lock_hashtable_cleanup(void)
 
        /* unlock the hashtable */
 
-       pthread_mutex_unlock(&(lock_hashtable.mutex));
+       mutex_unlock(&(lock_hashtable.mutex));
 }
 #endif
 
@@ -512,7 +506,7 @@ static lock_record_t *lock_hashtable_get(threadobject *t, java_handle_t *o)
 
        /* lock the hashtable */
 
-       pthread_mutex_lock(&(lock_hashtable.mutex));
+       mutex_lock(&(lock_hashtable.mutex));
 
        /* lookup the lock record in the hashtable */
 
@@ -556,7 +550,7 @@ static lock_record_t *lock_hashtable_get(threadobject *t, java_handle_t *o)
 
        /* unlock the hashtable */
 
-       pthread_mutex_unlock(&(lock_hashtable.mutex));
+       mutex_unlock(&(lock_hashtable.mutex));
 
        /* return the new lock record */
 
@@ -584,7 +578,7 @@ static void lock_hashtable_remove(threadobject *t, java_handle_t *o)
 
        /* lock the hashtable */
 
-       pthread_mutex_lock(&(lock_hashtable.mutex));
+       mutex_lock(&(lock_hashtable.mutex));
 
        /* get lock record */
 
@@ -623,7 +617,7 @@ static void lock_hashtable_remove(threadobject *t, java_handle_t *o)
 
        /* unlock the hashtable */
 
-       pthread_mutex_unlock(&(lock_hashtable.mutex));
+       mutex_unlock(&(lock_hashtable.mutex));
 
        /* free the lock record */
 
@@ -751,7 +745,7 @@ static inline void lock_lockword_set(threadobject *t, java_handle_t *o, uintptr_
 
 static inline void lock_record_enter(threadobject *t, lock_record_t *lr)
 {
-       pthread_mutex_lock(&(lr->mutex));
+       mutex_lock(&(lr->mutex));
        lr->owner = t;
 }
 
@@ -773,7 +767,7 @@ static inline void lock_record_enter(threadobject *t, lock_record_t *lr)
 static inline void lock_record_exit(threadobject *t, lock_record_t *lr)
 {
        lr->owner = NULL;
-       pthread_mutex_unlock(&(lr->mutex));
+       mutex_unlock(&(lr->mutex));
 }
 
 
@@ -854,7 +848,7 @@ static void sable_flc_waiting(ptrint lockword, threadobject *t, java_handle_t *o
 /*             failure, TODO: add statistics */
                return;
 
-       pthread_mutex_lock(&t_other->flc_lock);
+       mutex_lock(&t_other->flc_lock);
        old_flc = t_other->flc_bit;
        t_other->flc_bit = true;
 
@@ -901,14 +895,14 @@ static void sable_flc_waiting(ptrint lockword, threadobject *t, java_handle_t *o
        else
                t_other->flc_bit = old_flc;
 
-       pthread_mutex_unlock(&t_other->flc_lock);
+       mutex_unlock(&t_other->flc_lock);
 }
 
 static void notify_flc_waiters(threadobject *t, java_handle_t *o)
 {
        threadobject *current;
 
-       pthread_mutex_lock(&t->flc_lock);
+       mutex_lock(&t->flc_lock);
 
        current = t->flc_list;
        while (current)
@@ -938,7 +932,7 @@ static void notify_flc_waiters(threadobject *t, java_handle_t *o)
 
        t->flc_list = NULL;
        t->flc_bit = false;
-       pthread_mutex_unlock(&t->flc_lock);
+       mutex_unlock(&t->flc_lock);
 }
 
 /* lock_monitor_enter **********************************************************
@@ -1154,7 +1148,7 @@ bool lock_monitor_exit(java_handle_t *o)
                /* unlock this lock record */
 
                lr->owner = NULL;
-               pthread_mutex_unlock(&(lr->mutex));
+               mutex_unlock(&(lr->mutex));
 
                return true;
        }
@@ -1422,7 +1416,7 @@ static void lock_record_notify(threadobject *t, lock_record_t *lr, bool one)
 
                /* Enter the wait-mutex. */
 
-               pthread_mutex_lock(&(waitingthread->waitmutex));
+               mutex_lock(&(waitingthread->waitmutex));
 
                DEBUGLOCKS(("[lock_record_notify: lr=%p, t=%p, waitingthread=%p, sleeping=%d, one=%d]",
                                        lr, t, waitingthread, waitingthread->sleeping, one));
@@ -1442,7 +1436,7 @@ static void lock_record_notify(threadobject *t, lock_record_t *lr, bool one)
 
                /* Leave the wait-mutex. */
 
-               pthread_mutex_unlock(&(waitingthread->waitmutex));
+               mutex_unlock(&(waitingthread->waitmutex));
 
                /* if we should only wake one, we are done */
 
index 9aa83cacfe23ebfbf617e693695011c2518a4a25..56e17222c50a9489793d935526b1cc97757235fe 100644 (file)
@@ -34,6 +34,8 @@
 
 #include "native/llni.h"
 
+#include "threads/mutex.h"
+
 #include "toolbox/list.h"
 
 #include "vm/global.h"
@@ -69,7 +71,7 @@ struct lock_record_t {
        java_object_t       *object;             /* object for which this lock is */
        struct threadobject *owner;              /* current owner of this monitor */
        s4                   count;              /* recursive lock count          */
-       pthread_mutex_t      mutex;              /* mutex for synchronizing       */
+       mutex_t              mutex;              /* mutex for synchronizing       */
        list_t              *waiters;            /* list of threads waiting       */
        lock_record_t       *hashlink;           /* next record in hash chain     */
 };
@@ -82,7 +84,7 @@ struct lock_record_t {
 *******************************************************************************/
 
 struct lock_hashtable_t {
-       pthread_mutex_t      mutex;       /* mutex for synch. access to the table */
+       mutex_t              mutex;       /* mutex for synch. access to the table */
        u4                   size;        /* number of slots                      */
        u4                   entries;     /* current number of entries            */
        lock_record_t      **ptr;         /* the table of slots, uses ext. chain. */
diff --git a/src/threads/posix/mutex-posix.h b/src/threads/posix/mutex-posix.h
new file mode 100644 (file)
index 0000000..b178c51
--- /dev/null
@@ -0,0 +1,150 @@
+/* src/threads/posix/mutex-posix.h - POSIX mutual exclusion functions
+
+   Copyright (C) 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+
+   This file is part of CACAO.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   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., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+*/
+
+
+#ifndef _MUTEX_POSIX_H
+#define _MUTEX_POSIX_H
+
+#include "config.h"
+
+#include <pthread.h>
+
+#include "vm/vm.h"
+
+
+/* POSIX mutex object *********************************************************/
+
+typedef pthread_mutex_t mutex_t;
+
+
+/* static mutex initializer ***************************************************/
+
+#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+
+
+/* inline functions ***********************************************************/
+
+/* mutex_init ******************************************************************
+
+   Initializes the given mutex object and checks for errors.
+
+   ARGUMENTS:
+       mutex ... POSIX mutex object
+
+*******************************************************************************/
+
+inline static void mutex_init(mutex_t *mutex)
+{
+       int result;
+
+       result = pthread_mutex_init(mutex, NULL);
+
+       if (result != 0)
+               vm_abort_errnum(result, "mutex_init: pthread_mutex_init failed");
+}
+
+
+/* mutex_lock ******************************************************************
+
+   Locks the given mutex object and checks for errors. If the mutex is
+   already locked by another thread, the calling thread is suspended until
+   the mutex is unlocked.
+
+   If the mutex is already locked by the calling thread, the same applies,
+   thus effectively causing the calling thread to deadlock. (This is because
+   we use "fast" pthread mutexes without error checking.)
+
+   ARGUMENTS:
+       mutex ... POSIX mutex object
+
+*******************************************************************************/
+
+inline static void mutex_lock(mutex_t *mutex)
+{
+       int result;
+
+       result = pthread_mutex_lock(mutex);
+
+       if (result != 0)
+               vm_abort_errnum(result, "mutex_lock: pthread_mutex_lock failed");
+}
+
+
+/* mutex_unlock ****************************************************************
+
+   Unlocks the given mutex object and checks for errors. The mutex is
+   assumed to be locked and owned by the calling thread.
+
+   ARGUMENTS:
+       mutex ... POSIX mutex object
+
+*******************************************************************************/
+
+inline static void mutex_unlock(mutex_t *mutex)
+{
+       int result;
+
+       result = pthread_mutex_unlock(mutex);
+
+       if (result != 0)
+               vm_abort_errnum(result, "mutex_unlock: pthread_mutex_unlock failed");
+}
+
+
+/* mutex_destroy ***************************************************************
+
+   Destroys the given mutex object and checks for errors.
+
+   ARGUMENTS:
+       mutex ... POSIX mutex object
+
+*******************************************************************************/
+
+inline static void mutex_destroy(mutex_t *mutex)
+{
+       int result;
+
+       result = pthread_mutex_destroy(mutex);
+
+       if (result != 0)
+               vm_abort_errnum(result, "mutex_destroy: pthread_mutex_destroy failed");
+}
+
+
+#endif /* _MUTEX_POSIX_H */
+
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ * vim:noexpandtab:sw=4:ts=4:
+ */
index d04335335ca17468b686222e61472a4807ffca37..9fa07dc39a4ed2c759e30f8dfeb1bc00c4a3969c 100644 (file)
@@ -74,6 +74,7 @@
 #endif
 
 #include "threads/lock-common.h"
+#include "threads/mutex.h"
 #include "threads/threadlist.h"
 #include "threads/thread.h"
 
@@ -127,8 +128,7 @@ static int sem_init(sem_t *sem, int pshared, int value)
 
        sem->value = value;
     
-       if (pthread_mutex_init(&sem->mutex, NULL) < 0)
-               return -1;
+       mutex_init(&sem->mutex, NULL)
 
        if (pthread_cond_init(&sem->cond, NULL) < 0)
                return -1;
@@ -138,26 +138,23 @@ static int sem_init(sem_t *sem, int pshared, int value)
 
 static int sem_post(sem_t *sem)
 {
-       if (pthread_mutex_lock(&sem->mutex) < 0)
-               return -1;
+       mutex_lock(&sem->mutex)
 
        sem->value++;
 
        if (pthread_cond_signal(&sem->cond) < 0) {
-               pthread_mutex_unlock(&sem->mutex);
+               mutex_unlock(&sem->mutex);
                return -1;
        }
 
-       if (pthread_mutex_unlock(&sem->mutex) < 0)
-               return -1;
+       mutex_unlock(&sem->mutex)
 
        return 0;
 }
 
 static int sem_wait(sem_t *sem)
 {
-       if (pthread_mutex_lock(&sem->mutex) < 0)
-               return -1;
+       mutex_lock(&sem->mutex)
 
        while (sem->value == 0) {
                pthread_cond_wait(&sem->cond, &sem->mutex);
@@ -165,8 +162,7 @@ static int sem_wait(sem_t *sem)
 
        sem->value--;
 
-       if (pthread_mutex_unlock(&sem->mutex) < 0)
-               return -1;    
+       mutex_unlock(&sem->mutex)
 
        return 0;
 }
@@ -176,8 +172,7 @@ static int sem_destroy(sem_t *sem)
        if (pthread_cond_destroy(&sem->cond) < 0)
                return -1;
 
-       if (pthread_mutex_destroy(&sem->mutex) < 0)
-               return -1;
+       mutex_destroy(&sem->mutex)
 
        return 0;
 }
@@ -226,15 +221,15 @@ pthread_key_t thread_current_key;
 #endif
 
 /* global mutex for stop-the-world                                            */
-static pthread_mutex_t stopworldlock;
+static mutex_t stopworldlock;
 
 #if defined(ENABLE_GC_CACAO)
 /* global mutex for the GC */
-static pthread_mutex_t mutex_gc;
+static mutex_t mutex_gc;
 #endif
 
 /* global mutex and condition for joining threads on exit */
-static pthread_mutex_t mutex_join;
+static mutex_t mutex_join;
 static pthread_cond_t  cond_join;
 
 /* XXX We disable that whole bunch of code until we have the exact-GC
@@ -249,7 +244,7 @@ static volatile int stopworldwhere;
 /* semaphore used for acknowleding thread suspension                          */
 static sem_t suspend_ack;
 #if defined(__IRIX__)
-static pthread_mutex_t suspend_ack_lock = PTHREAD_MUTEX_INITIALIZER;
+static mutex_t suspend_ack_lock = MUTEX_INITIALIZER;
 static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
 #endif
 
@@ -257,8 +252,8 @@ static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
 
 /* mutexes used by the fake atomic instructions                               */
 #if defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
-pthread_mutex_t _cas_lock = PTHREAD_MUTEX_INITIALIZER;
-pthread_mutex_t _mb_lock = PTHREAD_MUTEX_INITIALIZER;
+mutex_t _cas_lock = MUTEX_INITIALIZER;
+mutex_t _mb_lock = MUTEX_INITIALIZER;
 #endif
 
 
@@ -354,7 +349,7 @@ void threads_sem_post(sem_t *sem)
 
 void lock_stopworld(int where)
 {
-       pthread_mutex_lock(&stopworldlock);
+       mutex_lock(&stopworldlock);
 /*     stopworldwhere = where; */
 }
 
@@ -368,7 +363,7 @@ void lock_stopworld(int where)
 void unlock_stopworld(void)
 {
 /*     stopworldwhere = 0; */
-       pthread_mutex_unlock(&stopworldlock);
+       mutex_unlock(&stopworldlock);
 }
 
 /* XXX We disable that whole bunch of code until we have the exact-GC
@@ -484,9 +479,9 @@ static void threads_cast_darwinresume(void)
 #if defined(__IRIX__)
 static void threads_cast_irixresume(void)
 {
-       pthread_mutex_lock(&suspend_ack_lock);
+       mutex_lock(&suspend_ack_lock);
        pthread_cond_broadcast(&suspend_cond);
-       pthread_mutex_unlock(&suspend_ack_lock);
+       mutex_unlock(&suspend_ack_lock);
 }
 #endif
 
@@ -504,10 +499,10 @@ static void threads_sigsuspend_handler(ucontext_t *_uc)
        /* Do as Boehm does. On IRIX a condition variable is used for wake-up
           (not POSIX async-safe). */
 #if defined(__IRIX__)
-       pthread_mutex_lock(&suspend_ack_lock);
+       mutex_lock(&suspend_ack_lock);
        threads_sem_post(&suspend_ack);
        pthread_cond_wait(&suspend_cond, &suspend_ack_lock);
-       pthread_mutex_unlock(&suspend_ack_lock);
+       mutex_unlock(&suspend_ack_lock);
 #elif defined(__CYGWIN__)
        /* TODO */
        assert(0);
@@ -680,25 +675,19 @@ void threads_impl_thread_init(threadobject *t)
 
        /* initialize the mutex and the condition */
 
-       result = pthread_mutex_init(&t->flc_lock, NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_new: pthread_mutex_init failed");
+       mutex_init(&t->flc_lock);
 
        result = pthread_cond_init(&t->flc_cond, NULL);
        if (result != 0)
                vm_abort_errnum(result, "threads_impl_thread_new: pthread_cond_init failed");
 
-       result = pthread_mutex_init(&(t->waitmutex), NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_new: pthread_mutex_init failed");
+       mutex_init(&(t->waitmutex));
 
        result = pthread_cond_init(&(t->waitcond), NULL);
        if (result != 0)
                vm_abort_errnum(result, "threads_impl_thread_new: pthread_cond_init failed");
 
-       result = pthread_mutex_init(&(t->suspendmutex), NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_new: pthread_mutex_init failed");
+       mutex_init(&(t->suspendmutex));
 
        result = pthread_cond_init(&(t->suspendcond), NULL);
        if (result != 0)
@@ -812,30 +801,21 @@ void threads_impl_thread_free(threadobject *t)
 
        /* Destroy the mutex and the condition. */
 
-       result = pthread_mutex_destroy(&(t->flc_lock));
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_free: pthread_mutex_destroy failed");
+       mutex_destroy(&(t->flc_lock));
 
        result = pthread_cond_destroy(&(t->flc_cond));
 
        if (result != 0)
                vm_abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
 
-       result = pthread_mutex_destroy(&(t->waitmutex));
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_free: pthread_mutex_destroy failed");
+       mutex_destroy(&(t->waitmutex));
 
        result = pthread_cond_destroy(&(t->waitcond));
 
        if (result != 0)
                vm_abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
 
-       result = pthread_mutex_destroy(&(t->suspendmutex));
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_thread_free: pthread_mutex_destroy failed");
+       mutex_destroy(&(t->suspendmutex));
 
        result = pthread_cond_destroy(&(t->suspendcond));
 
@@ -858,16 +838,12 @@ void threads_impl_preinit(void)
 {
        int result;
 
-       result = pthread_mutex_init(&stopworldlock, NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_preinit: pthread_mutex_init failed");
+       mutex_init(&stopworldlock);
 
        /* initialize exit mutex and condition (on exit we join all
           threads) */
 
-       result = pthread_mutex_init(&mutex_join, NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_preinit: pthread_mutex_init failed");
+       mutex_init(&mutex_join);
 
        result = pthread_cond_init(&cond_join, NULL);
        if (result != 0)
@@ -876,9 +852,7 @@ void threads_impl_preinit(void)
 #if defined(ENABLE_GC_CACAO)
        /* initialize the GC mutext */
 
-       result = pthread_mutex_init(&mutex_gc, NULL);
-       if (result != 0)
-               vm_abort_errnum(result, "threads_impl_preinit: pthread_mutex_init failed");
+       mutex_init(&mutex_gc);
 #endif
 
 #if !defined(HAVE___THREAD)
@@ -900,12 +874,7 @@ void threads_impl_preinit(void)
 #if defined(ENABLE_GC_CACAO)
 void threads_mutex_gc_lock(void)
 {
-       int result;
-
-       result = pthread_mutex_lock(&mutex_gc);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_mutex_gc_lock: pthread_mutex_lock failed");
+       mutex_lock(&mutex_gc);
 }
 #endif
 
@@ -919,12 +888,7 @@ void threads_mutex_gc_lock(void)
 #if defined(ENABLE_GC_CACAO)
 void threads_mutex_gc_unlock(void)
 {
-       int result;
-
-       result = pthread_mutex_unlock(&mutex_gc);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_mutex_gc_unlock: pthread_mutex_unlock failed");
+       mutex_unlock(&mutex_gc);
 }
 #endif
 
@@ -936,12 +900,7 @@ void threads_mutex_gc_unlock(void)
 
 void threads_mutex_join_lock(void)
 {
-       int result;
-
-       result = pthread_mutex_lock(&mutex_join);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_mutex_join_lock: pthread_mutex_lock failed");
+       mutex_lock(&mutex_join);
 }
 
 
@@ -953,12 +912,7 @@ void threads_mutex_join_lock(void)
 
 void threads_mutex_join_unlock(void)
 {
-       int result;
-
-       result = pthread_mutex_unlock(&mutex_join);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_mutex_join_unlock: pthread_mutex_unlock failed");
+       mutex_unlock(&mutex_join);
 }
 
 
@@ -1433,12 +1387,10 @@ bool threads_detach_thread(threadobject *t)
 bool threads_suspend_thread(threadobject *thread, s4 reason)
 {
        /* acquire the suspendmutex */
-       if (pthread_mutex_lock(&(thread->suspendmutex)) != 0)
-               vm_abort("threads_suspend_thread: pthread_mutex_lock failed: %s",
-                                strerror(errno));
+       mutex_lock(&(thread->suspendmutex));
 
        if (thread->suspended) {
-               pthread_mutex_unlock(&(thread->suspendmutex));
+               mutex_unlock(&(thread->suspendmutex));
                return false;
        }
 
@@ -1517,9 +1469,7 @@ void threads_suspend_ack(u1* pc, u1* sp)
        /* TODO: free dump memory */
 
        /* release the suspendmutex */
-       if (pthread_mutex_unlock(&(thread->suspendmutex)) != 0)
-               vm_abort("threads_suspend_ack: pthread_mutex_unlock failed: %s",
-                                strerror(errno));
+       mutex_unlock(&(thread->suspendmutex));
 }
 
 
@@ -1532,12 +1482,10 @@ void threads_suspend_ack(u1* pc, u1* sp)
 bool threads_resume_thread(threadobject *thread)
 {
        /* acquire the suspendmutex */
-       if (pthread_mutex_lock(&(thread->suspendmutex)) != 0)
-               vm_abort("threads_resume_ack: pthread_mutex_unlock failed: %s",
-                                strerror(errno));
+       mutex_lock(&(thread->suspendmutex));
 
        if (!thread->suspended) {
-               pthread_mutex_unlock(&(thread->suspendmutex));
+               mutex_unlock(&(thread->suspendmutex));
                return false;
        }
 
@@ -1548,7 +1496,7 @@ bool threads_resume_thread(threadobject *thread)
        pthread_cond_broadcast(&(thread->suspendcond));
 
        /* release the suspendmutex */
-       pthread_mutex_unlock(&(thread->suspendmutex));
+       mutex_unlock(&(thread->suspendmutex));
 
        return true;
 }
@@ -1661,7 +1609,7 @@ static void threads_wait_with_timeout(threadobject *t, struct timespec *wakeupTi
 {
        /* acquire the waitmutex */
 
-       pthread_mutex_lock(&t->waitmutex);
+       mutex_lock(&t->waitmutex);
 
        /* mark us as sleeping */
 
@@ -1697,7 +1645,7 @@ static void threads_wait_with_timeout(threadobject *t, struct timespec *wakeupTi
 
        /* release the waitmutex */
 
-       pthread_mutex_unlock(&t->waitmutex);
+       mutex_unlock(&t->waitmutex);
 }
 
 
@@ -1777,7 +1725,7 @@ void threads_thread_interrupt(threadobject *thread)
        /* Signal the thread a "waitcond" and tell it that it has been
           interrupted. */
 
-       pthread_mutex_lock(&thread->waitmutex);
+       mutex_lock(&thread->waitmutex);
 
        DEBUGTHREADS("interrupted", thread);
 
@@ -1790,7 +1738,7 @@ void threads_thread_interrupt(threadobject *thread)
 
        thread->interrupted = true;
 
-       pthread_mutex_unlock(&thread->waitmutex);
+       mutex_unlock(&thread->waitmutex);
 }
 
 
@@ -1811,7 +1759,7 @@ bool threads_check_if_interrupted_and_reset(void)
 
        thread = THREADOBJECT;
 
-       pthread_mutex_lock(&thread->waitmutex);
+       mutex_lock(&thread->waitmutex);
 
        /* get interrupted flag */
 
@@ -1821,7 +1769,7 @@ bool threads_check_if_interrupted_and_reset(void)
 
        thread->interrupted = false;
 
-       pthread_mutex_unlock(&thread->waitmutex);
+       mutex_unlock(&thread->waitmutex);
 
        return intr;
 }
index 73106f19f08e6e38e39956fb3aa69157607c0a4b..a3602c8a4e31620d5a3d210b851a0afb34e91694 100644 (file)
@@ -42,6 +42,8 @@ typedef struct threadobject threadobject;
 
 #include "native/localref.h"
 
+#include "threads/mutex.h"
+
 #include "threads/posix/lock.h"
 
 #include "vm/global.h"
@@ -61,7 +63,7 @@ typedef struct threadobject threadobject;
 # include <mach/mach.h>
 
 typedef struct {
-       pthread_mutex_t mutex;
+       mutex_t mutex;
        pthread_cond_t cond;
        int value;
 } sem_t;
@@ -126,14 +128,14 @@ struct threadobject {
        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 */
+       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       waitmutex;
+       mutex_t               waitmutex;
        pthread_cond_t        waitcond;
 
-       pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
+       mutex_t               suspendmutex; /* lock before suspending this thread */
        pthread_cond_t        suspendcond;  /* notify to resume this thread       */
 
        bool                  interrupted;
index 3a9f7b16e7bca226b00d5ac3a978b1ffd723706f..c8ba797ce7842b961330f1a307c2fe11c54a08b1 100644 (file)
@@ -25,7 +25,7 @@
 
 #include "config.h"
 
-#include <pthread.h>
+#include "threads/mutex.h"
 
 #include "vm/vm.h"
 
@@ -35,7 +35,7 @@
 /* global variables ***********************************************************/
 
 /* global mutex for the thread list */
-static pthread_mutex_t threadlist_mutex;
+static mutex_t threadlist_mutex;
 
 
 /* threadlist_impl_init ********************************************************
@@ -46,16 +46,11 @@ static pthread_mutex_t threadlist_mutex;
 
 void threadlist_impl_init(void)
 {
-       int result;
-
        TRACESUBSYSTEMINITIALIZATION("threadlist_impl_init");
 
        /* Initialize the thread list mutex. */
 
-       result = pthread_mutex_init(&threadlist_mutex, NULL);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threadlist_impl_init: pthread_mutex_init failed");
+       mutex_init(&threadlist_mutex);
 }
 
 
@@ -72,12 +67,7 @@ void threadlist_impl_init(void)
 
 void threadlist_lock(void)
 {
-       int result;
-
-       result = pthread_mutex_lock(&threadlist_mutex);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threads_list_lock: pthread_mutex_lock failed");
+       mutex_lock(&threadlist_mutex);
 }
 
 
@@ -89,12 +79,7 @@ void threadlist_lock(void)
 
 void threadlist_unlock(void)
 {
-       int result;
-
-       result = pthread_mutex_unlock(&threadlist_mutex);
-
-       if (result != 0)
-               vm_abort_errnum(result, "threadlist_unlock: pthread_mutex_unlock failed");
+       mutex_unlock(&threadlist_mutex);
 }
 
 
index 60c8f3df573c637ef4ce6152dc1c79fc644d34ee..4888eb3a3d1666035a8e54067bd8026941368ae1 100644 (file)
@@ -59,6 +59,7 @@
 #include "native/vm/nativevm.h"
 
 #include "threads/lock-common.h"
+#include "threads/mutex.h"
 #include "threads/threadlist.h"
 #include "threads/thread.h"
 
@@ -1922,9 +1923,9 @@ void vm_shutdown(s4 status)
 #if defined(ENABLE_JVMTI)
        /* terminate cacaodbgserver */
        if (dbgcom!=NULL) {
-               pthread_mutex_lock(&dbgcomlock);
+               mutex_lock(&dbgcomlock);
                dbgcom->running=1;
-               pthread_mutex_unlock(&dbgcomlock);
+               mutex_unlock(&dbgcomlock);
                jvmti_cacaodbgserver_quit();
        }       
 #endif