799 |
|
new PaddedAtomicReference<Node<E>>(null)); |
800 |
|
} |
801 |
|
|
802 |
< |
// Unsafe mechanics for jsr166y 3rd party package. |
802 |
> |
// Unsafe mechanics |
803 |
> |
|
804 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
805 |
> |
private static final long headOffset = |
806 |
> |
objectFieldOffset("head", LinkedTransferQueue.class); |
807 |
> |
private static final long tailOffset = |
808 |
> |
objectFieldOffset("tail", LinkedTransferQueue.class); |
809 |
> |
private static final long cleanMeOffset = |
810 |
> |
objectFieldOffset("cleanMe", LinkedTransferQueue.class); |
811 |
> |
|
812 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
813 |
> |
try { |
814 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
815 |
> |
} catch (NoSuchFieldException e) { |
816 |
> |
// Convert Exception to corresponding Error |
817 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
818 |
> |
error.initCause(e); |
819 |
> |
throw error; |
820 |
> |
} |
821 |
> |
} |
822 |
> |
|
823 |
> |
/** |
824 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
825 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
826 |
> |
* into a jdk. |
827 |
> |
* |
828 |
> |
* @return a sun.misc.Unsafe |
829 |
> |
*/ |
830 |
|
private static sun.misc.Unsafe getUnsafe() { |
831 |
|
try { |
832 |
|
return sun.misc.Unsafe.getUnsafe(); |
833 |
|
} catch (SecurityException se) { |
834 |
|
try { |
835 |
|
return java.security.AccessController.doPrivileged |
836 |
< |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
836 |
> |
(new java.security |
837 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
838 |
|
public sun.misc.Unsafe run() throws Exception { |
839 |
< |
return getUnsafeByReflection(); |
839 |
> |
java.lang.reflect.Field f = sun.misc |
840 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
841 |
> |
f.setAccessible(true); |
842 |
> |
return (sun.misc.Unsafe) f.get(null); |
843 |
|
}}); |
844 |
|
} catch (java.security.PrivilegedActionException e) { |
845 |
|
throw new RuntimeException("Could not initialize intrinsics", |
847 |
|
} |
848 |
|
} |
849 |
|
} |
819 |
– |
|
820 |
– |
private static sun.misc.Unsafe getUnsafeByReflection() |
821 |
– |
throws NoSuchFieldException, IllegalAccessException { |
822 |
– |
java.lang.reflect.Field f = |
823 |
– |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
824 |
– |
f.setAccessible(true); |
825 |
– |
return (sun.misc.Unsafe) f.get(null); |
826 |
– |
} |
827 |
– |
|
828 |
– |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
829 |
– |
try { |
830 |
– |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
831 |
– |
} catch (NoSuchFieldException e) { |
832 |
– |
// Convert Exception to Error |
833 |
– |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
834 |
– |
error.initCause(e); |
835 |
– |
throw error; |
836 |
– |
} |
837 |
– |
} |
838 |
– |
|
839 |
– |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
840 |
– |
private static final long headOffset = |
841 |
– |
fieldOffset("head", LinkedTransferQueue.class); |
842 |
– |
private static final long tailOffset = |
843 |
– |
fieldOffset("tail", LinkedTransferQueue.class); |
844 |
– |
private static final long cleanMeOffset = |
845 |
– |
fieldOffset("cleanMe", LinkedTransferQueue.class); |
846 |
– |
|
850 |
|
} |