feat: removing two proposed features that do not fit the pattern
This commit is contained in:
parent
678bfc8564
commit
a161673e36
2 changed files with 0 additions and 119 deletions
|
@ -1,105 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace MfGames.Locking
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Implements the basic pattern for getting an item from a cache using
|
|
||||||
/// the ReaderWriterLockSlim class. This attempts to get it using a read-only
|
|
||||||
/// lock. If that fails, it gets an upgradable lock, tries again, and if it
|
|
||||||
/// still can't find it, upgrades the lock to a write lock to create it.
|
|
||||||
/// </summary>
|
|
||||||
public static class TryGetCreate
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Invokes the try/get/create pattern used a condition to test for it
|
|
||||||
/// and a constructor function.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="readerWriterLockSlim">The reader writer lock slim.</param>
|
|
||||||
/// <param name="conditionHandler">The condition handler.</param>
|
|
||||||
/// <param name="createHandler">The create handler.</param>
|
|
||||||
public static void Invoke(
|
|
||||||
ReaderWriterLockSlim readerWriterLockSlim,
|
|
||||||
Func<bool> conditionHandler,
|
|
||||||
Action createHandler)
|
|
||||||
{
|
|
||||||
using (new ReadLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
// Verify that the condition for creating it is false.
|
|
||||||
if (!conditionHandler())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We failed to get the lock using the read-only. We create an upgradable lock
|
|
||||||
// and try again since it may have been created with a race condition when the
|
|
||||||
// last lock was released and this one was acquired.
|
|
||||||
using (new UpgradableLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
// Verify that the condition for creating it is false.
|
|
||||||
if (!conditionHandler())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We failed to get it in the lock. Upgrade the lock to a write and create it.
|
|
||||||
using (new WriteLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
createHandler();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Invokes the try/get/create pattern using a tryget retrieval and a
|
|
||||||
/// creator handler.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TInput">The type of the input.</typeparam>
|
|
||||||
/// <typeparam name="TOutput">The type of the output.</typeparam>
|
|
||||||
/// <param name="readerWriterLockSlim">The reader writer lock slim.</param>
|
|
||||||
/// <param name="input">The input.</param>
|
|
||||||
/// <param name="tryGetHandler">The try get handler.</param>
|
|
||||||
/// <param name="createHandler">The create handler.</param>
|
|
||||||
/// <returns>The requested output object.</returns>
|
|
||||||
public static TOutput Invoke<TInput, TOutput>(
|
|
||||||
ReaderWriterLockSlim readerWriterLockSlim,
|
|
||||||
TInput input,
|
|
||||||
TryGetHandler<TInput, TOutput> tryGetHandler,
|
|
||||||
Func<TInput, TOutput> createHandler)
|
|
||||||
{
|
|
||||||
// First attempt to get the item using a read-only lock.
|
|
||||||
TOutput output;
|
|
||||||
|
|
||||||
using (new ReadLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
// Try to get the item using the try/get handler.
|
|
||||||
if (tryGetHandler(input, out output))
|
|
||||||
{
|
|
||||||
// We successful got the item in the read-only cache, so just return it.
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We failed to get the lock using the read-only. We create an upgradable lock
|
|
||||||
// and try again since it may have been created with a race condition when the
|
|
||||||
// last lock was released and this one was acquired.
|
|
||||||
using (new UpgradableLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
// Try to get the item using the try/get handler.
|
|
||||||
if (tryGetHandler(input, out output))
|
|
||||||
{
|
|
||||||
// We successful got the item in this lock, so return it without
|
|
||||||
// upgrading the lock.
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We failed to get it in the lock. Upgrade the lock to a write and create it.
|
|
||||||
using (new WriteLock(readerWriterLockSlim))
|
|
||||||
{
|
|
||||||
return createHandler(input);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
namespace MfGames.Locking
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Defines the common try/get handler to retrieve an item of a given type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TInput">The type of the input or lookup key.</typeparam>
|
|
||||||
/// <typeparam name="TOutput">The type of the output.</typeparam>
|
|
||||||
/// <param name="input">The input value to search for.</param>
|
|
||||||
/// <param name="output">The resulting output value.</param>
|
|
||||||
/// <returns>True if the attempt was successful, otherwise false.</returns>
|
|
||||||
public delegate bool TryGetHandler<in TInput, TOutput>(
|
|
||||||
TInput input,
|
|
||||||
out TOutput output);
|
|
||||||
}
|
|
Reference in a new issue