blob: d5d744447983b8e246b5059aa20e41346eb03e7d [file] [log] [blame]
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001/*
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
17#include "jni_internal.h"
18#include "object.h"
19
20#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
21
22namespace art {
23
Elliott Hughes0512f022012-03-15 22:10:52 -070024static jlong Unsafe_objectFieldOffset0(JNIEnv* env, jclass, jobject javaField) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080025 // TODO: move to Java code
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070026 jfieldID fid = env->FromReflectedField(javaField);
27 Field* field = DecodeField(fid);
28 return field->GetOffset().Int32Value();
29}
30
Elliott Hughes0512f022012-03-15 22:10:52 -070031static jint Unsafe_arrayBaseOffset0(JNIEnv* env, jclass, jclass javaArrayClass) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080032 // TODO: move to Java code
33 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
34 Class* array_class = Decode<Class*>(env, javaArrayClass);
35 return Array::DataOffset(array_class->GetComponentSize()).Int32Value();
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070036}
37
Elliott Hughes0512f022012-03-15 22:10:52 -070038static jint Unsafe_arrayIndexScale0(JNIEnv* env, jclass, jclass javaClass) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070039 Class* c = Decode<Class*>(env, javaClass);
40 return c->GetComponentSize();
41}
42
Elliott Hughes0512f022012-03-15 22:10:52 -070043static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint expectedValue, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070044 Object* obj = Decode<Object*>(env, javaObj);
45 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
46 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
47 // Note: android_atomic_release_cas() returns 0 on success, not failure.
48 int result = android_atomic_release_cas(expectedValue, newValue, address);
49 return (result == 0);
50}
51
Elliott Hughes0512f022012-03-15 22:10:52 -070052static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong expectedValue, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070053 Object* obj = Decode<Object*>(env, javaObj);
54 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
55 volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr);
56 // Note: android_atomic_cmpxchg() returns 0 on success, not failure.
57 int result = QuasiAtomicCas64(expectedValue, newValue, address);
58 return (result == 0);
59}
60
Elliott Hughes0512f022012-03-15 22:10:52 -070061static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaExpectedValue, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070062 Object* obj = Decode<Object*>(env, javaObj);
63 Object* expectedValue = Decode<Object*>(env, javaExpectedValue);
64 Object* newValue = Decode<Object*>(env, javaNewValue);
65 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
66 int32_t* address = reinterpret_cast<int32_t*>(raw_addr);
67 // Note: android_atomic_cmpxchg() returns 0 on success, not failure.
68 int result = android_atomic_release_cas(reinterpret_cast<int32_t>(expectedValue),
69 reinterpret_cast<int32_t>(newValue), address);
Ian Rogers5d76c432011-10-31 21:42:49 -070070 if (result == 0) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080071 Runtime::Current()->GetHeap()->WriteBarrierField(obj, MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -070072 }
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070073 return (result == 0);
74}
75
Elliott Hughes0512f022012-03-15 22:10:52 -070076static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -070077 Object* obj = Decode<Object*>(env, javaObj);
78 return obj->GetField32(MemberOffset(offset), false);
79}
80
Elliott Hughes0512f022012-03-15 22:10:52 -070081static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070082 Object* obj = Decode<Object*>(env, javaObj);
83 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
84 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
85 return android_atomic_acquire_load(address);
86}
87
Elliott Hughes0512f022012-03-15 22:10:52 -070088static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -070089 Object* obj = Decode<Object*>(env, javaObj);
90 obj->SetField32(MemberOffset(offset), newValue, false);
91}
92
Elliott Hughes0512f022012-03-15 22:10:52 -070093static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070094 Object* obj = Decode<Object*>(env, javaObj);
95 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
96 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
97 android_atomic_release_store(newValue, address);
98}
99
Elliott Hughes0512f022012-03-15 22:10:52 -0700100static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700101 Object* obj = Decode<Object*>(env, javaObj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700102 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700103 obj->SetField32(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700104}
105
Elliott Hughes0512f022012-03-15 22:10:52 -0700106static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700107 Object* obj = Decode<Object*>(env, javaObj);
108 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
109 int64_t* address = reinterpret_cast<int64_t*>(raw_addr);
110 return *address;
111}
112
Elliott Hughes0512f022012-03-15 22:10:52 -0700113static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700114 Object* obj = Decode<Object*>(env, javaObj);
115 return obj->GetField64(MemberOffset(offset), true);
116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700119 Object* obj = Decode<Object*>(env, javaObj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700120 obj->SetField64(MemberOffset(offset), newValue, false);
121}
122
Elliott Hughes0512f022012-03-15 22:10:52 -0700123static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700124 Object* obj = Decode<Object*>(env, javaObj);
125 obj->SetField64(MemberOffset(offset), newValue, true);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700126}
127
Elliott Hughes0512f022012-03-15 22:10:52 -0700128static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700129 Object* obj = Decode<Object*>(env, javaObj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700130 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700131 obj->SetField64(MemberOffset(offset), newValue, false);
132}
133
Elliott Hughes0512f022012-03-15 22:10:52 -0700134static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700135 Object* obj = Decode<Object*>(env, javaObj);
136 Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), true);
137 return AddLocalReference<jobject>(env, value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700138}
139
Elliott Hughes0512f022012-03-15 22:10:52 -0700140static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700141 Object* obj = Decode<Object*>(env, javaObj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700142 Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), false);
143 return AddLocalReference<jobject>(env, value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700144}
145
Elliott Hughes0512f022012-03-15 22:10:52 -0700146static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700147 Object* obj = Decode<Object*>(env, javaObj);
148 Object* newValue = Decode<Object*>(env, javaNewValue);
Ian Rogers5d76c432011-10-31 21:42:49 -0700149 obj->SetFieldObject(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700150}
151
Elliott Hughes0512f022012-03-15 22:10:52 -0700152static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700153 Object* obj = Decode<Object*>(env, javaObj);
154 Object* newValue = Decode<Object*>(env, javaNewValue);
155 obj->SetFieldObject(MemberOffset(offset), newValue, true);
156}
157
Elliott Hughes0512f022012-03-15 22:10:52 -0700158static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700159 Object* obj = Decode<Object*>(env, javaObj);
160 Object* newValue = Decode<Object*>(env, javaNewValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700161 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700162 obj->SetFieldObject(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700163}
164
Elliott Hughes0512f022012-03-15 22:10:52 -0700165static JNINativeMethod gMethods[] = {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700166 NATIVE_METHOD(Unsafe, objectFieldOffset0, "(Ljava/lang/reflect/Field;)J"),
167 NATIVE_METHOD(Unsafe, arrayBaseOffset0, "(Ljava/lang/Class;)I"),
168 NATIVE_METHOD(Unsafe, arrayIndexScale0, "(Ljava/lang/Class;)I"),
169 NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"),
170 NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"),
171 NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"),
172 NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"),
173 NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"),
174 NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"),
175 NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"),
176 NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
177 NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
178 NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"),
179 NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"),
180 NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"),
181 NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"),
182 NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"),
183 NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"),
184 NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
185 NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
186 NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
187};
188
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700189void register_sun_misc_Unsafe(JNIEnv* env) {
190 jniRegisterNativeMethods(env, "sun/misc/Unsafe", gMethods, NELEM(gMethods));
191}
192
193} // namespace art