/// <summary> /// Release the handle, which will release the native instance immediately if in main thread /// otherwise, will queue /// </summary> override protected bool ReleaseHandle() { if (handle == IntPtr.Zero) { throw new InvalidOperationException("RefCountedSafeFileHandle.ReleaseHandle - native == IntPtr.Zero"); } // We can be called from Dispose in main thread or from finalizers, which aren't in the main thread if (AtomicNET.IsMainThread()) { NativeCore.csi_AtomicEngine_ReleaseRef(handle); } else { // We're in a finalizer, need to add to queue to release when // back in main thread lock (RefCounted.refCountedFinalizerQueue) { RefCounted.refCountedFinalizerQueue.Add(handle); } } handle = IntPtr.Zero; return(true); }
/// <summary> /// Releases RefCounted instances which were finalized (which can happen on any thread) /// </summary> static internal void ReleaseFinalized() { lock (refCountedFinalizerQueue) { foreach (var native in refCountedFinalizerQueue) { NativeCore.RemoveNative(native); NativeCore.csi_AtomicEngine_ReleaseRef(native); } refCountedFinalizerQueue.Clear(); } }