diff options
author | Stephen Toub <stoub@microsoft.com> | 2019-04-02 18:05:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-02 18:05:59 -0400 |
commit | 7eabcb1875d3a73c6e587c677ccc4882b1c2f396 (patch) | |
tree | 9186a088d91cc3ad1a8f96198589bc4cf559799b /src | |
parent | d8bcff11111e4da740986d3725722c2f28515e2a (diff) | |
download | coreclr-7eabcb1875d3a73c6e587c677ccc4882b1c2f396.tar.gz coreclr-7eabcb1875d3a73c6e587c677ccc4882b1c2f396.tar.bz2 coreclr-7eabcb1875d3a73c6e587c677ccc4882b1c2f396.zip |
Nullable: System.Threading (#23663)
Finishes off annotation of the System.Threading namespace, not including subnamespaces.
Diffstat (limited to 'src')
38 files changed, 155 insertions, 133 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Globalization/CultureData.cs b/src/System.Private.CoreLib/shared/System/Globalization/CultureData.cs index 193d3d99bf..12f8ce2bc7 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/CultureData.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/CultureData.cs @@ -796,8 +796,7 @@ namespace System.Globalization { case "zh-CHS": case "zh-CHT": - // TODO-NULLABLE: dotnet/roslyn#34273 - return _sName!; + return _sName!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34273 } return _sRealName!; } diff --git a/src/System.Private.CoreLib/shared/System/Threading/AbandonedMutexException.cs b/src/System.Private.CoreLib/shared/System/Threading/AbandonedMutexException.cs index bd504dd27a..1517c7e06e 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/AbandonedMutexException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/AbandonedMutexException.cs @@ -7,9 +7,7 @@ // AbandonedMutexs indicate serious error in user code or machine state. //////////////////////////////////////////////////////////////////////////////// -using System; -using System.Threading; -using System.Runtime.InteropServices; +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -19,7 +17,7 @@ namespace System.Threading public class AbandonedMutexException : SystemException { private int _mutexIndex = -1; - private Mutex _mutex = null; + private Mutex? _mutex = null; public AbandonedMutexException() : base(SR.Threading_AbandonedMutexException) @@ -27,33 +25,33 @@ namespace System.Threading HResult = HResults.COR_E_ABANDONEDMUTEX; } - public AbandonedMutexException(string message) + public AbandonedMutexException(string? message) : base(message) { HResult = HResults.COR_E_ABANDONEDMUTEX; } - public AbandonedMutexException(string message, Exception inner) + public AbandonedMutexException(string? message, Exception? inner) : base(message, inner) { HResult = HResults.COR_E_ABANDONEDMUTEX; } - public AbandonedMutexException(int location, WaitHandle handle) + public AbandonedMutexException(int location, WaitHandle? handle) : base(SR.Threading_AbandonedMutexException) { HResult = HResults.COR_E_ABANDONEDMUTEX; SetupException(location, handle); } - public AbandonedMutexException(string message, int location, WaitHandle handle) + public AbandonedMutexException(string? message, int location, WaitHandle? handle) : base(message) { HResult = HResults.COR_E_ABANDONEDMUTEX; SetupException(location, handle); } - public AbandonedMutexException(string message, Exception inner, int location, WaitHandle handle) + public AbandonedMutexException(string? message, Exception? inner, int location, WaitHandle? handle) : base(message, inner) { HResult = HResults.COR_E_ABANDONEDMUTEX; @@ -65,14 +63,13 @@ namespace System.Threading { } - private void SetupException(int location, WaitHandle handle) + private void SetupException(int location, WaitHandle? handle) { _mutexIndex = location; - if (handle != null) - _mutex = handle as Mutex; + _mutex = handle as Mutex; } - public Mutex Mutex => _mutex; + public Mutex? Mutex => _mutex; public int MutexIndex => _mutexIndex; } } diff --git a/src/System.Private.CoreLib/shared/System/Threading/ApartmentState.cs b/src/System.Private.CoreLib/shared/System/Threading/ApartmentState.cs index 47c1677cb5..961979282f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ApartmentState.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ApartmentState.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 namespace System.Threading { public enum ApartmentState diff --git a/src/System.Private.CoreLib/shared/System/Threading/CompressedStack.cs b/src/System.Private.CoreLib/shared/System/Threading/CompressedStack.cs index 7f209f5920..32861df822 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/CompressedStack.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/CompressedStack.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.Serialization; namespace System.Threading @@ -32,7 +33,7 @@ namespace System.Threading return new CompressedStack(); } - public static void Run(CompressedStack compressedStack, ContextCallback callback, object state) + public static void Run(CompressedStack compressedStack, ContextCallback callback, object? state) { if (compressedStack == null) { diff --git a/src/System.Private.CoreLib/shared/System/Threading/DeferredDisposableLifetime.cs b/src/System.Private.CoreLib/shared/System/Threading/DeferredDisposableLifetime.cs index e2b1eb983b..6038a3e752 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/DeferredDisposableLifetime.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/DeferredDisposableLifetime.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.Diagnostics; namespace System.Threading @@ -41,9 +42,7 @@ namespace System.Threading /// </remarks> internal struct DeferredDisposableLifetime<T> where T : class, IDeferredDisposable { - // - // _count is positive until Dispose is called, after which it's (-1 - refcount). - // + /// <summary>_count is positive until Dispose is called, after which it's (-1 - refcount).</summary> private int _count; public bool AddRef(T obj) diff --git a/src/System.Private.CoreLib/shared/System/Threading/EventResetMode.cs b/src/System.Private.CoreLib/shared/System/Threading/EventResetMode.cs index 7aac0f51eb..5de3cb5763 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/EventResetMode.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/EventResetMode.cs @@ -12,6 +12,7 @@ ** =============================================================================*/ +#nullable enable namespace System.Threading { public enum EventResetMode diff --git a/src/System.Private.CoreLib/shared/System/Threading/EventWaitHandle.cs b/src/System.Private.CoreLib/shared/System/Threading/EventWaitHandle.cs index 9b448c51c2..b01bfb2131 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/EventWaitHandle.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/EventWaitHandle.cs @@ -45,7 +45,7 @@ namespace System.Threading } } - public static bool TryOpenExisting(string name, out EventWaitHandle? result) // TODO-NULLABLE: Try pattern with non-null result when true + public static bool TryOpenExisting(string name, out EventWaitHandle? result) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { return OpenExistingWorker(name, out result) == OpenExistingResult.Success; } diff --git a/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs b/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs index 6f8a4d75eb..4b46ab3128 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs @@ -101,7 +101,7 @@ namespace System.Threading public static void RestoreFlow() { Thread currentThread = Thread.CurrentThread; - ExecutionContext executionContext = currentThread._executionContext; + ExecutionContext? executionContext = currentThread._executionContext; if (executionContext == null || !executionContext.m_isFlowSuppressed) { throw new InvalidOperationException(SR.InvalidOperation_CannotRestoreUnsupressedFlow); @@ -112,7 +112,7 @@ namespace System.Threading public static bool IsFlowSuppressed() { - ExecutionContext executionContext = Thread.CurrentThread._executionContext; + ExecutionContext? executionContext = Thread.CurrentThread._executionContext; return executionContext != null && executionContext.m_isFlowSuppressed; } @@ -153,7 +153,7 @@ namespace System.Threading // so that they won't "leak" back into caller. // These variables will cross EH so be forced to stack ExecutionContext? previousExecutionCtx = previousExecutionCtx0; - SynchronizationContext previousSyncCtx = currentThread0._synchronizationContext; + SynchronizationContext? previousSyncCtx = currentThread0._synchronizationContext; if (executionContext != null && executionContext.m_isDefault) { @@ -180,7 +180,7 @@ namespace System.Threading } // Re-enregistrer variables post EH with 1 post-fix so they can be used in registers rather than from stack - SynchronizationContext previousSyncCtx1 = previousSyncCtx; + SynchronizationContext? previousSyncCtx1 = previousSyncCtx; Thread currentThread1 = currentThread; // The common case is that these have not changed, so avoid the cost of a write barrier if not needed. if (currentThread1._synchronizationContext != previousSyncCtx1) @@ -190,7 +190,7 @@ namespace System.Threading } ExecutionContext? previousExecutionCtx1 = previousExecutionCtx; - ExecutionContext currentExecutionCtx1 = currentThread1._executionContext; + ExecutionContext? currentExecutionCtx1 = currentThread1._executionContext; if (currentExecutionCtx1 != previousExecutionCtx1) { RestoreChangedContextToThread(currentThread1, previousExecutionCtx1, currentExecutionCtx1); @@ -223,7 +223,7 @@ namespace System.Threading // so that they won't "leak" back into caller. // These variables will cross EH so be forced to stack ExecutionContext? previousExecutionCtx = previousExecutionCtx0; - SynchronizationContext previousSyncCtx = currentThread0._synchronizationContext; + SynchronizationContext? previousSyncCtx = currentThread0._synchronizationContext; if (executionContext != null && executionContext.m_isDefault) { @@ -250,7 +250,7 @@ namespace System.Threading } // Re-enregistrer variables post EH with 1 post-fix so they can be used in registers rather than from stack - SynchronizationContext previousSyncCtx1 = previousSyncCtx; + SynchronizationContext? previousSyncCtx1 = previousSyncCtx; Thread currentThread1 = currentThread; // The common case is that these have not changed, so avoid the cost of a write barrier if not needed. if (currentThread1._synchronizationContext != previousSyncCtx1) @@ -260,7 +260,7 @@ namespace System.Threading } ExecutionContext? previousExecutionCtx1 = previousExecutionCtx; - ExecutionContext currentExecutionCtx1 = currentThread1._executionContext; + ExecutionContext? currentExecutionCtx1 = currentThread1._executionContext; if (currentExecutionCtx1 != previousExecutionCtx1) { RestoreChangedContextToThread(currentThread1, previousExecutionCtx1, currentExecutionCtx1); @@ -299,7 +299,7 @@ namespace System.Threading // Enregister threadPoolThread as it crossed EH, and use enregistered variable Thread currentThread = threadPoolThread; - ExecutionContext currentExecutionCtx = currentThread._executionContext; + ExecutionContext? currentExecutionCtx = currentThread._executionContext; // Restore changed SynchronizationContext back to Default currentThread._synchronizationContext = null; @@ -353,7 +353,7 @@ namespace System.Threading [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void ResetThreadPoolThread(Thread currentThread) { - ExecutionContext currentExecutionCtx = currentThread._executionContext; + ExecutionContext? currentExecutionCtx = currentThread._executionContext; // Reset to defaults currentThread._synchronizationContext = null; @@ -485,7 +485,7 @@ namespace System.Threading internal static void SetLocalValue(IAsyncLocal local, object? newValue, bool needChangeNotifications) { - ExecutionContext current = Thread.CurrentThread._executionContext; + ExecutionContext? current = Thread.CurrentThread._executionContext; object? previousValue = null; bool hadPreviousValue = false; diff --git a/src/System.Private.CoreLib/shared/System/Threading/IOCompletionCallback.cs b/src/System.Private.CoreLib/shared/System/Threading/IOCompletionCallback.cs index 571ce467eb..f2ad774395 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/IOCompletionCallback.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/IOCompletionCallback.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 namespace System.Threading { [CLSCompliant(false)] diff --git a/src/System.Private.CoreLib/shared/System/Threading/IThreadPoolWorkItem.cs b/src/System.Private.CoreLib/shared/System/Threading/IThreadPoolWorkItem.cs index 31cd991813..1b79a11758 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/IThreadPoolWorkItem.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/IThreadPoolWorkItem.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 namespace System.Threading { /// <summary>Represents a work item that can be executed by the ThreadPool.</summary> @@ -9,4 +10,4 @@ namespace System.Threading { void Execute(); } -}
\ No newline at end of file +} diff --git a/src/System.Private.CoreLib/shared/System/Threading/LockRecursionException.cs b/src/System.Private.CoreLib/shared/System/Threading/LockRecursionException.cs index c76c7a5040..52d45af90b 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/LockRecursionException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/LockRecursionException.cs @@ -2,7 +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. -using System; +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -15,12 +15,12 @@ namespace System.Threading { } - public LockRecursionException(string message) + public LockRecursionException(string? message) : base(message) { } - public LockRecursionException(string message, Exception innerException) + public LockRecursionException(string? message, Exception? innerException) : base(message, innerException) { } diff --git a/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs b/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs index eb1aee594f..1d14037cc3 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs @@ -4,10 +4,8 @@ #nullable enable using System.IO; -using Microsoft.Win32; using Microsoft.Win32.SafeHandles; using System.Runtime.InteropServices; -using System.Diagnostics; namespace System.Threading { diff --git a/src/System.Private.CoreLib/shared/System/Threading/Mutex.cs b/src/System.Private.CoreLib/shared/System/Threading/Mutex.cs index 8cfeeb7cfc..45c0c30943 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Mutex.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Mutex.cs @@ -58,7 +58,7 @@ namespace System.Threading } } - public static bool TryOpenExisting(string name, out Mutex? result) => // TODO-NULLABLE: Try pattern with non-null result when true + public static bool TryOpenExisting(string name, out Mutex? result) => // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 OpenExistingWorker(name, out result) == OpenExistingResult.Success; } } diff --git a/src/System.Private.CoreLib/shared/System/Threading/NativeOverlapped.cs b/src/System.Private.CoreLib/shared/System/Threading/NativeOverlapped.cs index 933cb81ecc..7e9d29775d 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/NativeOverlapped.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/NativeOverlapped.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.InteropServices; namespace System.Threading diff --git a/src/System.Private.CoreLib/shared/System/Threading/ParameterizedThreadStart.cs b/src/System.Private.CoreLib/shared/System/Threading/ParameterizedThreadStart.cs index c0f29e8e80..4f49fc1aa7 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ParameterizedThreadStart.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ParameterizedThreadStart.cs @@ -12,7 +12,8 @@ ** =============================================================================*/ +#nullable enable namespace System.Threading { - public delegate void ParameterizedThreadStart(object obj); + public delegate void ParameterizedThreadStart(object? obj); } diff --git a/src/System.Private.CoreLib/shared/System/Threading/Semaphore.cs b/src/System.Private.CoreLib/shared/System/Threading/Semaphore.cs index 9d1b6e4d0d..7c75db3c1b 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Semaphore.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Semaphore.cs @@ -53,7 +53,7 @@ namespace System.Threading } } - public static bool TryOpenExisting(string name, out Semaphore? result) => // TODO-NULLABLE: Try pattern with non-null result when true + public static bool TryOpenExisting(string name, out Semaphore? result) => // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 OpenExistingWorker(name, out result) == OpenExistingResult.Success; public int Release() => ReleaseCore(1); diff --git a/src/System.Private.CoreLib/shared/System/Threading/SemaphoreFullException.cs b/src/System.Private.CoreLib/shared/System/Threading/SemaphoreFullException.cs index a6cff781df..5bee6108c0 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/SemaphoreFullException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/SemaphoreFullException.cs @@ -2,7 +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. -using System; +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -15,11 +15,11 @@ namespace System.Threading { } - public SemaphoreFullException(string message) : base(message) + public SemaphoreFullException(string? message) : base(message) { } - public SemaphoreFullException(string message, Exception innerException) : base(message, innerException) + public SemaphoreFullException(string? message, Exception? innerException) : base(message, innerException) { } diff --git a/src/System.Private.CoreLib/shared/System/Threading/SynchronizationContext.cs b/src/System.Private.CoreLib/shared/System/Threading/SynchronizationContext.cs index 8f4eec1865..3f9a2bec2f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/SynchronizationContext.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/SynchronizationContext.cs @@ -14,7 +14,7 @@ namespace System.Threading } #if !FEATURE_APPX && !ENABLE_WINRT - public static SynchronizationContext Current => Thread.CurrentThread._synchronizationContext; + public static SynchronizationContext? Current => Thread.CurrentThread._synchronizationContext; #endif protected void SetWaitNotificationRequired() => _requireWaitNotification = true; diff --git a/src/System.Private.CoreLib/shared/System/Threading/SynchronizationLockException.cs b/src/System.Private.CoreLib/shared/System/Threading/SynchronizationLockException.cs index 3b5d31f3c0..fbd685afc9 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/SynchronizationLockException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/SynchronizationLockException.cs @@ -12,6 +12,7 @@ ** =============================================================================*/ +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -26,13 +27,13 @@ namespace System.Threading HResult = HResults.COR_E_SYNCHRONIZATIONLOCK; } - public SynchronizationLockException(string message) + public SynchronizationLockException(string? message) : base(message) { HResult = HResults.COR_E_SYNCHRONIZATIONLOCK; } - public SynchronizationLockException(string message, Exception innerException) + public SynchronizationLockException(string? message, Exception? innerException) : base(message, innerException) { HResult = HResults.COR_E_SYNCHRONIZATIONLOCK; diff --git a/src/System.Private.CoreLib/shared/System/Threading/Thread.Unix.cs b/src/System.Private.CoreLib/shared/System/Threading/Thread.Unix.cs index 902fcf2650..e3ee7133da 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Thread.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Thread.Unix.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.ConstrainedExecution; namespace System.Threading diff --git a/src/System.Private.CoreLib/shared/System/Threading/Thread.Windows.cs b/src/System.Private.CoreLib/shared/System/Threading/Thread.Windows.cs index 44e5f38a76..b0bae93da6 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Thread.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Thread.Windows.cs @@ -2,8 +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. -using System.Runtime.ConstrainedExecution; - +#nullable enable namespace System.Threading { public sealed partial class Thread diff --git a/src/System.Private.CoreLib/shared/System/Threading/Thread.cs b/src/System.Private.CoreLib/shared/System/Threading/Thread.cs index ef492b0f8b..ff8d64611f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Thread.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Thread.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.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -15,7 +16,7 @@ namespace System.Threading #endif public sealed partial class Thread : CriticalFinalizerObject { - private static AsyncLocal<IPrincipal> s_asyncLocalPrincipal; + private static AsyncLocal<IPrincipal?>? s_asyncLocalPrincipal; [ThreadStatic] private static Thread t_currentThread; @@ -93,7 +94,7 @@ namespace System.Threading SetCultureOnUnstartedThreadNoCheck(value, uiCulture); } - partial void ThreadNameChanged(string value); + partial void ThreadNameChanged(string? value); public CultureInfo CurrentCulture { @@ -131,7 +132,7 @@ namespace System.Threading } } - public static IPrincipal CurrentPrincipal + public static IPrincipal? CurrentPrincipal { get { @@ -149,17 +150,17 @@ namespace System.Threading { return; } - Interlocked.CompareExchange(ref s_asyncLocalPrincipal, new AsyncLocal<IPrincipal>(), null); + Interlocked.CompareExchange(ref s_asyncLocalPrincipal, new AsyncLocal<IPrincipal?>(), null); } - s_asyncLocalPrincipal.Value = value; + s_asyncLocalPrincipal!.Value = value; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 } } public static Thread CurrentThread => t_currentThread ?? InitializeCurrentThread(); - public ExecutionContext ExecutionContext => ExecutionContext.Capture(); + public ExecutionContext? ExecutionContext => ExecutionContext.Capture(); - public string Name + public string? Name { get => _name; set @@ -183,7 +184,7 @@ namespace System.Threading throw new PlatformNotSupportedException(SR.PlatformNotSupported_ThreadAbort); } - public void Abort(object stateInfo) + public void Abort(object? stateInfo) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ThreadAbort); } @@ -216,8 +217,8 @@ namespace System.Threading public static LocalDataStoreSlot AllocateNamedDataSlot(string name) => LocalDataStore.AllocateNamedSlot(name); public static LocalDataStoreSlot GetNamedDataSlot(string name) => LocalDataStore.GetNamedSlot(name); public static void FreeNamedDataSlot(string name) => LocalDataStore.FreeNamedSlot(name); - public static object GetData(LocalDataStoreSlot slot) => LocalDataStore.GetData(slot); - public static void SetData(LocalDataStoreSlot slot, object data) => LocalDataStore.SetData(slot, data); + public static object? GetData(LocalDataStoreSlot slot) => LocalDataStore.GetData(slot); + public static void SetData(LocalDataStoreSlot slot, object? data) => LocalDataStore.SetData(slot, data); [Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)] public ApartmentState ApartmentState @@ -282,7 +283,7 @@ namespace System.Threading public static int VolatileRead(ref int address) => Volatile.Read(ref address); public static long VolatileRead(ref long address) => Volatile.Read(ref address); public static IntPtr VolatileRead(ref IntPtr address) => Volatile.Read(ref address); - public static object VolatileRead(ref object address) => Volatile.Read(ref address); + public static object? VolatileRead(ref object? address) => Volatile.Read(ref address); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 [CLSCompliant(false)] public static sbyte VolatileRead(ref sbyte address) => Volatile.Read(ref address); public static float VolatileRead(ref float address) => Volatile.Read(ref address); @@ -300,7 +301,7 @@ namespace System.Threading public static void VolatileWrite(ref int address, int value) => Volatile.Write(ref address, value); public static void VolatileWrite(ref long address, long value) => Volatile.Write(ref address, value); public static void VolatileWrite(ref IntPtr address, IntPtr value) => Volatile.Write(ref address, value); - public static void VolatileWrite(ref object address, object value) => Volatile.Write(ref address, value); + public static void VolatileWrite(ref object? address, object? value) => Volatile.Write(ref address, value); [CLSCompliant(false)] public static void VolatileWrite(ref sbyte address, sbyte value) => Volatile.Write(ref address, value); public static void VolatileWrite(ref float address, float value) => Volatile.Write(ref address, value); @@ -318,7 +319,7 @@ namespace System.Threading /// </summary> private static class LocalDataStore { - private static Dictionary<string, LocalDataStoreSlot> s_nameToSlotMap; + private static Dictionary<string, LocalDataStoreSlot>? s_nameToSlotMap; public static LocalDataStoreSlot AllocateSlot() { @@ -327,7 +328,7 @@ namespace System.Threading private static Dictionary<string, LocalDataStoreSlot> EnsureNameToSlotMap() { - Dictionary<string, LocalDataStoreSlot> nameToSlotMap = s_nameToSlotMap; + Dictionary<string, LocalDataStoreSlot>? nameToSlotMap = s_nameToSlotMap; if (nameToSlotMap != null) { return nameToSlotMap; @@ -372,7 +373,7 @@ namespace System.Threading } } - private static ThreadLocal<object> GetThreadLocal(LocalDataStoreSlot slot) + private static ThreadLocal<object?> GetThreadLocal(LocalDataStoreSlot slot) { if (slot == null) { @@ -383,12 +384,12 @@ namespace System.Threading return slot.Data; } - public static object GetData(LocalDataStoreSlot slot) + public static object? GetData(LocalDataStoreSlot slot) { return GetThreadLocal(slot).Value; } - public static void SetData(LocalDataStoreSlot slot, object value) + public static void SetData(LocalDataStoreSlot slot, object? value) { GetThreadLocal(slot).Value = value; } diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadAbortException.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadAbortException.cs index 360e84d256..aa4cb299a8 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadAbortException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadAbortException.cs @@ -14,6 +14,7 @@ ** =============================================================================*/ +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -27,7 +28,7 @@ namespace System.Threading HResult = HResults.COR_E_THREADABORTED; } - public object ExceptionState => null; + public object? ExceptionState => null; internal ThreadAbortException(SerializationInfo info, StreamingContext context) : base(info, context) diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadInterruptedException.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadInterruptedException.cs index 0d0288da24..01ff40d838 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadInterruptedException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadInterruptedException.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.Serialization; namespace System.Threading @@ -24,13 +25,13 @@ namespace System.Threading HResult = HResults.COR_E_THREADINTERRUPTED; } - public ThreadInterruptedException(string message) + public ThreadInterruptedException(string? message) : base(message) { HResult = HResults.COR_E_THREADINTERRUPTED; } - public ThreadInterruptedException(string message, Exception innerException) + public ThreadInterruptedException(string? message, Exception? innerException) : base(message, innerException) { HResult = HResults.COR_E_THREADINTERRUPTED; @@ -40,4 +41,4 @@ namespace System.Threading { } } -}
\ No newline at end of file +} diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadPriority.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadPriority.cs index 3b34bd5eac..9ef95a1042 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadPriority.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadPriority.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 namespace System.Threading { public enum ThreadPriority diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadStart.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadStart.cs index 5532539fc7..11310ac296 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadStart.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadStart.cs @@ -12,6 +12,7 @@ ** =============================================================================*/ +#nullable enable namespace System.Threading { public delegate void ThreadStart(); diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadStartException.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadStartException.cs index 5172555418..b22396bccc 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadStartException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadStartException.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.Serialization; namespace System.Threading diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadState.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadState.cs index 4bf3b5184d..9d0f374e22 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadState.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadState.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 namespace System.Threading { [Flags] diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadStateException.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadStateException.cs index d9ba48dfc8..9e0f26e9f5 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadStateException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadStateException.cs @@ -12,6 +12,7 @@ ** =============================================================================*/ +#nullable enable using System.Runtime.Serialization; namespace System.Threading @@ -26,13 +27,13 @@ namespace System.Threading HResult = HResults.COR_E_THREADSTATE; } - public ThreadStateException(string message) + public ThreadStateException(string? message) : base(message) { HResult = HResults.COR_E_THREADSTATE; } - public ThreadStateException(string message, Exception innerException) + public ThreadStateException(string? message, Exception? innerException) : base(message, innerException) { HResult = HResults.COR_E_THREADSTATE; diff --git a/src/System.Private.CoreLib/shared/System/Threading/Timeout.cs b/src/System.Private.CoreLib/shared/System/Threading/Timeout.cs index df1ea5f2bc..85151291f7 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Timeout.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Timeout.cs @@ -2,9 +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. -using System.Threading; -using System; - +#nullable enable namespace System.Threading { // A constant used by methods that take a timeout (Object.Wait, Thread.Sleep diff --git a/src/System.Private.CoreLib/shared/System/Threading/TimeoutHelper.cs b/src/System.Private.CoreLib/shared/System/Threading/TimeoutHelper.cs index dd3291281e..524fb2b52a 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/TimeoutHelper.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/TimeoutHelper.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.Diagnostics; namespace System.Threading diff --git a/src/System.Private.CoreLib/shared/System/Threading/Timer.cs b/src/System.Private.CoreLib/shared/System/Threading/Timer.cs index 180bcb5447..a21203070f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Timer.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Timer.cs @@ -2,13 +2,14 @@ // 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.Diagnostics; using System.Diagnostics.Tracing; using System.Threading.Tasks; namespace System.Threading { - public delegate void TimerCallback(object state); + public delegate void TimerCallback(object? state); // TimerQueue maintains a list of active timers. We use a single native timer to schedule all managed timers // in the process. @@ -109,8 +110,8 @@ namespace System.Threading // process the long list if the current time is greater than _currentAbsoluteThreshold (or // if the short list is now empty and we need to process the long list to know when to next // invoke FireNextTimers). - private TimerQueueTimer _shortTimers; - private TimerQueueTimer _longTimers; + private TimerQueueTimer? _shortTimers; + private TimerQueueTimer? _longTimers; // The current threshold, an absolute time where any timers scheduled to go off at or // before this time must be queued to the short list. @@ -135,7 +136,7 @@ namespace System.Threading { // We fire the first timer on this thread; any other timers that need to be fired // are queued to the ThreadPool. - TimerQueueTimer timerToFireOnThisThread = null; + TimerQueueTimer? timerToFireOnThisThread = null; lock (this) { @@ -151,7 +152,7 @@ namespace System.Threading // of sweeping the long timers, move anything that'll fire within the next threshold // to the short list. It's functionally ok if more timers end up in the short list // than is truly necessary (but not the opposite). - TimerQueueTimer timer = _shortTimers; + TimerQueueTimer? timer = _shortTimers; for (int listNum = 0; listNum < 2; listNum++) // short == 0, long == 1 { while (timer != null) @@ -160,7 +161,7 @@ namespace System.Threading // Save off the next timer to examine, in case our examination of this timer results // in our deleting or moving it; we'll continue after with this saved next timer. - TimerQueueTimer next = timer._next; + TimerQueueTimer? next = timer._next; uint elapsed = (uint)(nowTicks - timer._startTicks); int remaining = (int)timer._dueTime - (int)elapsed; @@ -334,7 +335,7 @@ namespace System.Threading private void LinkTimer(TimerQueueTimer timer) { // Use timer._short to decide to which list to add. - ref TimerQueueTimer listHead = ref timer._short ? ref _shortTimers : ref _longTimers; + ref TimerQueueTimer? listHead = ref timer._short ? ref _shortTimers : ref _longTimers; timer._next = listHead; if (timer._next != null) { @@ -346,7 +347,7 @@ namespace System.Threading private void UnlinkTimer(TimerQueueTimer timer) { - TimerQueueTimer t = timer._next; + TimerQueueTimer? t = timer._next; if (t != null) { t._prev = timer._prev; @@ -402,8 +403,8 @@ namespace System.Threading // The first six fields are maintained by TimerQueue. // Links to the next and prev timers in the list. - internal TimerQueueTimer _next; - internal TimerQueueTimer _prev; + internal TimerQueueTimer? _next; + internal TimerQueueTimer? _prev; // true if on the short list; otherwise, false. internal bool _short; @@ -419,8 +420,8 @@ namespace System.Threading // Info about the user's callback private readonly TimerCallback _timerCallback; - private readonly object _state; - private readonly ExecutionContext _executionContext; + private readonly object? _state; + private readonly ExecutionContext? _executionContext; // When Timer.Dispose(WaitHandle) is used, we need to signal the wait handle only // after all pending callbacks are complete. We set _canceled to prevent any callbacks that @@ -430,10 +431,10 @@ namespace System.Threading // instead of with a provided WaitHandle. private int _callbacksRunning; private volatile bool _canceled; - private volatile object _notifyWhenNoCallbacksRunning; // may be either WaitHandle or Task<bool> + private volatile object? _notifyWhenNoCallbacksRunning; // may be either WaitHandle or Task<bool> - internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, bool flowExecutionContext) + internal TimerQueueTimer(TimerCallback timerCallback, object? state, uint dueTime, uint period, bool flowExecutionContext) { _timerCallback = timerCallback; _state = state; @@ -529,7 +530,7 @@ namespace System.Threading { lock (_associatedTimerQueue) { - object notifyWhenNoCallbacksRunning = _notifyWhenNoCallbacksRunning; + object? notifyWhenNoCallbacksRunning = _notifyWhenNoCallbacksRunning; // Mark the timer as canceled if it's not already. if (_canceled) @@ -571,7 +572,7 @@ namespace System.Threading // there wasn't a previous CloseAsync call that did. if (notifyWhenNoCallbacksRunning == null) { - var t = new Task<bool>((object)null, TaskCreationOptions.RunContinuationsAsynchronously); + var t = new Task<bool>((object?)null, TaskCreationOptions.RunContinuationsAsynchronously); _notifyWhenNoCallbacksRunning = t; return new ValueTask(t); } @@ -613,12 +614,12 @@ namespace System.Threading internal void SignalNoCallbacksRunning() { - object toSignal = _notifyWhenNoCallbacksRunning; + object? toSignal = _notifyWhenNoCallbacksRunning; Debug.Assert(toSignal is WaitHandle || toSignal is Task<bool>); if (toSignal is WaitHandle wh) { - EventWaitHandle.Set(wh.SafeWaitHandle); + EventWaitHandle.Set(wh.SafeWaitHandle!); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/2384 } else { @@ -632,7 +633,7 @@ namespace System.Threading FrameworkEventSource.Log.ThreadTransferReceiveObj(this, 1, string.Empty); // Call directly if EC flow is suppressed - ExecutionContext context = _executionContext; + ExecutionContext? context = _executionContext; if (context == null) { _timerCallback(_state); @@ -652,7 +653,8 @@ namespace System.Threading private static readonly ContextCallback s_callCallbackInContext = state => { - TimerQueueTimer t = (TimerQueueTimer)state; + Debug.Assert(state is TimerQueueTimer); + var t = (TimerQueueTimer)state; t._timerCallback(t._state); }; } @@ -667,7 +669,7 @@ namespace System.Threading // unwittingly be changing the lifetime of those timers. internal sealed class TimerHolder { - internal TimerQueueTimer _timer; + internal readonly TimerQueueTimer _timer; public TimerHolder(TimerQueueTimer timer) { @@ -716,10 +718,10 @@ namespace System.Threading { private const uint MAX_SUPPORTED_TIMEOUT = (uint)0xfffffffe; - private TimerHolder _timer; + private TimerHolder _timer = null!; // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538 public Timer(TimerCallback callback, - object state, + object? state, int dueTime, int period) : this(callback, state, dueTime, period, flowExecutionContext: true) @@ -727,7 +729,7 @@ namespace System.Threading } internal Timer(TimerCallback callback, - object state, + object? state, int dueTime, int period, bool flowExecutionContext) @@ -741,7 +743,7 @@ namespace System.Threading } public Timer(TimerCallback callback, - object state, + object? state, TimeSpan dueTime, TimeSpan period) { @@ -762,7 +764,7 @@ namespace System.Threading [CLSCompliant(false)] public Timer(TimerCallback callback, - object state, + object? state, uint dueTime, uint period) { @@ -770,7 +772,7 @@ namespace System.Threading } public Timer(TimerCallback callback, - object state, + object? state, long dueTime, long period) { @@ -787,7 +789,7 @@ namespace System.Threading public Timer(TimerCallback callback) { - int dueTime = -1; // we want timer to be registered, but not activated. Requires caller to call + int dueTime = -1; // We want timer to be registered, but not activated. Requires caller to call int period = -1; // Change after a timer instance is created. This is to avoid the potential // for a timer to be fired before the returned value is assigned to the variable, // potentially causing the callback to reference a bogus value (if passing the timer to the callback). @@ -796,7 +798,7 @@ namespace System.Threading } private void TimerSetup(TimerCallback callback, - object state, + object? state, uint dueTime, uint period, bool flowExecutionContext = true) diff --git a/src/System.Private.CoreLib/shared/System/Threading/TimerQueue.Unix.cs b/src/System.Private.CoreLib/shared/System/Threading/TimerQueue.Unix.cs index 7c45637c49..d89a8f6a26 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/TimerQueue.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/TimerQueue.Unix.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 namespace System.Threading { internal partial class TimerQueue diff --git a/src/System.Private.CoreLib/shared/System/Threading/WaitHandle.cs b/src/System.Private.CoreLib/shared/System/Threading/WaitHandle.cs index 07462c1bc1..bf3c122906 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/WaitHandle.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/WaitHandle.cs @@ -329,8 +329,7 @@ namespace System.Threading { if (safeWaitHandles[i] != null) { - // TODO-NULLABLE: '!' below should not be required - safeWaitHandles[i]!.DangerousRelease(); + safeWaitHandles[i]!.DangerousRelease(); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34644 safeWaitHandles[i] = null; } } diff --git a/src/System.Private.CoreLib/shared/System/Threading/WaitHandleCannotBeOpenedException.cs b/src/System.Private.CoreLib/shared/System/Threading/WaitHandleCannotBeOpenedException.cs index 60821542b5..f54e013413 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/WaitHandleCannotBeOpenedException.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/WaitHandleCannotBeOpenedException.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.Serialization; namespace System.Threading @@ -15,12 +16,12 @@ namespace System.Threading HResult = HResults.COR_E_WAITHANDLECANNOTBEOPENED; } - public WaitHandleCannotBeOpenedException(string message) : base(message) + public WaitHandleCannotBeOpenedException(string? message) : base(message) { HResult = HResults.COR_E_WAITHANDLECANNOTBEOPENED; } - public WaitHandleCannotBeOpenedException(string message, Exception innerException) : base(message, innerException) + public WaitHandleCannotBeOpenedException(string? message, Exception? innerException) : base(message, innerException) { HResult = HResults.COR_E_WAITHANDLECANNOTBEOPENED; } diff --git a/src/System.Private.CoreLib/src/System/Threading/StackCrawlMark.cs b/src/System.Private.CoreLib/src/System/Threading/StackCrawlMark.cs index 7323d1dced..977cccb833 100644 --- a/src/System.Private.CoreLib/src/System/Threading/StackCrawlMark.cs +++ b/src/System.Private.CoreLib/src/System/Threading/StackCrawlMark.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 namespace System.Threading { /// <summary> diff --git a/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs index 4ab1cc98d4..a3bd3c6698 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.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.Diagnostics; using System.Globalization; using System.Runtime.CompilerServices; @@ -14,29 +15,31 @@ namespace System.Threading internal sealed class ThreadHelper { private Delegate _start; - internal CultureInfo _startCulture; - internal CultureInfo _startUICulture; - private object _startArg = null; - private ExecutionContext _executionContext = null; + internal CultureInfo? _startCulture; + internal CultureInfo? _startUICulture; + private object? _startArg = null; + private ExecutionContext? _executionContext = null; internal ThreadHelper(Delegate start) { _start = start; } - internal void SetExecutionContextHelper(ExecutionContext ec) + internal void SetExecutionContextHelper(ExecutionContext? ec) { _executionContext = ec; } internal static readonly ContextCallback s_threadStartContextCallback = new ContextCallback(ThreadStart_Context); - private static void ThreadStart_Context(object state) + private static void ThreadStart_Context(object? state) { + Debug.Assert(state is ThreadHelper); ThreadHelper t = (ThreadHelper)state; t.InitializeCulture(); + Debug.Assert(t._start is ThreadStart || t._start is ParameterizedThreadStart); if (t._start is ThreadStart threadStart) { threadStart(); @@ -63,11 +66,12 @@ namespace System.Threading } // call back helper - internal void ThreadStart(object obj) + internal void ThreadStart(object? obj) { + Debug.Assert(_start is ParameterizedThreadStart); _startArg = obj; - - ExecutionContext context = _executionContext; + + ExecutionContext? context = _executionContext; if (context != null) { ExecutionContext.RunInternal(context, s_threadStartContextCallback, this); @@ -82,7 +86,9 @@ namespace System.Threading // call back helper internal void ThreadStart() { - ExecutionContext context = _executionContext; + Debug.Assert(_start is ThreadStart); + + ExecutionContext? context = _executionContext; if (context != null) { ExecutionContext.RunInternal(context, s_threadStartContextCallback, this); @@ -112,13 +118,13 @@ namespace System.Threading ** ThreadBaseObject to maintain alignment between the two classes. ** DON'T CHANGE THESE UNLESS YOU MODIFY ThreadBaseObject in vm\object.h =========================================================================*/ - internal ExecutionContext _executionContext; // this call context follows the logical thread - internal SynchronizationContext _synchronizationContext; // maintained separately from ExecutionContext + internal ExecutionContext? _executionContext; // this call context follows the logical thread + internal SynchronizationContext? _synchronizationContext; // maintained separately from ExecutionContext - private string _name; - private Delegate _delegate; // Delegate + private string? _name; + private Delegate? _delegate; // Delegate - private object _threadStartArg; + private object? _threadStartArg; /*========================================================================= ** The base implementation of Thread is all native. The following fields @@ -179,7 +185,7 @@ namespace System.Threading /// method on the IThreadable interface passed in the constructor. Once the /// thread is dead, it cannot be restarted with another call to Start. /// </summary> - public void Start(object parameter) + public void Start(object? parameter) { // In the case of a null delegate (second call to start on same thread) // StartInternal method will take care of the error reporting. @@ -207,8 +213,10 @@ namespace System.Threading { // If we reach here with a null delegate, something is broken. But we'll let the StartInternal method take care of // reporting an error. Just make sure we don't try to dereference a null delegate. - ThreadHelper t = (ThreadHelper)_delegate.Target; - ExecutionContext ec = ExecutionContext.Capture(); + Debug.Assert(_delegate.Target is ThreadHelper); + var t = (ThreadHelper)_delegate.Target; + + ExecutionContext? ec = ExecutionContext.Capture(); t.SetExecutionContextHelper(ec); } @@ -218,8 +226,10 @@ namespace System.Threading private void SetCultureOnUnstartedThreadNoCheck(CultureInfo value, bool uiCulture) { Debug.Assert(_delegate != null); + Debug.Assert(_delegate.Target is ThreadHelper); + + var t = (ThreadHelper)(_delegate.Target); - ThreadHelper t = (ThreadHelper)(_delegate.Target); if (uiCulture) { t._startUICulture = value; @@ -274,14 +284,14 @@ namespace System.Threading { Debug.Assert(maxStackSize >= 0); - var threadStartCallBack = new ThreadHelper(start); + var helper = new ThreadHelper(start); if (start is ThreadStart) { - SetStart(new ThreadStart(threadStartCallBack.ThreadStart), maxStackSize); + SetStart(new ThreadStart(helper.ThreadStart), maxStackSize); } else { - SetStart(new ParameterizedThreadStart(threadStartCallBack.ThreadStart), maxStackSize); + SetStart(new ParameterizedThreadStart(helper.ThreadStart), maxStackSize); } } @@ -300,13 +310,13 @@ namespace System.Threading private extern void StartupSetApartmentStateInternal(); #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT - partial void ThreadNameChanged(string value) + partial void ThreadNameChanged(string? value) { InformThreadNameChange(GetNativeHandle(), value, value?.Length ?? 0); } [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] - private static extern void InformThreadNameChange(ThreadHandle t, string name, int len); + private static extern void InformThreadNameChange(ThreadHandle t, string? name, int len); [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern DeserializationTracker GetThreadDeserializationTracker(ref StackCrawlMark stackMark); diff --git a/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs index 3d31a3f134..6e8306b74a 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs @@ -2,7 +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. -using Microsoft.Win32; +#nullable enable using Microsoft.Win32.SafeHandles; using System.Diagnostics; using System.Runtime.CompilerServices; @@ -61,7 +61,7 @@ namespace System.Threading private readonly int _id; // TimerQueues[_id] == this - private AppDomainTimerSafeHandle m_appDomainTimer; + private AppDomainTimerSafeHandle? m_appDomainTimer; private TimerQueue(int id) { |