From c5997d2c62cec50b9880d13ec9db8e273dc941a2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Jul 2022 13:50:08 +0200 Subject: [PATCH] Add ReferenceTracker type --- .../Graphics/Interop/ReferenceTracker.cs | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs new file mode 100644 index 00000000..b55b26c5 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs @@ -0,0 +1,204 @@ +using System; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Interop; + +/// +/// An object acting as reference tracked for a given managed object. +/// +internal struct ReferenceTracker : IDisposable +{ + /// + /// The target object to track. + /// + private readonly ITrackedObject trackedObject; + + /// + /// An object to use to synchronize the reference tracking operations. + /// + private readonly object lockObject; + + /// + /// The number of existing reference tracking leases for . + /// + private int leasesCount; + + /// + /// Whether or not has been called on . + /// + private bool isDisposeRequested; + + /// + /// Creates a new instance with the specified paramters. + /// + /// The input tracked object to wrap. + public ReferenceTracker(ITrackedObject trackedObject) + { + this.trackedObject = trackedObject; + this.lockObject = new object(); + this.leasesCount = 0; + this.isDisposeRequested = false; + } + + /// + /// Gets a value to extend the lifetime of the tracked object. + /// + /// A object that can extend the lifetime of the tracked object. + /// Thrown if the tracked object has been disposed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Lease GetLease() + { + Lease lease = TryGetLease(out bool leaseTaken); + + if (!leaseTaken) + { + // We can't use ThrowHelper here as we only want to invoke ToString if we + // are about to throw an exception. The JIT will recognize this pattern + // as this method has a single basic block that always throws an exception. + static void Throw(object trackedObject) => throw new ObjectDisposedException(trackedObject.ToString()); + + Throw(this.trackedObject); + } + + return lease; + } + + /// + /// Tries to get a value to extend the lifetime of the tracked object. + /// + /// Whether or not the returned value is enabled. + /// A object that can extend the lifetime of the tracked object. + public Lease TryGetLease(out bool leaseTaken) + { + bool success = true; + + lock (this.lockObject) + { + if (this.isDisposeRequested) + { + success = false; + } + else + { + this.leasesCount++; + } + } + + ITrackedObject? trackedObject; + + if (success) + { + leaseTaken = true; + trackedObject = this.trackedObject; + } + else + { + leaseTaken = false; + trackedObject = null; + } + + return new(trackedObject); + } + + /// + public void Dispose() + { + bool shouldRelease = false; + + lock (this.lockObject) + { + if (!this.isDisposeRequested) + { + this.isDisposeRequested = 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; + } + } + } + + if (shouldRelease) + { + this.trackedObject.DangerousRelease(); + } + } + + /// + /// Returns a given lease (ie. decrements the ref count for the tracked object). + /// + private void ReturnLease() + { + bool shouldRelease = false; + + 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) + { + this.trackedObject.DangerousRelease(); + } + } + + /// + /// A reference tracking lease to extend the lifetime of a given tracked object while in a given scope. + /// + public struct Lease + { + /// + /// The tracked object being wrapped, if any. + /// + private ITrackedObject? trackedObject; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The input tracked object to wrap, if any. + public Lease(ITrackedObject? trackedObject) + { + this.trackedObject = trackedObject; + } + + /// + public void Dispose() + { + ITrackedObject? trackedObject = this.trackedObject; + + this.trackedObject = null; + + if (trackedObject is not null) + { + trackedObject.GetReferenceTracker().ReturnLease(); + } + } + } + + /// + /// An interface for an object being tracked by an owning object. + /// + public interface ITrackedObject + { + /// + /// Gets a reference to the owning for the object. + /// + /// A reference to the owning for the object. + ref ReferenceTracker GetReferenceTracker(); + + /// + /// Releases all resources (including unmanaged ones) for the tracked object. + /// + /// This method is guaranteed to only ever be called once for a given tracked object. + void DangerousRelease(); + } +}