You've already forked linux-packaging-mono
Imported Upstream version 5.0.0.42
Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
parent
1190d13a04
commit
6bdd276d05
@@ -33,5 +33,3 @@ using System.Runtime.CompilerServices;
|
||||
[assembly: AssemblyVersion ("4.0.1.0")]
|
||||
[assembly: AssemblyInformationalVersion ("4.0.0.0")]
|
||||
[assembly: AssemblyFileVersion ("4.0.0.0")]
|
||||
[assembly: AssemblyDelaySign (true)]
|
||||
[assembly: AssemblyKeyFile ("../../msfinal.pub")]
|
||||
|
@@ -1,315 +0,0 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace System.Threading
|
||||
{
|
||||
//
|
||||
// Implementation of ThreadPoolBoundHandle that sits on top of the CLR's ThreadPool and Overlapped infrastructure
|
||||
//
|
||||
|
||||
/// <summary>
|
||||
/// Represents an I/O handle that is bound to the system thread pool and enables low-level
|
||||
/// components to receive notifications for asynchronous I/O operations.
|
||||
/// </summary>
|
||||
public sealed partial class ThreadPoolBoundHandle : IDisposable
|
||||
{
|
||||
private readonly SafeHandle _handle;
|
||||
private bool _isDisposed;
|
||||
|
||||
private ThreadPoolBoundHandle(SafeHandle handle)
|
||||
{
|
||||
_handle = handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bound operating system handle.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A <see cref="SafeHandle"/> object that holds the bound operating system handle.
|
||||
/// </value>
|
||||
public SafeHandle Handle
|
||||
{
|
||||
get { return _handle; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ThreadPoolBoundHandle"/> for the specific handle,
|
||||
/// which is bound to the system thread pool.
|
||||
/// </summary>
|
||||
/// <param name="handle">
|
||||
/// A <see cref="SafeHandle"/> object that holds the operating system handle. The
|
||||
/// handle must have been opened for overlapped I/O on the unmanaged side.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <see cref="ThreadPoolBoundHandle"/> for <paramref name="handle"/>, which
|
||||
/// is bound to the system thread pool.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="handle"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="handle"/> has been disposed.
|
||||
/// <para>
|
||||
/// -or-
|
||||
/// </para>
|
||||
/// <paramref name="handle"/> does not refer to a valid I/O handle.
|
||||
/// <para>
|
||||
/// -or-
|
||||
/// </para>
|
||||
/// <paramref name="handle"/> refers to a handle that has not been opened
|
||||
/// for overlapped I/O.
|
||||
/// <para>
|
||||
/// -or-
|
||||
/// </para>
|
||||
/// <paramref name="handle"/> refers to a handle that has already been bound.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method should be called once per handle.
|
||||
/// <para>
|
||||
/// -or-
|
||||
/// </para>
|
||||
/// <see cref="ThreadPoolBoundHandle"/> does not take ownership of <paramref name="handle"/>,
|
||||
/// it remains the responsibility of the caller to call <see cref="SafeHandle.Dispose"/>.
|
||||
/// </remarks>
|
||||
public static ThreadPoolBoundHandle BindHandle(SafeHandle handle)
|
||||
{
|
||||
if (handle == null)
|
||||
throw new ArgumentNullException(nameof(handle));
|
||||
|
||||
if (handle.IsClosed || handle.IsInvalid)
|
||||
throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
|
||||
|
||||
try
|
||||
{
|
||||
// ThreadPool.BindHandle will always return true, otherwise, it throws. See the underlying FCall
|
||||
// implementation in ThreadPoolNative::CorBindIoCompletionCallback to see the implementation.
|
||||
bool succeeded = ThreadPool.BindHandle(handle);
|
||||
Debug.Assert(succeeded);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{ // BindHandle throws ApplicationException on full CLR and Exception on CoreCLR.
|
||||
// We do not let either of these leak and convert them to ArgumentException to
|
||||
// indicate that the specified handles are invalid.
|
||||
|
||||
if (ex.HResult == System.HResults.E_HANDLE) // Bad handle
|
||||
throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
|
||||
|
||||
if (ex.HResult == System.HResults.E_INVALIDARG) // Handle already bound or sync handle
|
||||
throw new ArgumentException(SR.Argument_AlreadyBoundOrSyncHandle, nameof(handle));
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return new ThreadPoolBoundHandle(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an unmanaged pointer to a <see cref="NativeOverlapped"/> structure, specifying
|
||||
/// a delegate that is invoked when the asynchronous I/O operation is complete, a user-provided
|
||||
/// object providing context, and managed objects that serve as buffers.
|
||||
/// </summary>
|
||||
/// <param name="callback">
|
||||
/// An <see cref="IOCompletionCallback"/> delegate that represents the callback method
|
||||
/// invoked when the asynchronous I/O operation completes.
|
||||
/// </param>
|
||||
/// <param name="state">
|
||||
/// A user-provided object that distinguishes this <see cref="NativeOverlapped"/> from other
|
||||
/// <see cref="NativeOverlapped"/> instances. Can be <see langword="null"/>.
|
||||
/// </param>
|
||||
/// <param name="pinData">
|
||||
/// An object or array of objects representing the input or output buffer for the operation. Each
|
||||
/// object represents a buffer, for example an array of bytes. Can be <see langword="null"/>.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// An unmanaged pointer to a <see cref="NativeOverlapped"/> structure.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The unmanaged pointer returned by this method can be passed to the operating system in
|
||||
/// overlapped I/O operations. The <see cref="NativeOverlapped"/> structure is fixed in
|
||||
/// physical memory until <see cref="FreeNativeOverlapped(NativeOverlapped*)"/> is called.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The buffer or buffers specified in <paramref name="pinData"/> must be the same as those passed
|
||||
/// to the unmanaged operating system function that performs the asynchronous I/O.
|
||||
/// </para>
|
||||
/// <note>
|
||||
/// The buffers specified in <paramref name="pinData"/> are pinned for the duration of
|
||||
/// the I/O operation.
|
||||
/// </note>
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="callback"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// This method was called after the <see cref="ThreadPoolBoundHandle"/> was disposed.
|
||||
/// </exception>
|
||||
public unsafe NativeOverlapped* AllocateNativeOverlapped(IOCompletionCallback callback, object state, object pinData)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException(nameof(callback));
|
||||
|
||||
EnsureNotDisposed();
|
||||
|
||||
ThreadPoolBoundHandleOverlapped overlapped = new ThreadPoolBoundHandleOverlapped(callback, state, pinData, preAllocated: null);
|
||||
overlapped._boundHandle = this;
|
||||
return overlapped._nativeOverlapped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an unmanaged pointer to a <see cref="NativeOverlapped"/> structure, using the callback,
|
||||
/// state, and buffers associated with the specified <see cref="PreAllocatedOverlapped"/> object.
|
||||
/// </summary>
|
||||
/// <param name="preAllocated">
|
||||
/// A <see cref="PreAllocatedOverlapped"/> object from which to create the NativeOverlapped pointer.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// An unmanaged pointer to a <see cref="NativeOverlapped"/> structure.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The unmanaged pointer returned by this method can be passed to the operating system in
|
||||
/// overlapped I/O operations. The <see cref="NativeOverlapped"/> structure is fixed in
|
||||
/// physical memory until <see cref="FreeNativeOverlapped(NativeOverlapped*)"/> is called.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="preAllocated"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="preAllocated"/> is currently in use for another I/O operation.
|
||||
/// </exception>
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// This method was called after the <see cref="ThreadPoolBoundHandle"/> was disposed, or
|
||||
/// this method was called after <paramref name="preAllocated"/> was disposed.
|
||||
/// </exception>
|
||||
/// <seealso cref="PreAllocatedOverlapped"/>
|
||||
public unsafe NativeOverlapped* AllocateNativeOverlapped(PreAllocatedOverlapped preAllocated)
|
||||
{
|
||||
if (preAllocated == null)
|
||||
throw new ArgumentNullException(nameof(preAllocated));
|
||||
|
||||
EnsureNotDisposed();
|
||||
|
||||
preAllocated.AddRef();
|
||||
try
|
||||
{
|
||||
ThreadPoolBoundHandleOverlapped overlapped = preAllocated._overlapped;
|
||||
|
||||
if (overlapped._boundHandle != null)
|
||||
throw new ArgumentException(SR.Argument_PreAllocatedAlreadyAllocated, nameof(preAllocated));
|
||||
|
||||
overlapped._boundHandle = this;
|
||||
|
||||
return overlapped._nativeOverlapped;
|
||||
}
|
||||
catch
|
||||
{
|
||||
preAllocated.Release();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frees the unmanaged memory associated with a <see cref="NativeOverlapped"/> structure
|
||||
/// allocated by the <see cref="AllocateNativeOverlapped"/> method.
|
||||
/// </summary>
|
||||
/// <param name="overlapped">
|
||||
/// An unmanaged pointer to the <see cref="NativeOverlapped"/> structure to be freed.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// <note type="caution">
|
||||
/// You must call the <see cref="FreeNativeOverlapped(NativeOverlapped*)"/> method exactly once
|
||||
/// on every <see cref="NativeOverlapped"/> unmanaged pointer allocated using the
|
||||
/// <see cref="AllocateNativeOverlapped"/> method.
|
||||
/// If you do not call the <see cref="FreeNativeOverlapped(NativeOverlapped*)"/> method, you will
|
||||
/// leak memory. If you call the <see cref="FreeNativeOverlapped(NativeOverlapped*)"/> method more
|
||||
/// than once on the same <see cref="NativeOverlapped"/> unmanaged pointer, memory will be corrupted.
|
||||
/// </note>
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="overlapped"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// This method was called after the <see cref="ThreadPoolBoundHandle"/> was disposed.
|
||||
/// </exception>
|
||||
public unsafe void FreeNativeOverlapped(NativeOverlapped* overlapped)
|
||||
{
|
||||
if (overlapped == null)
|
||||
throw new ArgumentNullException(nameof(overlapped));
|
||||
|
||||
// Note: we explicitly allow FreeNativeOverlapped calls after the ThreadPoolBoundHandle has been Disposed.
|
||||
|
||||
ThreadPoolBoundHandleOverlapped wrapper = GetOverlappedWrapper(overlapped, this);
|
||||
|
||||
if (wrapper._boundHandle != this)
|
||||
throw new ArgumentException(SR.Argument_NativeOverlappedWrongBoundHandle, nameof(overlapped));
|
||||
|
||||
if (wrapper._preAllocated != null)
|
||||
wrapper._preAllocated.Release();
|
||||
else
|
||||
Overlapped.Free(overlapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the user-provided object specified when the <see cref="NativeOverlapped"/> instance was
|
||||
/// allocated using the <see cref="AllocateNativeOverlapped(IOCompletionCallback, object, byte[])"/>.
|
||||
/// </summary>
|
||||
/// <param name="overlapped">
|
||||
/// An unmanaged pointer to the <see cref="NativeOverlapped"/> structure from which to return the
|
||||
/// asscociated user-provided object.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// A user-provided object that distinguishes this <see cref="NativeOverlapped"/>
|
||||
/// from other <see cref="NativeOverlapped"/> instances, otherwise, <see langword="null"/> if one was
|
||||
/// not specified when the instance was allocated using <see cref="AllocateNativeOverlapped"/>.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="overlapped"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
public unsafe static object GetNativeOverlappedState(NativeOverlapped* overlapped)
|
||||
{
|
||||
if (overlapped == null)
|
||||
throw new ArgumentNullException(nameof(overlapped));
|
||||
|
||||
ThreadPoolBoundHandleOverlapped wrapper = GetOverlappedWrapper(overlapped, null);
|
||||
Debug.Assert(wrapper._boundHandle != null);
|
||||
return wrapper._userState;
|
||||
}
|
||||
|
||||
private static unsafe ThreadPoolBoundHandleOverlapped GetOverlappedWrapper(NativeOverlapped* overlapped, ThreadPoolBoundHandle expectedBoundHandle)
|
||||
{
|
||||
ThreadPoolBoundHandleOverlapped wrapper;
|
||||
try
|
||||
{
|
||||
wrapper = (ThreadPoolBoundHandleOverlapped)Overlapped.Unpack(overlapped);
|
||||
}
|
||||
catch (NullReferenceException ex)
|
||||
{
|
||||
throw new ArgumentException(SR.Argument_NativeOverlappedAlreadyFree, nameof(overlapped), ex);
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// .NET Native's version of ThreadPoolBoundHandle that wraps the Win32 ThreadPool holds onto
|
||||
// native resources so it needs to be disposable. To match the contract, we are also disposable.
|
||||
// We also implement a disposable state to mimic behavior between this implementation and
|
||||
// .NET Native's version (code written against us, will also work against .NET Native's version).
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
|
||||
private void EnsureNotDisposed()
|
||||
{
|
||||
if (_isDisposed)
|
||||
throw new ObjectDisposedException(GetType().ToString());
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,50 +0,0 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace System.Threading
|
||||
{
|
||||
/// <summary>
|
||||
/// Overlapped subclass adding data needed by ThreadPoolBoundHandle.
|
||||
/// </summary>
|
||||
internal sealed class ThreadPoolBoundHandleOverlapped : Overlapped
|
||||
{
|
||||
private readonly IOCompletionCallback _userCallback;
|
||||
internal readonly object _userState;
|
||||
internal PreAllocatedOverlapped _preAllocated;
|
||||
internal unsafe NativeOverlapped* _nativeOverlapped;
|
||||
internal ThreadPoolBoundHandle _boundHandle;
|
||||
internal bool _completed;
|
||||
|
||||
public unsafe ThreadPoolBoundHandleOverlapped(IOCompletionCallback callback, object state, object pinData, PreAllocatedOverlapped preAllocated)
|
||||
{
|
||||
_userCallback = callback;
|
||||
_userState = state;
|
||||
_preAllocated = preAllocated;
|
||||
|
||||
_nativeOverlapped = Pack(CompletionCallback, pinData);
|
||||
_nativeOverlapped->OffsetLow = 0; // CLR reuses NativeOverlapped instances and does not reset these
|
||||
_nativeOverlapped->OffsetHigh = 0;
|
||||
}
|
||||
|
||||
private unsafe static void CompletionCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
|
||||
{
|
||||
ThreadPoolBoundHandleOverlapped overlapped = (ThreadPoolBoundHandleOverlapped)Overlapped.Unpack(nativeOverlapped);
|
||||
|
||||
//
|
||||
// The Win32 thread pool implementation of ThreadPoolBoundHandle does not permit reuse of NativeOverlapped
|
||||
// pointers without freeing them and allocating new a new one. We need to ensure that code using the CLR
|
||||
// ThreadPool implementation follows those rules.
|
||||
//
|
||||
if (overlapped._completed)
|
||||
throw new InvalidOperationException(SR.InvalidOperation_NativeOverlappedReused);
|
||||
|
||||
overlapped._completed = true;
|
||||
|
||||
if (overlapped._boundHandle == null)
|
||||
throw new InvalidOperationException(SR.Argument_NativeOverlappedAlreadyFree);
|
||||
|
||||
overlapped._userCallback(errorCode, numBytes, nativeOverlapped);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,104 +0,0 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace System.Threading
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents pre-allocated state for native overlapped I/O operations.
|
||||
/// </summary>
|
||||
/// <seealso cref="ThreadPoolBoundHandle.AllocateNativeOverlapped(PreAllocatedOverlapped)"/>
|
||||
public sealed class PreAllocatedOverlapped : IDisposable, IDeferredDisposable
|
||||
{
|
||||
internal readonly ThreadPoolBoundHandleOverlapped _overlapped;
|
||||
private DeferredDisposableLifetime<PreAllocatedOverlapped> _lifetime;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PreAllocatedOverlapped"/> class, specifying
|
||||
/// a delegate that is invoked when each asynchronous I/O operation is complete, a user-provided
|
||||
/// object providing context, and managed objects that serve as buffers.
|
||||
/// </summary>
|
||||
/// <param name="callback">
|
||||
/// An <see cref="IOCompletionCallback"/> delegate that represents the callback method
|
||||
/// invoked when each asynchronous I/O operation completes.
|
||||
/// </param>
|
||||
/// <param name="state">
|
||||
/// A user-provided object that distinguishes <see cref="NativeOverlapped"/> instance produced from this
|
||||
/// object from other <see cref="NativeOverlapped"/> instances. Can be <see langword="null"/>.
|
||||
/// </param>
|
||||
/// <param name="pinData">
|
||||
/// An object or array of objects representing the input or output buffer for the operations. Each
|
||||
/// object represents a buffer, for example an array of bytes. Can be <see langword="null"/>.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// The new <see cref="PreAllocatedOverlapped"/> instance can be passed to
|
||||
/// <see cref="ThreadPoolBoundHandle.AllocateNativeOverlapped(PreAllocatedOverlapped)"/>, to produce
|
||||
/// a <see cref="NativeOverlapped"/> instance that can be passed to the operating system in overlapped
|
||||
/// I/O operations. A single <see cref="PreAllocatedOverlapped"/> instance can only be used for
|
||||
/// a single native I/O operation at a time. However, the state stored in the <see cref="PreAllocatedOverlapped"/>
|
||||
/// instance can be reused for subsequent native operations.
|
||||
/// <note>
|
||||
/// The buffers specified in <paramref name="pinData"/> are pinned until <see cref="Dispose"/> is called.
|
||||
/// </note>
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="callback"/> is <see langword="null"/>.
|
||||
/// </exception>
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// This method was called after the <see cref="ThreadPoolBoundHandle"/> was disposed.
|
||||
/// </exception>
|
||||
public unsafe PreAllocatedOverlapped(IOCompletionCallback callback, object state, object pinData)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException(nameof(callback));
|
||||
|
||||
_overlapped = new ThreadPoolBoundHandleOverlapped(callback, state, pinData, this);
|
||||
}
|
||||
|
||||
internal bool AddRef()
|
||||
{
|
||||
return _lifetime.AddRef(this);
|
||||
}
|
||||
|
||||
internal void Release()
|
||||
{
|
||||
_lifetime.Release(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frees the resources associated with this <see cref="PreAllocatedOverlapped"/> instance.
|
||||
/// </summary>
|
||||
public unsafe void Dispose()
|
||||
{
|
||||
_lifetime.Dispose(this);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~PreAllocatedOverlapped()
|
||||
{
|
||||
//
|
||||
// During shutdown, don't automatically clean up, because this instance may still be
|
||||
// reachable/usable by other code.
|
||||
//
|
||||
if (!Environment.HasShutdownStarted)
|
||||
Dispose();
|
||||
}
|
||||
|
||||
unsafe void IDeferredDisposable.OnFinalRelease(bool disposed)
|
||||
{
|
||||
if (_overlapped != null)
|
||||
{
|
||||
if (disposed)
|
||||
{
|
||||
Overlapped.Free(_overlapped._nativeOverlapped);
|
||||
}
|
||||
else
|
||||
{
|
||||
_overlapped._boundHandle = null;
|
||||
_overlapped._completed = false;
|
||||
*_overlapped._nativeOverlapped = default(NativeOverlapped);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,116 +0,0 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace System.Threading
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides callbacks to objects whose lifetime is managed by <see cref="DeferredDisposableLifetime{T}"/>.
|
||||
/// </summary>
|
||||
internal interface IDeferredDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the object's refcount reaches zero.
|
||||
/// </summary>
|
||||
/// <param name="disposed">
|
||||
/// Indicates whether the object has been disposed.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// If the refount reaches zero before the object is disposed, this method will be called with
|
||||
/// <paramref name="disposed"/> set to false. If the object is then disposed, this method will be
|
||||
/// called again, with <paramref name="disposed"/> set to true. If the refcount reaches zero
|
||||
/// after the object has already been disposed, this will be called a single time, with
|
||||
/// <paramref name="disposed"/> set to true.
|
||||
/// </remarks>
|
||||
void OnFinalRelease(bool disposed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages the lifetime of an object which implements IDisposable, but which must defer the actual
|
||||
/// cleanup of state until all existing uses of the object are complete.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object whose lifetime will be managed.</typeparam>
|
||||
/// <remarks>
|
||||
/// This type maintains a reference count, and tracks whether the object has been disposed. When
|
||||
/// Callbacks are made to <see cref="IDeferredDisposable.OnFinalRelease(bool)"/> when the refcount
|
||||
/// reaches zero. Objects that need to defer cleanup until they have been disposed *and* they have
|
||||
/// no more references can do so in <see cref="IDeferredDisposable.OnFinalRelease(bool)"/> when
|
||||
/// 'disposed' is true.
|
||||
/// </remarks>
|
||||
internal struct DeferredDisposableLifetime<T> where T : class, IDeferredDisposable
|
||||
{
|
||||
//
|
||||
// _count is positive until Dispose is called, after which it's (-1 - refcount).
|
||||
//
|
||||
private int _count;
|
||||
|
||||
public bool AddRef(T obj)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int oldCount = Volatile.Read(ref _count);
|
||||
|
||||
// Have we been disposed?
|
||||
if (oldCount < 0)
|
||||
throw new ObjectDisposedException(typeof(T).ToString());
|
||||
|
||||
int newCount = checked(oldCount + 1);
|
||||
|
||||
if (Interlocked.CompareExchange(ref _count, newCount, oldCount) == oldCount)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Release(T obj)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int oldCount = Volatile.Read(ref _count);
|
||||
if (oldCount > 0)
|
||||
{
|
||||
// We haven't been disposed. Decrement _count.
|
||||
int newCount = oldCount - 1;
|
||||
if (Interlocked.CompareExchange(ref _count, newCount, oldCount) == oldCount)
|
||||
{
|
||||
if (newCount == 0)
|
||||
obj.OnFinalRelease(disposed: false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Assert(oldCount != 0 && oldCount != -1);
|
||||
|
||||
// We've been disposed. Increment _count.
|
||||
int newCount = oldCount + 1;
|
||||
if (Interlocked.CompareExchange(ref _count, newCount, oldCount) == oldCount)
|
||||
{
|
||||
if (newCount == -1)
|
||||
obj.OnFinalRelease(disposed: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose(T obj)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int oldCount = Volatile.Read(ref _count);
|
||||
if (oldCount < 0)
|
||||
return; // already disposed
|
||||
|
||||
int newCount = -1 - oldCount;
|
||||
if (Interlocked.CompareExchange(ref _count, newCount, oldCount) == oldCount)
|
||||
{
|
||||
if (newCount == -1)
|
||||
obj.OnFinalRelease(disposed: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
//
|
||||
// HResults.cs
|
||||
//
|
||||
// Author:
|
||||
// Alexander Köplinger (alexander.koeplinger@xamarin.com)
|
||||
//
|
||||
// (C) 2016 Xamarin, Inc.
|
||||
//
|
||||
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
namespace System
|
||||
{
|
||||
internal static class HResults
|
||||
{
|
||||
internal const int E_HANDLE = unchecked((int)0x80070006);
|
||||
internal const int E_INVALIDARG = unchecked((int)0x80070057);
|
||||
}
|
||||
}
|
@@ -12,7 +12,10 @@ LIBRARY = System.Threading.Overlapped.dll
|
||||
KEY_FILE = ../../msfinal.pub
|
||||
SIGN_FLAGS = /delaysign /keyfile:$(KEY_FILE) /nowarn:1616,1699
|
||||
LIB_REFS = System
|
||||
LIB_MCS_FLAGS = $(SIGN_FLAGS) -unsafe
|
||||
LIB_MCS_FLAGS = $(SIGN_FLAGS) -unsafe -nowarn:3021
|
||||
|
||||
RESX_RESOURCE_STRING = \
|
||||
../../../../external/corefx/src/System.Threading.Overlapped/src/Resources/Strings.resx
|
||||
|
||||
NO_TEST = yes
|
||||
|
||||
|
@@ -1,9 +1,10 @@
|
||||
TypeForwarders.cs
|
||||
AssemblyInfo.cs
|
||||
|
||||
ClrThreadPoolBoundHandle.cs
|
||||
ClrThreadPoolBoundHandleOverlapped.cs
|
||||
ClrThreadPoolPreAllocatedOverlapped.cs
|
||||
DeferredDisposableLifetime.cs
|
||||
SR.cs
|
||||
HResults.cs
|
||||
corefx/SR.cs
|
||||
|
||||
../../../../external/corefx/src/Common/src/System/HResults.cs
|
||||
../../../../external/corefx/src/System.Threading.Overlapped/src/System/Threading/ClrThreadPoolBoundHandle.cs
|
||||
../../../../external/corefx/src/System.Threading.Overlapped/src/System/Threading/ClrThreadPoolBoundHandleOverlapped.cs
|
||||
../../../../external/corefx/src/System.Threading.Overlapped/src/System/Threading/ClrThreadPoolPreAllocatedOverlapped.cs
|
||||
../../../../external/corefx/src/System.Threading.Overlapped/src/System/Threading/DeferredDisposableLifetime.cs
|
||||
|
@@ -1,6 +1,8 @@
|
||||
// strings taken from corefx
|
||||
//
|
||||
// This file was generated by resx2sr tool
|
||||
//
|
||||
|
||||
class SR
|
||||
partial class SR
|
||||
{
|
||||
public const string Argument_AlreadyBoundOrSyncHandle = "'handle' has already been bound to the thread pool, or was not opened for asynchronous I/O.";
|
||||
public const string Argument_InvalidHandle = "'handle' has been disposed or is an invalid handle.";
|
||||
@@ -8,4 +10,4 @@ class SR
|
||||
public const string Argument_NativeOverlappedWrongBoundHandle = "'overlapped' was not allocated by this ThreadPoolBoundHandle instance.";
|
||||
public const string Argument_PreAllocatedAlreadyAllocated = "'preAllocated' is already in use.";
|
||||
public const string InvalidOperation_NativeOverlappedReused = "NativeOverlapped cannot be reused for multiple operations.";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user