Beispiel #1
0
        public void Lock_Timeout()
        {
            var mutex = new AsyncMutex();

            mutex.Lock();

            var start        = Environment.TickCount;
            var capturedLock = mutex.Lock(100);

            Assert.False(capturedLock);
            Assert.True(Environment.TickCount - start >= 90);
        }
Beispiel #2
0
        public void Lock_CancellationToken()
        {
            var mutex = new AsyncMutex();

            mutex.Lock();

            var start = Environment.TickCount;

            using (var cancellationTokenSource = new CancellationTokenSource(100))
            {
                Assert.Throws <OperationCanceledException>(()
                                                           => mutex.Lock(cancellationTokenSource.Token));
                Assert.True(Environment.TickCount - start >= 90);
            }
        }
Beispiel #3
0
 public static async Task RunAsync()
 {
     using (await _asyncMutex.Lock())
     {
         _commonData.Add(42);
     }
 }
Beispiel #4
0
        public void Lock()
        {
            var mutex = new AsyncMutex();

            mutex.Lock();
            mutex.Unlock();
        }
Beispiel #5
0
        public void LockAndUnlock()
        {
            var mutex = new AsyncMutex();

            using (mutex.LockAndUnlock())
            {
                Assert.False(mutex.Lock(0));
            }
        }
Beispiel #6
0
 private async Task ReturnIndex()
 {
     using (var l = await _mutex.Lock())
     {
         if (_pool != null)
         {
             _pool.ReturnIndex(Value);
             _pool = null;
         }
     }
 }
Beispiel #7
0
        public async Task LockAsync_Timeout()
        {
            var mutex = new AsyncMutex();

            mutex.Lock();

            var start        = Environment.TickCount;
            var capturedLock = await mutex.LockAsync(100);

            Assert.False(capturedLock);
            Assert.True(Environment.TickCount - start >= 90);
        }
Beispiel #8
0
        public async Task MutualExclusion()
        {
            int sharedValue  = 0;
            var mutex        = new AsyncMutex();
            var hundredEvent = new AsyncManualResetEvent();

            // Create 10 threads that increment a value 10 times each
            for (int n = 0; n < 10; n++)
            {
                new Thread(async() =>
                {
                    await mutex.Lock();

                    int privateValue = sharedValue;
                    for (int m = 0; m < 10; m++)
                    {
                        // If the mutex works, no other thread will increment sharedValue
                        sharedValue++;
                        privateValue++;
                        Assert.Equal(privateValue, sharedValue);

                        if (sharedValue == 100)
                        {
                            // The test case is complete
                            hundredEvent.Set();
                        }

                        // Yield the CPU to give other threads a chance to run
                        await Task.Delay(10);
                    }

                    mutex.Unlock();
                }).Start();
            }

            await hundredEvent.WaitAsync();

            Assert.Equal(100, sharedValue);
        }