Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
0
        public static async Task WriterToUpgradeableReaderChain()
        {
            using (var are = new AutoResetEvent(false))
                using (var rwLock = new AsyncReaderWriterLock())
                {
                    await rwLock.EnterWriteLock(InfiniteTimeSpan);

                    var task = new TaskCompletionSource <bool>();
                    ThreadPool.QueueUserWorkItem(async state =>
                    {
                        False(await rwLock.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(10)));
                        True(ThreadPool.QueueUserWorkItem(ev => ev.Set(), are, false));
                        await rwLock.EnterUpgradeableReadLock(InfiniteTimeSpan);
                        rwLock.ExitUpgradeableReadLock();
                        task.SetResult(true);
                    });
                    are.WaitOne();
                    rwLock.ExitWriteLock();
                    await task.Task;
                }
        }