Skip to content

Commit

Permalink
Use the new more efficient .NET 9 Lock type (umbraco#18015)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henr1k80 authored Jan 20, 2025
1 parent 87916eb commit ed08923
Show file tree
Hide file tree
Showing 32 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Umbraco.Core/Cache/ValueEditorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Composing/CollectionBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Composing/TypeFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Composing/TypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Diagnostics/MiniDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Events/ScopedNotificationPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Extensions/WaitHandleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/IO/FileSystems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class FileSystems

// 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

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Models/PropertyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PublishedPropertyType : IPublishedPropertyType
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Scoping/LockingMechanism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/SimpleMainDom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/UdiParserServiceConnectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Infrastructure/Sync/DatabaseServerMessenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.PublishedCache.HybridCache/PublishedProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -270,7 +270,7 @@ public string? Segment

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,7 +66,7 @@ protected DynamicRequestCultureProviderBase(RequestLocalizationOptions requestLo
/// <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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public InMemoryModelFactory(
public Assembly? CurrentModelsAssembly { get; private set; }

/// <inheritdoc />
public object SyncRoot { get; } = new object();
public object SyncRoot { get; } = new();

private UmbracoServices UmbracoServices => _umbracoServices.Value;

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Web.Common/Routing/RoutableDocumentFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Web.Common/Security/BackofficeSecurity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Umbraco.TestData/LoadTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void ConcurrentWritersTest()
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void ConcurrentReadersTest()
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);
Expand Down

0 comments on commit ed08923

Please sign in to comment.