Skip to content

Commit cba7ebe

Browse files
decrease thread yield frequency in ConcurrentBag.unreserve()
1 parent 22cc9bd commit cba7ebe

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
HikariCP Changes
22

3+
Changes in 7.0.2
4+
5+
* decrease thread yield frequency in ConcurrentBag.unreserve()
6+
37
Changes in 7.0.1
48

59
* merged #2346 fix regression with setSchema behavior

src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @param <T> the templated type to store in the bag
5858
* @hidden
5959
*/
60+
@SuppressWarnings("DuplicatedCode")
6061
public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseable
6162
{
6263
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentBag.class);
@@ -318,8 +319,16 @@ public void unreserve(final T bagEntry)
318319
{
319320
if (bagEntry.compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) {
320321
// spin until a thread takes it or none are waiting
321-
while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE && !handoffQueue.offer(bagEntry)) {
322-
Thread.yield();
322+
for (int i = 1, waiting = waiters.get(); waiting > 0; i++, waiting = waiters.get()) {
323+
if (bagEntry.getState() != STATE_NOT_IN_USE || handoffQueue.offer(bagEntry)) {
324+
return;
325+
}
326+
else if ((i & 0xff) == 0xff || (waiting > 1 && i % waiting == 0)) {
327+
parkNanos(MICROSECONDS.toNanos(10));
328+
}
329+
else {
330+
Thread.yield();
331+
}
323332
}
324333
}
325334
else {

0 commit comments

Comments
 (0)