9 |
|
import java.util.concurrent.*; |
10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.util.concurrent.locks.*; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
12 |
|
|
13 |
|
/** |
14 |
|
* A thread managed by a {@link ForkJoinPool}. This class is |
731 |
|
do {} while (!tryActivate()); // re-activate on exit |
732 |
|
} |
733 |
|
|
734 |
< |
// Temporary Unsafe mechanics for preliminary release |
735 |
< |
private static Unsafe getUnsafe() throws Throwable { |
734 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
735 |
> |
private static sun.misc.Unsafe getUnsafe() { |
736 |
|
try { |
737 |
< |
return Unsafe.getUnsafe(); |
737 |
> |
return sun.misc.Unsafe.getUnsafe(); |
738 |
|
} catch (SecurityException se) { |
739 |
|
try { |
740 |
|
return java.security.AccessController.doPrivileged |
741 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
742 |
< |
public Unsafe run() throws Exception { |
743 |
< |
return getUnsafePrivileged(); |
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 e.getCause(); |
746 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
747 |
> |
e.getCause()); |
748 |
|
} |
749 |
|
} |
750 |
|
} |
751 |
|
|
752 |
< |
private static Unsafe getUnsafePrivileged() |
752 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
753 |
|
throws NoSuchFieldException, IllegalAccessException { |
754 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
754 |
> |
java.lang.reflect.Field f = |
755 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
756 |
|
f.setAccessible(true); |
757 |
< |
return (Unsafe) f.get(null); |
757 |
> |
return (sun.misc.Unsafe) f.get(null); |
758 |
|
} |
759 |
|
|
760 |
< |
private static long fieldOffset(String fieldName) |
761 |
< |
throws NoSuchFieldException { |
762 |
< |
return UNSAFE.objectFieldOffset |
763 |
< |
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
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 |
|
} |
770 |
|
|
771 |
< |
static final Unsafe UNSAFE; |
772 |
< |
static final long baseOffset; |
773 |
< |
static final long spOffset; |
774 |
< |
static final long runStateOffset; |
771 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
772 |
> |
static final long baseOffset = |
773 |
> |
fieldOffset("base", ForkJoinWorkerThread.class); |
774 |
> |
static final long spOffset = |
775 |
> |
fieldOffset("sp", ForkJoinWorkerThread.class); |
776 |
> |
static final long runStateOffset = |
777 |
> |
fieldOffset("runState", ForkJoinWorkerThread.class); |
778 |
|
static final long qBase; |
779 |
|
static final int qShift; |
780 |
+ |
|
781 |
|
static { |
782 |
< |
try { |
783 |
< |
UNSAFE = getUnsafe(); |
784 |
< |
baseOffset = fieldOffset("base"); |
785 |
< |
spOffset = fieldOffset("sp"); |
786 |
< |
runStateOffset = fieldOffset("runState"); |
778 |
< |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
779 |
< |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
780 |
< |
if ((s & (s-1)) != 0) |
781 |
< |
throw new Error("data type scale not a power of two"); |
782 |
< |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
783 |
< |
} catch (Throwable e) { |
784 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
785 |
< |
} |
782 |
> |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
783 |
> |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
784 |
> |
if ((s & (s-1)) != 0) |
785 |
> |
throw new Error("data type scale not a power of two"); |
786 |
> |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
787 |
|
} |
788 |
|
} |