--- jsr166/src/jsr166y/Phaser.java 2009/01/06 14:30:31 1.10 +++ jsr166/src/jsr166y/Phaser.java 2009/03/19 05:10:42 1.12 @@ -399,7 +399,7 @@ public class Phaser { phase = phaseOf(s); int unarrived = unarrivedOf(s) + registrations; int parties = partiesOf(s) + registrations; - if (phase < 0) + if (phase < 0) break; if (parties > ushortMask || unarrived > ushortMask) throw new IllegalStateException(badBounds(parties, unarrived)); @@ -557,7 +557,7 @@ public class Phaser { * @return the phase on exit from this method * @throws InterruptedException if thread interrupted while waiting */ - public int awaitAdvanceInterruptibly(int phase) + public int awaitAdvanceInterruptibly(int phase) throws InterruptedException { if (phase < 0) return phase; @@ -795,7 +795,7 @@ public class Phaser { try { ForkJoinPool.managedBlock(this, false); } catch (InterruptedException ie) { - } + } } return wasInterrupted; } @@ -913,22 +913,43 @@ public class Phaser { } // Temporary Unsafe mechanics for preliminary release + private static Unsafe getUnsafe() throws Throwable { + try { + return Unsafe.getUnsafe(); + } catch (SecurityException se) { + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public Unsafe run() throws Exception { + return getUnsafePrivileged(); + }}); + } catch (java.security.PrivilegedActionException e) { + throw e.getCause(); + } + } + } + + private static Unsafe getUnsafePrivileged() + throws NoSuchFieldException, IllegalAccessException { + Field f = Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (Unsafe) f.get(null); + } + + private static long fieldOffset(String fieldName) + throws NoSuchFieldException { + return _unsafe.objectFieldOffset + (Phaser.class.getDeclaredField(fieldName)); + } static final Unsafe _unsafe; static final long stateOffset; static { try { - if (Phaser.class.getClassLoader() != null) { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - _unsafe = (Unsafe)f.get(null); - } - else - _unsafe = Unsafe.getUnsafe(); - stateOffset = _unsafe.objectFieldOffset - (Phaser.class.getDeclaredField("state")); - } catch (Exception e) { + _unsafe = getUnsafe(); + stateOffset = fieldOffset("state"); + } catch (Throwable e) { throw new RuntimeException("Could not initialize intrinsics", e); } }