9 |
|
import java.util.concurrent.*; |
10 |
|
import java.util.concurrent.locks.*; |
11 |
|
import java.util.concurrent.atomic.*; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
12 |
|
|
13 |
|
/** |
14 |
|
* An {@link ExecutorService} for running {@link ForkJoinTask}s. A |
1843 |
|
} |
1844 |
|
|
1845 |
|
|
1846 |
< |
// Temporary Unsafe mechanics for preliminary release |
1847 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1846 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1847 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1848 |
|
try { |
1849 |
< |
return Unsafe.getUnsafe(); |
1849 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1850 |
|
} catch (SecurityException se) { |
1851 |
|
try { |
1852 |
|
return java.security.AccessController.doPrivileged |
1853 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1854 |
< |
public Unsafe run() throws Exception { |
1855 |
< |
return getUnsafePrivileged(); |
1853 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1854 |
> |
public sun.misc.Unsafe run() throws Exception { |
1855 |
> |
return getUnsafeByReflection(); |
1856 |
|
}}); |
1857 |
|
} catch (java.security.PrivilegedActionException e) { |
1858 |
< |
throw e.getCause(); |
1858 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1859 |
> |
e.getCause()); |
1860 |
|
} |
1861 |
|
} |
1862 |
|
} |
1863 |
|
|
1864 |
< |
private static Unsafe getUnsafePrivileged() |
1864 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1865 |
|
throws NoSuchFieldException, IllegalAccessException { |
1866 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1866 |
> |
java.lang.reflect.Field f = |
1867 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1868 |
|
f.setAccessible(true); |
1869 |
< |
return (Unsafe) f.get(null); |
1869 |
> |
return (sun.misc.Unsafe) f.get(null); |
1870 |
|
} |
1871 |
|
|
1872 |
< |
private static long fieldOffset(String fieldName) |
1873 |
< |
throws NoSuchFieldException { |
1874 |
< |
return UNSAFE.objectFieldOffset |
1875 |
< |
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1876 |
< |
} |
1877 |
< |
|
1878 |
< |
static final Unsafe UNSAFE; |
1879 |
< |
static final long eventCountOffset; |
1880 |
< |
static final long workerCountsOffset; |
1881 |
< |
static final long runControlOffset; |
1882 |
< |
static final long syncStackOffset; |
1883 |
< |
static final long spareStackOffset; |
1884 |
< |
|
1885 |
< |
static { |
1872 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1873 |
|
try { |
1874 |
< |
UNSAFE = getUnsafe(); |
1875 |
< |
eventCountOffset = fieldOffset("eventCount"); |
1876 |
< |
workerCountsOffset = fieldOffset("workerCounts"); |
1877 |
< |
runControlOffset = fieldOffset("runControl"); |
1878 |
< |
syncStackOffset = fieldOffset("syncStack"); |
1879 |
< |
spareStackOffset = fieldOffset("spareStack"); |
1893 |
< |
} catch (Throwable e) { |
1894 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1874 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1875 |
> |
} catch (NoSuchFieldException e) { |
1876 |
> |
// Convert Exception to Error |
1877 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1878 |
> |
error.initCause(e); |
1879 |
> |
throw error; |
1880 |
|
} |
1881 |
|
} |
1882 |
|
|
1883 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1884 |
+ |
static final long eventCountOffset = |
1885 |
+ |
fieldOffset("eventCount", ForkJoinPool.class); |
1886 |
+ |
static final long workerCountsOffset = |
1887 |
+ |
fieldOffset("workerCounts", ForkJoinPool.class); |
1888 |
+ |
static final long runControlOffset = |
1889 |
+ |
fieldOffset("runControl", ForkJoinPool.class); |
1890 |
+ |
static final long syncStackOffset = |
1891 |
+ |
fieldOffset("syncStack",ForkJoinPool.class); |
1892 |
+ |
static final long spareStackOffset = |
1893 |
+ |
fieldOffset("spareStack", ForkJoinPool.class); |
1894 |
+ |
|
1895 |
|
private boolean casEventCount(long cmp, long val) { |
1896 |
|
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1897 |
|
} |