Example #1
0
    public void GlobalLockTest()
    {
        GlobalLock k = new GlobalLock("test_lock1");

        Event   startEvent = new Event(true);
        Event   event2     = new Event(true);
        Event   event3     = new Event(true);
        RefBool fail       = new RefBool();

        var thread1 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                using (k.Lock())
                {
                    event2.Set();

                    if (event3.Wait(100))
                    {
                        fail.Set(true);
                    }
                }
            }
            catch
            {
                fail.Set(true);
            }
        });

        var thread2 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                event2.Wait();

                using (k.Lock())
                {
                    event3.Set();
                }
            }
            catch
            {
                fail.Set(true);
            }
        });

        startEvent.Set();

        Assert.True(thread1.WaitForEnd(5000));
        Assert.True(thread2.WaitForEnd(5000));

        Assert.False(fail.Value);
    }
Example #2
0
 public void GlobalLock_Multiple()
 {
     using (GlobalLock lock1 = new GlobalLock("a"),
            lock2 = new GlobalLock("b"))
     {
         lock1.Lock();
         lock2.Lock();
         lock2.Release();
         lock1.Release();
     }
 }
Example #3
0
        public void GlobalLock_Lock()
        {
            using (GlobalLock lock1 = new GlobalLock("a"),
                   lock2 = new GlobalLock("a"))
            {
                Assert.IsFalse(lock1.IsHeld);
                Assert.IsFalse(lock2.IsHeld);

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);
                Assert.IsFalse(lock2.IsHeld);
                lock1.Release();
                Assert.IsFalse(lock1.IsHeld);

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);

                try
                {
                    lock2.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);

                lock1.Release();
                Assert.IsTrue(lock1.IsHeld);

                try
                {
                    lock2.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock1.Release();
                Assert.IsFalse(lock1.IsHeld);

                lock2.Lock();
                Assert.IsTrue(lock2.IsHeld);

                try
                {
                    lock1.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock2.Release();
                Assert.IsFalse(lock2.IsHeld);
            }
        }
 public override void AcquireWriteLock()
 {
     Turnstile.Lock();
     GlobalLock.Lock();
 }