2 // System.Runtime.InteropServices.SafeHandle
4 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // This code is only API complete, but it lacks the runtime support
27 // for CriticalFinalizerObject and any P/Invoke wrapping that might
31 // http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx
33 // CER-like behavior is implemented for Close and DangerousAddRef
34 // via the try/finally uninterruptible pattern in case of async
35 // exceptions like ThreadAbortException.
37 // On implementing SafeHandles:
38 // http://blogs.msdn.com/bclteam/archive/2005/03/15/396335.aspx
42 // TODO: Although DangerousAddRef has been implemented, I need to
43 // find out whether the runtime performs the P/Invoke if the
44 // handle has been disposed already.
49 using System.Runtime.InteropServices;
50 using System.Runtime.ConstrainedExecution;
51 using System.Runtime.CompilerServices;
52 using System.Threading;
54 namespace System.Runtime.InteropServices
56 [StructLayout (LayoutKind.Sequential)]
57 public abstract class SafeHandle : CriticalFinalizerObject, IDisposable {
59 // Warning: the offset of handle is mapped inside the runtime
60 // if you move this, you must updated the runtime definition of
63 protected IntPtr handle;
66 bool closed, disposed;
69 protected SafeHandle ()
71 throw new NotImplementedException ();
74 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
75 protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
77 handle = invalidHandleValue;
80 GC.SuppressFinalize (this);
88 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
95 // I do not know when we could not be able to increment the
96 // reference count and set success to false. It might just
97 // be a convention used for the following code pattern:
99 // bool release = false
100 // try { x.DangerousAddRef (ref release); ... }
101 // finally { if (release) x.DangerousRelease (); }
103 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
104 public void DangerousAddRef (ref bool success)
107 throw new ObjectDisposedException ("SafeHandle was closed");
109 bool registered = false;
110 int newcount, current;
113 newcount = current + 1;
117 // In MS, calling sf.Close () followed by a call
118 // to P/Invoke with SafeHandles throws this, but
119 // am left wondering: when would "success" be
122 throw new ObjectDisposedException (GetType ().FullName);
125 // perform changes in finally to avoid async interruptions
126 RuntimeHelpers.PrepareConstrainedRegions ();
129 if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
130 registered = success = true;
132 } while (!registered);
135 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
136 public IntPtr DangerousGetHandle ()
141 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
142 public void DangerousRelease ()
147 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
148 public void Dispose ()
151 GC.SuppressFinalize (this);
155 // See documentation, this invalidates the handle without
158 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
159 public void SetHandleAsInvalid ()
164 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
165 protected virtual void Dispose (bool disposing)
174 if (owns_handle && !closed && !IsInvalid){
183 throw new ObjectDisposedException (GetType ().FullName);
185 int newcount = 0, current = 0;
186 bool registered = false;
187 RuntimeHelpers.PrepareConstrainedRegions ();
191 newcount = current-1;
193 // perform changes in finally to avoid async interruptions
196 if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
199 } while (!registered);
201 if (registered && newcount == 0) {
202 if (owns_handle && !closed && !IsInvalid)
210 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
211 protected abstract bool ReleaseHandle ();
213 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
214 protected void SetHandle (IntPtr handle)
216 this.handle = handle;
219 public bool IsClosed {
220 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
226 public abstract bool IsInvalid {
227 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]