Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.*;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.locks.LockSupport.parkNanos;

/**
* This is a specialized concurrent bag that achieves superior performance
Expand Down Expand Up @@ -188,17 +187,16 @@ public void requite(final T bagEntry)
{
bagEntry.setState(STATE_NOT_IN_USE);

for (int i = 1, waiting = waiters.get(); waiting > 0; i++, waiting = waiters.get()) {
if (bagEntry.getState() != STATE_NOT_IN_USE || handoffQueue.offer(bagEntry)) {
return;
}
else if ((i & 0xff) == 0xff || (waiting > 1 && i % waiting == 0)) {
parkNanos(MICROSECONDS.toNanos(10));
}
else {
Thread.yield();
try {
while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE) {
if (handoffQueue.offer(bagEntry, 10, MICROSECONDS)) {
return;
}
}
}
catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}

final var threadLocalEntries = this.threadLocalList.get();
if (threadLocalEntries.size() < 16) {
Expand Down Expand Up @@ -318,18 +316,16 @@ public boolean reserve(final T bagEntry)
public void unreserve(final T bagEntry)
{
if (bagEntry.compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) {
// spin until a thread takes it or none are waiting
for (int i = 1, waiting = waiters.get(); waiting > 0; i++, waiting = waiters.get()) {
if (bagEntry.getState() != STATE_NOT_IN_USE || handoffQueue.offer(bagEntry)) {
return;
}
else if ((i & 0xff) == 0xff || (waiting > 1 && i % waiting == 0)) {
parkNanos(MICROSECONDS.toNanos(10));
}
else {
Thread.yield();
try {
while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE) {
if (handoffQueue.offer(bagEntry, 10, MICROSECONDS)) {
return;
}
}
}
catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
else {
LOGGER.warn("Attempt to relinquish an object to the bag that was not reserved: {}", bagEntry);
Expand Down