//
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);
}
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,
return cookie;
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void ReleaseReaderLock()
{
lock (this) {
writer_queue.Pulse ();
}
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public void ReleaseWriterLock()
{
lock (this) {