[Ikvm-commit] ikvm/openjdk/java/util/concurrent ForkJoinPool.java, 1.1, 1.2
Brought to you by:
jfrijters
|
From: Jeroen F. <jfr...@us...> - 2014-11-10 13:37:28
|
Update of /cvsroot/ikvm/ikvm/openjdk/java/util/concurrent In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv13263 Modified Files: ForkJoinPool.java Log Message: Optimized unsafe field access. Index: ForkJoinPool.java =================================================================== RCS file: /cvsroot/ikvm/ikvm/openjdk/java/util/concurrent/ForkJoinPool.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ForkJoinPool.java 10 Nov 2014 10:00:02 -0000 1.1 --- ForkJoinPool.java 10 Nov 2014 13:37:26 -0000 1.2 *************** *** 809,813 **** if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null && base == b && U.compareAndSwapObject(a, j, t, null)) { ! U.putOrderedInt(this, QBASE, b + 1); return t; } --- 809,813 ---- if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null && base == b && U.compareAndSwapObject(a, j, t, null)) { ! base = b + 1; return t; } *************** *** 826,830 **** if (t != null) { if (U.compareAndSwapObject(a, j, t, null)) { ! U.putOrderedInt(this, QBASE, b + 1); return t; } --- 826,830 ---- if (t != null) { if (U.compareAndSwapObject(a, j, t, null)) { ! base = b + 1; return t; } *************** *** 989,993 **** if (base == b && U.compareAndSwapObject(a, j, t, null)) { ! U.putOrderedInt(this, QBASE, b + 1); t.doExec(); } --- 989,993 ---- if (base == b && U.compareAndSwapObject(a, j, t, null)) { ! base = b + 1; t.doExec(); } *************** *** 1013,1017 **** for (t = (CountedCompleter<?>)o, r = t;;) { if (r == root) { ! if (U.compareAndSwapInt(this, QLOCK, 0, 1)) { if (top == s && array == a && U.compareAndSwapObject(a, j, t, null)) { --- 1013,1017 ---- for (t = (CountedCompleter<?>)o, r = t;;) { if (r == root) { ! if (compareAndSwapIntQlock(0, 1)) { if (top == s && array == a && U.compareAndSwapObject(a, j, t, null)) { *************** *** 1069,1076 **** } // Unsafe mechanics private static final sun.misc.Unsafe U; - private static final long QBASE; - private static final long QLOCK; private static final int ABASE; private static final int ASHIFT; --- 1069,1077 ---- } + @ikvm.internal.InterlockedCompareAndSet("qlock") + final native boolean compareAndSwapIntQlock(int expect, int update); + // Unsafe mechanics private static final sun.misc.Unsafe U; private static final int ABASE; private static final int ASHIFT; *************** *** 1078,1087 **** try { U = sun.misc.Unsafe.getUnsafe(); - Class<?> k = WorkQueue.class; Class<?> ak = ForkJoinTask[].class; - QBASE = U.objectFieldOffset - (k.getDeclaredField("base")); - QLOCK = U.objectFieldOffset - (k.getDeclaredField("qlock")); ABASE = U.arrayBaseOffset(ak); int scale = U.arrayIndexScale(ak); --- 1079,1083 ---- *************** *** 1285,1289 **** for (;;) { if (((ps = plock) & PL_LOCK) == 0 && ! U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK)) return nps; else if (spins >= 0) { --- 1281,1285 ---- for (;;) { if (((ps = plock) & PL_LOCK) == 0 && ! compareAndSwapIntPlock(ps, nps = ps + PL_LOCK)) return nps; else if (spins >= 0) { *************** *** 1291,1295 **** --spins; } ! else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) { synchronized (this) { if ((plock & PL_SIGNAL) != 0) { --- 1287,1291 ---- --spins; } ! else if (compareAndSwapIntPlock(ps, ps | PL_SIGNAL)) { synchronized (this) { if ((plock & PL_SIGNAL) != 0) { *************** *** 1329,1333 **** long nc = ((long)(((u + UTC_UNIT) & UTC_MASK) | ((u + UAC_UNIT) & UAC_MASK)) << 32) | (long)e; ! if (U.compareAndSwapLong(this, CTL, c, nc)) { ForkJoinWorkerThreadFactory fac; Throwable ex = null; --- 1325,1329 ---- long nc = ((long)(((u + UTC_UNIT) & UTC_MASK) | ((u + UAC_UNIT) & UAC_MASK)) << 32) | (long)e; ! if (compareAndSwapLongCtl(c, nc)) { ForkJoinWorkerThreadFactory fac; Throwable ex = null; *************** *** 1365,1374 **** if ((handler = ueh) != null) wt.setUncaughtExceptionHandler(handler); ! do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, s += SEED_INCREMENT) || s == 0); // skip 0 WorkQueue w = new WorkQueue(this, wt, mode, s); if (((ps = plock) & PL_LOCK) != 0 || ! !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); --- 1361,1370 ---- if ((handler = ueh) != null) wt.setUncaughtExceptionHandler(handler); ! do {} while (!compareAndSwapIntIndexSeed(s = indexSeed, s += SEED_INCREMENT) || s == 0); // skip 0 WorkQueue w = new WorkQueue(this, wt, mode, s); if (((ps = plock) & PL_LOCK) != 0 || ! !compareAndSwapIntPlock(ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); *************** *** 1393,1397 **** } } finally { ! if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) releasePlock(nps); } --- 1389,1393 ---- } } finally { ! if (!compareAndSwapIntPlock(ps, nps)) releasePlock(nps); } *************** *** 1414,1420 **** int ps; w.qlock = -1; // ensure set ! U.getAndAddLong(this, STEALCOUNT, w.nsteals); // collect steals if (((ps = plock) & PL_LOCK) != 0 || ! !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); --- 1410,1416 ---- int ps; w.qlock = -1; // ensure set ! getAndAddLongStealCount(w.nsteals); // collect steals if (((ps = plock) & PL_LOCK) != 0 || ! !compareAndSwapIntPlock(ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); *************** *** 1425,1429 **** ws[idx] = null; } finally { ! if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) releasePlock(nps); } --- 1421,1425 ---- ws[idx] = null; } finally { ! if (!compareAndSwapIntPlock(ps, nps)) releasePlock(nps); } *************** *** 1431,1436 **** long c; // adjust ctl counts ! do {} while (!U.compareAndSwapLong ! (this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) | ((c - TC_UNIT) & TC_MASK) | (c & ~(AC_MASK|TC_MASK))))); --- 1427,1432 ---- long c; // adjust ctl counts ! do {} while (!compareAndSwapLongCtl ! (c = ctl, (((c - AC_UNIT) & AC_MASK) | ((c - TC_UNIT) & TC_MASK) | (c & ~(AC_MASK|TC_MASK))))); *************** *** 1449,1453 **** if (v.eventCount != (e | INT_SIGN)) break; ! if (U.compareAndSwapLong(this, CTL, c, nc)) { v.eventCount = (e + E_SEQ) & E_MASK; if ((p = v.parker) != null) --- 1445,1449 ---- if (v.eventCount != (e | INT_SIGN)) break; ! if (compareAndSwapLongCtl(c, nc)) { v.eventCount = (e + E_SEQ) & E_MASK; if ((p = v.parker) != null) *************** *** 1486,1490 **** if (ps > 0 && ws != null && (m = (ws.length - 1)) >= 0 && (q = ws[m & r & SQMASK]) != null && r != 0 && ! U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock if ((a = q.array) != null && (am = a.length - 1) > (n = (s = q.top) - q.base)) { --- 1482,1486 ---- if (ps > 0 && ws != null && (m = (ws.length - 1)) >= 0 && (q = ws[m & r & SQMASK]) != null && r != 0 && ! q.compareAndSwapIntQlock(0, 1)) { // lock if ((a = q.array) != null && (am = a.length - 1) > (n = (s = q.top) - q.base)) { *************** *** 1539,1552 **** new WorkQueue[n] : null); if (((ps = plock) & PL_LOCK) != 0 || ! !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) ps = acquirePlock(); if (((ws = workQueues) == null || ws.length == 0) && nws != null) workQueues = nws; int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); ! if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) releasePlock(nps); } else if ((q = ws[k = r & m & SQMASK]) != null) { ! if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) { ForkJoinTask<?>[] a = q.array; int s = q.top; --- 1535,1548 ---- new WorkQueue[n] : null); if (((ps = plock) & PL_LOCK) != 0 || ! !compareAndSwapIntPlock(ps, ps += PL_LOCK)) ps = acquirePlock(); if (((ws = workQueues) == null || ws.length == 0) && nws != null) workQueues = nws; int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); ! if (!compareAndSwapIntPlock(ps, nps)) releasePlock(nps); } else if ((q = ws[k = r & m & SQMASK]) != null) { ! if (q.qlock == 0 && q.compareAndSwapIntQlock(0, 1)) { ForkJoinTask<?>[] a = q.array; int s = q.top; *************** *** 1574,1583 **** q.poolIndex = (short)k; if (((ps = plock) & PL_LOCK) != 0 || ! !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) ps = acquirePlock(); if ((ws = workQueues) != null && k < ws.length && ws[k] == null) ws[k] = q; int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); ! if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) releasePlock(nps); } --- 1570,1579 ---- q.poolIndex = (short)k; if (((ps = plock) & PL_LOCK) != 0 || ! !compareAndSwapIntPlock(ps, ps += PL_LOCK)) ps = acquirePlock(); if ((ws = workQueues) != null && k < ws.length && ws[k] == null) ws[k] = q; int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); ! if (!compareAndSwapIntPlock(ps, nps)) releasePlock(nps); } *************** *** 1596,1601 **** final void incrementActiveCount() { long c; ! do {} while (!U.compareAndSwapLong ! (this, CTL, c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); } --- 1592,1597 ---- final void incrementActiveCount() { long c; ! do {} while (!compareAndSwapLongCtl ! (c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); } *************** *** 1624,1628 **** int ne = (e + E_SEQ) & E_MASK; if (w.eventCount == (e | INT_SIGN) && ! U.compareAndSwapLong(this, CTL, c, nc)) { w.eventCount = ne; if ((p = w.parker) != null) --- 1620,1624 ---- int ne = (e + E_SEQ) & E_MASK; if (w.eventCount == (e | INT_SIGN) && ! compareAndSwapLongCtl(c, nc)) { w.eventCount = ne; if ((p = w.parker) != null) *************** *** 1684,1688 **** else if (q.base == b && U.compareAndSwapObject(a, i, t, null)) { ! U.putOrderedInt(q, QBASE, b + 1); if ((b + 1) - q.top < 0) signalWork(ws, q); --- 1680,1684 ---- else if (q.base == b && U.compareAndSwapObject(a, i, t, null)) { ! q.base = b + 1; if ((b + 1) - q.top < 0) signalWork(ws, q); *************** *** 1699,1703 **** w.nextWait = e; w.eventCount = ec | INT_SIGN; ! if (!U.compareAndSwapLong(this, CTL, c, nc)) w.eventCount = ec; // back out } --- 1695,1699 ---- w.nextWait = e; w.eventCount = ec | INT_SIGN; ! if (!compareAndSwapLongCtl(c, nc)) w.eventCount = ec; // back out } *************** *** 1735,1739 **** else if ((ns = w.nsteals) != 0) { // collect steals and retry w.nsteals = 0; ! U.getAndAddLong(this, STEALCOUNT, (long)ns); } else { --- 1731,1735 ---- else if ((ns = w.nsteals) != 0) { // collect steals and retry w.nsteals = 0; ! getAndAddLongStealCount((long)ns); } else { *************** *** 1751,1763 **** if (w.eventCount == ec && ctl == c) { Thread wt = Thread.currentThread(); ! U.putObject(wt, PARKBLOCKER, this); w.parker = wt; // emulate LockSupport.park if (w.eventCount == ec && ctl == c) U.park(false, parkTime); // must recheck before park w.parker = null; ! U.putObject(wt, PARKBLOCKER, null); if (parkTime != 0L && ctl == c && deadline - System.nanoTime() <= 0L && ! U.compareAndSwapLong(this, CTL, c, pc)) stat = w.qlock = -1; // shrink pool } --- 1747,1759 ---- if (w.eventCount == ec && ctl == c) { Thread wt = Thread.currentThread(); ! wt.parkBlocker = this; w.parker = wt; // emulate LockSupport.park if (w.eventCount == ec && ctl == c) U.park(false, parkTime); // must recheck before park w.parker = null; ! wt.parkBlocker = null; if (parkTime != 0L && ctl == c && deadline - System.nanoTime() <= 0L && ! compareAndSwapLongCtl(c, pc)) stat = w.qlock = -1; // shrink pool } *************** *** 1784,1788 **** if (q != null && q.base == b && w.eventCount < 0 && v.eventCount == (e | INT_SIGN) && ! U.compareAndSwapLong(this, CTL, c, nc)) { v.eventCount = ne; if ((p = v.parker) != null) --- 1780,1784 ---- if (q != null && q.base == b && w.eventCount < 0 && v.eventCount == (e | INT_SIGN) && ! compareAndSwapLongCtl(c, nc)) { v.eventCount = ne; if ((p = v.parker) != null) *************** *** 1855,1859 **** break restart; if (U.compareAndSwapObject(a, i, t, null)) { ! U.putOrderedInt(v, QBASE, b + 1); ForkJoinTask<?> ps = joiner.currentSteal; int jt = joiner.top; --- 1851,1855 ---- break restart; if (U.compareAndSwapObject(a, i, t, null)) { ! v.base = b + 1; ForkJoinTask<?> ps = joiner.currentSteal; int jt = joiner.top; *************** *** 1955,1959 **** int ne = (e + E_SEQ) & E_MASK; if (w.eventCount == (e | INT_SIGN) && ! U.compareAndSwapLong(this, CTL, c, nc)) { w.eventCount = ne; if ((p = w.parker) != null) --- 1951,1955 ---- int ne = (e + E_SEQ) & E_MASK; if (w.eventCount == (e | INT_SIGN) && ! compareAndSwapLongCtl(c, nc)) { w.eventCount = ne; if ((p = w.parker) != null) *************** *** 1965,1974 **** (int)(c >> AC_SHIFT) + pc > 1) { long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK); ! if (U.compareAndSwapLong(this, CTL, c, nc)) return true; // no compensation } else if (tc + pc < MAX_CAP) { long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); ! if (U.compareAndSwapLong(this, CTL, c, nc)) { ForkJoinWorkerThreadFactory fac; Throwable ex = null; --- 1961,1970 ---- (int)(c >> AC_SHIFT) + pc > 1) { long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK); ! if (compareAndSwapLongCtl(c, nc)) return true; // no compensation } else if (tc + pc < MAX_CAP) { long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); ! if (compareAndSwapLongCtl(c, nc)) { ForkJoinWorkerThreadFactory fac; Throwable ex = null; *************** *** 2026,2031 **** } long c; // reactivate ! do {} while (!U.compareAndSwapLong ! (this, CTL, c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); --- 2022,2027 ---- } long c; // reactivate ! do {} while (!compareAndSwapLongCtl ! (c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); *************** *** 2099,2104 **** if (!active) { // re-establish active count active = true; ! do {} while (!U.compareAndSwapLong ! (this, CTL, c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); --- 2095,2100 ---- if (!active) { // re-establish active count active = true; ! do {} while (!compareAndSwapLongCtl ! (c = ctl, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))); *************** *** 2111,2120 **** if ((int)(nc >> AC_SHIFT) + parallelism == 0) break; // bypass decrement-then-increment ! if (U.compareAndSwapLong(this, CTL, c, nc)) active = false; } else if ((int)((c = ctl) >> AC_SHIFT) + parallelism <= 0 && ! U.compareAndSwapLong ! (this, CTL, c, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))) break; --- 2107,2116 ---- if ((int)(nc >> AC_SHIFT) + parallelism == 0) break; // bypass decrement-then-increment ! if (compareAndSwapLongCtl(c, nc)) active = false; } else if ((int)((c = ctl) >> AC_SHIFT) + parallelism <= 0 && ! compareAndSwapLongCtl ! (c, ((c & ~AC_MASK) | ((c & AC_MASK) + AC_UNIT)))) break; *************** *** 2224,2231 **** return false; if ((ps & PL_LOCK) != 0 || ! !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN; ! if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) releasePlock(nps); } --- 2220,2227 ---- return false; if ((ps & PL_LOCK) != 0 || ! !compareAndSwapIntPlock(ps, ps += PL_LOCK)) ps = acquirePlock(); int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN; ! if (!compareAndSwapIntPlock(ps, nps)) releasePlock(nps); } *************** *** 2254,2258 **** } } ! if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) { for (int pass = 0; pass < 3; ++pass) { WorkQueue[] ws; WorkQueue w; Thread wt; --- 2250,2254 ---- } } ! if (compareAndSwapLongCtl(c, c | STOP_BIT)) { for (int pass = 0; pass < 3; ++pass) { WorkQueue[] ws; WorkQueue w; Thread wt; *************** *** 2285,2289 **** (cc & (TC_MASK|STOP_BIT))); if (w.eventCount == (e | INT_SIGN) && ! U.compareAndSwapLong(this, CTL, cc, nc)) { w.eventCount = (e + E_SEQ) & E_MASK; w.qlock = -1; --- 2281,2285 ---- (cc & (TC_MASK|STOP_BIT))); if (w.eventCount == (e | INT_SIGN) && ! compareAndSwapLongCtl(cc, nc)) { w.eventCount = (e + E_SEQ) & E_MASK; w.qlock = -1; *************** *** 2327,2331 **** long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; if (U.getObject(a, j) == task && ! U.compareAndSwapInt(joiner, QLOCK, 0, 1)) { if (joiner.top == s && joiner.array == a && U.compareAndSwapObject(a, j, task, null)) { --- 2323,2327 ---- long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; if (U.getObject(a, j) == task && ! joiner.compareAndSwapIntQlock(0, 1)) { if (joiner.top == s && joiner.array == a && U.compareAndSwapObject(a, j, task, null)) { *************** *** 3240,3254 **** } // Unsafe mechanics private static final sun.misc.Unsafe U; - private static final long CTL; - private static final long PARKBLOCKER; private static final int ABASE; private static final int ASHIFT; - private static final long STEALCOUNT; - private static final long PLOCK; - private static final long INDEXSEED; - private static final long QBASE; - private static final long QLOCK; static { --- 3236,3264 ---- } + @ikvm.internal.InterlockedCompareAndSet("ctl") + private native boolean compareAndSwapLongCtl(long expect, long update); + + @ikvm.internal.InterlockedCompareAndSet("plock") + private native boolean compareAndSwapIntPlock(int expect, int update); + + @ikvm.internal.InterlockedCompareAndSet("indexSeed") + private native boolean compareAndSwapIntIndexSeed(int expect, int update); + + @ikvm.internal.InterlockedCompareAndSet("stealCount") + private native boolean compareAndSwapLongStealCount(long expect, long update); + + private long getAndAddLongStealCount(long delta) { + for (;;) { + long value = stealCount; + if (compareAndSwapLongStealCount(value, value + delta)) { + return value; + } + } + } + // Unsafe mechanics private static final sun.misc.Unsafe U; private static final int ABASE; private static final int ASHIFT; static { *************** *** 3256,3276 **** try { U = sun.misc.Unsafe.getUnsafe(); - Class<?> k = ForkJoinPool.class; - CTL = U.objectFieldOffset - (k.getDeclaredField("ctl")); - STEALCOUNT = U.objectFieldOffset - (k.getDeclaredField("stealCount")); - PLOCK = U.objectFieldOffset - (k.getDeclaredField("plock")); - INDEXSEED = U.objectFieldOffset - (k.getDeclaredField("indexSeed")); - Class<?> tk = Thread.class; - PARKBLOCKER = U.objectFieldOffset - (tk.getDeclaredField("parkBlocker")); - Class<?> wk = WorkQueue.class; - QBASE = U.objectFieldOffset - (wk.getDeclaredField("base")); - QLOCK = U.objectFieldOffset - (wk.getDeclaredField("qlock")); Class<?> ak = ForkJoinTask[].class; ABASE = U.arrayBaseOffset(ak); --- 3266,3269 ---- |