Merge pull request #2763 from esdrubal/mono-symbolicate-standalone2
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / CriticalHandle.cs
1 //
2 // System.Runtime.InteropServices.CriticalHandle
3 //
4 // Author:
5 //   Kazuki Oikawa  (kazuki@panicode.com)
6 //
7
8
9 using System;
10 using System.Runtime.ConstrainedExecution;
11 using System.Runtime.Serialization;
12
13 namespace System.Runtime.InteropServices
14 {
15         public abstract class CriticalHandle : CriticalFinalizerObject, IDisposable
16         {
17                 protected IntPtr handle;
18                 bool _disposed = false;
19
20                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
21                 protected CriticalHandle (IntPtr invalidHandleValue)
22                 {
23                         handle = invalidHandleValue;
24                 }
25
26                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
27                 ~CriticalHandle ()
28                 {
29                         Dispose (false);
30                 }
31
32                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
33                 public void Close ()
34                 {
35                         Dispose (true);
36                 }
37
38                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
39                 public void Dispose ()
40                 {
41                         Dispose (true);
42                 }
43
44                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
45                 protected virtual void Dispose (bool disposing)
46                 {
47                         if (_disposed)
48                                 return;
49
50                         if (!IsInvalid){
51                                 if (!_disposed && !ReleaseHandle ()) {
52                                         GC.SuppressFinalize (this);
53                                 } else {
54                                         // Failed in release...
55                                 }
56                         }
57                         _disposed = true;
58                 }
59
60                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
61                 protected abstract bool ReleaseHandle ();
62
63                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
64                 protected void SetHandle (IntPtr handle)
65                 {
66                         this.handle = handle;
67                 }
68
69                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
70                 public void SetHandleAsInvalid()
71                 {
72                         _disposed = true;
73                 }
74
75                 public bool IsClosed {
76                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
77                         get { return _disposed; }
78                 }
79
80                 public abstract bool IsInvalid {
81                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
82                         get;
83                 }
84         }
85 }