Browse Source

Optimize SafeHandleExtensions.Lease()

Implements changes suggested in https://github.com/dotnet/roslyn/pull/43462.
pull/577/head
Sam Harwell 5 years ago
parent
commit
9f95e97553
1 changed files with 9 additions and 4 deletions
  1. +9
    -4
      src/TensorFlowNET.Core/Util/SafeHandleExtensions.cs

+ 9
- 4
src/TensorFlowNET.Core/Util/SafeHandleExtensions.cs View File

@@ -15,6 +15,7 @@
******************************************************************************/

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Tensorflow.Util
@@ -22,9 +23,14 @@ namespace Tensorflow.Util
internal static class SafeHandleExtensions
{
/// <summary>
/// Acquires a lease on a safe handle. This method is intended to be used in the initializer of a <c>using</c>
/// statement.
/// Acquires a lease on a safe handle. The lease increments the reference count of the <see cref="SafeHandle"/>
/// to ensure the handle is not released prior to the lease being released.
/// </summary>
/// <remarks>
/// This method is intended to be used in the initializer of a <c>using</c> statement. Failing to release the
/// lease will permanently prevent the underlying <see cref="SafeHandle"/> from being released by the garbage
/// collector.
/// </remarks>
/// <param name="handle">The <see cref="SafeHandle"/> to lease.</param>
/// <returns>A <see cref="SafeHandleLease"/>, which must be disposed to release the resource.</returns>
/// <exception cref="ObjectDisposedException">If the lease could not be acquired.</exception>
@@ -37,8 +43,7 @@ namespace Tensorflow.Util
try
{
handle.DangerousAddRef(ref success);
if (!success)
throw new ObjectDisposedException(handle.GetType().FullName);
Debug.Assert(success, $"'{nameof(SafeHandle.DangerousAddRef)}' does not return when '{nameof(success)}' is false.");

return new SafeHandleLease(handle);
}


Loading…
Cancel
Save