Skip to content

Commit

Permalink
Improve the performance of CQueue
Browse files Browse the repository at this point in the history
Signed-off-by: Keshava Munegowda <keshava.gowda@gmail.com>
  • Loading branch information
kmgowda committed May 31, 2024
1 parent 239ae6a commit 46587eb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions perl/src/main/java/io/perl/api/impl/CQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T> node = new Node<>(data);
Expand All @@ -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);
/*
Expand Down

0 comments on commit 46587eb

Please sign in to comment.