Skip to content

Commit

Permalink
do bound checking in an atomic function to avoid exposing illegal values
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Jul 9, 2021
1 parent 1173d0d commit 8578783
Showing 1 changed file with 7 additions and 13 deletions.
Expand Up @@ -83,11 +83,8 @@ public boolean isDirect()
*/
void acquire()
{
if (references.getAndIncrement() != 0)
{
references.decrementAndGet();
if (references.getAndUpdate(c -> c == 0 ? 1 : c) != 0)
throw new IllegalStateException("re-pooled while still used " + this);
}
}

/**
Expand All @@ -96,11 +93,8 @@ void acquire()
@Override
public void retain()
{
if (references.getAndIncrement() == 0)
{
references.decrementAndGet();
if (references.getAndUpdate(c -> c == 0 ? 0 : c + 1) == 0)
throw new IllegalStateException("released " + this);
}
}

/**
Expand All @@ -109,12 +103,12 @@ public void retain()
*/
public boolean release()
{
int ref = references.decrementAndGet();
if (ref < 0)
int ref = references.updateAndGet(c ->
{
references.incrementAndGet();
throw new IllegalStateException("already released " + this);
}
if (c == 0)
throw new IllegalStateException("already released " + this);
return c - 1;
});
if (ref == 0)
{
lastUpdate.setOpaque(System.nanoTime());
Expand Down

0 comments on commit 8578783

Please sign in to comment.