Browse Source

More #nullable for scheduler code.

Bart De Smet 5 years ago
parent
commit
5eeeb25761

+ 6 - 3
Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayer.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 using System.ComponentModel;
 using System.Reactive.PlatformServices;
 
@@ -22,7 +20,12 @@ namespace System.Reactive.Concurrency
         private static IConcurrencyAbstractionLayer Initialize()
         {
 #pragma warning disable CS0618 // Type or member is obsolete
-            return PlatformEnlightenmentProvider.Current.GetService<IConcurrencyAbstractionLayer>();
+            //
+            // NB: For compat reasons, we allow null to leak here. Bad things will happen but we don't want
+            //     to trigger an exception earlier than we did before. The only case where this can happen
+            //     is when a custom PEP is installed, which is very rare (e.g. debugger, service hosting).
+            //
+            return PlatformEnlightenmentProvider.Current.GetService<IConcurrencyAbstractionLayer>()!;
 #pragma warning restore CS0618 // Type or member is obsolete
         }
     }

+ 0 - 2
Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.Windows.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 #if NO_THREAD && WINDOWS
 using System.Threading;
 

+ 6 - 8
Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 #if !NO_THREAD
 using System.Reactive.Disposables;
 using System.Threading;
@@ -54,7 +52,7 @@ namespace System.Reactive.Concurrency
         {
             ThreadPool.QueueUserWorkItem(itemObject =>
             {
-                var item = (WorkItem)itemObject;
+                var item = (WorkItem)itemObject!;
 
                 item.Action(item.State);
             }, new WorkItem(action, state));
@@ -72,7 +70,7 @@ namespace System.Reactive.Concurrency
         {
             new Thread(itemObject =>
             {
-                var item = (WorkItem)itemObject;
+                var item = (WorkItem)itemObject!;
 
                 item.Action(item.State);
             })
@@ -165,7 +163,7 @@ namespace System.Reactive.Concurrency
                 _state = state;
                 _action = action;
 
-                Disposable.SetSingle(ref _timer, new System.Threading.Timer(_ => Tick(_), this, dueTime, TimeSpan.FromMilliseconds(Timeout.Infinite)));
+                Disposable.SetSingle(ref _timer, new System.Threading.Timer(@this => Tick(@this!), this, dueTime, TimeSpan.FromMilliseconds(Timeout.Infinite)));
             }
 
             private static void Tick(object state)
@@ -199,7 +197,7 @@ namespace System.Reactive.Concurrency
         private sealed class PeriodicTimer : IDisposable
         {
             private Action _action;
-            private volatile System.Threading.Timer _timer;
+            private volatile System.Threading.Timer? _timer;
 
             public PeriodicTimer(Action action, TimeSpan period)
             {
@@ -209,7 +207,7 @@ namespace System.Reactive.Concurrency
                 // Rooting of the timer happens through the timer's state
                 // which is the current instance and has a field to store the Timer instance.
                 //
-                _timer = new System.Threading.Timer(_ => Tick(_), this, period, period);
+                _timer = new System.Threading.Timer(@this => Tick(@this!), this, period, period);
             }
 
             private static void Tick(object state)
@@ -241,7 +239,7 @@ namespace System.Reactive.Concurrency
             {
                 _action = action;
 
-                new Thread(_ => Loop(_))
+                new Thread(@this => Loop(@this!))
                 {
                     Name = "Rx-FastPeriodicTimer",
                     IsBackground = true

+ 0 - 2
Rx.NET/Source/src/System.Reactive/Concurrency/Synchronization.ObserveOn.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 using System.Threading;
 
 namespace System.Reactive.Concurrency

+ 1 - 3
Rx.NET/Source/src/System.Reactive/Concurrency/Synchronization.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 using System.ComponentModel;
 using System.Reactive.Disposables;
 using System.Threading;
@@ -119,7 +117,7 @@ namespace System.Reactive.Concurrency
                 private readonly IObservable<TSource> _source;
                 private readonly IObserver<TSource> _observer;
                 private readonly SynchronizationContext _context;
-                private IDisposable _cancel;
+                private IDisposable? _cancel;
 
                 public Subscription(IObservable<TSource> source, SynchronizationContext context, IObserver<TSource> observer)
                 {

+ 0 - 2
Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 #if LEGACY_WINRT
 using System.ComponentModel;
 using Windows.System.Threading;

+ 6 - 8
Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 #if !LEGACY_WINRT && !NO_THREAD
 using System.Reactive.Disposables;
 using System.Threading;
@@ -46,7 +44,7 @@ namespace System.Reactive.Concurrency
             var workItem = new UserWorkItem<TState>(this, state, action);
 
             ThreadPool.QueueUserWorkItem(
-                static closureWorkItem => ((UserWorkItem<TState>)closureWorkItem).Run(),
+                static closureWorkItem => ((UserWorkItem<TState>)closureWorkItem!).Run(),
                 workItem);
 
             return workItem;
@@ -77,7 +75,7 @@ namespace System.Reactive.Concurrency
             var workItem = new UserWorkItem<TState>(this, state, action);
 
             workItem.CancelQueueDisposable = new Timer(
-                static closureWorkItem => ((UserWorkItem<TState>)closureWorkItem).Run(),
+                static closureWorkItem => ((UserWorkItem<TState>)closureWorkItem!).Run(),
                 workItem,
                 dt,
                 Timeout.InfiniteTimeSpan);
@@ -158,7 +156,7 @@ namespace System.Reactive.Concurrency
                 _state = state;
                 _action = action;
 
-                ThreadPool.QueueUserWorkItem(static @this => Tick(@this), this);   // Replace with method group as soon as Roslyn will cache the delegate then.
+                ThreadPool.QueueUserWorkItem(static @this => Tick(@this!), this);   // Replace with method group as soon as Roslyn will cache the delegate then.
             }
 
             private static void Tick(object state)
@@ -168,7 +166,7 @@ namespace System.Reactive.Concurrency
                 if (!timer._disposed)
                 {
                     timer._state = timer._action(timer._state);
-                    ThreadPool.QueueUserWorkItem(static t => Tick(t), timer);
+                    ThreadPool.QueueUserWorkItem(static t => Tick(t!), timer);
                 }
             }
 
@@ -185,7 +183,7 @@ namespace System.Reactive.Concurrency
             private Func<TState, TState> _action;
 
             private readonly AsyncLock _gate;
-            private volatile Timer _timer;
+            private volatile Timer? _timer;
 
             public PeriodicTimer(TState state, TimeSpan period, Func<TState, TState> action)
             {
@@ -198,7 +196,7 @@ namespace System.Reactive.Concurrency
                 // Rooting of the timer happens through the this.Tick delegate's target object,
                 // which is the current instance and has a field to store the Timer instance.
                 //
-                _timer = new Timer(static @this => ((PeriodicTimer<TState>)@this).Tick(), this, period, period);
+                _timer = new Timer(static @this => ((PeriodicTimer<TState>)@this!).Tick(), this, period, period);
             }
 
             private void Tick()

+ 0 - 2
Rx.NET/Source/src/System.Reactive/Concurrency/VirtualTimeScheduler.Extensions.cs

@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#nullable disable
-
 using System.Reactive.Disposables;
 
 namespace System.Reactive.Concurrency