Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -61,9 +61,9 @@ namespace System.Runtime.InteropServices
// MonoSafeHandle
//
protected IntPtr handle;
IntPtr invalid_handle_value;
int refcount = 0;
int refcount;
bool owns_handle;
bool closed, disposed;
#if NET_2_1
protected SafeHandle ()
@@ -74,39 +74,21 @@ namespace System.Runtime.InteropServices
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
{
invalid_handle_value = invalidHandleValue;
owns_handle = ownsHandle;
handle = invalidHandleValue;
if (!ownsHandle) {
GC.SuppressFinalize (this);
} else {
owns_handle = true;
}
refcount = 1;
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void Close ()
{
if (refcount == 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount = 0, current = 0;
bool registered = false;
RuntimeHelpers.PrepareConstrainedRegions ();
try {
do {
current = refcount;
newcount = current-1;
// perform changes in finally to avoid async interruptions
try {}
finally {
if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
registered = true;
}
} while (!registered);
} finally {
if (registered && newcount == 0 && owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
refcount = -1;
}
}
Dispose ();
}
//
@@ -121,8 +103,8 @@ namespace System.Runtime.InteropServices
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
public void DangerousAddRef (ref bool success)
{
if (refcount <= 0)
throw new ObjectDisposedException (GetType ().FullName);
if (closed)
throw new ObjectDisposedException ("SafeHandle was closed");
bool registered = false;
int newcount, current;
@@ -153,29 +135,13 @@ namespace System.Runtime.InteropServices
[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;
}
RunRelease ();
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
@@ -192,21 +158,52 @@ namespace System.Runtime.InteropServices
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void SetHandleAsInvalid ()
{
handle = invalid_handle_value;
closed = true;
}
[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?
//
if (disposing) {
if (disposed)
return;
RunRelease ();
disposed = true;
} else {
if (owns_handle && !closed && !IsInvalid){
ReleaseHandle ();
}
}
}
void RunRelease ()
{
if (refcount == 0)
throw new ObjectDisposedException (GetType ().FullName);
int newcount = 0, current = 0;
bool registered = false;
RuntimeHelpers.PrepareConstrainedRegions ();
try {
do {
current = refcount;
newcount = current-1;
// perform changes in finally to avoid async interruptions
try {}
finally {
if (Interlocked.CompareExchange (ref refcount, newcount, current) == current)
registered = true;
}
} while (!registered);
} finally {
if (registered && newcount == 0) {
if (owns_handle && !closed && !IsInvalid)
ReleaseHandle ();
closed = true;
}
}
}
@@ -222,7 +219,7 @@ namespace System.Runtime.InteropServices
public bool IsClosed {
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
get {
return refcount <= 0;
return closed;
}
}
@@ -233,10 +230,7 @@ namespace System.Runtime.InteropServices
~SafeHandle ()
{
if (owns_handle && !IsInvalid){
ReleaseHandle ();
handle = invalid_handle_value;
}
Dispose (false);
}
}
}