Skip to content

Commit f1d7cc1

Browse files
authored
Add spin-wait hint to BlockingMpscQueue producer-side busy loops (#4831)
1 parent 451b6a5 commit f1d7cc1

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/collections/BlockingMpscQueue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ public BlockingMpscQueue(int size) {
3434

3535
@Override
3636
public void put(T e) throws InterruptedException {
37+
int idleCounter = 0;
3738
while (!this.relaxedOffer(e)) {
3839
// Do busy-spin loop
3940
if (Thread.interrupted()) {
4041
throw new InterruptedException();
4142
}
43+
44+
idleCounter = WAIT_STRATEGY.idle(idleCounter);
4245
}
4346
}
4447

4548
@Override
4649
public boolean offer(T e, long timeout, TimeUnit unit) throws InterruptedException {
4750
long absoluteEndTime = System.nanoTime() + unit.toNanos(timeout);
4851

52+
int idleCounter = 0;
4953
while (!this.relaxedOffer(e)) {
5054
// Do busy-spin loop
5155

@@ -56,6 +60,8 @@ public boolean offer(T e, long timeout, TimeUnit unit) throws InterruptedExcepti
5660
if (Thread.interrupted()) {
5761
throw new InterruptedException();
5862
}
63+
64+
idleCounter = WAIT_STRATEGY.idle(idleCounter);
5965
}
6066

6167
return true;

0 commit comments

Comments
 (0)