Ejemplo n.º 1
0
 public static async Task TrivialLock()
 {
     using (var rwLock = new AsyncReaderWriterLock())
     {
         //read lock
         True(await rwLock.TryEnterReadLock(TimeSpan.FromMilliseconds(10)));
         True(await rwLock.TryEnterReadLock(TimeSpan.FromMilliseconds(10)));
         False(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitReadLock();
         False(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitReadLock();
         //write lock
         True(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(20)));
         False(await rwLock.TryEnterReadLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitWriteLock();
         //upgradeable read lock
         True(await rwLock.TryEnterReadLock(TimeSpan.FromMilliseconds(10)));
         True(await rwLock.TryEnterReadLock(TimeSpan.FromMilliseconds(10)));
         True(await rwLock.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(20)));
         False(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(20)));
         False(await rwLock.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitUpgradeableReadLock();
         False(await rwLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(20)));
         True(await rwLock.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitReadLock();
         False(await rwLock.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(20)));
         rwLock.ExitReadLock();
         rwLock.ExitUpgradeableReadLock();
     }
 }
Ejemplo n.º 2
0
        public static async Task InvalidExits()
        {
            using (var rwLock = new AsyncReaderWriterLock())
            {
                Throws <SynchronizationLockException>(rwLock.ExitReadLock);
                Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
                Throws <SynchronizationLockException>(rwLock.ExitWriteLock);

                await rwLock.EnterReadLock(TimeSpan.FromMilliseconds(10));

                Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
                Throws <SynchronizationLockException>(rwLock.ExitWriteLock);
                rwLock.ExitReadLock();

                await rwLock.EnterUpgradeableReadLock(TimeSpan.FromMilliseconds(10));

                Throws <SynchronizationLockException>(rwLock.ExitReadLock);
                Throws <SynchronizationLockException>(rwLock.ExitWriteLock);
                rwLock.ExitUpgradeableReadLock();

                await rwLock.EnterWriteLock(TimeSpan.FromMilliseconds(10));

                Throws <SynchronizationLockException>(rwLock.ExitReadLock);
                Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
                rwLock.ExitWriteLock();
            }
        }
Ejemplo n.º 3
0
        public static async Task WriterToUpgradeableReaderChain()
        {
            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.TryEnterUpgradeableReadLockAsync(TimeSpan.FromMilliseconds(10)));
                True(ThreadPool.QueueUserWorkItem(ev => ev.Set(), are, false));
                await rwLock.EnterUpgradeableReadLockAsync(DefaultTimeout);
                rwLock.ExitUpgradeableReadLock();
                task.SetResult(true);
            });
            are.WaitOne(DefaultTimeout);
            rwLock.ExitWriteLock();
            await task.Task;
        }
        public static void GracefulShutdown4()
        {
            using var @lock = new AsyncReaderWriterLock();
            True(@lock.TryEnterWriteLock());
            var acquisition1 = @lock.EnterUpgradeableReadLockAsync(CancellationToken.None);

            False(acquisition1.IsCompleted);
            var task = @lock.DisposeAsync();

            False(task.IsCompleted);
            var acquisition2 = @lock.EnterReadLockAsync(CancellationToken.None);

            False(task.IsCompleted);

            @lock.ExitWriteLock();
            True(acquisition1.IsCompletedSuccessfully);
            False(acquisition2.IsCompleted);
            False(task.IsCompleted);

            @lock.ExitUpgradeableReadLock();
            True(acquisition2.IsFaulted);
            True(task.IsCompletedSuccessfully);
            Throws <ObjectDisposedException>(acquisition2.GetAwaiter().GetResult);
        }