Allow at max one Remove operation to fail in tests.
[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                         _disposed = true;
51                         if (IsInvalid)
52                                 return;
53
54                         if (disposing == true && !IsInvalid){
55                                 if (!ReleaseHandle ()) {
56                                         GC.SuppressFinalize (this);
57                                 } else {
58                                         // Failed in release...
59                                 }
60                         }
61                 }
62
63                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
64                 protected abstract bool ReleaseHandle ();
65
66                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
67                 protected void SetHandle (IntPtr handle)
68                 {
69                         this.handle = handle;
70                 }
71
72                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
73                 public void SetHandleAsInvalid()
74                 {
75                         _disposed = true;
76                 }
77
78                 public bool IsClosed {
79                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
80                         get { return _disposed; }
81                 }
82
83                 public abstract bool IsInvalid {
84                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
85                         get;
86                 }
87         }
88 }