[PlatformSpecific(TestPlatforms.Windows)] // ThreadPoolBoundHandle.BindHandle is not supported on Unix
    public unsafe void AllocateNativeOverlapped_PreAllocated_ReusedReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero(bool useUnsafe)
    {
        // The CLR reuses NativeOverlapped underneath, check to make sure that they reset fields back to zero
        using (ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            PreAllocatedOverlapped preAlloc = useUnsafe ?
                                              PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new byte[256]) :
                                              new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]);

            NativeOverlapped *overlapped = handle.AllocateNativeOverlapped(preAlloc);
            overlapped->OffsetHigh = 1;
            overlapped->OffsetLow  = 1;
            handle.FreeNativeOverlapped(overlapped);

            overlapped = handle.AllocateNativeOverlapped(preAlloc);

            Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
            Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
            Assert.Equal(0, overlapped->OffsetLow);
            Assert.Equal(0, overlapped->OffsetHigh);
            Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

            handle.FreeNativeOverlapped(overlapped);
        }
    }
    public unsafe void PreAllocatedOverlapped_NullAsCallback_ThrowsArgumentNullException()
    {
        AssertExtensions.Throws <ArgumentNullException>("callback", () => new PreAllocatedOverlapped(null, new object(), new byte[256]));
        AssertExtensions.Throws <ArgumentNullException>("callback", () => PreAllocatedOverlapped.UnsafeCreate(null, new object(), new byte[256]));

        // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
 public unsafe void PreAllocatedOverlapped_BlittableTypeAsPinData_DoesNotThrow()
 {
     using (new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new BlittableType()
     {
         i = 42
     })) { }
     using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new BlittableType()
     {
         i = 42
     })) { }
 }
 [PlatformSpecific(TestPlatforms.Windows)] // ThreadPoolBoundHandle.BindHandle is not supported on Unix
 public unsafe void AllocateNativeOverlapped_PreAllocated_WhenDisposed_ThrowsObjectDisposedException(bool useUnsafe)
 {
     using (ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
     {
         PreAllocatedOverlapped preAlloc = useUnsafe ?
                                           PreAllocatedOverlapped.UnsafeCreate(delegate { }, null, null) :
                                           new PreAllocatedOverlapped(delegate { }, null, null);
         preAlloc.Dispose();
         Assert.Throws <ObjectDisposedException>(() => handle.AllocateNativeOverlapped(preAlloc));
     }
 }
Exemple #5
0
    [PlatformSpecific(TestPlatforms.Windows)] // ThreadPoolBoundHandle.BindHandle is not supported on Unix
    public unsafe void FlowsAsyncLocalsToCallback_PreAllocatedOverlapped(bool shouldFlow)
    {
        // Makes sure that we flow async locals to callback

        const int DATA_SIZE = 2;

        SafeHandle            handle      = HandleFactory.CreateAsyncFileHandleForWrite(Path.Combine(TestDirectory, @"AsyncLocal.tmp"));
        ThreadPoolBoundHandle boundHandle = ThreadPoolBoundHandle.BindHandle(handle);

        OverlappedContext context = new OverlappedContext();

        byte[] data = new byte[DATA_SIZE];

        AsyncLocal <int> asyncLocal = new AsyncLocal <int>();

        asyncLocal.Value = 10;

        int?result = null;
        IOCompletionCallback callback = (_, __, ___) => {
            result = asyncLocal.Value;
            OnOverlappedOperationCompleted(_, __, ___);
        };

        using (PreAllocatedOverlapped preAlloc = shouldFlow ?
                                                 new PreAllocatedOverlapped(callback, context, data) :
                                                 PreAllocatedOverlapped.UnsafeCreate(callback, context, data))
        {
            NativeOverlapped *overlapped = boundHandle.AllocateNativeOverlapped(preAlloc);

            fixed(byte *p = data)
            {
                int retval = DllImport.WriteFile(boundHandle.Handle, p, DATA_SIZE, IntPtr.Zero, overlapped);

                if (retval == 0)
                {
                    Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError());
                }

                // Wait for overlapped operation to complete
                context.Event.WaitOne();
            }

            boundHandle.FreeNativeOverlapped(overlapped);
        }

        boundHandle.Dispose();
        handle.Dispose();

        Assert.Equal(
            shouldFlow ? 10 : 0,
            result);
    }
    public unsafe void PreAllocatedOverlapped_ObjectArrayAsPinData_DoesNotThrow()
    {
        var array = new object[]
        {
            new BlittableType()
            {
                i = 1
            },
            new byte[5],
        };

        using (new PreAllocatedOverlapped((_, __, ___) => { }, new object(), array)) { }
        using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), array)) { }
    }
    public unsafe void PreAllocatedOverlapped_NonBlittableTypeAsPinData_Throws()
    {
        Assert.Throws <ArgumentException>(() => new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new NonBlittableType()
        {
            s = "foo"
        }));
        Assert.Throws <ArgumentException>(() => PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new NonBlittableType()
        {
            s = "foo"
        }));

        // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    [PlatformSpecific(TestPlatforms.Windows)] // ThreadPoolBoundHandle.BindHandle is not supported on Unix
    public unsafe void AllocateNativeOverlapped_PreAllocated_WhenAlreadyAllocated_ThrowsArgumentException(bool useUnsafe)
    {
        using (ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            using PreAllocatedOverlapped preAlloc = useUnsafe ?
                                                    PreAllocatedOverlapped.UnsafeCreate(delegate { }, null, null) :
                                                    new PreAllocatedOverlapped(delegate { }, null, null);

            NativeOverlapped *overlapped = handle.AllocateNativeOverlapped(preAlloc);

            AssertExtensions.Throws <ArgumentException>("preAllocated", () => handle.AllocateNativeOverlapped(preAlloc));

            handle.FreeNativeOverlapped(overlapped);
        }
    }
    public unsafe void PreAllocatedOverlapped_ObjectArrayWithNonBlittableTypeAsPinData_Throws()
    {
        var array = new object[]
        {
            new NonBlittableType()
            {
                s = "foo"
            },
            new byte[5],
        };

        AssertExtensions.Throws <ArgumentException>(null, () => new PreAllocatedOverlapped((_, __, ___) => { }, new object(), array));
        AssertExtensions.Throws <ArgumentException>(null, () => PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), array));

        // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    [PlatformSpecific(TestPlatforms.Windows)] // ThreadPoolBoundHandle.BindHandle is not supported on Unix
    public unsafe void AllocateNativeOverlapped_PreAllocated_ReturnedNativeOverlapped_AllFieldsZero(bool useUnsafe)
    {
        using (ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            using (PreAllocatedOverlapped preAlloc = useUnsafe ?
                                                     PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new byte[256]) :
                                                     new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]))
            {
                NativeOverlapped *overlapped = handle.AllocateNativeOverlapped(preAlloc);

                Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
                Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
                Assert.Equal(0, overlapped->OffsetLow);
                Assert.Equal(0, overlapped->OffsetHigh);
                Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

                handle.FreeNativeOverlapped(overlapped);
            }
        }
    }
 internal OverlappedValueTaskSource(SafeFileHandle fileHandle)
 {
     _fileHandle = fileHandle;
     _source.RunContinuationsAsynchronously = true;
     _preallocatedOverlapped = PreAllocatedOverlapped.UnsafeCreate(s_ioCallback, this, null);
 }
 public unsafe void PreAllocatedOverlapped_EmptyArrayAsPinData_DoesNotThrow()
 {
     using (new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[0])) { }
     using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new byte[0])) { }
 }
 public unsafe void PreAllocatedOverlapped_NullAsPinData_DoesNotThrow()
 {
     using (new PreAllocatedOverlapped((_, __, ___) => { }, new object(), (byte[])null)) { }
     using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), (byte[])null)) { }
 }
 public unsafe void PreAllocatedOverlapped_NullAsContext_DoesNotThrow()
 {
     using (new PreAllocatedOverlapped((_, __, ___) => { }, (object)null, new byte[256])) { }
     using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, (object)null, new byte[256])) { }
 }
 public unsafe void PreAllocatedOverlapped_ReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
 {
     using (new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256])) { }
     using (PreAllocatedOverlapped.UnsafeCreate((_, __, ___) => { }, new object(), new byte[256])) { }
 }
Exemple #16
0
 internal ValueTaskSource(AsyncWindowsFileStreamStrategy strategy)
 {
     _strategy = strategy;
     _source.RunContinuationsAsynchronously = true;
     _preallocatedOverlapped = PreAllocatedOverlapped.UnsafeCreate(s_ioCallback, this, null);
 }