< Summary

Information
Class: MyNet.Collections.LockCollectionSynchronizer
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/LockCollectionSynchronizer.cs
Tag: 323_28699572109
Line coverage
56%
Covered lines: 14
Uncovered lines: 11
Coverable lines: 25
Total lines: 105
Line coverage: 56%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Write(...)100%11100%
Write(...)100%210%
Read(...)100%210%
Read(...)100%11100%
Dispose()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/LockCollectionSynchronizer.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="LockCollectionSynchronizer.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9
 10namespace MyNet.Collections;
 11
 12/// <summary>
 13/// Collection synchronizer using a reader-writer lock for thread-safe access to collections.
 14/// </summary>
 15public sealed class LockCollectionSynchronizer : ICollectionSynchronizer, IDisposable
 16{
 2717    private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion);
 18
 19    /// <summary>
 20    /// Initializes a new instance of the <see cref="LockCollectionSynchronizer"/> class.
 21    /// </summary>
 2722    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    {
 814530        _lock.EnterWriteLock();
 31
 32        try
 33        {
 814534            action();
 814535        }
 36        finally
 37        {
 814538            _lock.ExitWriteLock();
 814539        }
 814540    }
 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    {
 050        _lock.EnterWriteLock();
 51
 52        try
 53        {
 054            return func();
 55        }
 56        finally
 57        {
 058            _lock.ExitWriteLock();
 059        }
 060    }
 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    {
 068        _lock.EnterReadLock();
 69
 70        try
 71        {
 072            action();
 073        }
 74        finally
 75        {
 076            _lock.ExitReadLock();
 077        }
 078    }
 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    {
 152488        _lock.EnterReadLock();
 89
 90        try
 91        {
 152492            return func();
 93        }
 94        finally
 95        {
 152496            _lock.ExitReadLock();
 152497        }
 152498    }
 99
 100    /// <summary>
 101    /// Releases all resources used by the <see cref="LockCollectionSynchronizer"/> instance, including the underlying l
 102    /// </summary>
 6103    public void Dispose() => _lock.Dispose();
 104}
 105