diff --git a/perl/src/main/java/io/perl/api/impl/AtomicQueue.java b/perl/src/main/java/io/perl/api/impl/AtomicQueue.java index 90127ddf..a1dd3090 100644 --- a/perl/src/main/java/io/perl/api/impl/AtomicQueue.java +++ b/perl/src/main/java/io/perl/api/impl/AtomicQueue.java @@ -41,8 +41,7 @@ public AtomicQueue() { this.tail = new AtomicReference<>(firstNode); } - @Override - public T poll() { + public T pollOnce() { final Node curHead = head.getAndSet(null); if (curHead == null) { return null; @@ -56,6 +55,25 @@ public T poll() { return nxt.item; } + @Override + public T poll() { + Node curHead = head.get(); + Node nxt = curHead.next.get(); + + while ( nxt != null && !head.compareAndSet(curHead, nxt) ) { + curHead = head.get(); + nxt = curHead.next.get(); + } + + if (nxt == null) { + return null; + } + curHead.next.set(null); + + return nxt.item; + } + + @Override public boolean add(T data) { final Node node = new Node<>(data); @@ -66,6 +84,7 @@ public boolean add(T data) { @Override public void clear() { + firstNode.next.set(null); Node first = head.getAndSet(firstNode); tail.set(first); /* diff --git a/perl/src/main/java/io/perl/api/impl/CQueue.java b/perl/src/main/java/io/perl/api/impl/CQueue.java index 26f2a7df..6883e2ed 100644 --- a/perl/src/main/java/io/perl/api/impl/CQueue.java +++ b/perl/src/main/java/io/perl/api/impl/CQueue.java @@ -58,8 +58,7 @@ public CQueue() { this.tail = firstNode; } - @Override - public T poll() { + public T pollOnce() { final Object cur = NEXT.getAndSet(head, null); if (cur == null) { return null; @@ -68,6 +67,23 @@ public T poll() { return (T) ITEM.getAndSet(cur, null); } + @Override + public T poll() { + Object curHead = HEAD.get(this); + Object nxt = NEXT.get(curHead); + + while (nxt != null && !HEAD.compareAndSet(this, curHead, nxt)) { + curHead = HEAD.get(this); + nxt = NEXT.get(curHead); + } + + if (nxt == null) { + return null; + } + NEXT.set(curHead, null); + return (T) ITEM.getAndSet(nxt, null); + } + @Override public boolean add(T data) { final Node node = new Node<>(data); @@ -78,6 +94,7 @@ public boolean add(T data) { @Override public void clear() { + NEXT.set(firstNode, null); Object first = HEAD.getAndSet(this, firstNode); TAIL.set(this, firstNode); /*