From 95bba4b8aaa6f251b759377bac4a766884ade749 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 17 Apr 2019 17:38:37 -0400 Subject: [PATCH] Nullable: System.Threading.dll Commit migrated from https://github.com/dotnet/corefx/commit/3353a2c6020b1ee8f026c08a04325ec3858cfa48 --- .../System.Threading/src/System.Threading.csproj | 1 + .../src/System/Threading/Barrier.cs | 26 ++++---- .../src/System/Threading/HostExecutionContext.cs | 4 +- .../Threading/HostExecutionContextManager.cs | 6 +- .../src/System/Threading/LockCookie.cs | 2 +- .../src/System/Threading/ReaderWriterLock.cs | 70 +++++++++++----------- 6 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/libraries/System.Threading/src/System.Threading.csproj b/src/libraries/System.Threading/src/System.Threading.csproj index 68797bd..5bda8c8 100644 --- a/src/libraries/System.Threading/src/System.Threading.csproj +++ b/src/libraries/System.Threading/src/System.Threading.csproj @@ -5,6 +5,7 @@ true true netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;uapaot-Windows_NT-Debug;uapaot-Windows_NT-Release + enable diff --git a/src/libraries/System.Threading/src/System/Threading/Barrier.cs b/src/libraries/System.Threading/src/System/Threading/Barrier.cs index 69b2a5f..646846d 100644 --- a/src/libraries/System.Threading/src/System/Threading/Barrier.cs +++ b/src/libraries/System.Threading/src/System/Threading/Barrier.cs @@ -28,7 +28,7 @@ namespace System.Threading /// Initializes a new instance of the class. /// public BarrierPostPhaseException() - : this((string)null) + : this((string?)null) { } @@ -36,7 +36,7 @@ namespace System.Threading /// Initializes a new instance of the class with the specified inner exception. /// /// The exception that is the cause of the current exception. - public BarrierPostPhaseException(Exception innerException) + public BarrierPostPhaseException(Exception? innerException) : this(null, innerException) { } @@ -45,7 +45,7 @@ namespace System.Threading /// Initializes a new instance of the class with a specified error message. /// /// A string that describes the exception. - public BarrierPostPhaseException(string message) + public BarrierPostPhaseException(string? message) : this(message, null) { } @@ -55,7 +55,7 @@ namespace System.Threading /// /// A string that describes the exception. /// The exception that is the cause of the current exception. - public BarrierPostPhaseException(string message, Exception innerException) + public BarrierPostPhaseException(string? message, Exception? innerException) : base(message == null ? SR.BarrierPostPhaseException : message, innerException) { } @@ -130,16 +130,16 @@ namespace System.Threading private ManualResetEventSlim _evenEvent; // The execution context of the creator thread - private ExecutionContext _ownerThreadContext; + private ExecutionContext? _ownerThreadContext; // The EC callback that invokes the post phase action - private static ContextCallback s_invokePostPhaseAction; + private static ContextCallback? s_invokePostPhaseAction; // Post phase action after each phase - private Action _postPhaseAction; + private Action? _postPhaseAction; // In case the post phase action throws an exception, wraps it in BarrierPostPhaseException - private Exception _exception; + private Exception? _exception; // This is the ManagedThreadID of the postPhaseAction caller thread, this is used to determine if the SignalAndWait, Dispose or Add/RemoveParticipant caller thread is // the same thread as the postPhaseAction thread which means this method was called from the postPhaseAction which is illegal. @@ -213,7 +213,7 @@ namespace System.Threading /// will not be released to the next phase until the postPhaseAction delegate /// has completed execution. /// - public Barrier(int participantCount, Action postPhaseAction) + public Barrier(int participantCount, Action? postPhaseAction) { // the count must be non negative value if (participantCount < 0 || participantCount > MAX_PARTICIPANTS) @@ -766,7 +766,7 @@ namespace System.Threading { var currentContext = _ownerThreadContext; - ContextCallback handler = s_invokePostPhaseAction; + ContextCallback? handler = s_invokePostPhaseAction; if (handler == null) { s_invokePostPhaseAction = handler = InvokePostPhaseAction; @@ -802,10 +802,10 @@ namespace System.Threading /// Helper method to call the post phase action /// /// - private static void InvokePostPhaseAction(object obj) + private static void InvokePostPhaseAction(object? obj) { - var thisBarrier = (Barrier)obj; - thisBarrier._postPhaseAction(thisBarrier); + var thisBarrier = (Barrier)obj!; + thisBarrier._postPhaseAction!(thisBarrier); } /// diff --git a/src/libraries/System.Threading/src/System/Threading/HostExecutionContext.cs b/src/libraries/System.Threading/src/System/Threading/HostExecutionContext.cs index f711a7d..d18cfeb 100644 --- a/src/libraries/System.Threading/src/System/Threading/HostExecutionContext.cs +++ b/src/libraries/System.Threading/src/System/Threading/HostExecutionContext.cs @@ -10,12 +10,12 @@ namespace System.Threading { } - public HostExecutionContext(object state) + public HostExecutionContext(object? state) { State = state; } - protected internal object State + protected internal object? State { get; set; diff --git a/src/libraries/System.Threading/src/System/Threading/HostExecutionContextManager.cs b/src/libraries/System.Threading/src/System/Threading/HostExecutionContextManager.cs index 60a58fd..fa92751 100644 --- a/src/libraries/System.Threading/src/System/Threading/HostExecutionContextManager.cs +++ b/src/libraries/System.Threading/src/System/Threading/HostExecutionContextManager.cs @@ -12,9 +12,9 @@ namespace System.Threading /// separating itself from the to minimize unnecessary additions there. /// [ThreadStatic] - private static HostExecutionContext t_currentContext; + private static HostExecutionContext? t_currentContext; - public virtual HostExecutionContext Capture() + public virtual HostExecutionContext? Capture() { // Not hosted, so always capture null return null; @@ -55,7 +55,7 @@ namespace System.Threading private sealed class HostExecutionContextSwitcher { public readonly HostExecutionContext _currentContext; - public AsyncLocal _asyncLocal; + public AsyncLocal? _asyncLocal; public HostExecutionContextSwitcher(HostExecutionContext currentContext) { diff --git a/src/libraries/System.Threading/src/System/Threading/LockCookie.cs b/src/libraries/System.Threading/src/System/Threading/LockCookie.cs index d3d0ffd..e748f1b 100644 --- a/src/libraries/System.Threading/src/System/Threading/LockCookie.cs +++ b/src/libraries/System.Threading/src/System/Threading/LockCookie.cs @@ -20,7 +20,7 @@ namespace System.Threading return (int)_flags + _readerLevel + _writerLevel + _threadID; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is LockCookie && Equals((LockCookie)obj); } diff --git a/src/libraries/System.Threading/src/System/Threading/ReaderWriterLock.cs b/src/libraries/System.Threading/src/System/Threading/ReaderWriterLock.cs index 3973460..82df1c4 100644 --- a/src/libraries/System.Threading/src/System/Threading/ReaderWriterLock.cs +++ b/src/libraries/System.Threading/src/System/Threading/ReaderWriterLock.cs @@ -36,8 +36,8 @@ namespace System.Threading private static long s_mostRecentLockID; - private ManualResetEventSlim _readerEvent; - private AutoResetEvent _writerEvent; + private ManualResetEventSlim? _readerEvent; + private AutoResetEvent? _writerEvent; private long _lockID; private volatile int _state; private int _writerID = InvalidThreadID; @@ -53,7 +53,7 @@ namespace System.Threading { get { - ThreadLocalLockEntry threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); + ThreadLocalLockEntry? threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); if (threadLocalLockEntry != null) { return threadLocalLockEntry._readerLevel > 0; @@ -192,7 +192,7 @@ namespace System.Threading } int modifyState = -LockStates.WaitingReader; - ManualResetEventSlim readerEvent = null; + ManualResetEventSlim? readerEvent = null; bool waitSucceeded = false; try { @@ -262,7 +262,7 @@ namespace System.Threading if ((knownState & LockStates.WaitingReadersMask) == LockStates.WaitingReader) { // Reset the event and the reader signaled flag - readerEvent.Reset(); + readerEvent!.Reset(); Interlocked.Add(ref _state, -LockStates.ReaderSignaled); } @@ -364,7 +364,7 @@ namespace System.Threading } int modifyState = -LockStates.WaitingWriter; - AutoResetEvent writerEvent = null; + AutoResetEvent? writerEvent = null; bool waitSucceeded = false; try { @@ -453,7 +453,7 @@ namespace System.Threading return; } - ThreadLocalLockEntry threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); + ThreadLocalLockEntry? threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); if (threadLocalLockEntry == null) { throw GetNotOwnerException(); @@ -472,8 +472,8 @@ namespace System.Threading // Not a reader any more bool isLastReader; bool cacheEvents; - AutoResetEvent writerEvent = null; - ManualResetEventSlim readerEvent = null; + AutoResetEvent? writerEvent = null; + ManualResetEventSlim? readerEvent = null; int currentState = _state; int knownState; do @@ -585,8 +585,8 @@ namespace System.Threading // Not a writer any more _writerID = InvalidThreadID; bool cacheEvents; - ManualResetEventSlim readerEvent = null; - AutoResetEvent writerEvent = null; + ManualResetEventSlim? readerEvent = null; + AutoResetEvent? writerEvent = null; int currentState = _state; int knownState; do @@ -689,7 +689,7 @@ namespace System.Threading return lockCookie; } - ThreadLocalLockEntry threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); + ThreadLocalLockEntry? threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); if (threadLocalLockEntry == null) { lockCookie._flags = LockCookieFlags.Upgrade | LockCookieFlags.OwnedNone; @@ -779,7 +779,7 @@ namespace System.Threading // Downgrade to a reader _writerID = InvalidThreadID; _writerLevel = 0; - ManualResetEventSlim readerEvent = null; + ManualResetEventSlim? readerEvent = null; int currentState = _state; int knownState; do @@ -892,7 +892,7 @@ namespace System.Threading return lockCookie; } - ThreadLocalLockEntry threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); + ThreadLocalLockEntry? threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); if (threadLocalLockEntry == null) { lockCookie._flags = LockCookieFlags.Release | LockCookieFlags.OwnedNone; @@ -994,7 +994,7 @@ namespace System.Threading else if ((flags & LockCookieFlags.OwnedReader) != 0) { AcquireReaderLock(Timeout.Infinite); - ThreadLocalLockEntry threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); + ThreadLocalLockEntry? threadLocalLockEntry = ThreadLocalLockEntry.GetCurrent(_lockID); Debug.Assert(threadLocalLockEntry != null); threadLocalLockEntry._readerLevel = lockCookie._readerLevel; } @@ -1019,14 +1019,14 @@ namespace System.Threading /// Failed to allocate the event object private ManualResetEventSlim GetOrCreateReaderEvent() { - ManualResetEventSlim currentEvent = _readerEvent; + ManualResetEventSlim? currentEvent = _readerEvent; if (currentEvent != null) { return currentEvent; } currentEvent = new ManualResetEventSlim(false, 0); - ManualResetEventSlim previousEvent = Interlocked.CompareExchange(ref _readerEvent, currentEvent, null); + ManualResetEventSlim? previousEvent = Interlocked.CompareExchange(ref _readerEvent, currentEvent, null); if (previousEvent == null) { return currentEvent; @@ -1040,14 +1040,14 @@ namespace System.Threading /// Failed to create the system event due to some system error private AutoResetEvent GetOrCreateWriterEvent() { - AutoResetEvent currentEvent = _writerEvent; + AutoResetEvent? currentEvent = _writerEvent; if (currentEvent != null) { return currentEvent; } currentEvent = new AutoResetEvent(false); - AutoResetEvent previousEvent = Interlocked.CompareExchange(ref _writerEvent, currentEvent, null); + AutoResetEvent? previousEvent = Interlocked.CompareExchange(ref _writerEvent, currentEvent, null); if (previousEvent == null) { return currentEvent; @@ -1057,7 +1057,7 @@ namespace System.Threading return previousEvent; } - private ManualResetEventSlim TryGetOrCreateReaderEvent() + private ManualResetEventSlim? TryGetOrCreateReaderEvent() { // The intention is to catch all exceptions, so that the caller can try again. Typically, only OutOfMemoryException // would be thrown, but the idea is that any exception that may be thrown will propagate to the user on a different @@ -1072,7 +1072,7 @@ namespace System.Threading } } - private AutoResetEvent TryGetOrCreateWriterEvent() + private AutoResetEvent? TryGetOrCreateWriterEvent() { // The intention is to catch all exceptions, so that the caller can try again. Typically, only OutOfMemoryException // or any SystemException would be thrown. For instance, the EventWaitHandle constructor may throw IOException if @@ -1093,9 +1093,9 @@ namespace System.Threading Debug.Assert((_state & LockStates.CachingEvents) == LockStates.CachingEvents); // Save events - AutoResetEvent writerEvent = _writerEvent; + AutoResetEvent? writerEvent = _writerEvent; _writerEvent = null; - ManualResetEventSlim readerEvent = _readerEvent; + ManualResetEventSlim? readerEvent = _readerEvent; _readerEvent = null; // Allow readers and writers to continue @@ -1206,10 +1206,10 @@ namespace System.Threading private sealed class ThreadLocalLockEntry { [ThreadStatic] - private static ThreadLocalLockEntry t_lockEntryHead; + private static ThreadLocalLockEntry? t_lockEntryHead; private long _lockID; - private ThreadLocalLockEntry _next; + private ThreadLocalLockEntry? _next; public ushort _readerLevel; private ThreadLocalLockEntry(long lockID) @@ -1226,7 +1226,7 @@ namespace System.Threading Debug.Assert(lockID != 0); Debug.Assert(afterEntry != null); - for (ThreadLocalLockEntry currentEntry = afterEntry._next; + for (ThreadLocalLockEntry? currentEntry = afterEntry._next; currentEntry != null; currentEntry = currentEntry._next) { @@ -1234,12 +1234,12 @@ namespace System.Threading } } - public static ThreadLocalLockEntry GetCurrent(long lockID) + public static ThreadLocalLockEntry? GetCurrent(long lockID) { Debug.Assert(lockID != 0); - ThreadLocalLockEntry headEntry = t_lockEntryHead; - for (ThreadLocalLockEntry currentEntry = headEntry; currentEntry != null; currentEntry = currentEntry._next) + ThreadLocalLockEntry? headEntry = t_lockEntryHead; + for (ThreadLocalLockEntry? currentEntry = headEntry; currentEntry != null; currentEntry = currentEntry._next) { if (currentEntry._lockID == lockID) { @@ -1257,7 +1257,7 @@ namespace System.Threading { Debug.Assert(lockID != 0); - ThreadLocalLockEntry headEntry = t_lockEntryHead; + ThreadLocalLockEntry? headEntry = t_lockEntryHead; if (headEntry != null) { if (headEntry._lockID == lockID) @@ -1277,15 +1277,15 @@ namespace System.Threading return GetOrCreateCurrentSlow(lockID, headEntry); } - private static ThreadLocalLockEntry GetOrCreateCurrentSlow(long lockID, ThreadLocalLockEntry headEntry) + private static ThreadLocalLockEntry GetOrCreateCurrentSlow(long lockID, ThreadLocalLockEntry? headEntry) { Debug.Assert(lockID != 0); Debug.Assert(headEntry == t_lockEntryHead); Debug.Assert(headEntry == null || headEntry._lockID != lockID); - ThreadLocalLockEntry entry = null; - ThreadLocalLockEntry emptyEntryPrevious = null; - ThreadLocalLockEntry emptyEntry = null; + ThreadLocalLockEntry? entry = null; + ThreadLocalLockEntry? emptyEntryPrevious = null; + ThreadLocalLockEntry? emptyEntry = null; if (headEntry != null) { @@ -1294,7 +1294,7 @@ namespace System.Threading emptyEntry = headEntry; } - for (ThreadLocalLockEntry previousEntry = headEntry, currentEntry = headEntry._next; + for (ThreadLocalLockEntry? previousEntry = headEntry, currentEntry = headEntry._next; currentEntry != null; previousEntry = currentEntry, currentEntry = currentEntry._next) { -- 2.7.4