Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of System.Threading.Lock #20530

Open
wants to merge 26 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
24dba7f
Encourage use of System.Threading.Lock
MarkCiliaVincenti Aug 15, 2024
6214f61
Update Directory.Packages.props
MarkCiliaVincenti Aug 21, 2024
1fd1617
Merge branch 'abpframework:dev' into net9locking
MarkCiliaVincenti Aug 21, 2024
b71d4ec
Update Directory.Packages.props
MarkCiliaVincenti Sep 8, 2024
fba82b1
Fix compilation issue
MarkCiliaVincenti Sep 8, 2024
4632c20
Fix error
MarkCiliaVincenti Sep 8, 2024
a8ab6c7
Update Directory.Packages.props
MarkCiliaVincenti Sep 21, 2024
87c5d6e
Merge branch 'abpframework:dev' into net9locking
MarkCiliaVincenti Sep 21, 2024
173b9f2
Merge and updates
MarkCiliaVincenti Oct 15, 2024
ac409ce
merge
MarkCiliaVincenti Oct 15, 2024
7750026
Fix
MarkCiliaVincenti Oct 15, 2024
ae4680b
Cleaned up
MarkCiliaVincenti Oct 15, 2024
c4b7e40
Cleaning
MarkCiliaVincenti Oct 15, 2024
84bab33
Revert Volo.Abp.AspNetCore.Mvc.csproj
MarkCiliaVincenti Oct 15, 2024
cd126cb
Revert Volo.Abp.ObjectExtending.csproj
MarkCiliaVincenti Oct 15, 2024
2b4e640
Revert Volo.Abp.ObjectExtending.csproj
MarkCiliaVincenti Oct 15, 2024
3399d96
Fix so modules are unaffected
MarkCiliaVincenti Oct 15, 2024
04bfa5c
Merge branch 'net9locking' of https://github.com/MarkCiliaVincenti/ab…
MarkCiliaVincenti Oct 15, 2024
b31e078
Fix import for directory.build.props
MarkCiliaVincenti Oct 15, 2024
dfa3bce
merge
MarkCiliaVincenti Nov 18, 2024
6776802
Fix RabbitMqMessageConsumer
MarkCiliaVincenti Nov 18, 2024
ffad0f1
Update Directory.Build.props
MarkCiliaVincenti Nov 18, 2024
2021975
Update Directory.Build.props
MarkCiliaVincenti Nov 18, 2024
8fae92d
Update RabbitMqMessageConsumer.cs
MarkCiliaVincenti Nov 18, 2024
3909b3a
Optimizations
MarkCiliaVincenti Nov 18, 2024
8657bb7
Merge branch 'net9locking' of https://github.com/MarkCiliaVincenti/ab…
MarkCiliaVincenti Nov 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="AWSSDK.SecurityToken" Version="3.7.400.30" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.18.1" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.22.1" />
<PackageVersion Include="Backport.System.Threading.Lock" Version="3.0.1" />
<PackageVersion Include="Blazorise" Version="1.6.2" />
<PackageVersion Include="Blazorise.Components" Version="1.6.2" />
<PackageVersion Include="Blazorise.DataGrid" Version="1.6.2" />
Expand Down
18 changes: 18 additions & 0 deletions framework/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Backport.System.Threading.Lock">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Volo.Abp.AspNetCore.Components.Web.ExceptionHandling;
public class AbpExceptionHandlingLoggerProvider : ILoggerProvider
{
private AbpExceptionHandlingLogger? _logger;
private static readonly object SyncObj = new object();
private static readonly Lock SyncObj = new();
private readonly IServiceCollection _serviceCollection;

public AbpExceptionHandlingLoggerProvider(IServiceCollection serviceCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BundleConfigurationCollection
private readonly ConcurrentDictionary<string, BundleConfiguration> _bundles;
private readonly ConcurrentDictionary<string, List<Action<BundleConfiguration>>> _lazyBundleConfigurationActions;
private readonly List<Action<BundleConfiguration>> _lazyAllBundleConfigurationActions;
private readonly Lock _syncLock = new();

public BundleConfigurationCollection()
{
Expand Down Expand Up @@ -92,7 +93,7 @@ private BundleConfiguration CreateBundle(string bundleName, Action<BundleConfigu
}
}

lock (_lazyAllBundleConfigurationActions)
lock (_syncLock)
{
_lazyAllBundleConfigurationActions.ForEach(c => c.Invoke(bundle));
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public BundleConfigurationCollection ConfigureAll([NotNull] Action<BundleConfigu
configureAction.Invoke(bundle.Value);
}

lock (_lazyAllBundleConfigurationActions)
lock (_syncLock)
{
_lazyAllBundleConfigurationActions.Add(configureAction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class BundleCacheItem

public List<IDisposable> WatchDisposeHandles { get; }

public Lock SyncLock { get; } = new();

public BundleCacheItem(List<BundleFile> files)
{
Files = files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ private BundleCacheItem AddToBundleCache(string bundleName, IBundler bundler, Li

private void WatchChanges(BundleCacheItem cacheValue, List<string> files, string bundleRelativePath)
{
lock (cacheValue.WatchDisposeHandles)
lock (cacheValue.SyncLock)
{
foreach (var file in files)
{
var watchDisposeHandle = HostingEnvironment.WebRootFileProvider.Watch(file).RegisterChangeCallback(_ =>
{
lock (cacheValue.WatchDisposeHandles)
lock (cacheValue.SyncLock)
{
cacheValue.WatchDisposeHandles.ForEach(h => h.Dispose());
cacheValue.WatchDisposeHandles.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CachedObjectExtensionsDtoService : ICachedObjectExtensionsDtoServic
{
protected IExtensionPropertyAttributeDtoFactory ExtensionPropertyAttributeDtoFactory { get; }
protected volatile ObjectExtensionsDto? CachedValue;
protected readonly object SyncLock = new object();
protected readonly Lock SyncLock = new();

public CachedObjectExtensionsDtoService(IExtensionPropertyAttributeDtoFactory extensionPropertyAttributeDtoFactory)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Volo.Abp.DynamicProxy;
public static class DynamicProxyIgnoreTypes
{
private static HashSet<Type> IgnoredTypes { get; } = new HashSet<Type>();
private static readonly Lock _syncLock = LockFactory.Create();

public static void Add<T>()
{
Expand All @@ -23,23 +24,23 @@ public static void Add<T>()

public static void Add(Type type)
{
lock (IgnoredTypes)
lock (_syncLock)
{
IgnoredTypes.AddIfNotContains(type);
}
}

public static void Add(params Type[] types)
{
lock (IgnoredTypes)
lock (_syncLock)
{
IgnoredTypes.AddIfNotContains(types);
}
}

public static bool Contains(Type type, bool includeDerivedTypes = true)
{
lock (IgnoredTypes)
lock (_syncLock)
{
return includeDerivedTypes
? IgnoredTypes.Any(t => t.IsAssignableFrom(type))
Expand Down
7 changes: 4 additions & 3 deletions framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Volo.Abp;
public static class RandomHelper
{
private static readonly Random Rnd = new Random();
private static readonly Lock _syncLock = LockFactory.Create();

/// <summary>
/// Returns a random number within a specified range.
Expand All @@ -25,7 +26,7 @@ public static class RandomHelper
/// </returns>
public static int GetRandom(int minValue, int maxValue)
{
lock (Rnd)
lock (_syncLock)
{
return Rnd.Next(minValue, maxValue);
}
Expand All @@ -42,7 +43,7 @@ public static int GetRandom(int minValue, int maxValue)
/// </returns>
public static int GetRandom(int maxValue)
{
lock (Rnd)
lock (_syncLock)
{
return Rnd.Next(maxValue);
}
Expand All @@ -54,7 +55,7 @@ public static int GetRandom(int maxValue)
/// <returns>A 32-bit signed integer greater than or equal to zero and less than <see cref="int.MaxValue"/>.</returns>
public static int GetRandom()
{
lock (Rnd)
lock (_syncLock)
{
return Rnd.Next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class VirtualFileLocalizationResourceContributorBase : ILocaliza
private IVirtualFileProvider _virtualFileProvider = default!;
private Dictionary<string, ILocalizationDictionary>? _dictionaries;
private bool _subscribedForChanges;
private readonly object _syncObj = new object();
private readonly Lock _syncObj = LockFactory.Create();
private LocalizationResourceBase _resource = default!;

protected VirtualFileLocalizationResourceContributorBase(string virtualPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MongoModelBuilder : IMongoModelBuilder
{
private readonly Dictionary<Type, object> _entityModelBuilders;

private static readonly object SyncObj = new object();
private static readonly Lock SyncObj = LockFactory.Create();

public MongoModelBuilder()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\..\configureawait.props" />
<Import Project="..\..\..\common.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Volo.Abp.ObjectExtending.Modularity;

public static class ModuleExtensionConfigurationHelper
{
private static object SyncLock = new object();
private static Lock SyncLock = LockFactory.Create();

public static void ApplyEntityConfigurationToEntity(
string moduleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Volo.Abp.Testing.Utils;
public class TestCounter : ITestCounter, ISingletonDependency
{
private readonly Dictionary<string, int> _values;
private readonly Lock _syncLock = LockFactory.Create();

public TestCounter()
{
Expand All @@ -24,7 +25,7 @@ public int Decrement(string name)

public int Add(string name, int count)
{
lock (_values)
lock (_syncLock)
{
var newValue = _values.GetOrDefault(name) + count;
_values[name] = newValue;
Expand All @@ -34,15 +35,15 @@ public int Add(string name, int count)

public int GetValue(string name)
{
lock (_values)
lock (_syncLock)
{
return _values.GetOrDefault(name);
}
}

public void ResetCount(string name)
{
lock (_values)
lock (_syncLock)
{
_values[name] = 0;
}
Expand Down
Loading