diff -r 212417b74b72 -r bfaf3300b7ba rt/emul/compact/src/main/java/java/util/concurrent/SynchronousQueue.java --- a/rt/emul/compact/src/main/java/java/util/concurrent/SynchronousQueue.java Sat Mar 19 10:46:31 2016 +0100 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/SynchronousQueue.java Sat Mar 19 12:51:03 2016 +0100 @@ -36,7 +36,6 @@ package java.util.concurrent; import java.util.concurrent.locks.*; -import java.util.concurrent.atomic.*; import java.util.*; /** @@ -181,7 +180,7 @@ } /** The number of CPUs, for spin control */ - static final int NCPUS = Runtime.getRuntime().availableProcessors(); + static final int NCPUS = 1; /** * The number of times to spin before blocking in timed waits. @@ -242,8 +241,11 @@ } boolean casNext(SNode cmp, SNode val) { - return cmp == next && - UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); + if (next == cmp) { + next = val; + return true; + } + return false; } /** @@ -255,8 +257,8 @@ * @return true if successfully matched to s */ boolean tryMatch(SNode s) { - if (match == null && - UNSAFE.compareAndSwapObject(this, matchOffset, null, s)) { + if (match == null) { + match = s; Thread w = waiter; if (w != null) { // waiters need at most one unpark waiter = null; @@ -271,38 +273,25 @@ * Tries to cancel a wait by matching node to itself. */ void tryCancel() { - UNSAFE.compareAndSwapObject(this, matchOffset, null, this); + if (match == null) { + match = this; + } } boolean isCancelled() { return match == this; } - - // Unsafe mechanics - private static final sun.misc.Unsafe UNSAFE; - private static final long matchOffset; - private static final long nextOffset; - - static { - try { - UNSAFE = sun.misc.Unsafe.getUnsafe(); - Class k = SNode.class; - matchOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("match")); - nextOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("next")); - } catch (Exception e) { - throw new Error(e); - } - } } /** The head (top) of the stack */ volatile SNode head; boolean casHead(SNode h, SNode nh) { - return h == head && - UNSAFE.compareAndSwapObject(this, headOffset, h, nh); + if (head == h) { + head = nh; + return true; + } + return false; } /** @@ -506,20 +495,6 @@ p = n; } } - - // Unsafe mechanics - private static final sun.misc.Unsafe UNSAFE; - private static final long headOffset; - static { - try { - UNSAFE = sun.misc.Unsafe.getUnsafe(); - Class k = TransferStack.class; - headOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("head")); - } catch (Exception e) { - throw new Error(e); - } - } } /** Dual Queue */ @@ -546,20 +521,28 @@ } boolean casNext(QNode cmp, QNode val) { - return next == cmp && - UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); + if (next == cmp) { + next = val; + return true; + } + return false; } boolean casItem(Object cmp, Object val) { - return item == cmp && - UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); + if (item == cmp) { + item = val; + return true; + } + return false; } /** * Tries to cancel by CAS'ing ref to this as item. */ void tryCancel(Object cmp) { - UNSAFE.compareAndSwapObject(this, itemOffset, cmp, this); + if (item == cmp) { + item = this; + } } boolean isCancelled() { @@ -574,24 +557,6 @@ boolean isOffList() { return next == this; } - - // Unsafe mechanics - private static final sun.misc.Unsafe UNSAFE; - private static final long itemOffset; - private static final long nextOffset; - - static { - try { - UNSAFE = sun.misc.Unsafe.getUnsafe(); - Class k = QNode.class; - itemOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("item")); - nextOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("next")); - } catch (Exception e) { - throw new Error(e); - } - } } /** Head of queue */ @@ -616,25 +581,30 @@ * old head's next node to avoid garbage retention. */ void advanceHead(QNode h, QNode nh) { - if (h == head && - UNSAFE.compareAndSwapObject(this, headOffset, h, nh)) + if (head == h) { + head = nh; h.next = h; // forget old next + } } /** * Tries to cas nt as new tail. */ void advanceTail(QNode t, QNode nt) { - if (tail == t) - UNSAFE.compareAndSwapObject(this, tailOffset, t, nt); + if (tail == t) { + tail = nt; + } } /** * Tries to CAS cleanMe slot. */ boolean casCleanMe(QNode cmp, QNode val) { - return cleanMe == cmp && - UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val); + if (cleanMe == cmp) { + cleanMe = val; + return true; + } + return false; } /** @@ -819,25 +789,6 @@ return; // Postpone cleaning s } } - - private static final sun.misc.Unsafe UNSAFE; - private static final long headOffset; - private static final long tailOffset; - private static final long cleanMeOffset; - static { - try { - UNSAFE = sun.misc.Unsafe.getUnsafe(); - Class k = TransferQueue.class; - headOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("head")); - tailOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("tail")); - cleanMeOffset = UNSAFE.objectFieldOffset - (k.getDeclaredField("cleanMe")); - } catch (Exception e) { - throw new Error(e); - } - } } /** @@ -1180,17 +1131,5 @@ transferer = new TransferStack(); } - // Unsafe mechanics - static long objectFieldOffset(sun.misc.Unsafe UNSAFE, - String field, Class klazz) { - try { - return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); - } catch (NoSuchFieldException e) { - // Convert Exception to corresponding Error - NoSuchFieldError error = new NoSuchFieldError(field); - error.initCause(e); - throw error; - } - } }