731 |
|
do {} while (!tryActivate()); // re-activate on exit |
732 |
|
} |
733 |
|
|
734 |
< |
// Unsafe mechanics for jsr166y 3rd party package. |
735 |
< |
private static sun.misc.Unsafe getUnsafe() { |
736 |
< |
try { |
737 |
< |
return sun.misc.Unsafe.getUnsafe(); |
738 |
< |
} catch (SecurityException se) { |
739 |
< |
try { |
740 |
< |
return java.security.AccessController.doPrivileged |
741 |
< |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
742 |
< |
public sun.misc.Unsafe run() throws Exception { |
743 |
< |
return getUnsafeByReflection(); |
744 |
< |
}}); |
745 |
< |
} catch (java.security.PrivilegedActionException e) { |
746 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
747 |
< |
e.getCause()); |
748 |
< |
} |
749 |
< |
} |
750 |
< |
} |
751 |
< |
|
752 |
< |
private static sun.misc.Unsafe getUnsafeByReflection() |
753 |
< |
throws NoSuchFieldException, IllegalAccessException { |
754 |
< |
java.lang.reflect.Field f = |
755 |
< |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
756 |
< |
f.setAccessible(true); |
757 |
< |
return (sun.misc.Unsafe) f.get(null); |
758 |
< |
} |
759 |
< |
|
760 |
< |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
761 |
< |
try { |
762 |
< |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
763 |
< |
} catch (NoSuchFieldException e) { |
764 |
< |
// Convert Exception to Error |
765 |
< |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
766 |
< |
error.initCause(e); |
767 |
< |
throw error; |
768 |
< |
} |
769 |
< |
} |
734 |
> |
// Unsafe mechanics |
735 |
|
|
736 |
|
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
737 |
|
private static final long spOffset = |
738 |
< |
fieldOffset("sp", ForkJoinWorkerThread.class); |
738 |
> |
objectFieldOffset("sp", ForkJoinWorkerThread.class); |
739 |
|
private static final long runStateOffset = |
740 |
< |
fieldOffset("runState", ForkJoinWorkerThread.class); |
740 |
> |
objectFieldOffset("runState", ForkJoinWorkerThread.class); |
741 |
|
private static final long qBase; |
742 |
|
private static final int qShift; |
743 |
|
|
748 |
|
throw new Error("data type scale not a power of two"); |
749 |
|
qShift = 31 - Integer.numberOfLeadingZeros(s); |
750 |
|
} |
751 |
+ |
|
752 |
+ |
private static long objectFieldOffset(String field, Class<?> klazz) { |
753 |
+ |
try { |
754 |
+ |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
755 |
+ |
} catch (NoSuchFieldException e) { |
756 |
+ |
// Convert Exception to corresponding Error |
757 |
+ |
NoSuchFieldError error = new NoSuchFieldError(field); |
758 |
+ |
error.initCause(e); |
759 |
+ |
throw error; |
760 |
+ |
} |
761 |
+ |
} |
762 |
+ |
|
763 |
+ |
/** |
764 |
+ |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
765 |
+ |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
766 |
+ |
* into a jdk. |
767 |
+ |
* |
768 |
+ |
* @return a sun.misc.Unsafe |
769 |
+ |
*/ |
770 |
+ |
private static sun.misc.Unsafe getUnsafe() { |
771 |
+ |
try { |
772 |
+ |
return sun.misc.Unsafe.getUnsafe(); |
773 |
+ |
} catch (SecurityException se) { |
774 |
+ |
try { |
775 |
+ |
return java.security.AccessController.doPrivileged |
776 |
+ |
(new java.security |
777 |
+ |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
778 |
+ |
public sun.misc.Unsafe run() throws Exception { |
779 |
+ |
java.lang.reflect.Field f = sun.misc |
780 |
+ |
.Unsafe.class.getDeclaredField("theUnsafe"); |
781 |
+ |
f.setAccessible(true); |
782 |
+ |
return (sun.misc.Unsafe) f.get(null); |
783 |
+ |
}}); |
784 |
+ |
} catch (java.security.PrivilegedActionException e) { |
785 |
+ |
throw new RuntimeException("Could not initialize intrinsics", |
786 |
+ |
e.getCause()); |
787 |
+ |
} |
788 |
+ |
} |
789 |
+ |
} |
790 |
|
} |