public abstract class AbstractReferenceCounted extends Object implements ReferenceCountedTracer, ReferenceOwner, SingleThreadedChecked
This class provides the common functionality required for implementing reference counting mechanisms in resources, such as managing the number of references and releasing resources when they are no longer needed.
Modifier and Type | Field and Description |
---|---|
protected MonitorReferenceCounted |
referenceCounted |
protected static int |
WARN_COUNT |
protected static long |
WARN_NS |
INIT, TMP
DISABLE_SINGLE_THREADED_CHECK
Modifier | Constructor and Description |
---|---|
protected |
AbstractReferenceCounted()
Constructs an AbstractReferenceCounted with default monitoring.
|
protected |
AbstractReferenceCounted(boolean monitored)
Constructs an AbstractReferenceCounted.
|
Modifier and Type | Method and Description |
---|---|
void |
addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Adds a
ReferenceChangeListener that will be notified whenever the reference count changes. |
static void |
assertReferencesReleased()
Asserts that all references have been released.
|
protected void |
backgroundPerformRelease()
Performs the release operation in the background.
|
protected boolean |
canReleaseInBackground()
Returns whether the resource can be released in the background.
|
void |
clearUsedByThread()
Deprecated.
Use singleThreadedCheckReset() instead
|
StackTrace |
createdHere()
Returns the stack trace for where this resource was created.
|
static void |
disableReferenceTracing()
Disables reference tracing.
|
static void |
enableReferenceTracing()
Enables reference tracing.
|
protected abstract void |
performRelease()
Releases the resource.
|
int |
refCount()
Retrieves the current reference count.
|
void |
referenceCountedUnmonitored(boolean unmonitored) |
int |
referenceId()
Returns the unique reference ID for this resource.
|
void |
release(ReferenceOwner id)
Decrements the reference count by one.
|
void |
releaseLast(ReferenceOwner id)
Releases the last reference to the resource.
|
void |
removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Removes a
ReferenceChangeListener previously added via ReferenceCounted.addReferenceChangeListener(ReferenceChangeListener) . |
void |
reserve(ReferenceOwner id)
Increments the reference count by one, indicating that the resource is now shared among multiple owners.
|
boolean |
reservedBy(ReferenceOwner owner)
Deprecated.
|
void |
reserveTransfer(ReferenceOwner from,
ReferenceOwner to)
Transfers a reference from one owner to another.
|
void |
singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
Sets the flag to disable the single-threaded check.
|
void |
singleThreadedCheckReset()
Resets the thread-safety check state.
|
protected boolean |
threadSafetyCheck(boolean isUsed) |
void |
throwExceptionIfNotReleased()
Throws an exception if the resource has already been released.
|
void |
throwExceptionIfReleased()
Throws an exception if the resource has been released.
|
@NotNull String |
toString()
Returns a string representation of the object including its reference name.
|
boolean |
tryReserve(ReferenceOwner id)
Attempts to reserve the resource without throwing an exception if it is already released.
|
static void |
unmonitor(ReferenceCounted counted)
Marks a reference-counted resource as unmonitored.
|
void |
warnAndReleaseIfNotReleased()
If not released, logs a warning and releases the resource.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
onReleased
releaseLast
referenceName, temporary
protected static final long WARN_NS
protected static final int WARN_COUNT
protected final transient MonitorReferenceCounted referenceCounted
protected AbstractReferenceCounted()
protected AbstractReferenceCounted(boolean monitored)
monitored
- If true, the resource will be monitored for reference counted release.public static void enableReferenceTracing()
public static void disableReferenceTracing()
NOTE: The resources will still be released appropriately, however if detailed tracing won't be recorded
public static void assertReferencesReleased()
public static void unmonitor(ReferenceCounted counted)
NOTE: The resource will still be released appropriately, however it won't give a warning if it is not.
counted
- the resource to unmonitor.public int referenceId()
referenceId
in interface ReferenceOwner
public StackTrace createdHere()
createdHere
in interface ReferenceCountedTracer
public void throwExceptionIfNotReleased() throws IllegalStateException
throwExceptionIfNotReleased
in interface ReferenceCountedTracer
ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
- If the object has not been released (i.e., its reference count is greater than 0).protected void backgroundPerformRelease()
protected boolean canReleaseInBackground()
true
if the resource can be released in the background, false
otherwise.protected abstract void performRelease() throws IllegalStateException
IllegalStateException
- if the resource cannot be released.public void reserve(ReferenceOwner id) throws ClosedIllegalStateException, ThreadingIllegalStateException
When tracing is enabled, it checks the same owner doesn't try to reserve it twice (without releasing it in the meantime)
reserve
in interface ReferenceCounted
id
- The reference owner.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waypublic void release(ReferenceOwner id) throws ClosedIllegalStateException
When tracing is enabled, it checks the resource was an owner, and doesn't attempt to release twice.
release
in interface ReferenceCounted
id
- The reference owner.ClosedIllegalStateException
- If the resource has been released or closed.public void releaseLast(ReferenceOwner id) throws ClosedIllegalStateException
When tracing is enabled, it checks the resource was an owner, and doesn't attempt to release twice.
releaseLast
in interface ReferenceCounted
id
- The reference owner.ClosedIllegalStateException
- If the resource has been released or closed.public boolean tryReserve(ReferenceOwner id) throws ClosedIllegalStateException, IllegalArgumentException
tryReserve
in interface ReferenceCounted
id
- The reference owner.true
if the reservation was successful, false
otherwise.ClosedIllegalStateException
- If the resource has been released or closed.IllegalArgumentException
- If the reference owner is not valid.public void reserveTransfer(ReferenceOwner from, ReferenceOwner to) throws ClosedIllegalStateException, ThreadingIllegalStateException
reserveTransfer
in interface ReferenceCounted
from
- The current reference owner.to
- The new reference owner.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waypublic int refCount()
refCount
in interface ReferenceCounted
public void throwExceptionIfReleased() throws ClosedIllegalStateException
throwExceptionIfReleased
in interface ReferenceCountedTracer
ClosedIllegalStateException
- If the resource has been released or closed.public void warnAndReleaseIfNotReleased() throws IllegalStateException
warnAndReleaseIfNotReleased
in interface ReferenceCountedTracer
IllegalStateException
- If the resource hadn't been released.@Deprecated public boolean reservedBy(ReferenceOwner owner) throws ClosedIllegalStateException
ReferenceCounted
true
if unsure.
This method provides a best-effort check and may not be accurate.
reservedBy
in interface ReferenceCounted
owner
- The unique identifier representing the owner to check.false
if it is certain that the owner does not have the resource reserved; true
otherwise.ClosedIllegalStateException
- If the resource has been released or closed.public void singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
SingleThreadedChecked
true
, this resource can be shared between threads
as long as the users ensure that it is used in a thread-safe manner.singleThreadedCheckDisabled
in interface SingleThreadedChecked
singleThreadedCheckDisabled
- true
to turn off the thread safety check,
false
to enable it.public void addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceCounted
ReferenceChangeListener
that will be notified whenever the reference count changes.
This can be used to monitor the usage of the resource and execute code when certain conditions are met.addReferenceChangeListener
in interface ReferenceCounted
referenceChangeListener
- The listener that will receive notifications of reference count changes.public void removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceCounted
ReferenceChangeListener
previously added via ReferenceCounted.addReferenceChangeListener(ReferenceChangeListener)
.
Note: Object equality is used to determine which listener to remove, so be cautious if the listener implements the equals method in a non-standard way.
removeReferenceChangeListener
in interface ReferenceCounted
referenceChangeListener
- The listener to remove.protected boolean threadSafetyCheck(boolean isUsed) throws ThreadingIllegalStateException
ThreadingIllegalStateException
public void singleThreadedCheckReset()
singleThreadedCheckReset
in interface SingleThreadedChecked
@Deprecated public void clearUsedByThread()
@NotNull public @NotNull String toString()
public void referenceCountedUnmonitored(boolean unmonitored)
Copyright © 2023. All rights reserved.