Skip to content

Commit 448ea16

Browse files
authored
Use System.Lock instead of object for locking (#358)
1 parent 4eb7aa0 commit 448ea16

File tree

18 files changed

+38
-37
lines changed

18 files changed

+38
-37
lines changed

Diff for: Orm/Xtensive.Orm.Oracle/Sql.Drivers.Oracle/v09/Extractor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public ExtractionContext(Catalog catalog, Dictionary<string, string> replacement
106106
private const int DefaultDayPrecision = 2;
107107
private const int DefaultFSecondsPrecision = 6;
108108

109-
private readonly object accessGuard = new object();
109+
private readonly Lock accessGuard = new();
110110

111111
private string nonSystemSchemasFilter;
112112

Diff for: Orm/Xtensive.Orm.Sqlite/Sql.Drivers.Sqlite/ProviderInitializer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static class ProviderInitializer
2727
private const string FrameworkName = "Net40";
2828

2929
private static volatile bool IsInitialized;
30-
private static readonly object SyncRoot = new object();
30+
private static readonly Lock SyncRoot = new();
3131

3232
public static void Run(string nativeLibraryCacheFolder)
3333
{

Diff for: Orm/Xtensive.Orm.Tests.Core/DotNetFramework/ThreadingTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private class Target
2626
public int PassCount;
2727
public bool Stop;
2828
public Thread LastAccessor;
29-
public object ObjectLock = new object();
29+
public Lock ObjectLock = new();
3030
public ReaderWriterLockSlim SlimLock = new ReaderWriterLockSlim();
3131

3232
public void ExecuteLock(object argument)
@@ -280,4 +280,4 @@ private static void ThreadedTest(Target target, int passCount, ParameterizedThre
280280
TestHelper.CollectGarbage();
281281
}
282282
}
283-
}
283+
}

Diff for: Orm/Xtensive.Orm.Tests.Framework/StorageProviderInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Xtensive.Orm.Tests
1212
{
1313
public sealed class StorageProviderInfo
1414
{
15-
private static readonly object InstanceLock = new object();
15+
private static readonly Lock InstanceLock = new();
1616
private static StorageProviderInfo InstanceValue;
1717

1818
public static StorageProviderInfo Instance
@@ -113,4 +113,4 @@ private static IStorageTimeZoneProvider GetTimeZoneProvider(StorageProvider prov
113113
}
114114
}
115115
}
116-
}
116+
}

Diff for: Orm/Xtensive.Orm.Tests.Framework/TestConfiguration.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class TestConfiguration
1919

2020
private const string DefaultStorage = "default";
2121

22-
private static readonly object InstanceLock = new object();
22+
private static readonly Lock InstanceLock = new();
2323
private static TestConfiguration InstanceValue;
2424

2525
private readonly Dictionary<string, string> configuration;
@@ -103,4 +103,4 @@ private TestConfiguration()
103103
Storage = GetEnvironmentVariable(StorageKey) ?? GetStorageFromFile() ?? DefaultStorage;
104104
}
105105
}
106-
}
106+
}

Diff for: Orm/Xtensive.Orm.Tests/Issues/Issue0676_NonNullableReferenceBug.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private sealed class KeyExtension
6161
#region "Null entity" pattern implementation
6262

6363
public const string NullName = "<None>";
64-
private static object @lock = new object();
64+
private static Lock @lock = new();
6565

6666
public static Person Null {
6767
get {
@@ -190,4 +190,4 @@ public void HasNullEntityTest()
190190
}
191191
}
192192
}
193-
}
193+
}

Diff for: Orm/Xtensive.Orm.Tests/Issues/Issue0839_MultithreadingBug.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Issue0839_MultithreadingBug : AutoBuildTest
4545
private const int entityCount = 20;
4646
private const int readCount = 20;
4747

48-
private static object exceptionLock = new object();
48+
private static Lock exceptionLock = new();
4949
private static int exceptionCount;
5050
private static Key[] keys = new Key[entityCount];
5151

@@ -136,4 +136,4 @@ currentError is UniqueConstraintViolationException ||
136136
}
137137
}
138138
}
139-
}
139+
}

Diff for: Orm/Xtensive.Orm.Tests/Storage/ActivatorTest.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
// Created: 2008.06.11
66

77
using System;
8+
using System.Linq;
89
using System.Reflection;
910
using NUnit.Framework;
1011
using Xtensive.Orm.Configuration;
12+
using Xtensive.Orm.Tests.Issues.CustomerBug1Model;
1113
using Xtensive.Orm.Tests.Storage.ActivatorModel;
12-
using System.Linq;
1314

1415
namespace Xtensive.Orm.Tests.Storage.ActivatorModel
1516
{
@@ -32,19 +33,19 @@ public class Descendant : Ancestor
3233
[HierarchyRoot]
3334
public class InitializebleClass : Entity
3435
{
35-
public object syncRoot = new object();
36+
public Lock syncRoot = new();
3637

3738
protected override void OnInitialize()
3839
{
3940
base.OnInitialize();
40-
syncRoot = new object();
41+
syncRoot = new();
4142
}
4243

4344
public InitializebleClass()
4445
{
45-
syncRoot = new object();
46-
// Ëîãèêà, êîòîðàÿ þçàåò syncRoot.
47-
Assert.IsNotNull(syncRoot);
46+
syncRoot = new();
47+
// Логика, которая юзает syncRoot.
48+
Assert.That(syncRoot != null, Is.True);
4849
}
4950

5051
[Field, Key]
@@ -75,7 +76,7 @@ public void TestFieldInitializer()
7576
using (var t = session.OpenTransaction())
7677
{
7778
var obj1 = new InitializebleClass();
78-
Assert.IsNotNull(obj1.syncRoot);
79+
Assert.That(obj1.syncRoot != null, Is.True);
7980
t.Complete();
8081
}
8182
}
@@ -85,7 +86,7 @@ public void TestFieldInitializer()
8586
{
8687
var obj1 = session.Query.All<InitializebleClass>().First();
8788
Assert.IsNotNull(obj1);
88-
Assert.IsNotNull(obj1.syncRoot);
89+
Assert.That(obj1.syncRoot != null, Is.True);
8990
t.Complete();
9091
}
9192
}
@@ -113,4 +114,4 @@ public void Test()
113114
}
114115
}
115116
}
116-
}
117+
}

Diff for: Orm/Xtensive.Orm.Tests/Upgrade/UpgradeAndNamingRulesAdvanced/TestBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2016 Xtensive LLC.
1+
// Copyright (C) 2016 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alexey Kulakov
@@ -20,7 +20,7 @@ namespace Xtensive.Orm.Tests.Upgrade.UpgradeAndNamingRulesAdvanced
2020
[TestFixture]
2121
public abstract class TestBase
2222
{
23-
public readonly object guard = new object();
23+
public readonly Lock guard = new();
2424
private NamingConvention namingConvention;
2525

2626
[Test]

Diff for: Orm/Xtensive.Orm/Core/AssociateProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class AssociateProvider :
4343
private string[] typeSuffixes;
4444

4545
[NonSerialized]
46-
private object highPriorityLocationsLock = new object();
46+
private Lock highPriorityLocationsLock = new();
4747

4848
private List<(Assembly, string)> highPriorityLocations = new();
4949

Diff for: Orm/Xtensive.Orm/Core/Scope.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Xtensive.Core
2121
public class Scope<TContext> : IDisposable
2222
where TContext: class
2323
{
24-
internal static readonly object @lock = new object();
24+
internal static readonly Lock @lock = new();
2525
internal static volatile Type allowedType = null;
2626

2727
private static readonly AsyncLocal<Scope<TContext>> currentScopeAsync = new AsyncLocal<Scope<TContext>>();

Diff for: Orm/Xtensive.Orm/Core/SimpleScope.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Xtensive.Core
1919
/// <typeparam name="TVariator">The type of the variator. Must be an internal type.</typeparam>
2020
public class SimpleScope<TVariator> : IDisposable
2121
{
22-
private readonly static object @lock = new object();
22+
private readonly static Lock @lock = new();
2323
private volatile static Type allowedType = null;
2424

2525
private static readonly AsyncLocal<SimpleScope<TVariator>> currentAsync = new AsyncLocal<SimpleScope<TVariator>>();
@@ -117,4 +117,4 @@ public void Dispose()
117117
throw error;
118118
}
119119
}
120-
}
120+
}

Diff for: Orm/Xtensive.Orm/Orm/Domain.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ namespace Xtensive.Orm
3838
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
3939
public sealed class Domain : IDisposable, IAsyncDisposable, IHasExtensions, ISessionSource
4040
{
41-
private readonly object disposeGuard = new object();
42-
private readonly object singleConnectionGuard = new object();
41+
private readonly Lock disposeGuard = new();
42+
private readonly Lock singleConnectionGuard = new();
4343

4444
private bool isDisposed;
4545
private Session singleConnectionOwner;

Diff for: Orm/Xtensive.Orm/Orm/Logging/Internals/FileWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Xtensive.Orm.Logging
1111
internal sealed class FileWriter : LogWriter
1212
{
1313
private readonly string fileName;
14-
private readonly object syncRoot = new object();
14+
private readonly Lock syncRoot = new();
1515

1616
/// <inheritdoc/>
1717
public override void Write(in LogEventInfo logEvent)

Diff for: Orm/Xtensive.Orm/Orm/Logging/Internals/SystemClock.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2013 Xtensive LLC.
1+
// Copyright (C) 2013 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alexey Kulakov
@@ -10,7 +10,7 @@ namespace Xtensive.Orm.Logging
1010
{
1111
internal static class SystemClock
1212
{
13-
private static readonly object syncRoot = new object();
13+
private static readonly Lock syncRoot = new();
1414
private static int lastTick = -1;
1515
private static DateTime lastDateTime = DateTime.MinValue;
1616

Diff for: Orm/Xtensive.Orm/Orm/Logging/LogManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class LogManager
1818
{
1919
private static readonly LogManager defaultInstance = new LogManager();
2020

21-
private readonly object syncObj = new object();
21+
private readonly Lock syncObj = new();
2222
private LogProvider provider;
2323

2424
/// <summary>

Diff for: Orm/Xtensive.Orm/Orm/Upgrade/Internals/UpgradeServiceAccessor.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012 Xtensive LLC.
1+
// Copyright (C) 2012 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
@@ -17,7 +17,7 @@ namespace Xtensive.Orm.Upgrade
1717
{
1818
internal sealed class UpgradeServiceAccessor : LockableBase, IDisposable
1919
{
20-
private readonly object resourcesSyncRoot = new object();
20+
private readonly Lock resourcesSyncRoot = new();
2121
private readonly DisposableSet resources = new DisposableSet();
2222
private readonly DisposableSet temporaryResources = new DisposableSet();
2323

@@ -177,4 +177,4 @@ public void Dispose()
177177
}
178178
}
179179
}
180-
}
180+
}

Diff for: Orm/Xtensive.Orm/Reflection/TypeHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public int GetHashCode((Type, Type[]) obj)
4242

4343
private const string InvokeMethodName = "Invoke";
4444

45-
private static readonly object EmitLock = new object();
45+
private static readonly Lock EmitLock = new();
4646
private static readonly int NullableTypeMetadataToken = WellKnownTypes.NullableOfT.MetadataToken;
4747
private static readonly int ValueTuple1MetadataToken = typeof(ValueTuple<>).MetadataToken;
4848
private static readonly int ValueTuple8MetadataToken = typeof(ValueTuple<,,,,,,,>).MetadataToken;

0 commit comments

Comments
 (0)