diff options
Diffstat (limited to 'src/System.Private.CoreLib/shared')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Threading/LazyInitializer.cs | 14 | ||||
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Threading/Volatile.cs | 7 |
2 files changed, 11 insertions, 10 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Threading/LazyInitializer.cs b/src/System.Private.CoreLib/shared/System/Threading/LazyInitializer.cs index 7f28a9fd53..b25db6afaf 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/LazyInitializer.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/LazyInitializer.cs @@ -48,7 +48,7 @@ namespace System.Threading /// if an object was not used and to then dispose of the object appropriately. /// </para> /// </remarks> - public static T EnsureInitialized<T>(ref T target) where T : class => + public static T EnsureInitialized<T>(ref T target) where T : class? => Volatile.Read(ref target) ?? EnsureInitializedCore(ref target); /// <summary> @@ -57,7 +57,7 @@ namespace System.Threading /// <typeparam name="T">The reference type of the reference to be initialized.</typeparam> /// <param name="target">The variable that need to be initialized</param> /// <returns>The initialized variable</returns> - private static T EnsureInitializedCore<T>(ref T target) where T : class + private static T EnsureInitializedCore<T>(ref T target) where T : class? { try { @@ -100,7 +100,7 @@ namespace System.Threading /// if an object was not used and to then dispose of the object appropriately. /// </para> /// </remarks> - public static T EnsureInitialized<T>(ref T target, Func<T> valueFactory) where T : class => + public static T EnsureInitialized<T>(ref T target, Func<T> valueFactory) where T : class? => Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, valueFactory); /// <summary> @@ -110,7 +110,7 @@ namespace System.Threading /// <param name="target">The variable that need to be initialized</param> /// <param name="valueFactory">The delegate that will be executed to initialize the target</param> /// <returns>The initialized variable</returns> - private static T EnsureInitializedCore<T>(ref T target, Func<T> valueFactory) where T : class + private static T EnsureInitializedCore<T>(ref T target, Func<T> valueFactory) where T : class? { T value = valueFactory(); if (value == null) @@ -242,7 +242,7 @@ namespace System.Threading /// <paramref name="target"/>. If <paramref name="syncLock"/> is null, a new object will be instantiated.</param> /// <param name="valueFactory">The <see cref="T:System.Func{T}"/> invoked to initialize the reference.</param> /// <returns>The initialized value of type <typeparamref name="T"/>.</returns> - public static T EnsureInitialized<T>(ref T target, ref object? syncLock, Func<T> valueFactory) where T : class => + public static T EnsureInitialized<T>(ref T target, ref object? syncLock, Func<T> valueFactory) where T : class? => Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, ref syncLock, valueFactory); /// <summary> @@ -257,7 +257,7 @@ namespace System.Threading /// The <see cref="T:System.Func{T}"/> to invoke in order to produce the lazily-initialized value. /// </param> /// <returns>The initialized object.</returns> - private static T EnsureInitializedCore<T>(ref T target, ref object? syncLock, Func<T> valueFactory) where T : class + private static T EnsureInitializedCore<T>(ref T target, ref object? syncLock, Func<T> valueFactory) where T : class? { // Lazily initialize the lock if necessary and then double check if initialization is still required. lock (EnsureLockInitialized(ref syncLock)) @@ -284,6 +284,6 @@ namespace System.Threading private static object EnsureLockInitialized(ref object? syncLock) => syncLock ?? Interlocked.CompareExchange(ref syncLock, new object(), null) ?? - syncLock; + syncLock!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 } } diff --git a/src/System.Private.CoreLib/shared/System/Threading/Volatile.cs b/src/System.Private.CoreLib/shared/System/Threading/Volatile.cs index 845d2d089a..4ace8f994f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Volatile.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Volatile.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Runtime.CompilerServices; using System.Runtime.Versioning; using Internal.Runtime.CompilerServices; @@ -214,16 +215,16 @@ namespace System.Threading #endregion #region T - private struct VolatileObject { public volatile object Value; } + private struct VolatileObject { public volatile object? Value; } [Intrinsic] [NonVersionable] - public static T Read<T>(ref T location) where T : class => + public static T Read<T>(ref T location) where T : class? => Unsafe.As<T>(Unsafe.As<T, VolatileObject>(ref location).Value); [Intrinsic] [NonVersionable] - public static void Write<T>(ref T location, T value) where T : class => + public static void Write<T>(ref T location, T value) where T : class? => Unsafe.As<T, VolatileObject>(ref location).Value = value; #endregion } |