[corlib] Import System.Threading.WaitHandle
[mono.git] / mcs / class / corlib / System.Threading / ReaderWriterLock.cs
index 74e554031be7365dcb68e254af24f24633cf72fb..2103cb89717aad1a9aa082b42d73ebc97090fdc4 100644 (file)
 //
 
 using System.Collections;
+using System.Runtime.InteropServices;
+using System.Runtime.ConstrainedExecution;
+
 
 namespace System.Threading
 {
-       public sealed class ReaderWriterLock
+       [ComVisible (true)]
+       public sealed class ReaderWriterLock: CriticalFinalizerObject
        {
                private int seq_num = 1;
                private int state;
                private int readers;
+               private int writer_lock_owner;
                private LockQueue writer_queue;
                private Hashtable reader_locks;
-               private int writer_lock_owner;
 
                public ReaderWriterLock()
                {
                        writer_queue = new LockQueue (this);
                        reader_locks = new Hashtable ();
+
+                       GC.SuppressFinalize (this);
+               }
+
+               ~ReaderWriterLock ()
+               {
                }
 
                public bool IsReaderLockHeld {
+                       [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                        get {
                                lock (this) return reader_locks.ContainsKey (Thread.CurrentThreadId);
                        }
                }
 
                public bool IsWriterLockHeld {
+                       [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                        get {
                                lock (this) return (state < 0 && Thread.CurrentThreadId == writer_lock_owner);
                        }
@@ -161,11 +173,15 @@ namespace System.Threading
                        lock (this) {
                                if (!HasWriterLock())
                                        throw new ApplicationException ("The thread does not have the writer lock.");
-                               
-                               state = lockCookie.ReaderLocks;
-                               reader_locks [Thread.CurrentThreadId] = state;
-                               if (readers > 0) {
-                                       Monitor.PulseAll (this);
+
+                               if (lockCookie.WriterLocks != 0)
+                                       state++;
+                               else {
+                                       state = lockCookie.ReaderLocks;
+                                       reader_locks [Thread.CurrentThreadId] = state;
+                                       if (readers > 0) {
+                                               Monitor.PulseAll (this);
+                                       }
                                }
                                
                                // MSDN: A thread does not block when downgrading from the writer lock, 
@@ -187,6 +203,7 @@ namespace System.Threading
                        return cookie;
                }
                
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public void ReleaseReaderLock()
                {
                        lock (this) {
@@ -220,6 +237,7 @@ namespace System.Threading
                                writer_queue.Pulse ();
                }
 
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public void ReleaseWriterLock()
                {
                        lock (this) {