2007-02-08 Jonathan Chambers <joncham@gmail.com>
[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 #if NET_2_0
9
10 using System;
11 using System.Runtime.ConstrainedExecution;
12 using System.Runtime.Serialization;
13
14 namespace System.Runtime.InteropServices
15 {
16         public abstract class CriticalHandle : CriticalFinalizerObject, IDisposable
17         {
18                 protected IntPtr handle;
19                 bool _disposed = false;
20
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                 public void Dispose (bool disposing)
45                 {
46                         if (_disposed)
47                                 return;
48
49                         _disposed = true;
50                         if (IsInvalid)
51                                 return;
52
53                         if (disposing == true){
54                                 if (ReleaseHandle ()) {
55                                         GC.SuppressFinalize (this);
56                                 } else {
57                                         // Failed in release...
58                                 }
59                         }
60                 }
61
62                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
63                 protected abstract bool ReleaseHandle ();
64
65                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
66                 protected void SetHandle (IntPtr handle)
67                 {
68                         this.handle = handle;
69                 }
70
71                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
72                 public void SetHandleAsInvalid()
73                 {
74                         _disposed = true;
75                 }
76
77                 public bool IsClosed {
78                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
79                         get { return _disposed; }
80                 }
81
82                 public abstract bool IsInvalid {
83                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
84                         get;
85                 }
86         }
87 }
88 #endif