Merge branch 'single_writer_wip_subscriptions' into single_writer
# Conflicts: # src/dorkbox/util/messagebus/subscription/Subscription.java # src/dorkbox/util/messagebus/subscription/SubscriptionManager.java
This commit is contained in:
commit
d84098fe9d
@ -101,9 +101,7 @@ class MessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final MessageHandler[] messageHandlers = new MessageHandler[finalMethods.size()];
|
return finalMethods.toArray(new MessageHandler[0]);
|
||||||
finalMethods.toArray(messageHandlers);
|
|
||||||
return messageHandlers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final MethodAccess handler;
|
private final MethodAccess handler;
|
||||||
|
@ -20,9 +20,18 @@
|
|||||||
|
|
||||||
package dorkbox.util.messagebus.common.thread;
|
package dorkbox.util.messagebus.common.thread;
|
||||||
|
|
||||||
import org.jctools.util.UnsafeAccess;
|
import com.esotericsoftware.kryo.util.IdentityMap;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.AbstractQueue;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.Spliterator;
|
||||||
|
import java.util.Spliterators;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
|
* An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
|
||||||
@ -39,9 +48,9 @@ import java.util.*;
|
|||||||
* does not permit the use of {@code null} elements.
|
* does not permit the use of {@code null} elements.
|
||||||
*
|
*
|
||||||
* <p>This implementation employs an efficient <em>non-blocking</em>
|
* <p>This implementation employs an efficient <em>non-blocking</em>
|
||||||
* algorithm based on one described in
|
* algorithm based on one described in <a
|
||||||
* <a href="http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf">
|
* href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
|
||||||
* Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue
|
* Fast, and Practical Non-Blocking and Blocking Concurrent Queue
|
||||||
* Algorithms</a> by Maged M. Michael and Michael L. Scott.
|
* Algorithms</a> by Maged M. Michael and Michael L. Scott.
|
||||||
*
|
*
|
||||||
* <p>Iterators are <i>weakly consistent</i>, returning elements
|
* <p>Iterators are <i>weakly consistent</i>, returning elements
|
||||||
@ -81,8 +90,11 @@ import java.util.*;
|
|||||||
* @author Doug Lea
|
* @author Doug Lea
|
||||||
* @param <E> the type of elements held in this collection
|
* @param <E> the type of elements held in this collection
|
||||||
*/
|
*/
|
||||||
|
public class ConcurrentLinkedQueue2<E> extends AbstractQueue<E>
|
||||||
|
implements Queue<E>, java.io.Serializable {
|
||||||
|
private static final long serialVersionUID = 196745693267521676L;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is a modification of the Michael & Scott algorithm,
|
* This is a modification of the Michael & Scott algorithm,
|
||||||
* adapted for a garbage-collected environment, with support for
|
* adapted for a garbage-collected environment, with support for
|
||||||
* interior node deletion (to support remove(Object)). For
|
* interior node deletion (to support remove(Object)). For
|
||||||
@ -152,21 +164,10 @@ import java.util.*;
|
|||||||
* CAS, so they never regress, although again this is merely an
|
* CAS, so they never regress, although again this is merely an
|
||||||
* optimization.
|
* optimization.
|
||||||
*/
|
*/
|
||||||
abstract class Item0<E> {
|
|
||||||
public volatile E item;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class Pad0<E> extends Item0<E>{
|
|
||||||
volatile long z0, z1, z2, z4, z5, z6 = 7L;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class Item1<E> extends Pad0<E> {
|
|
||||||
public volatile Node<E> next;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Node<E> extends Item1<E> {
|
|
||||||
volatile long z0, z1, z2, z4, z5, z6 = 7L;
|
|
||||||
|
|
||||||
|
private static class Node<E> {
|
||||||
|
volatile E item;
|
||||||
|
volatile Node<E> next;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new node. Uses relaxed write because item can
|
* Constructs a new node. Uses relaxed write because item can
|
||||||
@ -196,18 +197,18 @@ class Node<E> extends Item1<E> {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
UNSAFE = UnsafeAccess.UNSAFE;
|
UNSAFE = sun.misc.Unsafe.getUnsafe();
|
||||||
|
Class<?> k = Node.class;
|
||||||
itemOffset = UNSAFE.objectFieldOffset(Node.class.getField("item"));
|
itemOffset = UNSAFE.objectFieldOffset
|
||||||
nextOffset = UNSAFE.objectFieldOffset(Node.class.getField("next"));
|
(k.getDeclaredField("item"));
|
||||||
|
nextOffset = UNSAFE.objectFieldOffset
|
||||||
|
(k.getDeclaredField("next"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract class CLQItem0<E> extends AbstractQueue<E> {
|
|
||||||
/**
|
/**
|
||||||
* A node from which the first live (non-deleted) node (if any)
|
* A node from which the first live (non-deleted) node (if any)
|
||||||
* can be reached in O(1) time.
|
* can be reached in O(1) time.
|
||||||
@ -220,15 +221,8 @@ abstract class CLQItem0<E> extends AbstractQueue<E> {
|
|||||||
* - it is permitted for tail to lag behind head, that is, for tail
|
* - it is permitted for tail to lag behind head, that is, for tail
|
||||||
* to not be reachable from head!
|
* to not be reachable from head!
|
||||||
*/
|
*/
|
||||||
public transient volatile Node<E> head;
|
private transient volatile Node<E> head;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
abstract class CLQPad0<E> extends CLQItem0<E> {
|
|
||||||
volatile long z0, z1, z2, z4, z5, z6 = 7L;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class CLQItem1<E> extends CLQPad0<E> {
|
|
||||||
/**
|
/**
|
||||||
* A node from which the last node on list (that is, the unique
|
* A node from which the last node on list (that is, the unique
|
||||||
* node with node.next == null) can be reached in O(1) time.
|
* node with node.next == null) can be reached in O(1) time.
|
||||||
@ -241,21 +235,13 @@ abstract class CLQItem1<E> extends CLQPad0<E> {
|
|||||||
* to not be reachable from head!
|
* to not be reachable from head!
|
||||||
* - tail.next may or may not be self-pointing to tail.
|
* - tail.next may or may not be self-pointing to tail.
|
||||||
*/
|
*/
|
||||||
public volatile Node<E> tail;
|
private transient volatile Node<E> tail;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|
||||||
implements Queue<E>, java.io.Serializable {
|
|
||||||
private static final long serialVersionUID = 196745693267521676L;
|
|
||||||
|
|
||||||
volatile long z0, z1, z2, z4, z5, z6 = 7L;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@code ConcurrentLinkedQueue} that is initially empty.
|
* Creates a {@code ConcurrentLinkedQueue} that is initially empty.
|
||||||
*/
|
*/
|
||||||
public ConcurrentLinkedQueue2() {
|
public ConcurrentLinkedQueue2() {
|
||||||
this.head = this.tail = new Node<E>(null);
|
head = tail = new Node<E>(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -272,18 +258,17 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
for (E e : c) {
|
for (E e : c) {
|
||||||
checkNotNull(e);
|
checkNotNull(e);
|
||||||
Node<E> newNode = new Node<E>(e);
|
Node<E> newNode = new Node<E>(e);
|
||||||
if (h == null) {
|
if (h == null)
|
||||||
h = t = newNode;
|
h = t = newNode;
|
||||||
} else {
|
else {
|
||||||
t.lazySetNext(newNode);
|
t.lazySetNext(newNode);
|
||||||
t = newNode;
|
t = newNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (h == null) {
|
if (h == null)
|
||||||
h = t = new Node<E>(null);
|
h = t = new Node<E>(null);
|
||||||
}
|
head = h;
|
||||||
this.head = h;
|
tail = t;
|
||||||
this.tail = t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Have to override just to update the javadoc
|
// Have to override just to update the javadoc
|
||||||
@ -296,7 +281,6 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* @return {@code true} (as specified by {@link Collection#add})
|
* @return {@code true} (as specified by {@link Collection#add})
|
||||||
* @throws NullPointerException if the specified element is null
|
* @throws NullPointerException if the specified element is null
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean add(E e) {
|
public boolean add(E e) {
|
||||||
return offer(e);
|
return offer(e);
|
||||||
}
|
}
|
||||||
@ -306,10 +290,9 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* as sentinel for succ(), below.
|
* as sentinel for succ(), below.
|
||||||
*/
|
*/
|
||||||
final void updateHead(Node<E> h, Node<E> p) {
|
final void updateHead(Node<E> h, Node<E> p) {
|
||||||
if (h != p && casHead(h, p)) {
|
if (h != p && casHead(h, p))
|
||||||
h.lazySetNext(h);
|
h.lazySetNext(h);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the successor of p, or the head node if p.next has been
|
* Returns the successor of p, or the head node if p.next has been
|
||||||
@ -318,9 +301,15 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
*/
|
*/
|
||||||
final Node<E> succ(Node<E> p) {
|
final Node<E> succ(Node<E> p) {
|
||||||
Node<E> next = p.next;
|
Node<E> next = p.next;
|
||||||
return p == next ? this.head : next;
|
return (p == next) ? head : next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// can ONLY be touched by a single reader/writer (for add/offer/remove/poll/etc)
|
||||||
|
private final IdentityMap<Object, Node<E>> quickLookup = new IdentityMap<Object, Node<E>>(32);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts the specified element at the tail of this queue.
|
* Inserts the specified element at the tail of this queue.
|
||||||
* As the queue is unbounded, this method will never return {@code false}.
|
* As the queue is unbounded, this method will never return {@code false}.
|
||||||
@ -328,17 +317,11 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* @return {@code true} (as specified by {@link Queue#offer})
|
* @return {@code true} (as specified by {@link Queue#offer})
|
||||||
* @throws NullPointerException if the specified element is null
|
* @throws NullPointerException if the specified element is null
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean offer(E e) {
|
public boolean offer(E e) {
|
||||||
offerNode(e);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node<E> offerNode(E e) {
|
|
||||||
checkNotNull(e);
|
checkNotNull(e);
|
||||||
final Node<E> newNode = new Node<E>(e);
|
final Node<E> newNode = new Node<E>(e);
|
||||||
|
|
||||||
for (Node<E> t = this.tail, p = t;;) {
|
for (Node<E> t = tail, p = t;;) {
|
||||||
Node<E> q = p.next;
|
Node<E> q = p.next;
|
||||||
if (q == null) {
|
if (q == null) {
|
||||||
// p is last node
|
// p is last node
|
||||||
@ -346,73 +329,69 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
// Successful CAS is the linearization point
|
// Successful CAS is the linearization point
|
||||||
// for e to become an element of this queue,
|
// for e to become an element of this queue,
|
||||||
// and for newNode to become "live".
|
// and for newNode to become "live".
|
||||||
if (p != t)
|
if (p != t) // hop two nodes at a time
|
||||||
{
|
|
||||||
casTail(t, newNode); // Failure is OK.
|
casTail(t, newNode); // Failure is OK.
|
||||||
}
|
|
||||||
return newNode;
|
quickLookup.put(e, newNode);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
// Lost CAS race to another thread; re-read next
|
// Lost CAS race to another thread; re-read next
|
||||||
}
|
}
|
||||||
else if (p == q) {
|
else if (p == q)
|
||||||
// We have fallen off list. If tail is unchanged, it
|
// We have fallen off list. If tail is unchanged, it
|
||||||
// will also be off-list, in which case we need to
|
// will also be off-list, in which case we need to
|
||||||
// jump to head, from which all live nodes are always
|
// jump to head, from which all live nodes are always
|
||||||
// reachable. Else the new tail is a better bet.
|
// reachable. Else the new tail is a better bet.
|
||||||
p = t != (t = this.tail) ? t : this.head;
|
p = (t != (t = tail)) ? t : head;
|
||||||
} else {
|
else
|
||||||
// Check for tail updates after two hops.
|
// Check for tail updates after two hops.
|
||||||
p = p != t && t != (t = this.tail) ? t : q;
|
p = (p != t && t != (t = tail)) ? t : q;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public E poll() {
|
public E poll() {
|
||||||
restartFromHead:
|
restartFromHead:
|
||||||
for (;;) {
|
for (;;) {
|
||||||
for (Node<E> h = this.head, p = h, q;;) {
|
for (Node<E> h = head, p = h, q;;) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
|
|
||||||
if (item != null && p.casItem(item, null)) {
|
if (item != null && p.casItem(item, null)) {
|
||||||
// Successful CAS is the linearization point
|
// Successful CAS is the linearization point
|
||||||
// for item to be removed from this queue.
|
// for item to be removed from this queue.
|
||||||
if (p != h) {
|
if (p != h) // hop two nodes at a time
|
||||||
updateHead(h, (q = p.next) != null ? q : p);
|
updateHead(h, ((q = p.next) != null) ? q : p);
|
||||||
}
|
|
||||||
|
quickLookup.remove(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
else if ((q = p.next) == null) {
|
else if ((q = p.next) == null) {
|
||||||
updateHead(h, p);
|
updateHead(h, p);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
else if (p == q) {
|
else if (p == q)
|
||||||
continue restartFromHead;
|
continue restartFromHead;
|
||||||
} else {
|
else
|
||||||
p = q;
|
p = q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E peek() {
|
public E peek() {
|
||||||
restartFromHead:
|
restartFromHead:
|
||||||
for (;;) {
|
for (;;) {
|
||||||
for (Node<E> h = this.head, p = h, q;;) {
|
for (Node<E> h = head, p = h, q;;) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
if (item != null || (q = p.next) == null) {
|
if (item != null || (q = p.next) == null) {
|
||||||
updateHead(h, p);
|
updateHead(h, p);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
else if (p == q) {
|
else if (p == q)
|
||||||
continue restartFromHead;
|
continue restartFromHead;
|
||||||
} else {
|
else
|
||||||
p = q;
|
p = q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the first live (non-deleted) node on list, or null if none.
|
* Returns the first live (non-deleted) node on list, or null if none.
|
||||||
@ -425,27 +404,25 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
Node<E> first() {
|
Node<E> first() {
|
||||||
restartFromHead:
|
restartFromHead:
|
||||||
for (;;) {
|
for (;;) {
|
||||||
for (Node<E> h = this.head, p = h, q;;) {
|
for (Node<E> h = head, p = h, q;;) {
|
||||||
boolean hasItem = p.item != null;
|
boolean hasItem = (p.item != null);
|
||||||
if (hasItem || (q = p.next) == null) {
|
if (hasItem || (q = p.next) == null) {
|
||||||
updateHead(h, p);
|
updateHead(h, p);
|
||||||
return hasItem ? p : null;
|
return hasItem ? p : null;
|
||||||
}
|
}
|
||||||
else if (p == q) {
|
else if (p == q)
|
||||||
continue restartFromHead;
|
continue restartFromHead;
|
||||||
} else {
|
else
|
||||||
p = q;
|
p = q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} if this queue contains no elements.
|
* Returns {@code true} if this queue contains no elements.
|
||||||
*
|
*
|
||||||
* @return {@code true} if this queue contains no elements
|
* @return {@code true} if this queue contains no elements
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return first() == null;
|
return first() == null;
|
||||||
}
|
}
|
||||||
@ -466,17 +443,13 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
*
|
*
|
||||||
* @return the number of elements in this queue
|
* @return the number of elements in this queue
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public int size() {
|
public int size() {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (Node<E> p = first(); p != null; p = succ(p)) {
|
for (Node<E> p = first(); p != null; p = succ(p))
|
||||||
if (p.item != null) {
|
if (p.item != null)
|
||||||
// Collection.size() spec says to max out
|
// Collection.size() spec says to max out
|
||||||
if (++count == Integer.MAX_VALUE) {
|
if (++count == Integer.MAX_VALUE)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -488,17 +461,13 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* @param o object to be checked for containment in this queue
|
* @param o object to be checked for containment in this queue
|
||||||
* @return {@code true} if this queue contains the specified element
|
* @return {@code true} if this queue contains the specified element
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean contains(Object o) {
|
public boolean contains(Object o) {
|
||||||
if (o == null) {
|
if (o == null) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (Node<E> p = first(); p != null; p = succ(p)) {
|
for (Node<E> p = first(); p != null; p = succ(p)) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
if (item != null && o.equals(item)) {
|
if (item != null && o.equals(item))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -513,11 +482,9 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* @param o element to be removed from this queue, if present
|
* @param o element to be removed from this queue, if present
|
||||||
* @return {@code true} if this queue changed as a result of the call
|
* @return {@code true} if this queue changed as a result of the call
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean remove(Object o) {
|
public boolean remove(Object o) {
|
||||||
if (o == null) {
|
if (o == null) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Node<E> pred = null;
|
Node<E> pred = null;
|
||||||
for (Node<E> p = first(); p != null; p = succ(p)) {
|
for (Node<E> p = first(); p != null; p = succ(p)) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
@ -525,9 +492,10 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
o.equals(item) &&
|
o.equals(item) &&
|
||||||
p.casItem(item, null)) {
|
p.casItem(item, null)) {
|
||||||
Node<E> next = succ(p);
|
Node<E> next = succ(p);
|
||||||
if (pred != null && next != null) {
|
if (pred != null && next != null)
|
||||||
pred.casNext(p, next);
|
pred.casNext(p, next);
|
||||||
}
|
|
||||||
|
quickLookup.remove(o);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
pred = p;
|
pred = p;
|
||||||
@ -547,31 +515,28 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* of its elements are null
|
* of its elements are null
|
||||||
* @throws IllegalArgumentException if the collection is this queue
|
* @throws IllegalArgumentException if the collection is this queue
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public boolean addAll(Collection<? extends E> c) {
|
public boolean addAll(Collection<? extends E> c) {
|
||||||
if (c == this) {
|
if (c == this)
|
||||||
// As historically specified in AbstractQueue#addAll
|
// As historically specified in AbstractQueue#addAll
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
|
||||||
|
|
||||||
// Copy c into a private chain of Nodes
|
// Copy c into a private chain of Nodes
|
||||||
Node<E> beginningOfTheEnd = null, last = null;
|
Node<E> beginningOfTheEnd = null, last = null;
|
||||||
for (E e : c) {
|
for (E e : c) {
|
||||||
checkNotNull(e);
|
checkNotNull(e);
|
||||||
Node<E> newNode = new Node<E>(e);
|
Node<E> newNode = new Node<E>(e);
|
||||||
if (beginningOfTheEnd == null) {
|
if (beginningOfTheEnd == null)
|
||||||
beginningOfTheEnd = last = newNode;
|
beginningOfTheEnd = last = newNode;
|
||||||
} else {
|
else {
|
||||||
last.lazySetNext(newNode);
|
last.lazySetNext(newNode);
|
||||||
last = newNode;
|
last = newNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (beginningOfTheEnd == null) {
|
if (beginningOfTheEnd == null)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
// Atomically append the chain at the tail of this collection
|
// Atomically append the chain at the tail of this collection
|
||||||
for (Node<E> t = this.tail, p = t;;) {
|
for (Node<E> t = tail, p = t;;) {
|
||||||
Node<E> q = p.next;
|
Node<E> q = p.next;
|
||||||
if (q == null) {
|
if (q == null) {
|
||||||
// p is last node
|
// p is last node
|
||||||
@ -581,25 +546,23 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
if (!casTail(t, last)) {
|
if (!casTail(t, last)) {
|
||||||
// Try a little harder to update tail,
|
// Try a little harder to update tail,
|
||||||
// since we may be adding many elements.
|
// since we may be adding many elements.
|
||||||
t = this.tail;
|
t = tail;
|
||||||
if (last.next == null) {
|
if (last.next == null)
|
||||||
casTail(t, last);
|
casTail(t, last);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Lost CAS race to another thread; re-read next
|
// Lost CAS race to another thread; re-read next
|
||||||
}
|
}
|
||||||
else if (p == q) {
|
else if (p == q)
|
||||||
// We have fallen off list. If tail is unchanged, it
|
// We have fallen off list. If tail is unchanged, it
|
||||||
// will also be off-list, in which case we need to
|
// will also be off-list, in which case we need to
|
||||||
// jump to head, from which all live nodes are always
|
// jump to head, from which all live nodes are always
|
||||||
// reachable. Else the new tail is a better bet.
|
// reachable. Else the new tail is a better bet.
|
||||||
p = t != (t = this.tail) ? t : this.head;
|
p = (t != (t = tail)) ? t : head;
|
||||||
} else {
|
else
|
||||||
// Check for tail updates after two hops.
|
// Check for tail updates after two hops.
|
||||||
p = p != t && t != (t = this.tail) ? t : q;
|
p = (p != t && t != (t = tail)) ? t : q;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -616,16 +579,14 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
*
|
*
|
||||||
* @return an array containing all of the elements in this queue
|
* @return an array containing all of the elements in this queue
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public Object[] toArray() {
|
public Object[] toArray() {
|
||||||
// Use ArrayList to deal with resizing.
|
// Use ArrayList to deal with resizing.
|
||||||
ArrayList<E> al = new ArrayList<E>();
|
ArrayList<E> al = new ArrayList<E>();
|
||||||
for (Node<E> p = first(); p != null; p = succ(p)) {
|
for (Node<E> p = first(); p != null; p = succ(p)) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
if (item != null) {
|
if (item != null)
|
||||||
al.add(item);
|
al.add(item);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return al.toArray();
|
return al.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,7 +625,6 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* this queue
|
* this queue
|
||||||
* @throws NullPointerException if the specified array is null
|
* @throws NullPointerException if the specified array is null
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T[] toArray(T[] a) {
|
public <T> T[] toArray(T[] a) {
|
||||||
// try to use sent-in array
|
// try to use sent-in array
|
||||||
@ -672,14 +632,12 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
Node<E> p;
|
Node<E> p;
|
||||||
for (p = first(); p != null && k < a.length; p = succ(p)) {
|
for (p = first(); p != null && k < a.length; p = succ(p)) {
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
if (item != null) {
|
if (item != null)
|
||||||
a[k++] = (T)item;
|
a[k++] = (T)item;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
if (k < a.length) {
|
if (k < a.length)
|
||||||
a[k] = null;
|
a[k] = null;
|
||||||
}
|
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -687,10 +645,9 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
ArrayList<E> al = new ArrayList<E>();
|
ArrayList<E> al = new ArrayList<E>();
|
||||||
for (Node<E> q = first(); q != null; q = succ(q)) {
|
for (Node<E> q = first(); q != null; q = succ(q)) {
|
||||||
E item = q.item;
|
E item = q.item;
|
||||||
if (item != null) {
|
if (item != null)
|
||||||
al.add(item);
|
al.add(item);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return al.toArray(a);
|
return al.toArray(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -698,16 +655,11 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* Returns an iterator over the elements in this queue in proper sequence.
|
* Returns an iterator over the elements in this queue in proper sequence.
|
||||||
* The elements will be returned in order from first (head) to last (tail).
|
* The elements will be returned in order from first (head) to last (tail).
|
||||||
*
|
*
|
||||||
* <p>The returned iterator is a "weakly consistent" iterator that
|
* <p>The returned iterator is
|
||||||
* will never throw {@link java.util.ConcurrentModificationException
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
|
||||||
* ConcurrentModificationException}, and guarantees to traverse
|
|
||||||
* elements as they existed upon construction of the iterator, and
|
|
||||||
* may (but is not guaranteed to) reflect any modifications
|
|
||||||
* subsequent to construction.
|
|
||||||
*
|
*
|
||||||
* @return an iterator over the elements in this queue in proper sequence
|
* @return an iterator over the elements in this queue in proper sequence
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public Iterator<E> iterator() {
|
public Iterator<E> iterator() {
|
||||||
return new Itr();
|
return new Itr();
|
||||||
}
|
}
|
||||||
@ -740,68 +692,62 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* next(), or null if no such.
|
* next(), or null if no such.
|
||||||
*/
|
*/
|
||||||
private E advance() {
|
private E advance() {
|
||||||
this.lastRet = this.nextNode;
|
lastRet = nextNode;
|
||||||
E x = this.nextItem;
|
E x = nextItem;
|
||||||
|
|
||||||
Node<E> pred, p;
|
Node<E> pred, p;
|
||||||
if (this.nextNode == null) {
|
if (nextNode == null) {
|
||||||
p = first();
|
p = first();
|
||||||
pred = null;
|
pred = null;
|
||||||
} else {
|
} else {
|
||||||
pred = this.nextNode;
|
pred = nextNode;
|
||||||
p = succ(this.nextNode);
|
p = succ(nextNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
this.nextNode = null;
|
nextNode = null;
|
||||||
this.nextItem = null;
|
nextItem = null;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
E item = p.item;
|
E item = p.item;
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
this.nextNode = p;
|
nextNode = p;
|
||||||
this.nextItem = item;
|
nextItem = item;
|
||||||
return x;
|
return x;
|
||||||
} else {
|
} else {
|
||||||
// skip over nulls
|
// skip over nulls
|
||||||
Node<E> next = succ(p);
|
Node<E> next = succ(p);
|
||||||
if (pred != null && next != null) {
|
if (pred != null && next != null)
|
||||||
pred.casNext(p, next);
|
pred.casNext(p, next);
|
||||||
}
|
|
||||||
p = next;
|
p = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return this.nextNode != null;
|
return nextNode != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public E next() {
|
public E next() {
|
||||||
if (this.nextNode == null) {
|
if (nextNode == null) throw new NoSuchElementException();
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
return advance();
|
return advance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove() {
|
public void remove() {
|
||||||
Node<E> l = this.lastRet;
|
Node<E> l = lastRet;
|
||||||
if (l == null) {
|
if (l == null) throw new IllegalStateException();
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
// rely on a future traversal to relink.
|
// rely on a future traversal to relink.
|
||||||
l.item = null;
|
l.item = null;
|
||||||
this.lastRet = null;
|
lastRet = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves this queue to a stream (that is, serializes it).
|
* Saves this queue to a stream (that is, serializes it).
|
||||||
*
|
*
|
||||||
|
* @param s the stream
|
||||||
|
* @throws java.io.IOException if an I/O error occurs
|
||||||
* @serialData All of the elements (each an {@code E}) in
|
* @serialData All of the elements (each an {@code E}) in
|
||||||
* the proper order, followed by a null
|
* the proper order, followed by a null
|
||||||
*/
|
*/
|
||||||
@ -814,10 +760,9 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
// Write out all elements in the proper order.
|
// Write out all elements in the proper order.
|
||||||
for (Node<E> p = first(); p != null; p = succ(p)) {
|
for (Node<E> p = first(); p != null; p = succ(p)) {
|
||||||
Object item = p.item;
|
Object item = p.item;
|
||||||
if (item != null) {
|
if (item != null)
|
||||||
s.writeObject(item);
|
s.writeObject(item);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Use trailing null as sentinel
|
// Use trailing null as sentinel
|
||||||
s.writeObject(null);
|
s.writeObject(null);
|
||||||
@ -825,6 +770,10 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reconstitutes this queue from a stream (that is, deserializes it).
|
* Reconstitutes this queue from a stream (that is, deserializes it).
|
||||||
|
* @param s the stream
|
||||||
|
* @throws ClassNotFoundException if the class of a serialized object
|
||||||
|
* could not be found
|
||||||
|
* @throws java.io.IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
private void readObject(java.io.ObjectInputStream s)
|
private void readObject(java.io.ObjectInputStream s)
|
||||||
throws java.io.IOException, ClassNotFoundException {
|
throws java.io.IOException, ClassNotFoundException {
|
||||||
@ -836,18 +785,124 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
while ((item = s.readObject()) != null) {
|
while ((item = s.readObject()) != null) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Node<E> newNode = new Node<E>((E) item);
|
Node<E> newNode = new Node<E>((E) item);
|
||||||
if (h == null) {
|
if (h == null)
|
||||||
h = t = newNode;
|
h = t = newNode;
|
||||||
} else {
|
else {
|
||||||
t.lazySetNext(newNode);
|
t.lazySetNext(newNode);
|
||||||
t = newNode;
|
t = newNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (h == null) {
|
if (h == null)
|
||||||
h = t = new Node<E>(null);
|
h = t = new Node<E>(null);
|
||||||
|
head = h;
|
||||||
|
tail = t;
|
||||||
}
|
}
|
||||||
this.head = h;
|
|
||||||
this.tail = t;
|
/** A customized variant of Spliterators.IteratorSpliterator */
|
||||||
|
static final class CLQSpliterator<E> implements Spliterator<E> {
|
||||||
|
static final int MAX_BATCH = 1 << 25; // max batch array size;
|
||||||
|
final ConcurrentLinkedQueue2<E> queue;
|
||||||
|
Node<E> current; // current node; null until initialized
|
||||||
|
int batch; // batch size for splits
|
||||||
|
boolean exhausted; // true when no more nodes
|
||||||
|
CLQSpliterator(ConcurrentLinkedQueue2<E> queue) {
|
||||||
|
this.queue = queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Spliterator<E> trySplit() {
|
||||||
|
Node<E> p;
|
||||||
|
final ConcurrentLinkedQueue2<E> q = this.queue;
|
||||||
|
int b = batch;
|
||||||
|
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
|
||||||
|
if (!exhausted &&
|
||||||
|
((p = current) != null || (p = q.first()) != null) &&
|
||||||
|
p.next != null) {
|
||||||
|
Object[] a = new Object[n];
|
||||||
|
int i = 0;
|
||||||
|
do {
|
||||||
|
if ((a[i] = p.item) != null)
|
||||||
|
++i;
|
||||||
|
if (p == (p = p.next))
|
||||||
|
p = q.first();
|
||||||
|
} while (p != null && i < n);
|
||||||
|
if ((current = p) == null)
|
||||||
|
exhausted = true;
|
||||||
|
if (i > 0) {
|
||||||
|
batch = i;
|
||||||
|
return Spliterators.spliterator
|
||||||
|
(a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
|
||||||
|
Spliterator.CONCURRENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void forEachRemaining(Consumer<? super E> action) {
|
||||||
|
Node<E> p;
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
final ConcurrentLinkedQueue2<E> q = this.queue;
|
||||||
|
if (!exhausted &&
|
||||||
|
((p = current) != null || (p = q.first()) != null)) {
|
||||||
|
exhausted = true;
|
||||||
|
do {
|
||||||
|
E e = p.item;
|
||||||
|
if (p == (p = p.next))
|
||||||
|
p = q.first();
|
||||||
|
if (e != null)
|
||||||
|
action.accept(e);
|
||||||
|
} while (p != null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean tryAdvance(Consumer<? super E> action) {
|
||||||
|
Node<E> p;
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
final ConcurrentLinkedQueue2<E> q = this.queue;
|
||||||
|
if (!exhausted &&
|
||||||
|
((p = current) != null || (p = q.first()) != null)) {
|
||||||
|
E e;
|
||||||
|
do {
|
||||||
|
e = p.item;
|
||||||
|
if (p == (p = p.next))
|
||||||
|
p = q.first();
|
||||||
|
} while (e == null && p != null);
|
||||||
|
if ((current = p) == null)
|
||||||
|
exhausted = true;
|
||||||
|
if (e != null) {
|
||||||
|
action.accept(e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long estimateSize() { return Long.MAX_VALUE; }
|
||||||
|
|
||||||
|
public int characteristics() {
|
||||||
|
return Spliterator.ORDERED | Spliterator.NONNULL |
|
||||||
|
Spliterator.CONCURRENT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Spliterator} over the elements in this queue.
|
||||||
|
*
|
||||||
|
* <p>The returned spliterator is
|
||||||
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
|
||||||
|
*
|
||||||
|
* <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
|
||||||
|
* {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
|
||||||
|
*
|
||||||
|
* @implNote
|
||||||
|
* The {@code Spliterator} implements {@code trySplit} to permit limited
|
||||||
|
* parallelism.
|
||||||
|
*
|
||||||
|
* @return a {@code Spliterator} over the elements in this queue
|
||||||
|
* @since 1.8
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Spliterator<E> spliterator() {
|
||||||
|
return new CLQSpliterator<E>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -856,10 +911,9 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
* @param v the element
|
* @param v the element
|
||||||
*/
|
*/
|
||||||
private static void checkNotNull(Object v) {
|
private static void checkNotNull(Object v) {
|
||||||
if (v == null) {
|
if (v == null)
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean casTail(Node<E> cmp, Node<E> val) {
|
private boolean casTail(Node<E> cmp, Node<E> val) {
|
||||||
return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
|
return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
|
||||||
@ -876,11 +930,12 @@ public class ConcurrentLinkedQueue2<E> extends CLQItem1<E>
|
|||||||
private static final long tailOffset;
|
private static final long tailOffset;
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
UNSAFE = UnsafeAccess.UNSAFE;
|
UNSAFE = sun.misc.Unsafe.getUnsafe();
|
||||||
Class<?> k = ConcurrentLinkedQueue2.class;
|
Class<?> k = ConcurrentLinkedQueue.class;
|
||||||
|
headOffset = UNSAFE.objectFieldOffset
|
||||||
headOffset = UNSAFE.objectFieldOffset(k.getField("head"));
|
(k.getDeclaredField("head"));
|
||||||
tailOffset = UNSAFE.objectFieldOffset(k.getField("tail"));
|
tailOffset = UNSAFE.objectFieldOffset
|
||||||
|
(k.getDeclaredField("tail"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
|
@ -1,315 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2015 dorkbox, llc
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package dorkbox.util.messagebus.common.thread;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This data structure is optimized for non-blocking reads even when write operations occur.
|
|
||||||
* Running read iterators will not be affected by add operations since writes always insert at the head of the
|
|
||||||
* structure. Remove operations can affect any running iterator such that a removed element that has not yet
|
|
||||||
* been reached by the iterator will not appear in that iterator anymore.
|
|
||||||
*/
|
|
||||||
public
|
|
||||||
class ConcurrentSet<T> extends ConcurrentLinkedQueue2<T> {
|
|
||||||
private static final long serialVersionUID = -2729855178402529784L;
|
|
||||||
|
|
||||||
private static final AtomicLong id = new AtomicLong();
|
|
||||||
private final transient long ID = id.getAndIncrement();
|
|
||||||
|
|
||||||
|
|
||||||
private final Node<T> IN_PROGRESS_MARKER = new Node<T>(null);
|
|
||||||
private ConcurrentMap<T, Node<T>> entries;
|
|
||||||
|
|
||||||
public
|
|
||||||
ConcurrentSet() {
|
|
||||||
this(16, 0.75f, Runtime.getRuntime().availableProcessors());
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
ConcurrentSet(int size, float loadFactor, int stripeSize) {
|
|
||||||
super();
|
|
||||||
this.entries = new ConcurrentHashMap<>(size, loadFactor, stripeSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean add(T element) {
|
|
||||||
if (element == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// had to modify the super implementation so we publish Node<T> back
|
|
||||||
Node<T> alreadyPresent = this.entries.putIfAbsent(element, this.IN_PROGRESS_MARKER);
|
|
||||||
if (alreadyPresent == null) {
|
|
||||||
// this doesn't already exist
|
|
||||||
Node<T> offerNode = super.offerNode(element);
|
|
||||||
|
|
||||||
this.entries.put(element, offerNode);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean contains(Object element) {
|
|
||||||
if (element == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node<T> node;
|
|
||||||
while ((node = this.entries.get(element)) == this.IN_PROGRESS_MARKER) {
|
|
||||||
; // data race
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return node.item != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
int size() {
|
|
||||||
return this.entries.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean isEmpty() {
|
|
||||||
return super.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return TRUE if the element was successfully removed
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean remove(Object element) {
|
|
||||||
while (this.entries.get(element) == this.IN_PROGRESS_MARKER) {
|
|
||||||
; // data race
|
|
||||||
}
|
|
||||||
|
|
||||||
Node<T> node = this.entries.remove(element);
|
|
||||||
if (node == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node<T> pred = null;
|
|
||||||
for (Node<T> p = this.head; p != null; p = succ(p)) {
|
|
||||||
T item = p.item;
|
|
||||||
if (item != null &&
|
|
||||||
element.equals(item) &&
|
|
||||||
p.casItem(item, null)) {
|
|
||||||
Node<T> next = succ(p);
|
|
||||||
if (pred != null && next != null) {
|
|
||||||
pred.casNext(p, next);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
pred = p;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
Iterator<T> iterator() {
|
|
||||||
return new Itr2();
|
|
||||||
}
|
|
||||||
|
|
||||||
private
|
|
||||||
class Itr2 implements Iterator<T> {
|
|
||||||
/**
|
|
||||||
* Next node to return item for.
|
|
||||||
*/
|
|
||||||
private Node<T> nextNode;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Node of the last returned item, to support remove.
|
|
||||||
*/
|
|
||||||
private Node<T> lastRet;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* nextItem holds on to item fields because once we claim
|
|
||||||
* that an element exists in hasNext(), we must return it in
|
|
||||||
* the following next() call even if it was in the process of
|
|
||||||
* being removed when hasNext() was called.
|
|
||||||
*/
|
|
||||||
private T nextItem;
|
|
||||||
|
|
||||||
Itr2() {
|
|
||||||
advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Moves to next valid node and returns item to return for
|
|
||||||
* next(), or null if no such.
|
|
||||||
*/
|
|
||||||
private
|
|
||||||
T advance() {
|
|
||||||
this.lastRet = this.nextNode; // for removing items via iterator
|
|
||||||
T nextItem = this.nextItem;
|
|
||||||
|
|
||||||
Node<T> pred, p;
|
|
||||||
if (this.nextNode == null) {
|
|
||||||
p = first();
|
|
||||||
pred = null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
pred = this.nextNode;
|
|
||||||
p = succ(this.nextNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; ; ) {
|
|
||||||
if (p == null) {
|
|
||||||
this.nextNode = null;
|
|
||||||
this.nextItem = null;
|
|
||||||
return nextItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
T item = p.item;
|
|
||||||
if (item != null) {
|
|
||||||
this.nextNode = p;
|
|
||||||
this.nextItem = item;
|
|
||||||
return nextItem;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// skip over nulls
|
|
||||||
Node<T> next = succ(p);
|
|
||||||
if (pred != null && next != null) {
|
|
||||||
pred.casNext(p, next);
|
|
||||||
}
|
|
||||||
p = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean hasNext() {
|
|
||||||
return this.nextNode != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
T next() {
|
|
||||||
if (this.nextNode == null) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
return advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void remove() {
|
|
||||||
Node<T> l = this.lastRet;
|
|
||||||
if (l == null) {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
|
|
||||||
T value = l.item;
|
|
||||||
|
|
||||||
if (value != null) {
|
|
||||||
Map<T, Node<T>> entries2 = ConcurrentSet.this.entries;
|
|
||||||
while (entries2.get(value) == ConcurrentSet.this.IN_PROGRESS_MARKER) {
|
|
||||||
; // data race
|
|
||||||
}
|
|
||||||
|
|
||||||
entries2.remove(value);
|
|
||||||
|
|
||||||
// rely on a future traversal to relink.
|
|
||||||
l.item = null;
|
|
||||||
this.lastRet = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
Object[] toArray() {
|
|
||||||
return this.entries.keySet().toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
<T> T[] toArray(T[] a) {
|
|
||||||
return this.entries.keySet().toArray(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean containsAll(Collection<?> c) {
|
|
||||||
throw new UnsupportedOperationException("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean removeAll(Collection<?> c) {
|
|
||||||
throw new UnsupportedOperationException("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean retainAll(Collection<?> c) {
|
|
||||||
throw new UnsupportedOperationException("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
void clear() {
|
|
||||||
super.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + (int) (this.ID ^ this.ID >>> 32);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
boolean equals(Object obj) {
|
|
||||||
if (this == obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (obj == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (getClass() != obj.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ConcurrentSet other = (ConcurrentSet) obj;
|
|
||||||
if (this.ID != other.ID) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -37,28 +37,27 @@
|
|||||||
*/
|
*/
|
||||||
package dorkbox.util.messagebus.subscription;
|
package dorkbox.util.messagebus.subscription;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryo.util.IdentityMap;
|
||||||
import com.esotericsoftware.reflectasm.MethodAccess;
|
import com.esotericsoftware.reflectasm.MethodAccess;
|
||||||
import dorkbox.util.messagebus.common.MessageHandler;
|
import dorkbox.util.messagebus.common.MessageHandler;
|
||||||
import dorkbox.util.messagebus.dispatch.IHandlerInvocation;
|
import dorkbox.util.messagebus.dispatch.IHandlerInvocation;
|
||||||
import dorkbox.util.messagebus.dispatch.ReflectiveHandlerInvocation;
|
import dorkbox.util.messagebus.dispatch.ReflectiveHandlerInvocation;
|
||||||
import dorkbox.util.messagebus.dispatch.SynchronizedHandlerInvocation;
|
import dorkbox.util.messagebus.dispatch.SynchronizedHandlerInvocation;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.concurrent.ConcurrentSkipListSet;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A subscription is a thread-safe container that manages exactly one message handler of all registered
|
* A subscription is a container that manages exactly one message handler of all registered
|
||||||
* message listeners of the same class, i.e. all subscribed instances (excluding subclasses) of a SingleMessageHandler.class
|
* message listeners of the same class, i.e. all subscribed instances (excluding subclasses) of a message
|
||||||
* will be referenced in the subscription created for SingleMessageHandler.class.
|
* will be referenced in the subscription created for a message.
|
||||||
* <p/>
|
* <p/>
|
||||||
* There will be as many unique subscription objects per message listener class as there are message handlers
|
* There will be as many unique subscription objects per message listener class as there are message handlers
|
||||||
* defined in the message listeners class hierarchy.
|
* defined in the message listeners class hierarchy.
|
||||||
* <p/>
|
* <p/>
|
||||||
* The subscription provides functionality for message publication by means of delegation to the respective
|
* This class uses the "single writer principle", so that the subscription are only MODIFIED by a single thread,
|
||||||
* message dispatcher.
|
* but are READ by X number of threads (in a safe way). This uses object thread visibility/publication to work.
|
||||||
*
|
*
|
||||||
* @author bennidi
|
* @author bennidi
|
||||||
* @author dorkbox, llc
|
* @author dorkbox, llc
|
||||||
@ -66,42 +65,32 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
*/
|
*/
|
||||||
public final
|
public final
|
||||||
class Subscription {
|
class Subscription {
|
||||||
|
private static final int GROW_SIZE = 8;
|
||||||
|
|
||||||
private static final AtomicInteger ID_COUNTER = new AtomicInteger();
|
private static final AtomicInteger ID_COUNTER = new AtomicInteger();
|
||||||
public final int ID = ID_COUNTER.getAndIncrement();
|
public final int ID = ID_COUNTER.getAndIncrement();
|
||||||
|
|
||||||
|
|
||||||
// What is the listener class that created this subscription?
|
|
||||||
private final Class<?> listenerClass;
|
|
||||||
|
|
||||||
// the handler's metadata -> for each handler in a listener, a unique subscription context is created
|
// the handler's metadata -> for each handler in a listener, a unique subscription context is created
|
||||||
private final MessageHandler handler;
|
private final MessageHandler handler;
|
||||||
|
|
||||||
private final IHandlerInvocation invocation;
|
private final IHandlerInvocation invocation;
|
||||||
private final Collection<Object> listeners;
|
|
||||||
|
// NOTE: this is still inside the single-writer! can use the same techniques as subscription manager (for thread safe publication)
|
||||||
|
private int firstFreeSpot = 0; // only touched by a single thread
|
||||||
|
private volatile Object[] listeners = new Object[GROW_SIZE]; // only modified by a single thread
|
||||||
|
|
||||||
|
private final IdentityMap<Object, Integer> listenerMap = new IdentityMap<>(GROW_SIZE);
|
||||||
|
|
||||||
|
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||||
|
private static final AtomicReferenceFieldUpdater<Subscription, Object[]> listenersREF =
|
||||||
|
AtomicReferenceFieldUpdater.newUpdater(Subscription.class,
|
||||||
|
Object[].class,
|
||||||
|
"listeners");
|
||||||
|
|
||||||
|
|
||||||
public
|
public
|
||||||
Subscription(final Class<?> listenerClass, final MessageHandler handler) {
|
Subscription(final MessageHandler handler) {
|
||||||
this.listenerClass = listenerClass;
|
|
||||||
|
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
|
||||||
// this.listeners = Collections.newSetFromMap(new ConcurrentHashMap<Object, Boolean>(8)); // really bad performance
|
|
||||||
// this.listeners = new StrongConcurrentSetV8<Object>(16, 0.7F, 8);
|
|
||||||
|
|
||||||
///this is by far, the fastest
|
|
||||||
this.listeners = new ConcurrentSkipListSet<>(new Comparator() {
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
int compare(final Object o1, final Object o2) {
|
|
||||||
return Integer.compare(o1.hashCode(), o2.hashCode());
|
|
||||||
// return 0;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// this.listeners = new StrongConcurrentSet<Object>(16, 0.85F);
|
|
||||||
// this.listeners = new ConcurrentLinkedQueue2<Object>();
|
|
||||||
// this.listeners = new CopyOnWriteArrayList<Object>();
|
|
||||||
// this.listeners = new CopyOnWriteArraySet<Object>(); // not very good
|
|
||||||
|
|
||||||
IHandlerInvocation invocation = new ReflectiveHandlerInvocation();
|
IHandlerInvocation invocation = new ReflectiveHandlerInvocation();
|
||||||
if (handler.isSynchronized()) {
|
if (handler.isSynchronized()) {
|
||||||
invocation = new SynchronizedHandlerInvocation(invocation);
|
invocation = new SynchronizedHandlerInvocation(invocation);
|
||||||
@ -110,39 +99,88 @@ class Subscription {
|
|||||||
this.invocation = invocation;
|
this.invocation = invocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only used by unit-tests to verify that the subscriptionManager is working correctly
|
|
||||||
public
|
|
||||||
Class<?> getListenerClass() {
|
|
||||||
return listenerClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
public
|
||||||
MessageHandler getHandler() {
|
MessageHandler getHandler() {
|
||||||
return handler;
|
return handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
boolean isEmpty() {
|
void subscribe(final Object listener) {
|
||||||
return this.listeners.isEmpty();
|
// single writer principle!
|
||||||
|
|
||||||
|
Object[] localListeners = listenersREF.get(this);
|
||||||
|
|
||||||
|
final int length = localListeners.length;
|
||||||
|
int spotToPlace = firstFreeSpot;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (spotToPlace >= length) {
|
||||||
|
// if we couldn't find a place to put the listener, grow the array, but it is never shrunk
|
||||||
|
localListeners = Arrays.copyOf(localListeners, length + GROW_SIZE, Object[].class);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
if (localListeners[spotToPlace] == null) {
|
||||||
void subscribe(Object listener) {
|
break;
|
||||||
this.listeners.add(listener);
|
}
|
||||||
|
spotToPlace++;
|
||||||
|
}
|
||||||
|
|
||||||
|
listenerMap.put(listener, spotToPlace);
|
||||||
|
localListeners[spotToPlace] = listener;
|
||||||
|
|
||||||
|
// mark this spot as taken, so the next subscribe starts out a little ahead
|
||||||
|
firstFreeSpot = spotToPlace + 1;
|
||||||
|
listenersREF.lazySet(this, localListeners);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return TRUE if the element was removed
|
* @return TRUE if the element was removed
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
boolean unsubscribe(Object existingListener) {
|
boolean unsubscribe(final Object listener) {
|
||||||
return this.listeners.remove(existingListener);
|
// single writer principle!
|
||||||
|
|
||||||
|
final Integer integer = listenerMap.remove(listener);
|
||||||
|
Object[] localListeners = listenersREF.get(this);
|
||||||
|
|
||||||
|
if (integer != null) {
|
||||||
|
final int index = integer;
|
||||||
|
firstFreeSpot = index;
|
||||||
|
localListeners[index] = null;
|
||||||
|
listenersREF.lazySet(this, localListeners);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (int i = 0; i < localListeners.length; i++) {
|
||||||
|
if (localListeners[i] == listener) {
|
||||||
|
firstFreeSpot = i;
|
||||||
|
localListeners[i] = null;
|
||||||
|
listenersREF.lazySet(this, localListeners);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// only used in unit-test
|
firstFreeSpot = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* only used in unit tests
|
||||||
|
*/
|
||||||
public
|
public
|
||||||
int size() {
|
int size() {
|
||||||
return this.listeners.size();
|
// since this is ONLY used in unit tests, we count how many are non-null
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < listeners.length; i++) {
|
||||||
|
if (listeners[i] != null) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
@ -151,62 +189,62 @@ class Subscription {
|
|||||||
final int handleIndex = this.handler.getMethodIndex();
|
final int handleIndex = this.handler.getMethodIndex();
|
||||||
final IHandlerInvocation invocation = this.invocation;
|
final IHandlerInvocation invocation = this.invocation;
|
||||||
|
|
||||||
Iterator<Object> iterator;
|
|
||||||
Object listener;
|
Object listener;
|
||||||
|
final Object[] localListeners = listenersREF.get(this);
|
||||||
for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
for (int i = 0; i < localListeners.length; i++) {
|
||||||
listener = iterator.next();
|
listener = localListeners[i];
|
||||||
|
if (listener != null) {
|
||||||
invocation.invoke(listener, handler, handleIndex, message);
|
invocation.invoke(listener, handler, handleIndex, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
void publish(final Object message1, final Object message2) throws Throwable {
|
void publish(final Object message1, final Object message2) throws Throwable {
|
||||||
final MethodAccess handler = this.handler.getHandler();
|
// final MethodAccess handler = this.handler.getHandler();
|
||||||
final int handleIndex = this.handler.getMethodIndex();
|
// final int handleIndex = this.handler.getMethodIndex();
|
||||||
final IHandlerInvocation invocation = this.invocation;
|
// final IHandlerInvocation invocation = this.invocation;
|
||||||
|
//
|
||||||
Iterator<Object> iterator;
|
// Iterator<Object> iterator;
|
||||||
Object listener;
|
// Object listener;
|
||||||
|
//
|
||||||
for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
// for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
||||||
listener = iterator.next();
|
// listener = iterator.next();
|
||||||
|
//
|
||||||
invocation.invoke(listener, handler, handleIndex, message1, message2);
|
// invocation.invoke(listener, handler, handleIndex, message1, message2);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
void publish(final Object message1, final Object message2, final Object message3) throws Throwable {
|
void publish(final Object message1, final Object message2, final Object message3) throws Throwable {
|
||||||
final MethodAccess handler = this.handler.getHandler();
|
// final MethodAccess handler = this.handler.getHandler();
|
||||||
final int handleIndex = this.handler.getMethodIndex();
|
// final int handleIndex = this.handler.getMethodIndex();
|
||||||
final IHandlerInvocation invocation = this.invocation;
|
// final IHandlerInvocation invocation = this.invocation;
|
||||||
|
//
|
||||||
Iterator<Object> iterator;
|
// Iterator<Object> iterator;
|
||||||
Object listener;
|
// Object listener;
|
||||||
|
//
|
||||||
for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
// for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
||||||
listener = iterator.next();
|
// listener = iterator.next();
|
||||||
|
//
|
||||||
invocation.invoke(listener, handler, handleIndex, message1, message2, message3);
|
// invocation.invoke(listener, handler, handleIndex, message1, message2, message3);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public
|
public
|
||||||
void publish(final Object... messages) throws Throwable {
|
void publish(final Object... messages) throws Throwable {
|
||||||
final MethodAccess handler = this.handler.getHandler();
|
// final MethodAccess handler = this.handler.getHandler();
|
||||||
final int handleIndex = this.handler.getMethodIndex();
|
// final int handleIndex = this.handler.getMethodIndex();
|
||||||
final IHandlerInvocation invocation = this.invocation;
|
// final IHandlerInvocation invocation = this.invocation;
|
||||||
|
//
|
||||||
Iterator<Object> iterator;
|
// Iterator<Object> iterator;
|
||||||
Object listener;
|
// Object listener;
|
||||||
|
//
|
||||||
for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
// for (iterator = this.listeners.iterator(); iterator.hasNext(); ) {
|
||||||
listener = iterator.next();
|
// listener = iterator.next();
|
||||||
|
//
|
||||||
invocation.invoke(listener, handler, handleIndex, messages);
|
// invocation.invoke(listener, handler, handleIndex, messages);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -218,7 +256,7 @@ class Subscription {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
boolean equals(Object obj) {
|
boolean equals(final Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -65,9 +65,7 @@ class ReflectionUtils {
|
|||||||
ArrayList<Method> methods = new ArrayList<Method>();
|
ArrayList<Method> methods = new ArrayList<Method>();
|
||||||
|
|
||||||
getMethods(target, methods);
|
getMethods(target, methods);
|
||||||
final Method[] array = new Method[methods.size()];
|
return methods.toArray(new Method[0]);
|
||||||
methods.toArray(array);
|
|
||||||
return array;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static
|
private static
|
||||||
@ -124,9 +122,7 @@ class ReflectionUtils {
|
|||||||
collectInterfaces(from, superclasses);
|
collectInterfaces(from, superclasses);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Class<?>[] classes = new Class<?>[superclasses.size()];
|
return superclasses.toArray(new Class<?>[0]);
|
||||||
superclasses.toArray(classes);
|
|
||||||
return classes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class[] getSuperTypes(Class from) {
|
public static Class[] getSuperTypes(Class from) {
|
||||||
|
@ -86,7 +86,7 @@ class VarArgUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
varArgSubs = new Subscription[varArgSubsAsList.size()];
|
varArgSubs = new Subscription[0];
|
||||||
varArgSubsAsList.toArray(varArgSubs);
|
varArgSubsAsList.toArray(varArgSubs);
|
||||||
|
|
||||||
local.put(messageClass, varArgSubs);
|
local.put(messageClass, varArgSubs);
|
||||||
|
@ -25,8 +25,23 @@ public class MultiMessageTest extends MessageBusTest {
|
|||||||
|
|
||||||
MultiListener listener1 = new MultiListener();
|
MultiListener listener1 = new MultiListener();
|
||||||
bus.subscribe(listener1);
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
bus.subscribe(listener1);
|
||||||
|
|
||||||
|
bus.unsubscribe(listener1);
|
||||||
|
bus.unsubscribe(listener1);
|
||||||
|
bus.unsubscribe(listener1);
|
||||||
bus.unsubscribe(listener1);
|
bus.unsubscribe(listener1);
|
||||||
|
|
||||||
|
bus.publish("s");
|
||||||
|
bus.publish("s");
|
||||||
|
bus.publish("s");
|
||||||
bus.publish("s");
|
bus.publish("s");
|
||||||
bus.publish("s", "s");
|
bus.publish("s", "s");
|
||||||
bus.publish("s", "s", "s");
|
bus.publish("s", "s", "s");
|
||||||
@ -84,40 +99,40 @@ public class MultiMessageTest extends MessageBusTest {
|
|||||||
System.err.println("match String");
|
System.err.println("match String");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Handler
|
// @Handler
|
||||||
public void handleSync(String o1, String o2) {
|
// public void handleSync(String o1, String o2) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match String, String");
|
// System.err.println("match String, String");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Handler
|
// @Handler
|
||||||
public void handleSync(String o1, String o2, String o3) {
|
// public void handleSync(String o1, String o2, String o3) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match String, String, String");
|
// System.err.println("match String, String, String");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Handler
|
// @Handler
|
||||||
public void handleSync(Integer o1, Integer o2, String o3) {
|
// public void handleSync(Integer o1, Integer o2, String o3) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match Integer, Integer, String");
|
// System.err.println("match Integer, Integer, String");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Handler(acceptVarargs = true)
|
// @Handler(acceptVarargs = true)
|
||||||
public void handleSync(String... o) {
|
// public void handleSync(String... o) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match String[]");
|
// System.err.println("match String[]");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Handler
|
// @Handler
|
||||||
public void handleSync(Integer... o) {
|
// public void handleSync(Integer... o) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match Integer[]");
|
// System.err.println("match Integer[]");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Handler(acceptVarargs = true)
|
// @Handler(acceptVarargs = true)
|
||||||
public void handleSync(Object... o) {
|
// public void handleSync(Object... o) {
|
||||||
count.getAndIncrement();
|
// count.getAndIncrement();
|
||||||
System.err.println("match Object[]");
|
// System.err.println("match Object[]");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,15 +61,22 @@ public class SubscriptionManagerTest extends AssertSupport {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIMessageListener() {
|
public void testIMessageListener() {
|
||||||
ListenerFactory listeners = listeners(IMessageListener.DefaultListener.class, IMessageListener.DisabledListener.class,
|
ListenerFactory listeners = listeners(IMessageListener.DefaultListener.class
|
||||||
IMessageListener.NoSubtypesListener.class);
|
// ,
|
||||||
|
// IMessageListener.DisabledListener.class,
|
||||||
|
// IMessageListener.NoSubtypesListener.class
|
||||||
|
);
|
||||||
|
|
||||||
SubscriptionValidator expectedSubscriptions = new SubscriptionValidator(listeners).listener(IMessageListener.DefaultListener.class)
|
SubscriptionValidator expectedSubscriptions = new SubscriptionValidator(listeners);
|
||||||
.handles(IMessage.class, AbstractMessage.class,
|
expectedSubscriptions.listener(IMessageListener.DefaultListener.class)
|
||||||
|
.handles(IMessage.class,
|
||||||
|
AbstractMessage.class,
|
||||||
IMultipartMessage.class,
|
IMultipartMessage.class,
|
||||||
StandardMessage.class,
|
StandardMessage.class,
|
||||||
MessageTypes.class).listener(
|
MessageTypes.class);
|
||||||
IMessageListener.NoSubtypesListener.class).handles(IMessage.class);
|
|
||||||
|
// expectedSubscriptions.listener(IMessageListener.NoSubtypesListener.class)
|
||||||
|
// .handles(IMessage.class);
|
||||||
|
|
||||||
runTestWith(listeners, expectedSubscriptions);
|
runTestWith(listeners, expectedSubscriptions);
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,7 @@ public class SubscriptionValidator extends AssertSupport {
|
|||||||
private SubscriptionValidator expect(Class subscriber, Class messageType) {
|
private SubscriptionValidator expect(Class subscriber, Class messageType) {
|
||||||
this.validations.add(new ValidationEntry(messageType, subscriber));
|
this.validations.add(new ValidationEntry(messageType, subscriber));
|
||||||
this.messageTypes.add(messageType);
|
this.messageTypes.add(messageType);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,8 +123,11 @@ public class SubscriptionValidator extends AssertSupport {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Expectation {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Expectation {
|
||||||
private Class listener;
|
private Class listener;
|
||||||
|
|
||||||
private Expectation(Class listener) {
|
private Expectation(Class listener) {
|
||||||
@ -134,24 +138,19 @@ public class SubscriptionValidator extends AssertSupport {
|
|||||||
for (Class message : messages) {
|
for (Class message : messages) {
|
||||||
expect(this.listener, message);
|
expect(this.listener, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return SubscriptionValidator.this;
|
return SubscriptionValidator.this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class ValidationEntry {
|
private class ValidationEntry {
|
||||||
|
|
||||||
|
|
||||||
private Class subscriber;
|
private Class subscriber;
|
||||||
|
|
||||||
private Class messageType;
|
private Class messageType;
|
||||||
|
|
||||||
private ValidationEntry(Class messageType, Class subscriber) {
|
private ValidationEntry(Class messageType, Class subscriber) {
|
||||||
this.messageType = messageType;
|
this.messageType = messageType;
|
||||||
this.subscriber = subscriber;
|
this.subscriber = subscriber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,22 +33,21 @@ import dorkbox.util.messagebus.messages.IMessage;
|
|||||||
public class IMessageListener {
|
public class IMessageListener {
|
||||||
|
|
||||||
private static abstract class BaseListener {
|
private static abstract class BaseListener {
|
||||||
|
|
||||||
@Handler
|
@Handler
|
||||||
public void handle(IMessage message){
|
public void handle(IMessage message){
|
||||||
message.handled(this.getClass());
|
message.handled(this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DefaultListener extends BaseListener {
|
|
||||||
|
|
||||||
|
public static class DefaultListener extends BaseListener {
|
||||||
@Override
|
@Override
|
||||||
public void handle(IMessage message){
|
public void handle(IMessage message){
|
||||||
super.handle(message);
|
super.handle(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class NoSubtypesListener extends BaseListener {
|
public static class NoSubtypesListener extends BaseListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,7 +59,6 @@ public class IMessageListener {
|
|||||||
|
|
||||||
|
|
||||||
public static class DisabledListener extends BaseListener {
|
public static class DisabledListener extends BaseListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Handler(enabled = false)
|
@Handler(enabled = false)
|
||||||
public void handle(IMessage message){
|
public void handle(IMessage message){
|
||||||
@ -68,6 +66,4 @@ public class IMessageListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,18 +13,23 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.messagebus;
|
package dorkbox.util.messagebus.queuePerf;
|
||||||
|
|
||||||
import dorkbox.util.messagebus.annotations.Handler;
|
import dorkbox.util.messagebus.annotations.Handler;
|
||||||
import dorkbox.util.messagebus.common.MessageHandler;
|
import dorkbox.util.messagebus.common.MessageHandler;
|
||||||
import dorkbox.util.messagebus.common.StrongConcurrentSet;
|
import dorkbox.util.messagebus.common.StrongConcurrentSet;
|
||||||
import dorkbox.util.messagebus.common.StrongConcurrentSetV8;
|
import dorkbox.util.messagebus.common.StrongConcurrentSetV8;
|
||||||
import dorkbox.util.messagebus.common.thread.ConcurrentLinkedQueue2;
|
import dorkbox.util.messagebus.common.thread.ConcurrentLinkedQueue2;
|
||||||
import dorkbox.util.messagebus.common.thread.ConcurrentSet;
|
|
||||||
import dorkbox.util.messagebus.subscription.Subscription;
|
import dorkbox.util.messagebus.subscription.Subscription;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.*;
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.LinkedTransferQueue;
|
import java.util.concurrent.LinkedTransferQueue;
|
||||||
@ -64,7 +69,6 @@ class PerfTest_Collections {
|
|||||||
System.err.println("Done");
|
System.err.println("Done");
|
||||||
|
|
||||||
bench(size, new ArrayList<Subscription>(size * 2));
|
bench(size, new ArrayList<Subscription>(size * 2));
|
||||||
bench(size, new ConcurrentSet<Subscription>(size * 2, LOAD_FACTOR, 5));
|
|
||||||
bench(size, new ConcurrentLinkedQueue2<Subscription>());
|
bench(size, new ConcurrentLinkedQueue2<Subscription>());
|
||||||
bench(size, new ConcurrentLinkedQueue<Subscription>());
|
bench(size, new ConcurrentLinkedQueue<Subscription>());
|
||||||
bench(size, new LinkedTransferQueue<Subscription>());
|
bench(size, new LinkedTransferQueue<Subscription>());
|
Loading…
Reference in New Issue
Block a user