5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import java.util.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.util.concurrent.locks.*; |
10 |
> |
|
11 |
> |
import java.util.Collection; |
12 |
|
|
13 |
|
/** |
14 |
|
* A thread managed by a {@link ForkJoinPool}. This class is |
731 |
|
do {} while (!tryActivate()); // re-activate on exit |
732 |
|
} |
733 |
|
|
734 |
< |
// Unsafe mechanics for jsr166y 3rd party package. |
734 |
> |
// Unsafe mechanics |
735 |
> |
|
736 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
737 |
> |
private static final long spOffset = |
738 |
> |
objectFieldOffset("sp", ForkJoinWorkerThread.class); |
739 |
> |
private static final long runStateOffset = |
740 |
> |
objectFieldOffset("runState", ForkJoinWorkerThread.class); |
741 |
> |
private static final long qBase; |
742 |
> |
private static final int qShift; |
743 |
> |
|
744 |
> |
static { |
745 |
> |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
746 |
> |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
747 |
> |
if ((s & (s-1)) != 0) |
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.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
776 |
> |
(new java.security |
777 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
778 |
|
public sun.misc.Unsafe run() throws Exception { |
779 |
< |
return getUnsafeByReflection(); |
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", |
787 |
|
} |
788 |
|
} |
789 |
|
} |
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 |
– |
} |
770 |
– |
|
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 |
– |
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 |
– |
} |
790 |
|
} |