Skip to content

Commit

Permalink
#12790 quick fix
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Mar 3, 2025
1 parent dada868 commit e1a889d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private static class QueuedEntry<P> implements Entry<P>
// val/false -> idle
// val/true -> in use
private final AtomicMarkableReference<P> pooled = new AtomicMarkableReference<>(null, false);
private P removedReference; // Only written before setting 'pooled', only read after getting 'pooled'.

private QueuedEntry(QueuedPool<P> pool)
{
Expand Down Expand Up @@ -243,7 +244,8 @@ public boolean enable(P pooled, boolean acquire)
@Override
public P getPooled()
{
return pooled.getReference();
P reference = pooled.getReference();
return reference != null ? reference : removedReference;
}

void acquire()
Expand Down Expand Up @@ -273,6 +275,7 @@ public boolean remove()
P p = pooled.get(inUse);
if (p == null && inUse[0])
return false;
removedReference = p;
pooled.set(null, true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class ArrayByteBufferPoolTest
{
@Test
public void testRemoveAndReleaseCompoundPool()
public void testRemoveAndReleaseFromCompoundPool()
{
int bufferCount = ConcurrentPool.OPTIMAL_MAX_SIZE * 2;
List<RetainableByteBuffer> rbbs = new ArrayList<>();
Expand All @@ -61,8 +61,12 @@ public void testRemoveAndReleaseCompoundPool()
RetainableByteBuffer rbb = pool.acquire(1, false);
rbbs.add(rbb);
}

rbbs.forEach(pool::removeAndRelease);

for (RetainableByteBuffer rbb : rbbs)
{
assertThat(rbb.getByteBuffer(), notNullValue());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public void testAcquireRelease()
assertThat(e2.release(), is(false));
}

@Test
public void testReleaseBeforeRemove()
{
Pool<String> pool = new QueuedPool<>(1);
pool.reserve().enable("aaa", false);

Pool.Entry<String> e1 = pool.acquire();

assertThat(e1.remove(), is(true));
assertThat(e1.remove(), is(false));
assertThat(e1.release(), is(false));
assertThat(e1.getPooled(), notNullValue());
}

@Test
public void testRemoveBeforeRelease()
{
Expand All @@ -85,6 +99,7 @@ public void testRemoveBeforeRelease()
assertThat(e1.remove(), is(true));
assertThat(e1.remove(), is(false));
assertThat(e1.release(), is(false));
assertThat(e1.getPooled(), notNullValue());
}

@Test
Expand Down

0 comments on commit e1a889d

Please sign in to comment.