public static void GracefulShutdown()
        {
            using var @lock = new AsyncReaderWriterLock();
            True(@lock.TryEnterWriteLock());
            var task = @lock.DisposeAsync();

            False(task.IsCompleted);
            @lock.ExitWriteLock();
            True(task.IsCompletedSuccessfully);
            Throws <ObjectDisposedException>(() => @lock.TryEnterReadLock());
        }
        public static void GracefulShutdown2()
        {
            using var @lock = new AsyncReaderWriterLock();
            True(@lock.TryEnterReadLock());
            var task = @lock.DisposeAsync();

            False(task.IsCompleted);
            var acquisition = @lock.EnterWriteLockAsync(CancellationToken.None);

            False(acquisition.IsCompleted);
            @lock.ExitReadLock();
            True(task.IsCompletedSuccessfully);
            True(acquisition.IsFaulted);
            Throws <ObjectDisposedException>(acquisition.GetAwaiter().GetResult);
        }
Ejemplo n.º 3
0
        public static void OptimisticRead()
        {
            using var rwLock = new AsyncReaderWriterLock();
            var stamp = rwLock.TryOptimisticRead();

            True(stamp.IsValid);
            True(rwLock.TryEnterReadLock());
            Equal(1, rwLock.CurrentReadCount);
            True(stamp.IsValid);
            rwLock.ExitReadLock();
            Equal(stamp, rwLock.TryOptimisticRead());
            True(rwLock.TryEnterWriteLock());
            False(rwLock.IsReadLockHeld);
            True(rwLock.IsWriteLockHeld);
            False(stamp.IsValid);
        }
Ejemplo n.º 4
0
        public static async Task WriterToReaderChain()
        {
            using var are    = new AutoResetEvent(false);
            using var rwLock = new AsyncReaderWriterLock();
            await rwLock.EnterWriteLockAsync(DefaultTimeout);

            var task = new TaskCompletionSource <bool>();

            ThreadPool.QueueUserWorkItem(async state =>
            {
                False(await rwLock.TryEnterReadLockAsync(TimeSpan.FromMilliseconds(10)));
                True(ThreadPool.QueueUserWorkItem(static ev => ev.Set(), are, false));
                await rwLock.EnterReadLockAsync(DefaultTimeout);
                rwLock.ExitReadLock();
                task.SetResult(true);
            });
Ejemplo n.º 5
0
 public static async Task WriterToWriterChain()
 {
     using (var are = new AutoResetEvent(false))
         using (var rwLock = new AsyncReaderWriterLock())
         {
             True(await rwLock.TryEnterWriteLock(TimeSpan.Zero));
             var task = new TaskCompletionSource <bool>();
             ThreadPool.QueueUserWorkItem(async state =>
             {
                 False(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(10)));
                 True(ThreadPool.QueueUserWorkItem(ev => ev.Set(), are, false));
                 await rwLock.EnterWriteLock(InfiniteTimeSpan);
                 rwLock.ExitWriteLock();
                 task.SetResult(true);
             });
             are.WaitOne();
             rwLock.ExitWriteLock();
             await task.Task;
         }
 }
 public static void DisposeAsyncCompletedAsynchronously()
 {
     using var @lock = new AsyncReaderWriterLock();
     True(@lock.DisposeAsync().IsCompletedSuccessfully);
 }