/** Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.*/
packagesun.misc;importdalvik.system.VMStack;importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;/*** The package name notwithstanding, this class is the quasi-standard
* way for Java code to gain access to and use functionality which,
* when unsupervised, would allow one to break the pointer/type safety
* of Java.
*
* 该包名不具有代表性.可以通过该类来获取和访问数据,但是容易破坏Java指针和类型的安全性。*/
public final classUnsafe {/*** Traditional dalvik name.
* 内部维护一个静态的 Unsafe 实例*/
private static final Unsafe THE_ONE = newUnsafe();/*** Traditional RI name.*/
private static final Unsafe theUnsafe =THE_ONE;/*** This class is only privately instantiable.
*
* 说明该类不能被new 出来*/
privateUnsafe() {
}/*** Gets the unique instance of this class. This is only allowed in
* very limited situations.
*
* 静态方法,通过getUnsafe方法获取一个Unsafe 实例*/
public staticUnsafe getUnsafe() {/** Only code on the bootclasspath is allowed to get at the
* Unsafe instance.
*
* 只有在 bootclasspath下的Java类才能获取得到该实例,否则抛异常。*/ClassLoader calling=VMStack.getCallingClassLoader();if ((calling != null) && (calling != Unsafe.class.getClassLoader())) {throw new SecurityException("Unsafe access denied");
}returnTHE_ONE;
}/*** Gets the raw byte offset from the start of an object's memory to
* the memory used to store the indicated instance field.
*
* 获取从对象内存开头到用于存储指示实例字段的内存的原始字节偏移量。也就是
* 获取某对象内的变量的相对地址偏移量
*
*@paramfield non-null; the field in question, which must be an
* instance field
*@returnthe offset to the field*/
public longobjectFieldOffset(Field field) {if(Modifier.isStatic(field.getModifiers())) {throw newIllegalArgumentException("valid for instance fields only");
}returnobjectFieldOffset0(field);
}/*** Helper for {@link#objectFieldOffset}, which does all the work,
* assuming the parameter is deemed valid.
*
* 本地方法,获取对象属性的偏移量
*
*@paramfield non-null; the instance field
*@returnthe offset to the field*/
private static native longobjectFieldOffset0(Field field);/*** Gets the offset from the start of an array object's memory to
* the memory used to store its initial (zeroeth) element.
*
* 获取数组对象的起始位置
*
*@paramclazz non-null; class in question; must be an array class
*@returnthe offset to the initial element*/
public intarrayBaseOffset(Class clazz) {if (!clazz.isArray()) {throw newIllegalArgumentException("valid for array classes only");
}returnarrayBaseOffset0(clazz);
}/*** Helper for {@link#arrayBaseOffset}, which does all the work,
* assuming the parameter is deemed valid.
*
*@returnthe offset to the field*/
private static native intarrayBaseOffset0(Class clazz);/*** Gets the size of each element of the given array class.
*
* 获取给定数组类的每个元素的大小
*
*@paramclazz non-null; class in question; must be an array class
*@return> 0; the size of each element of the array*/
public intarrayIndexScale(Class clazz) {if (!clazz.isArray()) {throw newIllegalArgumentException("valid for array classes only");
}returnarrayIndexScale0(clazz);
}/*** Helper for {@link#arrayIndexScale}, which does all the work,
* assuming the parameter is deemed valid.
*
*@returnthe offset to the field*/
private static native intarrayIndexScale0(Class clazz);/*** Performs a compare-and-set operation on an int
* field within the given object.
*
* 比较置换操作(CAS)for int
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramexpectedValue expected value of the field
*@paramnewValue new value to store in the field if the contents are
* as expected
*@returntrue
if the new value was in fact stored, and
* false
if not*/
public native boolean compareAndSwapInt(Object obj, longoffset,int expectedValue, intnewValue);/*** Performs a compare-and-set operation on a long
* field within the given object.
*
* 比较置换操作(CAS)for long
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramexpectedValue expected value of the field
*@paramnewValue new value to store in the field if the contents are
* as expected
*@returntrue
if the new value was in fact stored, and
* false
if not*/
public native boolean compareAndSwapLong(Object obj, longoffset,long expectedValue, longnewValue);/*** Performs a compare-and-set operation on an Object
* field (that is, a reference field) within the given object.
*
* 比较置换操作(CAS) for object
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramexpectedValue expected value of the field
*@paramnewValue new value to store in the field if the contents are
* as expected
*@returntrue
if the new value was in fact stored, and
* false
if not*/
public native boolean compareAndSwapObject(Object obj, longoffset,
Object expectedValue, Object newValue);/*** Gets an int
field from the given object,
* using volatile
semantics.
* 获取 对象 obj 偏移量offset 的值 (int)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native int getIntVolatile(Object obj, longoffset);/*** Stores an int
field into the given object,
* using volatile
semantics.
*
* 把值newValue 放到对象obj 偏移量为offset 上 (int)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putIntVolatile(Object obj, long offset, intnewValue);/*** Gets a long
field from the given object,
* using volatile
semantics.
*
* 获取 对象 obj 偏移量offset 的值 (long)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native long getLongVolatile(Object obj, longoffset);/*** Stores a long
field into the given object,
* using volatile
semantics.
*
* 把值newValue 放到对象obj 偏移量为offset 上 (long)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putLongVolatile(Object obj, long offset, longnewValue);/*** Gets an Object
field from the given object,
* using volatile
semantics.
* 获取 对象 obj 偏移量offset 的值 (object)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native Object getObjectVolatile(Object obj, longoffset);/*** Stores an Object
field into the given object,
* using volatile
semantics.
*
* 把值newValue 放到对象obj 偏移量为offset 上 (object)
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putObjectVolatile(Object obj, longoffset,
Object newValue);/*** Gets an int
field from the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native int getInt(Object obj, longoffset);/*** Stores an int
field into the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putInt(Object obj, long offset, intnewValue);/*** Lazy set an int field.*/
public native void putOrderedInt(Object obj, long offset, intnewValue);/*** Gets a long
field from the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native long getLong(Object obj, longoffset);/*** Stores a long
field into the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putLong(Object obj, long offset, longnewValue);/*** Lazy set a long field.*/
public native void putOrderedLong(Object obj, long offset, longnewValue);/*** Gets an Object
field from the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@returnthe retrieved value*/
public native Object getObject(Object obj, longoffset);/*** Stores an Object
field into the given object.
*
*@paramobj non-null; object containing the field
*@paramoffset offset to the field within obj
*@paramnewValue the value to store*/
public native void putObject(Object obj, longoffset, Object newValue);/*** Lazy set an object field.*/
public native void putOrderedObject(Object obj, longoffset,
Object newValue);/*** Parks the calling thread for the specified amount of time,
* unless the "permit" for the thread is already available (due to
* a previous call to {@link#unpark}. This method may also return
* spuriously (that is, without the thread being told to unpark
* and without the indicated amount of time elapsing).
*
*
See {@linkjava.util.concurrent.locks.LockSupport} for more
* in-depth information of the behavior of this method.
*
* 除非当前线程已经可用(比如调用unpark),否则挂起当前线程time 时间,
*
*@paramabsolute whether the given time value is absolute
* milliseconds-since-the-epoch (true
) or relative
* nanoseconds-from-now (false
)
*@paramtime the (absolute millis or relative nanos) time value*/
public void park(boolean absolute, longtime) {if(absolute) {
Thread.currentThread().parkUntil(time);
}else{
Thread.currentThread().parkFor(time);
}
}/*** Unparks the given object, which must be a {@linkThread}.
*
* 恢复指定线程
*
*
See {@linkjava.util.concurrent.locks.LockSupport} for more
* in-depth information of the behavior of this method.
*
*@paramobj non-null; the object to unpark*/
public voidunpark(Object obj) {if (obj instanceofThread) {
((Thread) obj).unpark();
}else{throw new IllegalArgumentException("valid for Threads only");
}
}/*** Allocates an instance of the given class without running the constructor.
*
* 不通过构造器,分配一个类的实例
* The class' will be run, if necessary.*/
public native Object allocateInstance(Class>c);
}