blob: 360f241f19a93915ed1333bff02c9008772cdc55 [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
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070020namespace art {
21
Elliott Hughes0512f022012-03-15 22:10:52 -070022static jlong Unsafe_objectFieldOffset0(JNIEnv* env, jclass, jobject javaField) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080023 // TODO: move to Java code
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070024 jfieldID fid = env->FromReflectedField(javaField);
25 Field* field = DecodeField(fid);
26 return field->GetOffset().Int32Value();
27}
28
Elliott Hughes0512f022012-03-15 22:10:52 -070029static jint Unsafe_arrayBaseOffset0(JNIEnv* env, jclass, jclass javaArrayClass) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080030 // TODO: move to Java code
Elliott Hughes34e06962012-04-09 13:55:55 -070031 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogersa15e67d2012-02-28 13:51:55 -080032 Class* array_class = Decode<Class*>(env, javaArrayClass);
33 return Array::DataOffset(array_class->GetComponentSize()).Int32Value();
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070034}
35
Elliott Hughes0512f022012-03-15 22:10:52 -070036static jint Unsafe_arrayIndexScale0(JNIEnv* env, jclass, jclass javaClass) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070037 Class* c = Decode<Class*>(env, javaClass);
38 return c->GetComponentSize();
39}
40
Elliott Hughes0512f022012-03-15 22:10:52 -070041static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint expectedValue, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070042 Object* obj = Decode<Object*>(env, javaObj);
43 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
44 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
45 // Note: android_atomic_release_cas() returns 0 on success, not failure.
46 int result = android_atomic_release_cas(expectedValue, newValue, address);
47 return (result == 0);
48}
49
Elliott Hughes0512f022012-03-15 22:10:52 -070050static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong expectedValue, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070051 Object* obj = Decode<Object*>(env, javaObj);
52 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
53 volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr);
54 // Note: android_atomic_cmpxchg() returns 0 on success, not failure.
Elliott Hughes7c6169d2012-05-02 16:11:48 -070055 int result = QuasiAtomic::Cas64(expectedValue, newValue, address);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070056 return (result == 0);
57}
58
Elliott Hughes0512f022012-03-15 22:10:52 -070059static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaExpectedValue, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070060 Object* obj = Decode<Object*>(env, javaObj);
61 Object* expectedValue = Decode<Object*>(env, javaExpectedValue);
62 Object* newValue = Decode<Object*>(env, javaNewValue);
63 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
64 int32_t* address = reinterpret_cast<int32_t*>(raw_addr);
65 // Note: android_atomic_cmpxchg() returns 0 on success, not failure.
66 int result = android_atomic_release_cas(reinterpret_cast<int32_t>(expectedValue),
67 reinterpret_cast<int32_t>(newValue), address);
Ian Rogers5d76c432011-10-31 21:42:49 -070068 if (result == 0) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080069 Runtime::Current()->GetHeap()->WriteBarrierField(obj, MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -070070 }
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070071 return (result == 0);
72}
73
Elliott Hughes0512f022012-03-15 22:10:52 -070074static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -070075 Object* obj = Decode<Object*>(env, javaObj);
76 return obj->GetField32(MemberOffset(offset), false);
77}
78
Elliott Hughes0512f022012-03-15 22:10:52 -070079static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070080 Object* obj = Decode<Object*>(env, javaObj);
81 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
82 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
83 return android_atomic_acquire_load(address);
84}
85
Elliott Hughes0512f022012-03-15 22:10:52 -070086static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -070087 Object* obj = Decode<Object*>(env, javaObj);
88 obj->SetField32(MemberOffset(offset), newValue, false);
89}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070092 Object* obj = Decode<Object*>(env, javaObj);
93 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
94 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
95 android_atomic_release_store(newValue, address);
96}
97
Elliott Hughes0512f022012-03-15 22:10:52 -070098static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070099 Object* obj = Decode<Object*>(env, javaObj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700100 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700101 obj->SetField32(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700105 Object* obj = Decode<Object*>(env, javaObj);
106 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
107 int64_t* address = reinterpret_cast<int64_t*>(raw_addr);
108 return *address;
109}
110
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700112 Object* obj = Decode<Object*>(env, javaObj);
113 return obj->GetField64(MemberOffset(offset), true);
114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700117 Object* obj = Decode<Object*>(env, javaObj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700118 obj->SetField64(MemberOffset(offset), newValue, false);
119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700122 Object* obj = Decode<Object*>(env, javaObj);
123 obj->SetField64(MemberOffset(offset), newValue, true);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700124}
125
Elliott Hughes0512f022012-03-15 22:10:52 -0700126static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700127 Object* obj = Decode<Object*>(env, javaObj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700128 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700129 obj->SetField64(MemberOffset(offset), newValue, false);
130}
131
Elliott Hughes0512f022012-03-15 22:10:52 -0700132static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700133 Object* obj = Decode<Object*>(env, javaObj);
134 Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), true);
135 return AddLocalReference<jobject>(env, value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700136}
137
Elliott Hughes0512f022012-03-15 22:10:52 -0700138static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700139 Object* obj = Decode<Object*>(env, javaObj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700140 Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), false);
141 return AddLocalReference<jobject>(env, value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700142}
143
Elliott Hughes0512f022012-03-15 22:10:52 -0700144static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700145 Object* obj = Decode<Object*>(env, javaObj);
146 Object* newValue = Decode<Object*>(env, javaNewValue);
Ian Rogers5d76c432011-10-31 21:42:49 -0700147 obj->SetFieldObject(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700148}
149
Elliott Hughes0512f022012-03-15 22:10:52 -0700150static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700151 Object* obj = Decode<Object*>(env, javaObj);
152 Object* newValue = Decode<Object*>(env, javaNewValue);
153 obj->SetFieldObject(MemberOffset(offset), newValue, true);
154}
155
Elliott Hughes0512f022012-03-15 22:10:52 -0700156static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700157 Object* obj = Decode<Object*>(env, javaObj);
158 Object* newValue = Decode<Object*>(env, javaNewValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700159 ANDROID_MEMBAR_STORE();
Ian Rogers5d76c432011-10-31 21:42:49 -0700160 obj->SetFieldObject(MemberOffset(offset), newValue, false);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700161}
162
Elliott Hughes0512f022012-03-15 22:10:52 -0700163static JNINativeMethod gMethods[] = {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700164 NATIVE_METHOD(Unsafe, objectFieldOffset0, "(Ljava/lang/reflect/Field;)J"),
165 NATIVE_METHOD(Unsafe, arrayBaseOffset0, "(Ljava/lang/Class;)I"),
166 NATIVE_METHOD(Unsafe, arrayIndexScale0, "(Ljava/lang/Class;)I"),
167 NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"),
168 NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"),
169 NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"),
170 NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"),
171 NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"),
172 NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"),
173 NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"),
174 NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
175 NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
176 NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"),
177 NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"),
178 NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"),
179 NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"),
180 NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"),
181 NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"),
182 NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
183 NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
184 NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
185};
186
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700187void register_sun_misc_Unsafe(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700188 REGISTER_NATIVE_METHODS("sun/misc/Unsafe");
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700189}
190
191} // namespace art