* src/native/vm/sun_misc_Unsafe.c: Use new atomic primitve functions.
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Thu, 19 Jun 2008 15:18:58 +0000 (17:18 +0200)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Thu, 19 Jun 2008 15:18:58 +0000 (17:18 +0200)
* src/threads/posix/lock.c: Likewise.
* src/threads/posix/thread-posix.c: Likewise.
* src/vm/jit/x86_64/Makefile.am (noinst_HEADERS): Removed
machine-instr.h, added atomic.hpp.
* src/vm/jit/x86_64/asmpart.S (asm_compare_and_swap): Removed.
(asm_memory_barrier): Likewise.
* src/vm/jit/x86_64/atomic.hpp: New file.
* src/vm/jit/x86_64/machine-instr.h: Removed.

--HG--
branch : twisti

src/native/vm/sun_misc_Unsafe.c
src/threads/posix/lock.c
src/threads/posix/thread-posix.c
src/vm/jit/x86_64/Makefile.am
src/vm/jit/x86_64/asmpart.S
src/vm/jit/x86_64/atomic.hpp [new file with mode: 0644]
src/vm/jit/x86_64/machine-instr.h [deleted file]

index 737559da5cafcc99ee642645f7f5c7d593e0cdcd..3b55b84aa2c0c3fc767241bb17b813aee06c7e85 100644 (file)
 #include <stdint.h>
 #include <unistd.h>
 
+#if 0
 #include "machine-instr.h"
+#else
+#include "atomic.hpp"
+#endif
 
 #include "mm/memory.h"
 
@@ -1074,7 +1078,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_throwException(JNIEnv *env, sun_misc
  */
 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, java_lang_Object *expected, java_lang_Object *x)
 {
-#if 1
+#if 0
        void **p;
        void  *value;
 
@@ -1099,7 +1103,7 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env,
 
        p = (volatile void **) (((uint8_t *) o) + offset);
 
-       result = atomic_compare_and_swap_address(p, expected, x);
+       result = Atomic_cas_ptr(p, expected, x);
 
        if (result == expected)
                return true;
@@ -1116,7 +1120,7 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env,
  */
 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, sun_misc_Unsafe* this, java_lang_Object* o, int64_t offset, int32_t expected, int32_t x)
 {
-#if 1
+#if 0
        int32_t *p;
        int32_t  value;
 
@@ -1134,14 +1138,14 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, su
 
        return false;
 #else
-       int32_t *p;
-       int32_t  result;
+       uint32_t *p;
+       uint32_t  result;
 
        /* XXX Use LLNI */
 
-       p = (int32_t *) (((uint8_t *) o) + offset);
+       p = (uint32_t *) (((uint8_t *) o) + offset);
 
-       result = atomic_compare_and_swap_int(p, expected, x);
+       result = Atomic_cas_32(p, expected, x);
 
        if (result == expected)
                return true;
@@ -1158,6 +1162,7 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, su
  */
 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapLong(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t expected, int64_t x)
 {
+#if 0
        int64_t *p;
        int64_t  value;
 
@@ -1174,6 +1179,21 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapLong(JNIEnv *env, s
        }
 
        return false;
+#else
+       uint64_t *p;
+       uint64_t  result;
+
+       /* XXX Use LLNI */
+
+       p = (uint64_t *) (((uint8_t *) o) + offset);
+
+       result = Atomic_cas_64(p, expected, x);
+
+       if (result == expected)
+               return true;
+
+       return false;
+#endif
 }
 
 
@@ -1244,7 +1264,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, sun_m
                                                                                                                        \
        *_p = x;                                                                                                \
                                                                                                                        \
-       MEMORY_BARRIER();                                                                               \
+       Atomic_mb();                                                                                    \
                                                                                                                        \
        LLNI_CRITICAL_END;
 
@@ -1317,7 +1337,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedObject(JNIEnv *env, sun_mi
 
        *_p = _x;
 
-       MEMORY_BARRIER();
+       Atomic_mb();
 
        LLNI_CRITICAL_END;
 }
index 71e5a0077132212fb2704d721e2057344bf81e7b..36607054bc1bcbf4adb7432fb7c8ae6c9444d564 100644 (file)
@@ -72,7 +72,7 @@
 #if defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
 #include "threads/posix/generic-primitives.h"
 #else
-#include "machine-instr.h"
+#include "atomic.hpp"
 #endif
 
 #if defined(ENABLE_JVMTI)
@@ -856,7 +856,7 @@ static void sable_flc_waiting(ptrint lockword, threadobject *t, java_handle_t *o
                                t->index, t_other->index));
 
        /* Set FLC bit first, then read the lockword again */
-       MEMORY_BARRIER();
+       Atomic_mb();
 
        lockword = lock_lockword_get(t, o);
 
@@ -973,7 +973,7 @@ retry:
        /* most common case: try to thin-lock an unlocked object */
 
        LLNI_CRITICAL_START_THREAD(t);
-       lockword = COMPARE_AND_SWAP_OLD_VALUE(&(LLNI_DIRECT(o)->lockword), THIN_UNLOCKED, thinlock);
+       lockword = Atomic_cas_ptr(&(LLNI_DIRECT(o)->lockword), THIN_UNLOCKED, thinlock);
        LLNI_CRITICAL_END_THREAD(t);
 
        if (lockword == THIN_UNLOCKED) {
@@ -1097,8 +1097,8 @@ bool lock_monitor_exit(java_handle_t *o)
                /* memory barrier for Java Memory Model */
                STORE_ORDER_BARRIER();
                lock_lockword_set(t, o, THIN_UNLOCKED);
-               /* memory barrier for thin locking */
-               MEMORY_BARRIER();
+               /* Memory barrier for thin locking. */
+               Atomic_mb();
 
                /* check if there has been a flat lock contention on this object */
 
index d64e18d5eec19b7284b3814a897fea6cbe815601..61d6a9d081b92c2d5eed173943d58641796e355d 100644 (file)
@@ -43,9 +43,7 @@
 
 #include "arch.h"
 
-#if !defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
-# include "machine-instr.h"
-#else
+#if defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
 # include "threads/posix/generic-primitives.h"
 #endif
 
index 839f39c7bcef3faf045f13b81974c4071f50968f..19f9498f313da2d57981ecbe0218fb80397a2563 100644 (file)
@@ -34,7 +34,7 @@ SUBDIRS = $(OS_DIR)
 
 noinst_HEADERS = \
        arch.h \
-       machine-instr.h \
+       atomic.hpp \
        \
        md-asm.h
 
index 4f7a69f9fa074b5c22ccfaed0c05c59ea077b545..5807491ecbac01e65242b9c65be0c10615d4faaa 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/x86_64/asmpart.S - Java-C interface functions for x86_64
 
-   Copyright (C) 1996-2005, 2006, 2007, 2008 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.
 
@@ -58,9 +56,6 @@
        .globl asm_builtin_d2i
        .globl asm_builtin_d2l
 
-       .globl asm_compare_and_swap
-       .globl asm_memory_barrier
-
        .globl asm_get_cycle_count
 
 
@@ -394,30 +389,6 @@ asm_builtin_d2l:
        ret
 
 
-/* asm_compare_and_swap ********************************************************
-
-   Does an atomic compare and swap.  Required for the lock
-   implementation.
-
-*******************************************************************************/
-
-asm_compare_and_swap:
-       mov     a1,v0                       /* v0 is %rax                         */
-       lock cmpxchg a2,(a0)
-       ret
-
-
-/* asm_memory_barrier **********************************************************
-
-   A memory barrier for the Java Memory Model.
-
-*******************************************************************************/
-
-asm_memory_barrier:
-       mfence
-       ret
-
-
 /* asm_get_cycle_count *********************************************************
 
    Get the current time-stamp counter from the CPU.
diff --git a/src/vm/jit/x86_64/atomic.hpp b/src/vm/jit/x86_64/atomic.hpp
new file mode 100644 (file)
index 0000000..d5aea70
--- /dev/null
@@ -0,0 +1,118 @@
+/* src/vm/jit/i386/atomic.hpp - i386 atomic instructions
+
+   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 _ATOMIC_HPP
+#define _ATOMIC_HPP
+
+#include "config.h"
+
+#include <stdint.h>
+
+
+/**
+ * An atomic compare and swap for 32-bit integer values.
+ *
+ * @param p      Pointer to memory address.
+ * @param oldval Old value to be expected.
+ * @param newval New value to be stored.
+ *
+ * @return value of the memory location before the store
+ */
+inline static uint32_t Atomic_cas_32(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
+{
+       int32_t result;
+
+       __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
+                                                 : "=a" (result), "=m" (*p)
+                                                 : "r" (newval), "m" (*p), "0" (oldval));
+
+       return result;
+}
+
+
+/**
+ * An atomic compare and swap for 64-bit integer values.
+ *
+ * @param p      Pointer to memory address.
+ * @param oldval Old value to be expected.
+ * @param newval New value to be stored.
+ *
+ * @return value of the memory location before the store
+ */
+inline static uint64_t Atomic_cas_64(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
+{
+       int64_t result;
+
+       __asm__ __volatile__ ("lock; cmpxchgq %2, %1"
+                                                 : "=a" (result), "=m" (*p)
+                                                 : "r" (newval), "m" (*p), "0" (oldval));
+
+       return result;
+}
+
+
+/**
+ * An atomic compare and swap for pointer values.
+ *
+ * @param p      Pointer to memory address.
+ * @param oldval Old value to be expected.
+ * @param newval New value to be stored.
+ *
+ * @return value of the memory location before the store
+ */
+inline static void* Atomic_cas_ptr(volatile void** p, void* oldval, void* newval)
+{
+       return (void*) Atomic_cas_64((volatile uint64_t*) p, (uint64_t) oldval, (uint64_t) newval);
+}
+
+
+/**
+ * A memory barrier.
+ */
+inline static void Atomic_mb(void)
+{
+       __asm__ __volatile__ ("mfence" : : : "memory");
+}
+
+
+#define STORE_ORDER_BARRIER() __asm__ __volatile__ ("" : : : "memory");
+#define MEMORY_BARRIER_AFTER_ATOMIC() /* nothing */
+
+#endif /* _ATOMIC_HPP */
+
+
+/*
+ * 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:
+ */
diff --git a/src/vm/jit/x86_64/machine-instr.h b/src/vm/jit/x86_64/machine-instr.h
deleted file mode 100644 (file)
index 3afe548..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef _MACHINE_INSTR_H
-#define _MACHINE_INSTR_H
-
-static inline long
-__attribute__ ((unused))
-compare_and_swap (volatile long *p, long oldval, long newval)
-{
-  long ret;
-
-  __asm__ __volatile__ ("lock; cmpxchgq %2, %1"
-                        : "=a" (ret), "=m" (*p)
-                        : "r" (newval), "m" (*p), "0" (oldval));
-  return ret;
-}
-
-#define STORE_ORDER_BARRIER() __asm__ __volatile__ ("" : : : "memory");
-#define MEMORY_BARRIER_AFTER_ATOMIC() /* nothing */
-#define MEMORY_BARRIER() __asm__ __volatile__ ( \
-               "mfence" : : : "memory" );
-
-#endif