Exemple #1
0
        public void LockAndLoadAsync_WaitsForLock_Timeout_NoThrow()
        {
            const int    numberOfLocks = 512;
            const string key           = "key1";

            multiLock = new MultiLock(numberOfLocks, 10, false, doEnableReentrantLocking: false);

            long[] entered = { 0L };
            long[] locked  = { 0L };

            multiLock.Take(key);

            var lockNumber = GetLockNumber(key);
            var lck        = GetLock(lockNumber);

            try
            {
                #pragma warning disable 1998
                var _ = multiLock.LockAndLoadAsync(key, async() =>
                {
                    var result = Interlocked.Increment(ref entered[0]);
                    var ln     = GetLockNumber(key);
                    var l      = GetLock(ln);
                    if (l.CurrentCount > 0)
                    {
                        Interlocked.Increment(ref locked[0]);
                    }
                    return(result);
                });
                _.Wait();
                #pragma warning restore 1998
            }
            finally
            {
                var lockCount = lck.CurrentCount;

                multiLock.Release(key);

                Assert.IsTrue(Interlocked.Read(ref entered[0]) != 0L, "Should have timed out and entered");
                Assert.IsTrue(lockCount == 0, "Lock should have been taken");
                Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released");;
            }
        }
Exemple #2
0
        private async Task <long> ExternalIncPosZero(MultiLock locks, string key, long[] counter)
        {
            await locks.LockAndLoadAsync(key, async() => await InternalIncPosZero(counter));

            return(Interlocked.Increment(ref counter[0]));
        }