| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LockCollectionSynchronizer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Collections; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Collection synchronizer using a reader-writer lock for thread-safe access to collections. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class LockCollectionSynchronizer : ICollectionSynchronizer, IDisposable |
| | | 16 | | { |
| | 27 | 17 | | private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="LockCollectionSynchronizer"/> class. |
| | | 21 | | /// </summary> |
| | 27 | 22 | | public LockCollectionSynchronizer() { } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Executes the specified action within a write lock, ensuring exclusive access for modifications. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="action">The action to execute.</param> |
| | | 28 | | public void Write(Action action) |
| | | 29 | | { |
| | 8145 | 30 | | _lock.EnterWriteLock(); |
| | | 31 | | |
| | | 32 | | try |
| | | 33 | | { |
| | 8145 | 34 | | action(); |
| | 8145 | 35 | | } |
| | | 36 | | finally |
| | | 37 | | { |
| | 8145 | 38 | | _lock.ExitWriteLock(); |
| | 8145 | 39 | | } |
| | 8145 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Executes the specified function within a write lock, ensuring exclusive access for modifications. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="func">The function to execute.</param> |
| | | 46 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | | 47 | | /// <returns>The result of the function.</returns> |
| | | 48 | | public TResult Write<TResult>(Func<TResult> func) |
| | | 49 | | { |
| | 0 | 50 | | _lock.EnterWriteLock(); |
| | | 51 | | |
| | | 52 | | try |
| | | 53 | | { |
| | 0 | 54 | | return func(); |
| | | 55 | | } |
| | | 56 | | finally |
| | | 57 | | { |
| | 0 | 58 | | _lock.ExitWriteLock(); |
| | 0 | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Executes the specified action within a read lock, allowing concurrent read access. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="action">The action to execute.</param> |
| | | 66 | | public void Read(Action action) |
| | | 67 | | { |
| | 0 | 68 | | _lock.EnterReadLock(); |
| | | 69 | | |
| | | 70 | | try |
| | | 71 | | { |
| | 0 | 72 | | action(); |
| | 0 | 73 | | } |
| | | 74 | | finally |
| | | 75 | | { |
| | 0 | 76 | | _lock.ExitReadLock(); |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Executes the specified function within a read lock, allowing concurrent read access. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="func">The function to execute.</param> |
| | | 84 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | | 85 | | /// <returns>The result of the function.</returns> |
| | | 86 | | public TResult Read<TResult>(Func<TResult> func) |
| | | 87 | | { |
| | 1524 | 88 | | _lock.EnterReadLock(); |
| | | 89 | | |
| | | 90 | | try |
| | | 91 | | { |
| | 1524 | 92 | | return func(); |
| | | 93 | | } |
| | | 94 | | finally |
| | | 95 | | { |
| | 1524 | 96 | | _lock.ExitReadLock(); |
| | 1524 | 97 | | } |
| | 1524 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Releases all resources used by the <see cref="LockCollectionSynchronizer"/> instance, including the underlying l |
| | | 102 | | /// </summary> |
| | 6 | 103 | | public void Dispose() => _lock.Dispose(); |
| | | 104 | | } |
| | | 105 | | |