未验证 提交 1ae0fe92 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #40669 from sharwell/nullable-rcd

Enable Nullable Reference Types for ReferenceCountedDisposable<T>
......@@ -96,7 +96,7 @@ public Task<Stream> RequestServiceAsync(string serviceName)
// this is what consumer actually use to communicate information
var serviceStream = await _inprocServices.RequestServiceAsync(serviceName).ConfigureAwait(false);
return new JsonRpcConnection(_inprocServices.Logger, callbackTarget, serviceStream, _remotableDataRpc.TryAddReference());
return new JsonRpcConnection(_inprocServices.Logger, callbackTarget, serviceStream, _remotableDataRpc.TryAddReference() ?? throw new ObjectDisposedException(GetType().FullName));
}
protected override void OnStarted()
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Runtime.CompilerServices;
namespace Roslyn.Utilities
{
......@@ -38,6 +39,6 @@ internal interface IReferenceCountedDisposable<out T> : IDisposable
/// <returns>A new <see cref="ReferenceCountedDisposable{T}"/> pointing to the same underlying object, if it
/// has not yet been disposed; otherwise, <see langword="null"/> if this reference to the underlying object
/// has already been disposed.</returns>
IReferenceCountedDisposable<T> TryAddReference();
IReferenceCountedDisposable<T>? TryAddReference();
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Runtime.CompilerServices;
......@@ -60,7 +62,7 @@ internal sealed class ReferenceCountedDisposable<T> : IReferenceCountedDisposabl
/// <para>This value is only cleared in order to support cases where one or more references is garbage
/// collected without having <see cref="Dispose"/> called.</para>
/// </remarks>
private T _instance;
private T? _instance;
/// <summary>
/// The boxed reference count, which is shared by all references with the same <see cref="Target"/> object.
......@@ -123,12 +125,12 @@ private ReferenceCountedDisposable(T instance, StrongBox<int> referenceCount)
/// <returns>A new <see cref="ReferenceCountedDisposable{T}"/> pointing to the same underlying object, if it
/// has not yet been disposed; otherwise, <see langword="null"/> if this reference to the underlying object
/// has already been disposed.</returns>
public ReferenceCountedDisposable<T> TryAddReference()
public ReferenceCountedDisposable<T>? TryAddReference()
{
return TryAddReferenceImpl(_instance, _boxedReferenceCount);
}
IReferenceCountedDisposable<T> IReferenceCountedDisposable<T>.TryAddReference()
IReferenceCountedDisposable<T>? IReferenceCountedDisposable<T>.TryAddReference()
{
return TryAddReference();
}
......@@ -137,7 +139,7 @@ IReferenceCountedDisposable<T> IReferenceCountedDisposable<T>.TryAddReference()
/// Provides the implementation for <see cref="TryAddReference"/> and
/// <see cref="WeakReference.TryAddReference"/>.
/// </summary>
private static ReferenceCountedDisposable<T> TryAddReferenceImpl(T target, StrongBox<int> referenceCount)
private static ReferenceCountedDisposable<T>? TryAddReferenceImpl(T? target, StrongBox<int> referenceCount)
{
lock (referenceCount)
{
......@@ -176,7 +178,7 @@ private static ReferenceCountedDisposable<T> TryAddReferenceImpl(T target, Stron
/// </remarks>
public void Dispose()
{
T instanceToDispose = null;
T? instanceToDispose = null;
lock (_boxedReferenceCount)
{
if (_instance == null)
......@@ -207,8 +209,8 @@ public struct WeakReference
/// <summary>
/// DO NOT DISPOSE OF THE TARGET.
/// </summary>
private readonly WeakReference<T> _weakInstance;
private readonly StrongBox<int> _boxedReferenceCount;
private readonly WeakReference<T>? _weakInstance;
private readonly StrongBox<int>? _boxedReferenceCount;
public WeakReference(ReferenceCountedDisposable<T> reference)
: this()
......@@ -247,10 +249,10 @@ public WeakReference(ReferenceCountedDisposable<T> reference)
/// <returns>A new <see cref="ReferenceCountedDisposable{T}"/> pointing to the same underlying object,
/// if it has not yet been disposed; otherwise, <see langword="null"/> if the underlying object has
/// already been disposed.</returns>
public ReferenceCountedDisposable<T> TryAddReference()
public ReferenceCountedDisposable<T>? TryAddReference()
{
var weakInstance = _weakInstance;
if (weakInstance == null || !_weakInstance.TryGetTarget(out var target))
if (weakInstance == null || !weakInstance.TryGetTarget(out var target))
{
return null;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册