From e2ce0a60798fe51d9b0cdfb4025dabe4556f1f38 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Sep 2023 22:42:00 +0200 Subject: [PATCH] Add cancellation support to DynamicCache<,> --- .../Helpers/DynamicCache{TKey,TValue}.cs | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs b/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs index 4ecb52dd..78775089 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using System.Threading; namespace ComputeSharp.SourceGeneration.Helpers; @@ -31,20 +32,26 @@ public sealed class DynamicCache /// /// The key to use as lookup. /// The callback to use to create new values, if needed. + /// A cancellation token for the operation of creating a new value. /// The resulting value. + /// Thrown if is canceled. /// /// This method might replace with a new instance that has the same /// value according to its equality comparison logic. Callers should always use the last value of /// after this method returns and discard the previous one, if different. /// - public TValue GetOrCreate(ref TKey key, GetOrCreateCallback callback) + public TValue GetOrCreate(ref TKey key, GetOrCreateCallback callback, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + // Create a new entry that we will use to perform the lookup. // Each entry simply forwards equality logic to the wrapped object. Entry entry = new(key); while (true) { + cancellationToken.ThrowIfCancellationRequested(); + // We're performing a lookup on this temporary entry. We need it to // track the value it will potentially match against, so we can // return it to the caller. This ensures the same object is used. @@ -56,6 +63,8 @@ public sealed class DynamicCache // object). If we find it, we return it and throw away the new entry. if (this.map.TryGetValue(entry, out TValue? value)) { + cancellationToken.ThrowIfCancellationRequested(); + // We have a match, so replace the object with the one that actually matched. // This guarantees that it will remain alive, so the entry will not die. key = entry.GetLastMatchedValue(); @@ -63,14 +72,21 @@ public sealed class DynamicCache return value; } + cancellationToken.ThrowIfCancellationRequested(); + // Execute the slow fallback path, invoking the callback and trying to add a new // value to the cache. If this succeeds, it means that the current key object has // been added to the cache, so there is nothing left to do. If this fails, it means // another thread has beat us to adding the key, so we should perform the initial // lookup again to make sure we can find the exact instance that is in the cache. // This is needed to ensure valid cache entries remain alive over time. - if (TryGetOrCreate(entry, key, callback, out value)) + if (TryGetOrCreate(entry, key, callback, cancellationToken, out value)) { + // If the operation has been canceled after inserting a new key and value, + // the value will just be wasted, but there isn't really anything we can do + // about it at this point. We also ignore the unnecessary remover finalizer. + cancellationToken.ThrowIfCancellationRequested(); + return value; } } @@ -82,13 +98,22 @@ public sealed class DynamicCache /// The instance to try to insert into the cache. /// The key to use as lookup. /// The callback to use to create new values, if needed. + /// A cancellation token for the operation of creating a new value. /// The resulting value (should be ignored if the method fails). /// Whether was successfully inserted into the cache. + /// Thrown if is canceled. [MethodImpl(MethodImplOptions.NoInlining)] - private bool TryGetOrCreate(Entry entry, TKey key, GetOrCreateCallback callback, out TValue value) + private bool TryGetOrCreate( + Entry entry, + TKey key, + GetOrCreateCallback callback, + CancellationToken cancellationToken, + out TValue value) { // No value is present, so we can create it now - value = callback(key); + value = callback(key, cancellationToken); + + cancellationToken.ThrowIfCancellationRequested(); // We're about to try to add the item to the cache, so we no longer need to // track the last matched value. We already have a reference to the object. @@ -116,8 +141,9 @@ public sealed class DynamicCache /// A callback to create a new value from a given key. /// /// The resulting instance. + /// A cancellation token for the operation of creating a new value. /// - public delegate TValue GetOrCreateCallback(TKey key); + public delegate TValue GetOrCreateCallback(TKey key, CancellationToken cancellationToken); /// /// An entry to use in .