//
using System.Collections;
-
-#if NET_2_0
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
-#endif
+
namespace System.Threading
{
-#if NET_2_0
[ComVisible (true)]
public sealed class ReaderWriterLock: CriticalFinalizerObject
-#else
- public sealed class ReaderWriterLock
-#endif
{
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 ();
-#if NET_2_0
GC.SuppressFinalize (this);
-#endif
}
-#if NET_2_0
- [MonoTODO]
~ReaderWriterLock ()
{
}
-#endif
public bool IsReaderLockHeld {
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
get {
lock (this) return reader_locks.ContainsKey (Thread.CurrentThreadId);
}
}
public bool IsWriterLockHeld {
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
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;
}
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
public void ReleaseReaderLock()
{
lock (this) {
writer_queue.Pulse ();
}
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
public void ReleaseWriterLock()
{
lock (this) {