Skip to content

Commit

Permalink
[Java] Allow AtomicCounter to be disconnected from the CountersManage…
Browse files Browse the repository at this point in the history
…r which allocated it.
  • Loading branch information
mjpt777 committed Feb 21, 2020
1 parent baa0cbc commit f50e8fd
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AtomicCounter implements AutoCloseable
private final int id;
private final long addressOffset;
private final byte[] byteArray;
private final CountersManager countersManager;
private CountersManager countersManager;

@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final ByteBuffer byteBuffer; // retained to keep the buffer from being GC'ed
Expand Down Expand Up @@ -76,6 +76,29 @@ public int id()
return id;
}

/**
* Disconnect from {@link CountersManager} if allocated so it can be closed without freeing the slot.
*/
public void disconnectCountersManager()
{
countersManager = null;
}

/**
* Close counter and free the counter slot for reuse of connected to {@link CountersManager}.
*/
public void close()
{
if (!isClosed)
{
isClosed = true;
if (null != countersManager)
{
countersManager.free(id);
}
}
}

/**
* Has this counter been closed?
*
Expand Down Expand Up @@ -291,27 +314,13 @@ public boolean proposeMaxOrdered(final long proposedValue)
return updated;
}

/**
* Free the counter slot for reuse.
*/
public void close()
{
if (!isClosed)
{
isClosed = true;
if (null != countersManager)
{
countersManager.free(id);
}
}
}

public String toString()
{
return "AtomicCounter{" +
"isClosed=" + isClosed() +
", id=" + id +
", value=" + (isClosed() ? -1 : get()) +
", countersManager=" + countersManager +
'}';
}
}

0 comments on commit f50e8fd

Please sign in to comment.