Skip to content

Commit 5a59a6d

Browse files
committed
refactor: replace object locks with scoped locking for improved resource management
1 parent 6e16189 commit 5a59a6d

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

CodeLineCounter.Tests/TestBase.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace CodeLineCounter.Tests
77
{
88
public abstract class TestBase : IDisposable
99
{
10-
private static readonly object _consoleLock = new object();
10+
private static readonly Lock _consoleLock = new ();
1111
private readonly ThreadLocal<StringWriter> _stringWriter = new ThreadLocal<StringWriter>(() => new StringWriter());
1212
private readonly ThreadLocal<StringReader> _stringReader = new ThreadLocal<StringReader>(() => new StringReader(string.Empty));
1313
private readonly ThreadLocal<TextWriter> _originalConsoleOut = new ThreadLocal<TextWriter>();
@@ -22,7 +22,7 @@ protected TestBase()
2222

2323
protected void initialization()
2424
{
25-
lock (_consoleLock)
25+
using (_consoleLock.EnterScope())
2626
{
2727
_originalConsoleOut.Value = Console.Out;
2828
_originalConsoleIn.Value = Console.In;
@@ -32,7 +32,7 @@ protected void initialization()
3232

3333
protected void RedirectConsoleInputOutput()
3434
{
35-
lock (_consoleLock)
35+
using (_consoleLock.EnterScope())
3636
{
3737
if (_stringWriter.Value != null)
3838
{
@@ -47,7 +47,7 @@ protected void RedirectConsoleInputOutput()
4747

4848
protected string GetConsoleOutput()
4949
{
50-
lock (_consoleLock)
50+
using (_consoleLock.EnterScope())
5151
{
5252
if (_stringWriter.Value != null)
5353
{
@@ -57,17 +57,10 @@ protected string GetConsoleOutput()
5757
}
5858
}
5959

60-
protected void SetConsoleInput(string input)
61-
{
62-
lock (_consoleLock)
63-
{
64-
_stringReader.Value = new StringReader(input);
65-
}
66-
}
6760

6861
protected void ResetConsoleInputOutput()
6962
{
70-
lock (_consoleLock)
63+
using (_consoleLock.EnterScope())
7164
{
7265
if (_originalConsoleOut != null && _originalConsoleOut.Value != null)
7366
{

CodeLineCounter/Services/CodeDuplicationChecker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CodeDuplicationChecker
1111
{
1212
private readonly ConcurrentDictionary<string, HashSet<DuplicationCode>> duplicationMap;
1313
private readonly ConcurrentDictionary<string, HashSet<DuplicationCode>> hashMap;
14-
private readonly object duplicationLock = new();
14+
private readonly Lock duplicationLock = new();
1515

1616
public CodeDuplicationChecker()
1717
{
@@ -81,7 +81,7 @@ public void DetectCodeDuplicationInSourceCode(string normalizedPath, string sour
8181

8282
public void UpdateDuplicationMap()
8383
{
84-
lock (duplicationLock)
84+
using (duplicationLock.EnterScope())
8585
{
8686
// Clear previous results to avoid stale data.
8787
duplicationMap.Clear();

CodeLineCounter/Services/DependencyAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class DependencyAnalyzer
1212
{
1313
private static readonly ConcurrentDictionary<string, HashSet<DependencyRelation>> _dependencyMap = new();
1414
private static readonly HashSet<string> _solutionClasses = new();
15-
private static readonly object _dependencyLock = new();
15+
private static readonly Lock _dependencyLock = new();
1616

1717
public static void AnalyzeSolution(string solutionFilePath)
1818
{
@@ -39,7 +39,7 @@ private static void CollectAllClasses(IEnumerable<string> projectFiles)
3939
.OfType<ClassDeclarationSyntax>()
4040
.Select(c => GetFullTypeName(c));
4141

42-
lock (_dependencyLock)
42+
using (_dependencyLock.EnterScope())
4343
{
4444
foreach (var className in classes)
4545
{

0 commit comments

Comments
 (0)