| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a registry of named <see cref="SemaphoreSlim"/> instances used to |
| | | 7 | | /// synchronize access to shared resources within the current process. |
| | | 8 | | /// The same key always returns the same semaphore instance. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class KestrunLockRegistry |
| | | 11 | | { |
| | 1 | 12 | | private static readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new() { ["default"] = new SemaphoreSlim |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets an existing semaphore for the specified key, or creates a new one with an initial and maximum count of 1. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="key">The resource key.</param> |
| | | 18 | | /// <returns>A <see cref="SemaphoreSlim"/> that can be awaited to serialize access to the resource identified by <pa |
| | | 19 | | /// <exception cref="ArgumentException">Thrown when <paramref name="key"/> is empty, or whitespace.</exception> |
| | | 20 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is null.</exception> |
| | | 21 | | public static SemaphoreSlim GetOrCreate(string key) |
| | | 22 | | { |
| | 11 | 23 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 11 | 24 | | return _locks.GetOrAdd(key.Trim().ToLowerInvariant(), static _ => Create()); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Attempts to get the semaphore associated with the specified key without creating a new one if it doesn't exist. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="key">The resource key.</param> |
| | | 31 | | /// <param name="semaphore">The semaphore associated with the specified key, if it exists.</param> |
| | | 32 | | /// <returns>True if the semaphore exists; otherwise, false.</returns> |
| | | 33 | | /// <exception cref="ArgumentException">Thrown when <paramref name="key"/> is empty, or whitespace.</exception> |
| | | 34 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is null.</exception> |
| | | 35 | | public static bool TryGet(string key, out SemaphoreSlim? semaphore) |
| | | 36 | | { |
| | 5 | 37 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 1 | 38 | | key = key.Trim().ToLowerInvariant(); |
| | 1 | 39 | | return _locks.TryGetValue(key, out semaphore); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets a default semaphore instance that can be used for general synchronization needs when a specific key is not |
| | | 44 | | /// This is useful for simple scenarios where a single lock is sufficient to protect a critical section of code or a |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public static SemaphoreSlim Default => _locks["default"]; |
| | | 47 | | |
| | 4 | 48 | | private static SemaphoreSlim Create() => new(1, 1); |
| | | 49 | | } |