Beispiel #1
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
#pragma warning disable CA1063 // Implement IDisposable Correctly
        private void Dispose(bool disposing)
#pragma warning restore CA1063 // Implement IDisposable Correctly
        {
            // If already 0, return.
            if (!AtomicHelper.DecrementIfGreaterThan(ref refCounter_, 0))
            {
                Debug.Assert(RefCount == 0);
                return;
            }
            var currRef = RefCount;

            if (currRef == 0 && !IsDisposed)
            {
                if (Interlocked.Increment(ref disposeCount_) == 1)
                {
                    AddBackToPool = null;
                    Disposing?.Invoke(this, disposing ? BoolEventArgs.TrueArgs : BoolEventArgs.FalseArgs);
                    Disposing = null;
                    OnDispose(disposing);
                    //GC.SuppressFinalize(this);

                    IsDisposed = true;

                    Disposed?.Invoke(this, disposing ? BoolEventArgs.TrueArgs : BoolEventArgs.FalseArgs);
                    Disposed = null;
                }
            }
            else if (currRef == 1)
            {
                AddBackToPool?.Invoke(this);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Forces the dispose.
 /// </summary>
 public void ForceDispose()
 {
     // Set ref counter to 1 if greater than 1
     AtomicHelper.ExchangeIfGreaterThan(ref refCounter_, 1, 1);
     Dispose();
 }
Beispiel #3
0
 /// <summary>
 /// Increase reference counter
 /// </summary>
 /// <returns></returns>
 public int IncRef()
 {
     // Increment only greater than 1
     AtomicHelper.IncrementIfGreaterThan(ref refCounter_, 0);
     return(AtomicHelper.Read(ref refCounter_));
 }