12cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom/* 22cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Copyright (C) 2007 The Android Open Source Project 32cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 42cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 52cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * you may not use this file except in compliance with the License. 62cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * You may obtain a copy of the License at 72cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 82cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 92cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 112cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 122cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * See the License for the specific language governing permissions and 142cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * limitations under the License. 152cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 162cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 172cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrompackage sun.misc; 182cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 192cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstromimport dalvik.system.VMStack; 202cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstromimport java.lang.reflect.Field; 212cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstromimport java.lang.reflect.Modifier; 222cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 232cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom/** 242cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * The package name notwithstanding, this class is the quasi-standard 252cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * way for Java code to gain access to and use functionality which, 262cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * when unsupervised, would allow one to break the pointer/type safety 272cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * of Java. 282cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 292cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrompublic final class Unsafe { 302cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** Traditional dalvik name. */ 312cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom private static final Unsafe THE_ONE = new Unsafe(); 322cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** Traditional RI name. */ 332cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom private static final Unsafe theUnsafe = THE_ONE; 342cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 352cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 362cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * This class is only privately instantiable. 372cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 382cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom private Unsafe() {} 392cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 402cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 412cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets the unique instance of this class. This is only allowed in 422cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * very limited situations. 432cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 442cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public static Unsafe getUnsafe() { 452cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /* 462cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Only code on the bootclasspath is allowed to get at the 472cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Unsafe instance. 482cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 492cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom ClassLoader calling = VMStack.getCallingClassLoader(); 502cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if ((calling != null) && (calling != Unsafe.class.getClassLoader())) { 512cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new SecurityException("Unsafe access denied"); 522cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 532cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 542cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return THE_ONE; 552cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 562cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 572cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 582cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets the raw byte offset from the start of an object's memory to 592cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * the memory used to store the indicated instance field. 602cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 612cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param field non-null; the field in question, which must be an 622cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * instance field 632cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the offset to the field 642cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 652cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public long objectFieldOffset(Field field) { 662cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (Modifier.isStatic(field.getModifiers())) { 672cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new IllegalArgumentException("valid for instance fields only"); 682cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 692cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return field.getOffset(); 702cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 712cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 722cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 732cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets the offset from the start of an array object's memory to 742cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * the memory used to store its initial (zeroeth) element. 752cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 762cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param clazz non-null; class in question; must be an array class 772cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the offset to the initial element 782cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 792cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public int arrayBaseOffset(Class clazz) { 802cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Class<?> component = clazz.getComponentType(); 812cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (component == null) { 822cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new IllegalArgumentException("Valid for array classes only: " + clazz); 832cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 842cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom // TODO: make the following not specific to the object model. 852cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom int offset = 12; 862cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (component == long.class || component == double.class) { 872cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom offset += 4; // 4 bytes of padding. 882cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 892cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return offset; 902cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 912cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 922cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 932cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets the size of each element of the given array class. 942cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 952cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param clazz non-null; class in question; must be an array class 962cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return > 0; the size of each element of the array 972cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 982cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public int arrayIndexScale(Class clazz) { 992cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Class<?> component = clazz.getComponentType(); 1002cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (component == null) { 1012cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new IllegalArgumentException("Valid for array classes only: " + clazz); 1022cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1032cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom // TODO: make the following not specific to the object model. 1042239fc1b8955313cb880a53a73447daeb89e6f11Ian Rogers if (!component.isPrimitive()) { 1052cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return 4; 1062cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else if (component == long.class || component == double.class) { 1072cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return 8; 1082cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else if (component == int.class || component == float.class) { 1092cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return 4; 1102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else if (component == char.class || component == short.class) { 1112cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return 2; 1122cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else { 1132cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom // component == byte.class || component == boolean.class. 1142cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return 1; 1152cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1162cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1172cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1182cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1192cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Performs a compare-and-set operation on an <code>int</code> 1202cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * field within the given object. 1212cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1222cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1232cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1242cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param expectedValue expected value of the field 1252cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue new value to store in the field if the contents are 1262cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * as expected 1272cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return <code>true</code> if the new value was in fact stored, and 1282cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * <code>false</code> if not 1292cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1302cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native boolean compareAndSwapInt(Object obj, long offset, 1312cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom int expectedValue, int newValue); 1322cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1332cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1342cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Performs a compare-and-set operation on a <code>long</code> 1352cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * field within the given object. 1362cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1372cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1382cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1392cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param expectedValue expected value of the field 1402cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue new value to store in the field if the contents are 1412cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * as expected 1422cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return <code>true</code> if the new value was in fact stored, and 1432cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * <code>false</code> if not 1442cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1452cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native boolean compareAndSwapLong(Object obj, long offset, 1462cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom long expectedValue, long newValue); 1472cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1482cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1492cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Performs a compare-and-set operation on an <code>Object</code> 1502cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * field (that is, a reference field) within the given object. 1512cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1522cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1532cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1542cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param expectedValue expected value of the field 1552cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue new value to store in the field if the contents are 1562cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * as expected 1572cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return <code>true</code> if the new value was in fact stored, and 1582cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * <code>false</code> if not 1592cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1602cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native boolean compareAndSwapObject(Object obj, long offset, 1612cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Object expectedValue, Object newValue); 1622cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1632cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1642cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets an <code>int</code> field from the given object, 1652cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 1662cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1672cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1682cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1692cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 1702cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1712cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native int getIntVolatile(Object obj, long offset); 1722cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1732cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1742cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores an <code>int</code> field into the given object, 1752cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 1762cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1772cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1782cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1792cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 1802cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1812cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putIntVolatile(Object obj, long offset, int newValue); 1822cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1832cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1842cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets a <code>long</code> field from the given object, 1852cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 1862cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1872cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1882cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1892cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 1902cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 1912cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native long getLongVolatile(Object obj, long offset); 1922cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1932cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 1942cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores a <code>long</code> field into the given object, 1952cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 1962cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 1972cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 1982cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 1992cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 2002cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2012cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putLongVolatile(Object obj, long offset, long newValue); 2022cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2032cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2042cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets an <code>Object</code> field from the given object, 2052cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 2062cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2072cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2082cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2092cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 2102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2112cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native Object getObjectVolatile(Object obj, long offset); 2122cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2132cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2142cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores an <code>Object</code> field into the given object, 2152cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * using <code>volatile</code> semantics. 2162cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2172cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2182cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2192cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 2202cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2212cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putObjectVolatile(Object obj, long offset, 2222cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Object newValue); 2232cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2242cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2252cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets an <code>int</code> field from the given object. 2262cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2272cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2282cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2292cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 2302cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2312cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native int getInt(Object obj, long offset); 2322cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2332cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2342cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores an <code>int</code> field into the given object. 2352cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2362cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2372cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2382cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 2392cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2402cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putInt(Object obj, long offset, int newValue); 2412cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2422cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2432cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Lazy set an int field. 2442cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2452cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putOrderedInt(Object obj, long offset, int newValue); 2462cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2472cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2482cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets a <code>long</code> field from the given object. 2492cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2502cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2512cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2522cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 2532cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2542cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native long getLong(Object obj, long offset); 2552cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2562cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2572cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores a <code>long</code> field into the given object. 2582cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2592cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2602cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2612cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 2622cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2632cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putLong(Object obj, long offset, long newValue); 2642cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2652cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2662cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Lazy set a long field. 2672cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2682cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putOrderedLong(Object obj, long offset, long newValue); 2692cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2702cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2712cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Gets an <code>Object</code> field from the given object. 2722cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2732cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2742cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2752cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @return the retrieved value 2762cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2772cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native Object getObject(Object obj, long offset); 2782cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2792cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2802cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Stores an <code>Object</code> field into the given object. 2812cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 2822cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; object containing the field 2832cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param offset offset to the field within <code>obj</code> 2842cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param newValue the value to store 2852cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2862cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putObject(Object obj, long offset, Object newValue); 2872cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2882cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2892cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Lazy set an object field. 2902cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 2912cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native void putOrderedObject(Object obj, long offset, 2922cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Object newValue); 2932cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 2942cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 2952cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Parks the calling thread for the specified amount of time, 2962cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * unless the "permit" for the thread is already available (due to 2972cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * a previous call to {@link #unpark}. This method may also return 2982cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * spuriously (that is, without the thread being told to unpark 2992cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * and without the indicated amount of time elapsing). 3002cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 3012cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * <p>See {@link java.util.concurrent.locks.LockSupport} for more 3022cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * in-depth information of the behavior of this method.</p> 3032cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 3042cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param absolute whether the given time value is absolute 3052cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * milliseconds-since-the-epoch (<code>true</code>) or relative 3062cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * nanoseconds-from-now (<code>false</code>) 3072cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param time the (absolute millis or relative nanos) time value 3082cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 3092cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public void park(boolean absolute, long time) { 3102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (absolute) { 3112cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Thread.currentThread().parkUntil(time); 3122cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else { 3132cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom Thread.currentThread().parkFor(time); 3142cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 3152cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 3162cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 3172cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 3182cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Unparks the given object, which must be a {@link Thread}. 3192cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 3202cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * <p>See {@link java.util.concurrent.locks.LockSupport} for more 3212cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * in-depth information of the behavior of this method.</p> 3222cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 3232cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * @param obj non-null; the object to unpark 3242cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 3252cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public void unpark(Object obj) { 3262cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom if (obj instanceof Thread) { 3272cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom ((Thread) obj).unpark(); 3282cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } else { 3292cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new IllegalArgumentException("valid for Threads only"); 3302cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 3312cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 3322cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 3332cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 3342cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Allocates an instance of the given class without running the constructor. 3352cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * The class' <clinit> will be run, if necessary. 3362cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 3372cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public native Object allocateInstance(Class<?> c); 3382cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom} 339