public void Lock() { lock (rwLock.internalLock) { if (rwLock.IsCounted(rwLock.threadToReadCountDict)) { rwLock.IncrementCounter(rwLock.threadToReadCountDict); return; } if (rwLock.IsCounted(rwLock.threadToWriteCountDict)) { rwLock.IncrementCounter(rwLock.threadToReadCountDict); return; } while (rwLock.threadToWriteCountDict.Count > 0) { Monitor.Wait(rwLock.internalLock); } rwLock.IncrementCounter(rwLock.threadToReadCountDict); } }
public void Lock() { lock (rwLock.internalLock) { if (rwLock.IsCounted(rwLock.threadToWriteCountDict)) { rwLock.IncrementCounter(rwLock.threadToWriteCountDict); return; } if (rwLock.IsCounted(rwLock.threadToReadCountDict)) { throw new NotSupportedException("It is not allowed to aquire a write lock while maintaining a readlock"); } while (rwLock.threadToWriteCountDict.Count > 0) { Monitor.Wait(rwLock.internalLock); } rwLock.IncrementCounter(rwLock.threadToWriteCountDict); while (rwLock.threadToReadCountDict.Count > 0) { Monitor.Wait(rwLock.internalLock); } } }