Forráskód Böngészése

Clean up some code.

Bart De Smet 5 éve
szülő
commit
85d87f4096

+ 18 - 31
Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs

@@ -50,7 +50,7 @@ namespace System.Reactive.Concurrency
 
         public IDisposable QueueUserWorkItem(Action<object?> action, object? state)
         {
-            ThreadPool.QueueUserWorkItem(itemObject =>
+            ThreadPool.QueueUserWorkItem(static itemObject =>
             {
                 var item = (WorkItem)itemObject!;
 
@@ -68,7 +68,7 @@ namespace System.Reactive.Concurrency
 
         public void StartThread(Action<object?> action, object? state)
         {
-            new Thread(itemObject =>
+            new Thread(static itemObject =>
             {
                 var item = (WorkItem)itemObject!;
 
@@ -163,34 +163,31 @@ namespace System.Reactive.Concurrency
                 _state = state;
                 _action = action;
 
-                Disposable.SetSingle(ref _timer, new System.Threading.Timer(static @this => Tick(@this!), this, dueTime, TimeSpan.FromMilliseconds(Timeout.Infinite)));
+                Disposable.SetSingle(ref _timer, new System.Threading.Timer(static @this => ((Timer)@this!).Tick(), this, dueTime, TimeSpan.FromMilliseconds(Timeout.Infinite)));
             }
 
-            private static void Tick(object state)
+            private void Tick()
             {
-                var timer = (Timer)state;
-
                 try
                 {
-                    var timerState = timer._state;
+                    var timerState = _state;
                     if (timerState != DisposedState)
                     {
-                        timer._action(timerState);
+                        _action(timerState);
                     }
                 }
                 finally
                 {
-                    Disposable.Dispose(ref timer._timer);
+                    Disposable.Dispose(ref _timer);
                 }
             }
 
             public void Dispose()
             {
-                if (Disposable.TryDispose(ref _timer))
-                {
-                    _action = Stubs<object?>.Ignore;
-                    _state = DisposedState;
-                }
+                Disposable.Dispose(ref _timer);
+
+                _action = Stubs<object?>.Ignore;
+                _state = DisposedState;
             }
         }
 
@@ -207,15 +204,10 @@ 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(static @this => Tick(@this!), this, period, period);
+                _timer = new System.Threading.Timer(static @this => ((PeriodicTimer)@this!).Tick(), this, period, period);
             }
 
-            private static void Tick(object state)
-            {
-                var timer = (PeriodicTimer)state;
-
-                timer._action();
-            }
+            private void Tick() => _action();
 
             public void Dispose()
             {
@@ -239,7 +231,7 @@ namespace System.Reactive.Concurrency
             {
                 _action = action;
 
-                new Thread(static @this => Loop(@this!))
+                new Thread(static @this => ((FastPeriodicTimer)@this!).Loop())
                 {
                     Name = "Rx-FastPeriodicTimer",
                     IsBackground = true
@@ -247,20 +239,15 @@ namespace System.Reactive.Concurrency
                 .Start(this);
             }
 
-            private static void Loop(object threadParam)
+            private void Loop()
             {
-                var timer = (FastPeriodicTimer)threadParam;
-
-                while (!timer._disposed)
+                while (!_disposed)
                 {
-                    timer._action();
+                    _action();
                 }
             }
 
-            public void Dispose()
-            {
-                _disposed = true;
-            }
+            public void Dispose() => _disposed = true;
         }
     }
 }

+ 9 - 9
Rx.NET/Source/src/System.Reactive/Disposables/ContextDisposable.cs

@@ -12,7 +12,7 @@ namespace System.Reactive.Disposables
     /// </summary>
     public sealed class ContextDisposable : ICancelable
     {
-        private IDisposable _disposable;
+        private volatile IDisposable _disposable;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="ContextDisposable"/> class that uses the specified <see cref="SynchronizationContext"/> on which to dispose the specified disposable resource.
@@ -22,13 +22,8 @@ namespace System.Reactive.Disposables
         /// <exception cref="ArgumentNullException"><paramref name="context"/> or <paramref name="disposable"/> is null.</exception>
         public ContextDisposable(SynchronizationContext context, IDisposable disposable)
         {
-            if (disposable == null)
-            {
-                throw new ArgumentNullException(nameof(disposable));
-            }
-
             Context = context ?? throw new ArgumentNullException(nameof(context));
-            Disposable.SetSingle(ref _disposable, disposable);
+            _disposable = disposable ?? throw new ArgumentNullException(nameof(disposable));
         }
 
         /// <summary>
@@ -39,14 +34,19 @@ namespace System.Reactive.Disposables
         /// <summary>
         /// Gets a value that indicates whether the object is disposed.
         /// </summary>
-        public bool IsDisposed => Disposable.GetIsDisposed(ref _disposable);
+        public bool IsDisposed => _disposable == BooleanDisposable.True;
 
         /// <summary>
         /// Disposes the underlying disposable on the provided <see cref="SynchronizationContext"/>.
         /// </summary>
         public void Dispose()
         {
-            Disposable.TryRelease(ref _disposable, Context, static (disposable, context) => context.PostWithStartComplete(static d => d.Dispose(), disposable));
+            var old = Interlocked.Exchange(ref _disposable, BooleanDisposable.True);
+
+            if (old != BooleanDisposable.True)
+            {
+                Context.PostWithStartComplete(static d => d.Dispose(), old);
+            }
         }
     }
 }

+ 0 - 31
Rx.NET/Source/src/System.Reactive/Disposables/Disposable.Utils.cs

@@ -158,24 +158,6 @@ namespace System.Reactive.Disposables
             return Volatile.Read(ref fieldRef) == BooleanDisposable.True;
         }
 
-        /// <summary>
-        /// Tries to dispose <paramref name="fieldRef" />. 
-        /// </summary>
-        /// <returns>true if <paramref name="fieldRef" /> was not disposed previously and was successfully disposed.</returns>
-        /// <returns>false if <paramref name="fieldRef" /> was disposed previously.</returns>
-        internal static bool TryDispose(ref IDisposable fieldRef)
-        {
-            var old = Interlocked.Exchange(ref fieldRef, BooleanDisposable.True);
-
-            if (old == BooleanDisposable.True)
-            {
-                return false;
-            }
-
-            old?.Dispose();
-            return true;
-        }
-
         /// <summary>
         /// Disposes <paramref name="fieldRef" />. 
         /// </summary>
@@ -188,18 +170,5 @@ namespace System.Reactive.Disposables
                 old?.Dispose();
             }
         }
-
-        internal static bool TryRelease<TState>(ref IDisposable fieldRef, TState state, Action<IDisposable, TState> disposeAction)
-        {
-            var old = Interlocked.Exchange(ref fieldRef, BooleanDisposable.True);
-
-            if (old == BooleanDisposable.True)
-            {
-                return false;
-            }
-
-            disposeAction(old, state);
-            return true;
-        }
     }
 }

+ 0 - 14
Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs

@@ -887,19 +887,5 @@ namespace ReactiveTests.Tests
             Assert.True(disp3);
             Assert.True(d.IsDisposed);
         }
-
-        [Fact]
-        public void Disposable_TryRelease_Already_Disposed()
-        {
-            var field = default(IDisposable);
-
-            Disposable.Dispose(ref field);
-
-            var count = 0;
-
-            Assert.False(Disposable.TryRelease(ref field, 1, (d, i) => count = i));
-
-            Assert.Equal(0, count);
-        }
     }
 }