From 43269e32e5c79a1d96ede9e29102a5aaf74ebf3a Mon Sep 17 00:00:00 2001 From: Christian Thalinger Date: Wed, 3 Sep 2008 11:00:26 +0200 Subject: [PATCH] * src/vm/javaobjects.hpp (FieldAccess::get_volatile): New function. (FieldAccess::set_volatile): Likewise. * src/native/vm/sun_misc_Unsafe.cpp: Use FieldAccess functions for get and put methods. --- src/native/vm/sun_misc_Unsafe.cpp | 228 ++++-------------------------- src/vm/javaobjects.hpp | 98 +++++++++---- 2 files changed, 103 insertions(+), 223 deletions(-) diff --git a/src/native/vm/sun_misc_Unsafe.cpp b/src/native/vm/sun_misc_Unsafe.cpp index 6202e6814..1117cd851 100644 --- a/src/native/vm/sun_misc_Unsafe.cpp +++ b/src/native/vm/sun_misc_Unsafe.cpp @@ -72,14 +72,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_registerNatives(JNIEnv *env, jclass */ JNIEXPORT jint JNICALL Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int32_t *p; - int32_t value; - - p = (int32_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -90,11 +83,7 @@ JNIEXPORT jint JNICALL Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J(JNIEnv */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI(JNIEnv *env, jobject _this, jobject o, jlong offset, jint x) { - int32_t *p; - - p = (int32_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, x); } @@ -105,14 +94,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI(JNIEnv */ JNIEXPORT jobject JNICALL Java_sun_misc_Unsafe_getObject(JNIEnv *env, jobject _this, jobject o, jlong offset) { - void **p; - void *value; - - p = (void **) (((uint8_t *) o) + offset); - - value = *p; - - return (jobject) value; + return FieldAccess::get(o, offset); } @@ -123,11 +105,7 @@ JNIEXPORT jobject JNICALL Java_sun_misc_Unsafe_getObject(JNIEnv *env, jobject _t */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObject(JNIEnv *env, jobject _this, jobject o, jlong offset, jobject x) { - void **p; - - p = (void **) (((uint8_t *) o) + offset); - - *p = (void *) x; + FieldAccess::set(o, offset, x); } @@ -138,14 +116,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObject(JNIEnv *env, jobject _this */ JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_getBoolean(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int32_t *p; - int32_t value; - - p = (int32_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -156,11 +127,7 @@ JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_getBoolean(JNIEnv *env, jobject */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putBoolean(JNIEnv *env, jobject _this, jobject o, jlong offset, jboolean x) { - int32_t *p; - - p = (int32_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, (int32_t) x); } @@ -171,14 +138,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putBoolean(JNIEnv *env, jobject _thi */ JNIEXPORT jbyte JNICALL Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int32_t *p; - int32_t value; - - p = (int32_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -189,11 +149,7 @@ JNIEXPORT jbyte JNICALL Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J(JNIEn */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB(JNIEnv *env, jobject _this, jobject o, jlong offset, jbyte x) { - int32_t *p; - - p = (int32_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, (int32_t) x); } @@ -204,14 +160,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB(JNIEn */ JNIEXPORT jshort JNICALL Java_sun_misc_Unsafe_getShort__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int32_t *p; - int32_t value; - - p = (int32_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -222,11 +171,7 @@ JNIEXPORT jshort JNICALL Java_sun_misc_Unsafe_getShort__Ljava_lang_Object_2J(JNI */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS(JNIEnv *env, jobject _this, jobject o, jlong offset, jshort x) { - int32_t *p; - - p = (int32_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, (int32_t) x); } @@ -237,14 +182,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS(JNIE */ JNIEXPORT jchar JNICALL Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int32_t *p; - int32_t value; - - p = (int32_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -255,11 +193,7 @@ JNIEXPORT jchar JNICALL Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J(JNIEn */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC(JNIEnv *env, jobject _this, jobject o, jlong offset, jchar x) { - int32_t *p; - - p = (int32_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, (int32_t) x); } @@ -270,14 +204,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC(JNIEn */ JNIEXPORT jlong JNICALL Java_sun_misc_Unsafe_getLong__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - int64_t *p; - int64_t value; - - p = (int64_t *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -288,11 +215,7 @@ JNIEXPORT jlong JNICALL Java_sun_misc_Unsafe_getLong__Ljava_lang_Object_2J(JNIEn */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__Ljava_lang_Object_2JJ(JNIEnv *env, jobject _this, jobject o, jlong offset, jlong x) { - int64_t *p; - - p = (int64_t *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, x); } @@ -303,14 +226,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__Ljava_lang_Object_2JJ(JNIEn */ JNIEXPORT jfloat JNICALL Java_sun_misc_Unsafe_getFloat__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - float *p; - float value; - - p = (float *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -321,11 +237,7 @@ JNIEXPORT jfloat JNICALL Java_sun_misc_Unsafe_getFloat__Ljava_lang_Object_2J(JNI */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF(JNIEnv *env, jobject _this, jobject o, jlong offset, jfloat x) { - float *p; - - p = (float *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, x); } @@ -336,14 +248,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF(JNIE */ JNIEXPORT jdouble JNICALL Java_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J(JNIEnv *env, jobject _this, jobject o, jlong offset) { - double *p; - double value; - - p = (double *) (((uint8_t *) o) + offset); - - value = *p; - - return value; + return FieldAccess::get(o, offset); } @@ -354,11 +259,7 @@ JNIEXPORT jdouble JNICALL Java_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J(J */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putDouble__Ljava_lang_Object_2JD(JNIEnv *env, jobject _this, jobject o, jlong offset, jdouble x) { - double *p; - - p = (double *) (((uint8_t *) o) + offset); - - *p = x; + FieldAccess::set(o, offset, x); } @@ -1034,14 +935,7 @@ JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_compareAndSwapLong(JNIEnv *env, */ JNIEXPORT jobject JNICALL Java_sun_misc_Unsafe_getObjectVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset) { - volatile void **p; - volatile void *value; - - p = (volatile void **) (((uint8_t *) o) + offset); - - value = *p; - - return (jobject ) value; + return FieldAccess::get_volatile(o, offset); } @@ -1052,51 +946,8 @@ JNIEXPORT jobject JNICALL Java_sun_misc_Unsafe_getObjectVolatile(JNIEnv *env, jo */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset, jobject x) { - volatile void **p; - - p = (volatile void **) (((uint8_t *) o) + offset); - - *p = x; -} - - -#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; \ - \ - Atomic::memory_barrier(); \ - \ - LLNI_CRITICAL_END; + FieldAccess::set_volatile(o, offset, x); +} /* @@ -1106,7 +957,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, jobje */ JNIEXPORT jint JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset) { - UNSAFE_GET_VOLATILE(int32_t); + return FieldAccess::get_volatile(o, offset); } @@ -1117,7 +968,7 @@ JNIEXPORT jint JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, jobject */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putIntVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset, jint x) { - UNSAFE_PUT_VOLATILE(int32_t); + FieldAccess::set_volatile(o, offset, x); } @@ -1128,7 +979,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putIntVolatile(JNIEnv *env, jobject */ JNIEXPORT jlong JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset) { - UNSAFE_GET_VOLATILE(int64_t); + return FieldAccess::get_volatile(o, offset); } @@ -1139,7 +990,7 @@ JNIEXPORT jlong JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, jobjec */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLongVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset, jlong x) { - UNSAFE_PUT_VOLATILE(int64_t); + FieldAccess::set_volatile(o, offset, x); } @@ -1150,7 +1001,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLongVolatile(JNIEnv *env, jobject */ JNIEXPORT jfloat JNICALL Java_sun_misc_Unsafe_getFloatVolatile(JNIEnv* env, jobject _this, jobject o, jlong offset) { - UNSAFE_GET_VOLATILE(float); + return FieldAccess::get_volatile(o, offset); } @@ -1161,7 +1012,7 @@ JNIEXPORT jfloat JNICALL Java_sun_misc_Unsafe_getFloatVolatile(JNIEnv* env, jobj */ JNIEXPORT jdouble JNICALL Java_sun_misc_Unsafe_getDoubleVolatile(JNIEnv *env, jobject _this, jobject o, jlong offset) { - UNSAFE_GET_VOLATILE(double); + return FieldAccess::get_volatile(o, offset); } @@ -1172,26 +1023,7 @@ JNIEXPORT jdouble JNICALL Java_sun_misc_Unsafe_getDoubleVolatile(JNIEnv *env, jo */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedObject(JNIEnv *env, jobject _this, jobject o, jlong offset, jobject 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; - - Atomic::memory_barrier(); - - LLNI_CRITICAL_END; + FieldAccess::set_volatile(o, offset, x); } @@ -1202,7 +1034,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedObject(JNIEnv *env, jobjec */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedInt(JNIEnv *env, jobject _this, jobject o, jlong offset, jint x) { - UNSAFE_PUT_VOLATILE(int32_t); + FieldAccess::set_volatile(o, offset, x); } @@ -1213,7 +1045,7 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedInt(JNIEnv *env, jobject _ */ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedLong(JNIEnv *env, jobject _this, jobject o, jlong offset, jlong x) { - UNSAFE_PUT_VOLATILE(int64_t); + FieldAccess::set_volatile(o, offset, x); } diff --git a/src/vm/javaobjects.hpp b/src/vm/javaobjects.hpp index 0b4aa46e9..068ac5831 100644 --- a/src/vm/javaobjects.hpp +++ b/src/vm/javaobjects.hpp @@ -33,6 +33,8 @@ #include "native/llni.h" +#include "threads/atomic.hpp" + #include "vm/class.h" #include "vm/field.hpp" #include "vm/global.h" @@ -77,22 +79,24 @@ template inline void RawFieldAccess::raw_set(void* address, const off_t * afterwards. */ class FieldAccess : private RawFieldAccess { -protected: +public: + // Normal field accessors. template static inline T get(java_handle_t* h, const off_t offset); template static inline void set(java_handle_t* h, const off_t offset, T value); + + // Volatile field accessors. + template static inline T get_volatile(java_handle_t* h, const off_t offset); + template static inline void set_volatile(java_handle_t* h, const off_t offset, T value); }; + template inline T FieldAccess::get(java_handle_t* h, const off_t offset) { - java_object_t* o; - T result; - GC::critical_enter(); // XXX This should be _handle->get_object(); - o = LLNI_UNWRAP(h); - - result = raw_get(o, offset); + java_object_t* ho = LLNI_UNWRAP(h); + T result = raw_get(ho, offset); GC::critical_leave(); @@ -101,18 +105,12 @@ template inline T FieldAccess::get(java_handle_t* h, const off_t offset template<> inline java_handle_t* FieldAccess::get(java_handle_t* h, const off_t offset) { - java_object_t* o; - java_object_t* result; - java_handle_t* hresult; - GC::critical_enter(); // XXX This should be _handle->get_object(); - o = LLNI_UNWRAP(h); - - result = raw_get(o, offset); - - hresult = LLNI_WRAP(result); + java_object_t* o = LLNI_UNWRAP(h); + java_object_t* result = raw_get(o, offset); + java_handle_t* hresult = LLNI_WRAP(result); GC::critical_leave(); @@ -122,30 +120,80 @@ template<> inline java_handle_t* FieldAccess::get(java_handle_t* h, const off_t template inline void FieldAccess::set(java_handle_t* h, const off_t offset, T value) { - java_object_t* o; + GC::critical_enter(); + java_object_t* ho = LLNI_UNWRAP(h); + raw_set(ho, offset, value); + + GC::critical_leave(); +} + +template<> inline void FieldAccess::set(java_handle_t* h, const off_t offset, java_handle_t* value) +{ GC::critical_enter(); // XXX This should be h->get_object(); - o = LLNI_UNWRAP(h); + java_object_t* o = LLNI_UNWRAP(h); + java_object_t* ovalue = LLNI_UNWRAP(value); - raw_set(o, offset, value); + raw_set(o, offset, ovalue); GC::critical_leave(); } -template<> inline void FieldAccess::set(java_handle_t* h, const off_t offset, java_handle_t* value) + +template inline T FieldAccess::get_volatile(java_handle_t* h, const off_t offset) { - java_object_t* o; - java_object_t* ovalue; + GC::critical_enter(); + + // XXX This should be _handle->get_object(); + java_object_t* ho = LLNI_UNWRAP(h); + T result = raw_get(ho, offset); + GC::critical_leave(); + + return result; +} + +template<> inline java_handle_t* FieldAccess::get_volatile(java_handle_t* h, const off_t offset) +{ + GC::critical_enter(); + + // XXX This should be _handle->get_object(); + java_object_t* o = LLNI_UNWRAP(h); + java_object_t* result = (java_object_t*) raw_get(o, offset); + java_handle_t* hresult = LLNI_WRAP(result); + + GC::critical_leave(); + + return hresult; +} + + +template inline void FieldAccess::set_volatile(java_handle_t* h, const off_t offset, T value) +{ + GC::critical_enter(); + + java_object_t* ho = LLNI_UNWRAP(h); + raw_set(ho, offset, (volatile T) value); + + // Memory barrier for the Java Memory Model. + Atomic::memory_barrier(); + + GC::critical_leave(); +} + +template<> inline void FieldAccess::set_volatile(java_handle_t* h, const off_t offset, java_handle_t* value) +{ GC::critical_enter(); // XXX This should be h->get_object(); - o = LLNI_UNWRAP(h); - ovalue = LLNI_UNWRAP(value); + java_object_t* o = LLNI_UNWRAP(h); + java_object_t* ovalue = LLNI_UNWRAP(value); + raw_set(o, offset, (volatile java_object_t*) ovalue); - raw_set(o, offset, ovalue); + // Memory barrier for the Java Memory Model. + Atomic::memory_barrier(); GC::critical_leave(); } -- 2.25.1