Mercurial > pub > ImplabNet
comparison Implab.Test/AsyncTests.cs @ 136:e9e7940c7d98 v2
shared locks + tests
author | cin |
---|---|
date | Mon, 16 Feb 2015 01:14:09 +0300 |
parents | f803565868a4 |
children | f75cfa58e3d4 |
comparison
equal
deleted
inserted
replaced
135:656815cb7147 | 136:e9e7940c7d98 |
---|---|
772 p.Join(); | 772 p.Join(); |
773 } catch (OperationCanceledException) { | 773 } catch (OperationCanceledException) { |
774 Assert.IsTrue(pSurvive.Join()); | 774 Assert.IsTrue(pSurvive.Join()); |
775 } | 775 } |
776 } | 776 } |
777 | |
778 [TestMethod] | |
779 public void SharedLockTest() { | |
780 var l = new SharedLock(); | |
781 int shared = 0; | |
782 int exclusive = 0; | |
783 var s1 = new Signal(); | |
784 var log = new AsyncQueue<string>(); | |
785 | |
786 try { | |
787 AsyncPool.RunThread( | |
788 () => { | |
789 log.Enqueue("Reader #1 started"); | |
790 try { | |
791 l.LockShared(); | |
792 log.Enqueue("Reader #1 lock got"); | |
793 if (Interlocked.Increment(ref shared) == 2) | |
794 s1.Set(); | |
795 s1.Wait(); | |
796 log.Enqueue("Reader #1 finished"); | |
797 Interlocked.Decrement(ref shared); | |
798 } finally { | |
799 l.Release(); | |
800 log.Enqueue("Reader #1 lock released"); | |
801 } | |
802 }, | |
803 () => { | |
804 log.Enqueue("Reader #2 started"); | |
805 | |
806 try { | |
807 l.LockShared(); | |
808 log.Enqueue("Reader #2 lock got"); | |
809 | |
810 if (Interlocked.Increment(ref shared) == 2) | |
811 s1.Set(); | |
812 s1.Wait(); | |
813 log.Enqueue("Reader #2 upgrading to writer"); | |
814 Interlocked.Decrement(ref shared); | |
815 l.Upgrade(); | |
816 log.Enqueue("Reader #2 upgraded"); | |
817 | |
818 Assert.AreEqual(1, Interlocked.Increment(ref exclusive)); | |
819 Assert.AreEqual(0, shared); | |
820 log.Enqueue("Reader #2 finished"); | |
821 Interlocked.Decrement(ref exclusive); | |
822 } finally { | |
823 l.Release(); | |
824 log.Enqueue("Reader #2 lock released"); | |
825 } | |
826 }, | |
827 () => { | |
828 log.Enqueue("Writer #1 started"); | |
829 try { | |
830 l.LockExclusive(); | |
831 log.Enqueue("Writer #1 got the lock"); | |
832 Assert.AreEqual(1, Interlocked.Increment(ref exclusive)); | |
833 Interlocked.Decrement(ref exclusive); | |
834 log.Enqueue("Writer #1 is finished"); | |
835 } finally { | |
836 l.Release(); | |
837 log.Enqueue("Writer #1 lock released"); | |
838 } | |
839 } | |
840 ).Bundle().Join(1000); | |
841 log.Enqueue("Done"); | |
842 } catch(Exception error) { | |
843 log.Enqueue(error.Message); | |
844 throw; | |
845 } finally { | |
846 foreach (var m in log) | |
847 Console.WriteLine(m); | |
848 } | |
849 } | |
777 } | 850 } |
778 } | 851 } |
779 | 852 |