Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 17 | #include "atomic.h" |
| 18 | #include "gc/card_table-inl.h" |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "mirror/object.h" |
| 21 | #include "mirror/object-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 26 | static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint expectedValue, jint newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 27 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 29 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 30 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 31 | // Note: android_atomic_release_cas() returns 0 on success, not failure. |
| 32 | int result = android_atomic_release_cas(expectedValue, newValue, address); |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 33 | return (result == 0) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 36 | static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong expectedValue, jlong newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 39 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 40 | volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr); |
| 41 | // Note: android_atomic_cmpxchg() returns 0 on success, not failure. |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 42 | bool success = QuasiAtomic::Cas64(expectedValue, newValue, address); |
| 43 | return success ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 46 | static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaExpectedValue, jobject javaNewValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 49 | mirror::Object* expectedValue = soa.Decode<mirror::Object*>(javaExpectedValue); |
| 50 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 51 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 52 | int32_t* address = reinterpret_cast<int32_t*>(raw_addr); |
| 53 | // Note: android_atomic_cmpxchg() returns 0 on success, not failure. |
| 54 | int result = android_atomic_release_cas(reinterpret_cast<int32_t>(expectedValue), |
| 55 | reinterpret_cast<int32_t>(newValue), address); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 56 | if (result == 0) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 57 | Runtime::Current()->GetHeap()->WriteBarrierField(obj, MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 58 | } |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 59 | return (result == 0) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 62 | static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 65 | return obj->GetField32(MemberOffset(offset), false); |
| 66 | } |
| 67 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 68 | static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 3eace70 | 2012-08-16 10:44:47 -0700 | [diff] [blame] | 71 | return obj->GetField32(MemberOffset(offset), true); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 74 | static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 77 | obj->SetField32(MemberOffset(offset), newValue, false); |
| 78 | } |
| 79 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 80 | static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 81 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 3eace70 | 2012-08-16 10:44:47 -0700 | [diff] [blame] | 83 | obj->SetField32(MemberOffset(offset), newValue, true); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 86 | static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 88 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 89 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 90 | obj->SetField32(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 93 | static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 94 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 3eace70 | 2012-08-16 10:44:47 -0700 | [diff] [blame] | 96 | return obj->GetField64(MemberOffset(offset), false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 99 | static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 100 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 102 | return obj->GetField64(MemberOffset(offset), true); |
| 103 | } |
| 104 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 105 | static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 106 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 108 | obj->SetField64(MemberOffset(offset), newValue, false); |
| 109 | } |
| 110 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 111 | static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 112 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 114 | obj->SetField64(MemberOffset(offset), newValue, true); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 117 | static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 120 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 121 | obj->SetField64(MemberOffset(offset), newValue, false); |
| 122 | } |
| 123 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 124 | static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 127 | mirror::Object* value = obj->GetFieldObject<mirror::Object*>(MemberOffset(offset), true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 128 | return soa.AddLocalReference<jobject>(value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 131 | static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 132 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 134 | mirror::Object* value = obj->GetFieldObject<mirror::Object*>(MemberOffset(offset), false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | return soa.AddLocalReference<jobject>(value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 138 | static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 139 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 141 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 142 | obj->SetFieldObject(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 145 | static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 146 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 147 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 148 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 149 | obj->SetFieldObject(MemberOffset(offset), newValue, true); |
| 150 | } |
| 151 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 152 | static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 154 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 155 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 156 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 157 | obj->SetFieldObject(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 160 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 161 | NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"), |
| 162 | NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"), |
| 163 | NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"), |
| 164 | NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"), |
| 165 | NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"), |
| 166 | NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"), |
| 167 | NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"), |
| 168 | NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 169 | NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 170 | NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"), |
| 171 | NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"), |
| 172 | NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"), |
| 173 | NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"), |
| 174 | NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"), |
| 175 | NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"), |
| 176 | NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 177 | NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 178 | NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 179 | }; |
| 180 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 181 | void register_sun_misc_Unsafe(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 182 | REGISTER_NATIVE_METHODS("sun/misc/Unsafe"); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace art |