diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.ReferenceTracking.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.ReferenceTracking.cs
index a5c914d3..36e926cf 100644
--- a/src/ComputeSharp/Graphics/Interop/NativeObject.ReferenceTracking.cs
+++ b/src/ComputeSharp/Graphics/Interop/NativeObject.ReferenceTracking.cs
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
+using System.Threading;
namespace ComputeSharp.Interop;
@@ -55,24 +56,52 @@ partial class NativeObject
///
/// Whether or not the returned value is enabled.
/// A object that can extend the lifetime of the tracked object.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Lease TryGetReferenceTrackingLease(out bool leaseTaken)
{
bool success = true;
- lock (this.lockObject)
+ int currentValue;
+ int originalValue;
+
+ // To get a reference tracking lease, the procedure is as follows:
+ // - If the object has been disposed (ie. if Disposed() has been called),
+ // even if the object hasn't actually released the unmanaged resources
+ // yet, then getting a tracking lease will fail and have no effect.
+ // - If the object hasn't been disposed, the reference count is incremented.
+ // This can be done without taking a look, as follows:
+ // - Do an interlocked read to get the current reference tracking mask.
+ // - If the object has been disposed, the 32nd bit will be set. Due to the
+ // mask being a signed integer in two-complement, we can just compare and
+ // check whether the mask is lower than 0. If that is the case, just bail.
+ // - Do an interlocked compare exchange incrementing the reference count by 1.
+ // If the original value is the same as the current one, it means no other
+ // thread performed a concurrent update between our read and write, so we can
+ // stop. Otherwise, just loop until a compare exchange completes successfully.
+ // The assumption is contention will be extremely rare, given that taking and
+ // returning a lease is incredibly fast compared to the time other operations need.
+ do
{
- if (this.isDisposeRequested)
+ currentValue = this.referenceTrackingMask;
+
+ if (currentValue < 0)
{
success = false;
+
+ break;
}
- else
- {
- this.leasesCount++;
- }
+
+ originalValue = Interlocked.CompareExchange(
+ location1: ref this.referenceTrackingMask,
+ value: currentValue + 1,
+ comparand: currentValue);
}
+ while (currentValue != originalValue);
NativeObject? nativeObject;
+ // If the reference count was incremented, return a valid lease. Otherwise,
+ // return one just wrapping a null instance, which will no-op when disposed.
if (success)
{
leaseTaken = true;
@@ -90,25 +119,47 @@ partial class NativeObject
///
private void RequestDispose()
{
- bool shouldRelease = false;
+ bool isDisposed = false;
- lock (this.lockObject)
+ int currentValue;
+ int originalValue;
+
+ // To request a dispose operation, the procedure is as follows:
+ // - If the dispose bit has already been set, just do nothing. This means
+ // that another thread was the first to call Dispose(). In that case, the
+ // actual releasing of unmanaged resources will be performed either by that
+ // thread if there are no active leases, or by the last returned lease.
+ // - Do an interlocked compare exchange setting the dispose bit (32nd bit).
+ // Like above, if the original value doesn't match the current one, it means
+ // that another thread raced against this one, so the value is invalid, and
+ // another loop is executed. If the value matches, the loop just ends.
+ // After this atomic update, we can then check whether (1) this was the first
+ // thread to call Dispose() (ie. the dispose flag wasn't previously set and it
+ // was set successfully by this call), and (2) there are no other active leases.
+ // If both checks are true, the object is effectively dead and we can safely release
+ // unmanaged resources. All other leases will just fail to be taken after this anyway.
+ do
{
- if (!this.isDisposeRequested)
+ currentValue = this.referenceTrackingMask;
+
+ if (currentValue < 0)
{
- this.isDisposeRequested = true;
+ isDisposed = true;
- // Only release resources if this is the first time Dipose() has been called, and
- // there are no outstanding leases. If there is one, don't do anything now. The
- // tracked object will just be released once the last active lease is returned.
- if (this.leasesCount == 0)
- {
- shouldRelease = true;
- }
+ break;
}
- }
- if (shouldRelease)
+ originalValue = Interlocked.CompareExchange(
+ location1: ref this.referenceTrackingMask,
+ value: currentValue | (1 << 31),
+ comparand: currentValue);
+ }
+ while (currentValue != originalValue);
+
+ // Only release resources if this is the first time Dipose() has been called, and
+ // there are no outstanding leases. If there is one, don't do anything now. The
+ // tracked object will just be released once the last active lease is returned.
+ if (!isDisposed && currentValue == 0)
{
OnDispose();
}
@@ -119,20 +170,17 @@ partial class NativeObject
///
private void ReturnReferenceTrackingLease()
{
- bool shouldRelease = false;
+ // To return a lease, we can simply do an interlocked decrement on the reference tracking
+ // mask. Each lease is guaranteed to only be disposed once (the contract states to only
+ // ever use it in a using statement), and a valid lease existing implies that the reference
+ // counting mask had previously been incremented by 1. There is also no need to check for
+ // disposal, becausse returning a lease on a disposed object is perfectly valid (given that
+ // the actual disposal is deferred until all active leases are returned).
+ int currentValue = Interlocked.Decrement(ref this.referenceTrackingMask);
- lock (this.lockObject)
- {
- this.leasesCount--;
-
- // If Dipose() has been called and this was the last lease, release the tracked object
- if (this.isDisposeRequested && this.leasesCount == 0)
- {
- shouldRelease = true;
- }
- }
-
- if (shouldRelease)
+ // If Dipose() has been called and this was the last lease, release the tracked object.
+ // This is the case if the dispose bit is set (the 32nd one), and no other bit is set.
+ if (currentValue == 1 << 31)
{
OnDispose();
}
@@ -141,6 +189,10 @@ partial class NativeObject
///
/// A reference tracking lease to extend the lifetime of a given instance while in a given scope.
///
+ ///
+ /// This type must always be used in a statement and disposed properly. Not doing
+ /// so is undefined behavior and may result in memory leaks and inability to correctly restore lost devices.
+ ///
internal struct Lease : IDisposable
{
///
diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
index 1057f476..261990ec 100644
--- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs
+++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
@@ -8,28 +8,21 @@ namespace ComputeSharp.Interop;
public abstract partial class NativeObject : IDisposable
{
///
- /// An object to use to synchronize the reference tracking operations.
+ /// A mask that indicates the state of the current object.
+ /// The mask uses the following schema:
+ ///
+ /// - [0, 30]: the number of existing reference tracking leases.
+ /// - [31]: whether or not has been called.
+ ///
///
- private readonly object lockObject;
-
- ///
- /// The number of existing reference tracking leases for the current instance.
- ///
- private int leasesCount;
-
- ///
- /// Whether or not has been called on the current instance.
- ///
- private bool isDisposeRequested;
+ private volatile int referenceTrackingMask;
///
/// Creates a new instance.
///
private protected NativeObject()
{
- this.lockObject = new object();
- this.leasesCount = 0;
- this.isDisposeRequested = false;
+ this.referenceTrackingMask = 0;
}
///