* m4/classpath.m4: Renamed --with-classpath* options to
[cacao.git] / src / native / vm / sun_misc_Unsafe.c
index 70349a02b68f45f737d3b211232f483c510bd5ab..737559da5cafcc99ee642645f7f5c7d593e0cdcd 100644 (file)
@@ -28,6 +28,8 @@
 #include <stdint.h>
 #include <unistd.h>
 
+#include "machine-instr.h"
+
 #include "mm/memory.h"
 
 #include "native/jni.h"
@@ -37,7 +39,7 @@
 #include "native/include/java_lang_Object.h"                  /* before c.l.C */
 #include "native/include/java_lang_String.h"            /* required by j.l.CL */
 
-#if defined(WITH_CLASSPATH_SUN)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 # include "native/include/java_nio_ByteBuffer.h"        /* required by j.l.CL */
 #endif
 
 #include "native/include/java_lang_Thread.h"             /* required by s.m.U */
 #include "native/include/java_lang_Throwable.h"
 
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+# include "native/include/java_lang_reflect_VMField.h"
+#endif
+
 #include "native/include/java_security_ProtectionDomain.h" /* required by smU */
 
 #include "native/include/sun_misc_Unsafe.h"
@@ -120,7 +126,12 @@ static JNINativeMethod methods[] = {
        { "getObjectVolatile",      "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void *) (intptr_t) &Java_sun_misc_Unsafe_getObjectVolatile              },
        { "putObjectVolatile",      "(Ljava/lang/Object;JLjava/lang/Object;)V",                   (void *) (intptr_t) &Java_sun_misc_Unsafe_putObjectVolatile              },
        { "getIntVolatile",         "(Ljava/lang/Object;J)I",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getIntVolatile                 },
+       { "putIntVolatile",         "(Ljava/lang/Object;JI)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putIntVolatile                 },
        { "getLongVolatile",        "(Ljava/lang/Object;J)J",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getLongVolatile                },
+       { "putLongVolatile",        "(Ljava/lang/Object;JJ)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putLongVolatile                },
+       { "putOrderedObject",       "(Ljava/lang/Object;JLjava/lang/Object;)V",                   (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedObject               },
+       { "putOrderedInt",          "(Ljava/lang/Object;JI)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedInt                  },
+       { "putOrderedLong",         "(Ljava/lang/Object;JJ)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedLong                 },
        { "unpark",                 "(Ljava/lang/Object;)V",                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_unpark                         },
        { "park",                   "(ZJ)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_park                           },
 };
@@ -646,10 +657,26 @@ JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_objectFieldOffset(JNIEnv *env, su
        fieldinfo *f;
        int32_t    slot;
 
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       java_lang_reflect_VMField *rvmf;
+#endif
+
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+
+       LLNI_field_get_ref(field, f,     rvmf);
+       LLNI_field_get_cls(rvmf,  clazz, c);
+       LLNI_field_get_val(rvmf,  slot , slot);
+
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+
        LLNI_field_get_cls(field, clazz, c);
        LLNI_field_get_val(field, slot , slot);
 
-       f = &c->fields[slot];
+#else
+# error unknown configuration
+#endif
+
+       f = &(c->fields[slot]);
 
        return (int64_t) f->offset;
 }
@@ -835,9 +862,25 @@ JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_staticFieldBase(JNIEnv
        fieldinfo *f;
        int32_t    slot;
 
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       java_lang_reflect_VMField *rvmf;
+#endif
+
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+
+       LLNI_field_get_ref(rf,   f,     rvmf);
+       LLNI_field_get_cls(rvmf, clazz, c);
+       LLNI_field_get_val(rvmf, slot , slot);
+
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+
        LLNI_field_get_cls(rf, clazz, c);
        LLNI_field_get_val(rf, slot , slot);
 
+#else
+# error unknown configuration
+#endif
+
        f = &(c->fields[slot]);
 
        return (java_lang_Object *) (f->value);
@@ -939,7 +982,7 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_pageSize(JNIEnv *env, sun_misc_Un
  */
 JNIEXPORT java_lang_Class* JNICALL Java_sun_misc_Unsafe_defineClass__Ljava_lang_String_2_3BIILjava_lang_ClassLoader_2Ljava_security_ProtectionDomain_2(JNIEnv *env, sun_misc_Unsafe *this, java_lang_String *name, java_handle_bytearray_t *b, int32_t off, int32_t len, java_lang_ClassLoader *loader, java_security_ProtectionDomain *protectionDomain)
 {
-       classloader     *cl;
+       classloader_t   *cl;
        utf             *utfname;
        classinfo       *c;
        java_lang_Class *o;
@@ -981,7 +1024,7 @@ JNIEXPORT java_lang_Class* JNICALL Java_sun_misc_Unsafe_defineClass__Ljava_lang_
 
        o = LLNI_classinfo_wrap(c);
 
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
        /* set ProtectionDomain */
 
        LLNI_field_set_ref(o, pd, protectionDomain);
@@ -1031,6 +1074,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
        void **p;
        void  *value;
 
@@ -1047,6 +1091,21 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env,
        }
 
        return false;
+#else
+       volatile void **p;
+       void           *result;
+
+       /* XXX Use LLNI */
+
+       p = (volatile void **) (((uint8_t *) o) + offset);
+
+       result = atomic_compare_and_swap_address(p, expected, x);
+
+       if (result == expected)
+               return true;
+
+       return false;
+#endif
 }
 
 
@@ -1055,24 +1114,40 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env,
  * Method:    compareAndSwapInt
  * Signature: (Ljava/lang/Object;JII)Z
  */
-JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, sun_misc_Unsafe* this, java_lang_Object* obj, int64_t offset, int32_t expect, int32_t update)
+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
        int32_t *p;
        int32_t  value;
 
-       p = (int32_t *) (((uint8_t *) obj) + offset);
+       p = (int32_t *) (((uint8_t *) o) + offset);
 
        /* XXX this should be atomic */
 
        value = *p;
 
-       if (value == expect) {
-               *p = update;
+       if (value == expected) {
+               *p = x;
 
                return true;
        }
 
        return false;
+#else
+       int32_t *p;
+       int32_t  result;
+
+       /* XXX Use LLNI */
+
+       p = (int32_t *) (((uint8_t *) o) + offset);
+
+       result = atomic_compare_and_swap_int(p, expected, x);
+
+       if (result == expected)
+               return true;
+
+       return false;
+#endif
 }
 
 
@@ -1135,6 +1210,45 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, sun_m
 }
 
 
+#define UNSAFE_GET_VOLATILE(type)                                                      \
+       java_handle_t *_h;                                                                              \
+       java_object_t *_o;                                                                              \
+       volatile type *_p;                                                                              \
+       volatile type  _x;                                                                              \
+                                                                                                                       \
+       _h = (java_handle_t *) o;                                                               \
+                                                                                                                       \
+       LLNI_CRITICAL_START;                                                                    \
+                                                                                                                       \
+       _o = LLNI_UNWRAP(_h);                                                                   \
+       _p = (volatile type *) (((uint8_t *) _o) + offset);             \
+                                                                                                                       \
+       _x = *_p;                                                                                               \
+                                                                                                                       \
+       LLNI_CRITICAL_END;                                                                              \
+                                                                                                                       \
+       return _x;
+
+
+#define UNSAFE_PUT_VOLATILE(type)                                                      \
+       java_handle_t *_h;                                                                              \
+       java_object_t *_o;                                                                              \
+       volatile type *_p;                                                                              \
+                                                                                                                       \
+       _h = (java_handle_t *) o;                                                               \
+                                                                                                                       \
+       LLNI_CRITICAL_START;                                                                    \
+                                                                                                                       \
+       _o = LLNI_UNWRAP(_h);                                                                   \
+       _p = (volatile type *) (((uint8_t *) _o) + offset);             \
+                                                                                                                       \
+       *_p = x;                                                                                                \
+                                                                                                                       \
+       MEMORY_BARRIER();                                                                               \
+                                                                                                                       \
+       LLNI_CRITICAL_END;
+
+
 /*
  * Class:     sun/misc/Unsafe
  * Method:    getIntVolatile
@@ -1142,14 +1256,18 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, sun_m
  */
 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
 {
-       volatile int32_t *p;
-       volatile int32_t  value;
-
-       p = (volatile int32_t *) (((uint8_t *) o) + offset);
+       UNSAFE_GET_VOLATILE(int32_t);
+}
 
-       value = *p;
 
-       return value;
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putIntVolatile
+ * Signature: (Ljava/lang/Object;JI)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
+{
+       UNSAFE_PUT_VOLATILE(int32_t);
 }
 
 
@@ -1160,14 +1278,70 @@ JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, sun_m
  */
 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
 {
-       volatile int64_t *p;
-       volatile int64_t  value;
+       UNSAFE_GET_VOLATILE(int64_t);
+}
 
-       p = (volatile int64_t *) (((uint8_t *) o) + offset);
 
-       value = *p;
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putLongVolatile
+ * Signature: (Ljava/lang/Object;JJ)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t x)
+{
+       UNSAFE_PUT_VOLATILE(int64_t);
+}
 
-       return value;
+
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putOrderedObject
+ * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, java_lang_Object *x)
+{
+       java_handle_t  *_h;
+       java_handle_t  *_hx;
+       java_object_t  *_o;
+       java_object_t  *_x;
+       volatile void **_p;
+
+       _h  = (java_handle_t *) o;
+       _hx = (java_handle_t *) x;
+
+       LLNI_CRITICAL_START;
+
+       _o = LLNI_UNWRAP(_h);
+       _x = LLNI_UNWRAP(_hx);
+       _p = (volatile void **) (((uint8_t *) _o) + offset);
+
+       *_p = _x;
+
+       MEMORY_BARRIER();
+
+       LLNI_CRITICAL_END;
+}
+
+
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putOrderedInt
+ * Signature: (Ljava/lang/Object;JI)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedInt(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
+{
+       UNSAFE_PUT_VOLATILE(int32_t);
+}
+
+
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putOrderedLong
+ * Signature: (Ljava/lang/Object;JJ)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedLong(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t x)
+{
+       UNSAFE_PUT_VOLATILE(int64_t);
 }