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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
//
// System.Runtime.InteropServices.SafeHandle
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Notes:
// This code is only API complete, but it lacks the runtime support
// for CriticalFinalizerObject and any P/Invoke wrapping that might
// happen.
//
// For details, see:
// http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx
//
// On implementing SafeHandles:
// http://blogs.msdn.com/bclteam/archive/2005/03/15/396335.aspx
//
// Issues:
// The System.Runtime.ConstrainedExecution.ReliabilityContractAttribute has
// not been applied to any APIs here yet.
//
// TODO: Although DangerousAddRef has been implemented, I need to
// find out whether the runtime performs the P/Invoke if the
// handle has been disposed already.
//
//
#if NET_2_0
using System;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Threading;
namespace System.Runtime.InteropServices
{
public abstract class SafeHandle : CriticalFinalizerObject, IDisposable {
//
// Warning: the offset of handle is mapped inside the runtime
// if you move this, you must updated the runtime definition of
// MonoSafeHandle
//
protected IntPtr handle;
IntPtr invalid_handle_value;
int refcount = 0;
bool owns_handle;
#if NET_2_1
protected SafeHandle ()
{
throw new NotImplementedException ();
}
#endif
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
{
invalid_handle_value = invalidHandleValue;
owns_handle = ownsHandle;
refcount = 1;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void Close ()
{
if (refcount == 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount, current;
do {
current = refcount;
newcount = current-1;
} while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
if (newcount == 0 && owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
refcount = -1;
}
}
//
// I do not know when we could not be able to increment the
// reference count and set success to false. It might just
// be a convention used for the following code pattern:
//
// bool release = false
// try { x.DangerousAddRef (ref release); ... }
// finally { if (release) x.DangerousRelease (); }
//
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
public void DangerousAddRef (ref bool success)
{
if (refcount <= 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount, current;
do {
current = refcount;
newcount = current + 1;
if (current <= 0){
//
// In MS, calling sf.Close () followed by a call
// to P/Invoke with SafeHandles throws this, but
// am left wondering: when would "success" be
// set to false?
//
throw new ObjectDisposedException (GetType ().FullName);
}
} while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
success = true;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public IntPtr DangerousGetHandle ()
{
if (refcount <= 0){
throw new ObjectDisposedException (GetType ().FullName);
}
return handle;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void DangerousRelease ()
{
if (refcount <= 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount, current;
do {
current = refcount;
newcount = current-1;
} while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
if (newcount == 0 && owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
}
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
//
// See documentation, this invalidates the handle without
// closing it.
//
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void SetHandleAsInvalid ()
{
handle = invalid_handle_value;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected virtual void Dispose (bool disposing)
{
if (disposing)
Close ();
else {
//
// The docs say `never call this with disposing=false',
// the question is whether:
// * The runtime will ever call Dipose(false) for SafeHandles (special runtime case)
// * Whether we should just call ReleaseHandle regardless?
//
}
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected abstract bool ReleaseHandle ();
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
protected void SetHandle (IntPtr handle)
{
this.handle = handle;
}
public bool IsClosed {
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
get {
return refcount <= 0;
}
}
public abstract bool IsInvalid {
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
get;
}
~SafeHandle ()
{
if (owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
}
}
}
}
#endif
|