13 |
import java.util.NoSuchElementException; |
import java.util.NoSuchElementException; |
14 |
import java.util.Queue; |
import java.util.Queue; |
15 |
import java.util.concurrent.locks.LockSupport; |
import java.util.concurrent.locks.LockSupport; |
16 |
|
|
17 |
/** |
/** |
18 |
* An unbounded {@link TransferQueue} based on linked nodes. |
* An unbounded {@link TransferQueue} based on linked nodes. |
19 |
* This queue orders elements FIFO (first-in-first-out) with respect |
* This queue orders elements FIFO (first-in-first-out) with respect |
421 |
} |
} |
422 |
|
|
423 |
final boolean casItem(Object cmp, Object val) { |
final boolean casItem(Object cmp, Object val) { |
424 |
assert cmp == null || cmp.getClass() != Node.class; |
// assert cmp == null || cmp.getClass() != Node.class; |
425 |
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
426 |
} |
} |
427 |
|
|
445 |
/** |
/** |
446 |
* Sets item to self and waiter to null, to avoid garbage |
* Sets item to self and waiter to null, to avoid garbage |
447 |
* retention after matching or cancelling. Uses relaxed writes |
* retention after matching or cancelling. Uses relaxed writes |
448 |
* bacause order is already constrained in the only calling |
* because order is already constrained in the only calling |
449 |
* contexts: item is forgotten only after volatile/atomic |
* contexts: item is forgotten only after volatile/atomic |
450 |
* mechanics that extract items. Similarly, clearing waiter |
* mechanics that extract items. Similarly, clearing waiter |
451 |
* follows either CAS or return from park (if ever parked; |
* follows either CAS or return from park (if ever parked; |
487 |
* Tries to artificially match a data node -- used by remove. |
* Tries to artificially match a data node -- used by remove. |
488 |
*/ |
*/ |
489 |
final boolean tryMatchData() { |
final boolean tryMatchData() { |
490 |
assert isData; |
// assert isData; |
491 |
Object x = item; |
Object x = item; |
492 |
if (x != null && x != this && casItem(x, null)) { |
if (x != null && x != this && casItem(x, null)) { |
493 |
LockSupport.unpark(waiter); |
LockSupport.unpark(waiter); |
540 |
|
|
541 |
@SuppressWarnings("unchecked") |
@SuppressWarnings("unchecked") |
542 |
static <E> E cast(Object item) { |
static <E> E cast(Object item) { |
543 |
assert item == null || item.getClass() != Node.class; |
// assert item == null || item.getClass() != Node.class; |
544 |
return (E) item; |
return (E) item; |
545 |
} |
} |
546 |
|
|
655 |
for (;;) { |
for (;;) { |
656 |
Object item = s.item; |
Object item = s.item; |
657 |
if (item != e) { // matched |
if (item != e) { // matched |
658 |
assert item != s; |
// assert item != s; |
659 |
s.forgetContents(); // avoid garbage |
s.forgetContents(); // avoid garbage |
660 |
return this.<E>cast(item); |
return this.<E>cast(item); |
661 |
} |
} |