[Mono-bugs] [Bug 55909][Wis] Changed - Deadlock in ReaderWriterLock.UpgradeToWriterLock()

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 12 Apr 2004 11:21:35 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by mono-bug@jerryweb.info.

http://bugzilla.ximian.com/show_bug.cgi?id=55909

--- shadow/55909	2004-04-11 16:49:16.000000000 -0400
+++ shadow/55909.tmp.14321	2004-04-12 11:21:35.000000000 -0400
@@ -223,6 +223,91 @@
 
 
 ------- Additional Comments From mono-bug@jerryweb.info  2004-04-11 16:49 -------
 Created an attachment (id=7314)
 Test case - next deadlocks...
 
+
+------- Additional Comments From mono-bug@jerryweb.info  2004-04-12 11:21 -------
+Deadlocks ar fixed by the attached patch (see next attachment) but
+unfortunately another bug appeared - see the foolowing code:
+
+======================================================
+using System;
+using System.Threading;
+using _lc = System.Threading.LockCookie;
+
+namespace RWLTest {
+   ///<summary></summary>
+   public class TestClass {
+      static ReaderWriterLock _rwl = new ReaderWriterLock();
+      static int _thCount = 0;
+      const int _timeout = -1;
+
+      [STAThread]
+      static void Main(string[] args) {
+
+         new Thread(new ThreadStart(Start)).Start();
+         Thread.Sleep(100);
+         new Thread(new ThreadStart(Start)).Start();
+         Thread.Sleep(100);
+         new Thread(new ThreadStart(Start)).Start();
+         Thread.Sleep(100);
+         new Thread(new ThreadStart(Start)).Start();
+      }
+
+      static void Start() {
+         int cnt = Interlocked.Increment(ref _thCount);
+         if(cnt == 2 || cnt == 4)
+            Reader(cnt);
+         else if(cnt == 3)
+            Writer(cnt);
+         else
+            ReaderWriter(cnt);
+      }
+
+      static void Reader(int cnt) {
+         _rwl.AcquireReaderLock(-1);
+         Console.WriteLine("Reader {0}", cnt);
+         Thread.Sleep(2000);
+         _rwl.ReleaseReaderLock();
+      }
+
+      static void ReaderWriter(int cnt) {
+         _rwl.AcquireReaderLock(-1);
+         _lc lc = _rwl.UpgradeToWriterLock(-1);
+         Console.WriteLine("ReaderWriter {0}", cnt);
+         Thread.Sleep(1000);
+         _rwl.DowngradeFromWriterLock(ref lc);
+         _rwl.ReleaseReaderLock();
+      }
+
+      static void Writer(int cnt) {
+         _rwl.AcquireWriterLock(-1);
+         Console.WriteLine("Writer {0}", cnt);
+         _rwl.ReleaseWriterLock();
+      }
+   }
+}
+======================================================
+
+Actual results:
+======================================================
+ReaderWriter 1
+Reader 2
+Writer 3
+Reader 4
+
+Unhandled Exception: System.ApplicationException: The thread does not
+have any reader or writer locks.
+in <0x00117> System.Threading.ReaderWriterLock:ReleaseReaderLock ()
+in <0x0005b> RWLTest.TestClass:Reader (int)
+in <0x0002d> RWLTest.TestClass:Start ()
+in <0x00050> (wrapper delegate-invoke)
+System.MulticastDelegate:invoke_void ()
+======================================================
+
+In the case that you eventually decide to use my version instead of
+bugfixing, you can find it at
+http://download.jerryweb.info/reader_writer_lock. I removed from the
+code the unnecessary parts. It has passed all the tests I made.
+