| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.ConstrainedExecution; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | namespace LiveKit.Rtc.Internal |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// SafeHandle wrapper for FFI handles returned by the native library. |
| | | 12 | | /// Ensures proper cleanup of native resources. |
| | | 13 | | /// </summary> |
| | | 14 | | public class FfiHandle : SafeHandle |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Invalid handle constant. |
| | | 18 | | /// </summary> |
| | | 19 | | public const ulong InvalidHandle = 0; |
| | | 20 | | |
| | | 21 | | // Process-wide counters read by the reliability tests (internal, not part of the public |
| | | 22 | | // API): a handle that is never released, or released twice, is a native leak or a |
| | | 23 | | // double free that no exception reveals |
| | | 24 | | private static long s_wrapped; |
| | | 25 | | private static long s_released; |
| | | 26 | | private static long s_failedReleases; |
| | | 27 | | |
| | | 28 | | private readonly ulong _handleId; |
| | | 29 | | private bool _disposed; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Valid handles wrapped and not released yet, in the whole process. |
| | | 33 | | /// </summary> |
| | | 34 | | internal static long LiveCount => |
| | | 35 | | Interlocked.Read(ref s_wrapped) - Interlocked.Read(ref s_released); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Releases the native library rejected because it did not know the handle: a handle |
| | | 39 | | /// released twice, or wrapped by two owners. |
| | | 40 | | /// </summary> |
| | | 41 | | internal static long FailedReleaseCount => Interlocked.Read(ref s_failedReleases); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Creates a new FfiHandle from a native handle ID. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="handleId">The native handle ID.</param> |
| | | 47 | | internal FfiHandle(ulong handleId) |
| | | 48 | | : base(IntPtr.Zero, true) |
| | | 49 | | { |
| | | 50 | | _handleId = handleId; |
| | | 51 | | SetHandle(new IntPtr((long)handleId)); |
| | | 52 | | if (handleId != InvalidHandle) |
| | | 53 | | Interlocked.Increment(ref s_wrapped); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates an FfiHandle from an IntPtr. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="ptr">The pointer value.</param> |
| | | 60 | | internal FfiHandle(IntPtr ptr) |
| | | 61 | | : base(ptr, true) |
| | | 62 | | { |
| | | 63 | | _handleId = (ulong)ptr.ToInt64(); |
| | | 64 | | if (!IsInvalid) |
| | | 65 | | Interlocked.Increment(ref s_wrapped); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets whether this handle is invalid. |
| | | 70 | | /// </summary> |
| | | 71 | | public override bool IsInvalid => handle == IntPtr.Zero || _handleId == InvalidHandle; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the underlying handle ID. |
| | | 75 | | /// </summary> |
| | | 76 | | public ulong HandleId => _handleId; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Releases the native handle. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <returns>True if the handle was released successfully.</returns> |
| | | 82 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 83 | | protected override bool ReleaseHandle() |
| | | 84 | | { |
| | | 85 | | if (_disposed || IsInvalid) |
| | | 86 | | return true; |
| | | 87 | | |
| | | 88 | | _disposed = true; |
| | | 89 | | var dropped = NativeMethods.DropHandle(_handleId); |
| | | 90 | | Interlocked.Increment(ref s_released); |
| | | 91 | | if (!dropped) |
| | | 92 | | Interlocked.Increment(ref s_failedReleases); |
| | | 93 | | return dropped; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Records that a request handed the handle over to the native library, which now owns it |
| | | 98 | | /// (the incremental read of a data stream reader takes its reader): it counts as released |
| | | 99 | | /// and is never dropped. Only the sole owner of the handle may call it. |
| | | 100 | | /// </summary> |
| | | 101 | | internal void MarkConsumed() |
| | | 102 | | { |
| | | 103 | | if (_disposed || IsInvalid || IsClosed) |
| | | 104 | | return; |
| | | 105 | | _disposed = true; |
| | | 106 | | Interlocked.Increment(ref s_released); |
| | | 107 | | SetHandleAsInvalid(); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Creates an FfiHandle from an owned handle proto message. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="id">The handle ID.</param> |
| | | 114 | | /// <returns>A new FfiHandle instance.</returns> |
| | | 115 | | public static FfiHandle FromId(ulong id) |
| | | 116 | | { |
| | | 117 | | return new FfiHandle(id); |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Represents an owned handle that can be converted to an FfiHandle. |
| | | 123 | | /// </summary> |
| | | 124 | | public readonly struct OwnedHandle |
| | | 125 | | { |
| | | 126 | | /// <summary> |
| | | 127 | | /// The handle ID. |
| | | 128 | | /// </summary> |
| | | 129 | | public ulong Id { get; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Creates a new OwnedHandle. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="id">The handle ID.</param> |
| | | 135 | | public OwnedHandle(ulong id) |
| | 0 | 136 | | { |
| | | 137 | | Id = id; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Converts this OwnedHandle to an FfiHandle. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <returns>A new FfiHandle instance.</returns> |
| | | 144 | | public FfiHandle ToFfiHandle() |
| | 0 | 145 | | { |
| | 0 | 146 | | return new FfiHandle(Id); |
| | 0 | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |