using mbts.utils; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace mbts { public static class Disposable { private class AnonymousDisposable : IDisposable { private Action _action; public AnonymousDisposable(Action a) { _action = a; } public void Dispose() { _action?.Invoke(); } } private class FinalizeableAnonymousDisposable : IDisposable { private Action _action; public FinalizeableAnonymousDisposable(Action dispose) { _action = dispose; } ~FinalizeableAnonymousDisposable() { Dispose(false); } public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } protected virtual void Dispose(bool disposing) { _action?.Invoke(disposing); _action = null; } } public static IDisposable Create(Action a) { return new AnonymousDisposable(a); } public static IDisposable Create(Action a) { return new FinalizeableAnonymousDisposable(a); } public static IDisposable Create(params Action[] actions) { return Create(actions.Select(a => Disposable.Create(a)).ToArray()); } public static IDisposable Create(params IDisposable[] disposables) { if (disposables.Length == 1) return disposables.First(); return Create(() => { foreach (var d in disposables) { try { if(d != null) d.Dispose(); } catch { } } }); } // enforces Dispose is only executed once public static IDisposable CreateDisposeOnce(Action a) { var dispose = Create(a); return Create(() => Disposable.dispose(ref dispose)); } public static IDisposable empty => Create(() => { }); // IDempotent dispose public static void dispose(ref T item) where T : class, IDisposable { if (item != null) item.Dispose(); item = null; } // IDempotent dispose public static void dispose(IList items) where T : IDisposable { if (items != null) { foreach (var item in items) { item.Dispose(); } items.Clear(); } } // throws ObjectDisposedException if item is already null public static void dispose_once(ref T item) where T : class, IDisposable { if (item == null) throw new ObjectDisposedException(nameof(item)); else { item.Dispose(); item = null; } } public static void replace(ref T current, T newval) where T : IDisposable { if (current != null) { current.Dispose(); } current = newval; } } // inspired by: https://blogs.msdn.microsoft.com/blambert/2009/07/24/a-simple-and-totally-thread-safe-implementation-of-idisposable/ /// /// Uses lock-free [Interlocked] code to ensure that a dispose-action is only executed once. /// class ThreadSafeDisposable : IDisposable { readonly Action _dispose_once; /// /// A value which indicates the disposable state. 0 indicates undisposed, 1 indicates disposing /// or disposed. /// int _state; public ThreadSafeDisposable(Action dispose_once) => _dispose_once = dispose_once; public bool has_dispose_started => Thread.VolatileRead(ref _state) != 0; /// /// Guarenteed to only execute the dispose-action once. This is thread-safe. /// public void Dispose() { // Attempt to move the disposable state from 0 to 1. If successful, we can be assured that // this thread is the first thread to do so, and can safely dispose of the object. if (Interlocked.CompareExchange(ref _state, 1, 0) == 0) _dispose_once(); } } }