-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5622 from JBBee/cs_wrapper_fix3
C# Wrapper Reference Counting for Sensor/Device
- Loading branch information
Showing
4 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
wrappers/csharp/Intel.RealSense/Base/RefCountedPooledObject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters