-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandle.cs
48 lines (43 loc) · 1.08 KB
/
Handle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace PiGameSharp
{
internal class Handle : CriticalHandle
{
private string Tag;
private Action<IntPtr> Release;
public Handle(string tag, IntPtr hnd, Action<IntPtr> release) : base(IntPtr.Zero)
{
Tag = tag;
handle = hnd;
Release = release;
System.Diagnostics.Debug.WriteLine("Allocated handle 0x" + handle.ToString("X") + " tag: " + Tag);
}
public override bool IsInvalid
{
get
{
return handle == IntPtr.Zero || Release == null;
}
}
protected override bool ReleaseHandle()
{
Action<IntPtr> rel = Interlocked.Exchange(ref Release, null);
if (rel != null)
{
System.Diagnostics.Debug.WriteLine("Releasing handle 0x" + handle.ToString("X") + " tag: " + Tag);
rel(handle);
}
else
System.Diagnostics.Debug.WriteLine("Double release handle 0x" + handle.ToString("X") + " tag: " + Tag);
return rel != null;
}
public static implicit operator IntPtr(Handle inst)
{
if (inst == null)
return IntPtr.Zero;
return inst.handle;
}
}
}