You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
// License: Apache 2.0. See LICENSE file in root directory.
|
|
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
|
|
|
|
//Adapted from: https://github.com/dotnet/reactive/blob/master/Rx.NET/Source/src/System.Reactive/Disposables/RefCountDisposable.cs
|
|
namespace Intel.RealSense.Base
|
|
{
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// Base class for objects in an <cref see="ObjectPool">ObjectPool</cref>
|
|
/// </summary>
|
|
public class RefCountedPooledObject : PooledObject
|
|
{
|
|
protected RefCount refCount;
|
|
|
|
protected RefCountedPooledObject(IntPtr ptr, Deleter deleter)
|
|
: base(ptr, deleter)
|
|
{
|
|
}
|
|
|
|
internal void Retain()
|
|
{
|
|
if (m_instance.IsInvalid)
|
|
{
|
|
throw new ObjectDisposedException("RefCountedPooledObject");
|
|
}
|
|
|
|
if (refCount.count == int.MaxValue)
|
|
{
|
|
throw new OverflowException("RefCountedPooledObject can't handle more than " + int.MaxValue + " disposables");
|
|
}
|
|
refCount.count++;
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (m_instance.IsInvalid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool didDispose = Release(disposing);
|
|
|
|
//Dispose of this instance even if the underlying resource still exists
|
|
if (!didDispose)
|
|
{
|
|
m_instance.SetHandleAsInvalid();
|
|
ObjectPool.Release(this);
|
|
}
|
|
}
|
|
|
|
private bool Release(bool disposing)
|
|
{
|
|
System.Diagnostics.Debug.Assert(refCount.count > 0);
|
|
|
|
refCount.count--;
|
|
if (refCount.count == 0)
|
|
{
|
|
base.Dispose(disposing);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
internal override void Initialize()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public sealed class RefCount
|
|
{
|
|
public int count;
|
|
}
|
|
}
|