Use the new more efficient .NET 9 Lock type (#18015)

This commit is contained in:
Henrik
2025-01-20 11:41:13 +01:00
committed by GitHub
parent 87916eb9cf
commit ed08923b46
32 changed files with 37 additions and 37 deletions

View File

@@ -5,13 +5,13 @@ namespace Umbraco.Cms.Core.Cache;
public class ValueEditorCache : IValueEditorCache
{
private readonly object _dictionaryLocker;
private readonly Lock _dictionaryLocker;
private readonly Dictionary<string, Dictionary<int, IDataValueEditor>> _valueEditorCache;
public ValueEditorCache()
{
_valueEditorCache = new Dictionary<string, Dictionary<int, IDataValueEditor>>();
_dictionaryLocker = new object();
_dictionaryLocker = new Lock();
}
public IDataValueEditor GetValueEditor(IDataEditor editor, IDataType dataType)

View File

@@ -13,7 +13,7 @@ public abstract class CollectionBuilderBase<TBuilder, TCollection, TItem> : ICol
where TBuilder : CollectionBuilderBase<TBuilder, TCollection, TItem>
where TCollection : class, IBuilderCollection<TItem>
{
private readonly object _locker = new();
private readonly Lock _locker = new();
private readonly List<Type> _types = new();
private Type[]? _registeredTypes;

View File

@@ -40,7 +40,7 @@ public class TypeFinder : ITypeFinder
private static readonly ConcurrentDictionary<string, Type?> TypeNamesCache = new();
private readonly IAssemblyProvider _assemblyProvider;
private readonly object _localFilteredAssemblyCacheLocker = new();
private readonly Lock _localFilteredAssemblyCacheLocker = new();
private readonly ILogger<TypeFinder> _logger;
private readonly List<string> _notifiedLoadExceptionAssemblies = new();

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Cms.Core.Composing;
/// </remarks>
public sealed class TypeLoader
{
private readonly object _locko = new();
private readonly Lock _locko = new();
private readonly ILogger<TypeLoader> _logger;
private readonly Dictionary<CompositeTypeTypeKey, TypeList> _types = new();

View File

@@ -9,7 +9,7 @@ namespace Umbraco.Cms.Core.Diagnostics;
// which itself got it from http://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/
public static class MiniDump
{
private static readonly object LockO = new();
private static readonly Lock LockO = new();
[Flags]
public enum Option : uint

View File

@@ -18,7 +18,7 @@ public class ScopedNotificationPublisher<TNotificationHandler> : IScopedNotifica
private readonly IEventAggregator _eventAggregator;
private readonly List<INotification> _notificationOnScopeCompleted = new List<INotification>();
private readonly bool _publishCancelableNotificationOnScopeExit;
private readonly object _locker = new();
private readonly Lock _locker = new();
private bool _isSuppressed;
public ScopedNotificationPublisher(IEventAggregator eventAggregator, bool publishCancelableNotificationOnScopeExit = false)

View File

@@ -12,7 +12,7 @@ public static class WaitHandleExtensions
public static Task WaitOneAsync(this WaitHandle handle, int millisecondsTimeout = Timeout.Infinite)
{
var tcs = new TaskCompletionSource<object?>();
var callbackHandleInitLock = new object();
var callbackHandleInitLock = new Lock();
lock (callbackHandleInitLock)
{
RegisteredWaitHandle? callbackHandle = null;

View File

@@ -31,7 +31,7 @@ namespace Umbraco.Cms.Core.IO
// shadow support
private readonly List<ShadowWrapper> _shadowWrappers = new();
private readonly object _shadowLocker = new();
private readonly Lock _shadowLocker = new();
private static string? _shadowCurrentId; // static - unique!!
#region Constructor

View File

@@ -13,7 +13,7 @@ namespace Umbraco.Cms.Core.Models;
[DataContract(IsReference = true)]
public class PropertyCollection : KeyedCollection<string, IProperty>, IPropertyCollection
{
private readonly object _addLocker = new();
private readonly Lock _addLocker = new();
/// <summary>
/// Initializes a new instance of the <see cref="PropertyCollection" /> class.

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Cms.Core.Models.PublishedContent
{
private readonly IPublishedModelFactory _publishedModelFactory;
private readonly PropertyValueConverterCollection _propertyValueConverters;
private readonly object _locker = new object();
private readonly Lock _locker = new();
private volatile bool _initialized;
private IPropertyValueConverter? _converter;
private PropertyCacheLevel _cacheLevel;

View File

@@ -5,7 +5,7 @@ namespace Umbraco.Cms.Core.PropertyEditors;
public class PropertyValueConverterCollection : BuilderCollectionBase<IPropertyValueConverter>
{
private readonly object _locker = new();
private readonly Lock _locker = new();
private Dictionary<IPropertyValueConverter, Type[]>? _defaultConverters;
public PropertyValueConverterCollection(Func<IEnumerable<IPropertyValueConverter>> items)

View File

@@ -14,7 +14,7 @@ internal class PublishedElementPropertyBase : PublishedPropertyBase
// means faster execution, but uses memory - not sure if we want it
// so making it configurable.
private const bool FullCacheWhenPreviewing = true;
private readonly object _locko = new();
private readonly Lock _locko = new();
private readonly object? _sourceValue;
protected readonly bool IsMember;
protected readonly bool IsPreviewing;

View File

@@ -13,7 +13,7 @@ public class LockingMechanism : ILockingMechanism
{
private readonly IDistributedLockingMechanismFactory _distributedLockingMechanismFactory;
private readonly ILogger<LockingMechanism> _logger;
private readonly object _locker = new();
private readonly Lock _locker = new();
private StackQueue<(DistributedLockType lockType, TimeSpan timeout, Guid instanceId, int lockId)>? _queuedLocks;
private HashSet<int>? _readLocks;
private Dictionary<Guid, Dictionary<int, int>>? _readLocksDictionary;

View File

@@ -20,7 +20,7 @@ public class NotificationService : INotificationService
{
// manage notifications
// ideally, would need to use IBackgroundTasks - but they are not part of Core!
private static readonly object Locker = new();
private static readonly Lock Locker = new();
private readonly IContentService _contentService;
private readonly ContentSettings _contentSettings;

View File

@@ -9,7 +9,7 @@ namespace Umbraco.Cms.Core;
public class SimpleMainDom : IMainDom, IDisposable
{
private readonly List<KeyValuePair<int, Action>> _callbacks = new();
private readonly object _locko = new();
private readonly Lock _locko = new();
private bool _disposedValue;
private bool _isStopping;

View File

@@ -7,7 +7,7 @@ namespace Umbraco.Cms.Core;
[Obsolete("This class will be removed in a future version.")]
public static class UdiParserServiceConnectors
{
private static readonly object ScanLocker = new();
private static readonly Lock ScanLocker = new();
// notes - see U4-10409
// if this class is used during application pre-start it cannot scans the assemblies,

View File

@@ -8,7 +8,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Mappers;
public abstract class BaseMapper
{
private readonly object _definedLock = new();
private readonly Lock _definedLock = new();
private readonly MapperConfigurationStore _maps;

View File

@@ -36,7 +36,7 @@ internal class UserRepository : EntityRepositoryBase<Guid, IUser>, IUserReposito
private readonly IRuntimeState _runtimeState;
private string? _passwordConfigJson;
private bool _passwordConfigInitialized;
private readonly object _sqliteValidateSessionLock = new();
private readonly Lock _sqliteValidateSessionLock = new();
private readonly IDictionary<string, IPermissionMapper> _permissionMappers;
private readonly IAppPolicyCache _globalCache;
private readonly IScopeAccessor _scopeAccessor;

View File

@@ -23,7 +23,7 @@ public class SqlMainDomLock : IMainDomLock
private readonly IUmbracoDatabase? _db;
private readonly UmbracoDatabaseFactory _dbFactory;
private readonly IOptions<GlobalSettings> _globalSettings;
private readonly object _locker = new();
private readonly Lock _locker = new();
private readonly string _lockId;
private readonly ILogger<SqlMainDomLock> _logger;
private bool _acquireWhenTablesNotAvailable;

View File

@@ -23,7 +23,7 @@ public abstract class DatabaseServerMessenger : ServerMessengerBase, IDisposable
private readonly Lazy<SyncBootState?> _initialized;
private readonly LastSyncedFileManager _lastSyncedFileManager;
private readonly object _locko = new();
private readonly Lock _locko = new();
/*
* this messenger writes ALL instructions to the database,
* but only processes instructions coming from remote servers,

View File

@@ -27,7 +27,7 @@ internal class PublishedProperty : PublishedPropertyBase
private CacheValues? _cacheValues;
// the variant source and inter values
private readonly object _locko = new();
private readonly Lock _locko = new();
private ConcurrentDictionary<CompositeStringStringKey, SourceInterValue>? _sourceValues;
// initializes a published content property with a value
@@ -270,7 +270,7 @@ internal class PublishedProperty : PublishedPropertyBase
private class CacheValues : CacheValue
{
private readonly object _locko = new();
private readonly Lock _locko = new();
private ConcurrentDictionary<CompositeStringStringKey, CacheValue>? _values;
public CacheValue For(string? culture, string? segment)

View File

@@ -11,7 +11,7 @@ namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
[DebuggerDisplay("{Display}")]
internal struct LazyCompressedString
{
private readonly object _locker;
private readonly Lock _locker;
private byte[]? _bytes;
private string? _str;
@@ -21,7 +21,7 @@ internal struct LazyCompressedString
/// <param name="bytes">LZ4 Pickle compressed UTF8 String</param>
public LazyCompressedString(byte[] bytes)
{
_locker = new object();
_locker = new Lock();
_bytes = bytes;
_str = null;
}

View File

@@ -12,8 +12,8 @@ namespace Umbraco.Cms.Web.Common.Localization;
public abstract class DynamicRequestCultureProviderBase : RequestCultureProvider
{
private readonly RequestLocalizationOptions _options;
private readonly object _lockerSupportedCultures = new();
private readonly object _lockerSupportedUICultures = new();
private readonly Lock _lockerSupportedCultures = new();
private readonly Lock _lockerSupportedUICultures = new();
/// <summary>
/// Initializes a new instance of the <see cref="DynamicRequestCultureProviderBase" /> class.
@@ -66,7 +66,7 @@ public abstract class DynamicRequestCultureProviderBase : RequestCultureProvider
/// <param name="cultures">The cultures.</param>
/// <param name="locker">The locker object to use.</param>
/// <param name="addAction">The add action to execute.</param>
private static void TryAddLocked(IEnumerable<CultureInfo>? supportedCultures, IEnumerable<StringSegment> cultures, object locker, Action<StringSegment> addAction)
private static void TryAddLocked(IEnumerable<CultureInfo>? supportedCultures, IEnumerable<StringSegment> cultures, Lock locker, Action<StringSegment> addAction)
{
foreach (StringSegment culture in cultures)
{

View File

@@ -11,7 +11,7 @@ namespace Umbraco.Cms.Web.Common.Logging;
/// </remarks>
internal class RegisteredReloadableLogger
{
private static readonly object _frozenLock = new();
private static readonly Lock _frozenLock = new();
private static bool _frozen;
private readonly ReloadableLogger _logger;

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto;
internal class CollectibleRuntimeViewCompiler : IViewCompiler
{
private readonly object _cacheLock = new object();
private readonly Lock _cacheLock = new();
private readonly Dictionary<string, CompiledViewDescriptor> _precompiledViews;
private readonly ConcurrentDictionary<string, string> _normalizedPathCache;
private readonly IFileProvider _fileProvider;

View File

@@ -162,7 +162,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto
public Assembly? CurrentModelsAssembly { get; private set; }
/// <inheritdoc />
public object SyncRoot { get; } = new object();
public object SyncRoot { get; } = new();
private UmbracoServices UmbracoServices => _umbracoServices.Value;

View File

@@ -21,7 +21,7 @@ public sealed class RoutableDocumentFilter : IRoutableDocumentFilter
private readonly GlobalSettings _globalSettings;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ConcurrentDictionary<string, bool> _routeChecks = new(StringComparer.OrdinalIgnoreCase);
private readonly object _routeLocker = new();
private readonly Lock _routeLocker = new();
private readonly WebRoutingSettings _routingSettings;
private object _initLocker = new();
private bool _isInit;

View File

@@ -10,7 +10,7 @@ namespace Umbraco.Cms.Web.Common.Security;
public class BackOfficeSecurity : IBackOfficeSecurity
{
private readonly object _currentUserLock = new();
private readonly Lock _currentUserLock = new();
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUserService _userService;
private IUser? _currentUser;

View File

@@ -28,7 +28,7 @@ public class LoadTestController : Controller
</html>";
private static readonly Random s_random = new();
private static readonly object s_locko = new();
private static readonly Lock s_locko = new();
private static volatile int s_containerId = -1;

View File

@@ -27,7 +27,7 @@ namespace Umbraco.Cms.Tests.Integration.Testing;
[NonParallelizable]
public abstract class UmbracoIntegrationTestBase
{
private static readonly object s_dbLocker = new();
private static readonly Lock s_dbLocker = new();
private static ITestDatabase? s_dbInstance;
private static TestDbMeta s_fixtureDbMeta;
private static int s_testCount = 1;

View File

@@ -191,7 +191,7 @@ public class LocksTests : UmbracoIntegrationTest
const int threadCount = 8;
var threads = new Thread[threadCount];
var exceptions = new Exception[threadCount];
var locker = new object();
Lock locker = new();
var acquired = 0;
var entered = 0;
var ms = new AutoResetEvent[threadCount];

View File

@@ -125,7 +125,7 @@ public class EFCoreLockTests : UmbracoIntegrationTest
const int threadCount = 8;
var threads = new Thread[threadCount];
var exceptions = new Exception[threadCount];
var locker = new object();
Lock locker = new();
var acquired = 0;
var m2 = new ManualResetEventSlim(false);
var m1 = new ManualResetEventSlim(false);