📄 readerwriterlockslim.cs
字号:
{
int millisecondsTimeout = (int) timeout.TotalMilliseconds;
if ((millisecondsTimeout < -1) || (millisecondsTimeout > 0x7fffffff))
{
throw new ArgumentOutOfRangeException("timeout");
}
return this.TryEnterUpgradeableReadLock(millisecondsTimeout);
}
public bool TryEnterWriteLock(int millisecondsTimeout)
{
ReaderWriterCount lrwc;
int id = Thread.CurrentThread.get_ManagedThreadId();
bool flag = false;
if (!this.fIsReentrant)
{
if (id == this.writeLockOwnerId)
{
throw new LockRecursionException("LockRecursionException_RecursiveWriteNotAllowed");
}
if (id == this.upgradeLockOwnerId)
{
flag = true;
}
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, true);
if ((lrwc != null) && (lrwc.readercount > 0))
{
this.ExitMyLock();
throw new LockRecursionException("LockRecursionException_WriteAfterReadNotAllowed");
}
}
else
{
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, false);
if (id == this.writeLockOwnerId)
{
lrwc.rc.writercount++;
this.ExitMyLock();
return true;
}
if (id == this.upgradeLockOwnerId)
{
flag = true;
}
else if (lrwc.readercount > 0)
{
this.ExitMyLock();
throw new LockRecursionException("LockRecursionException_WriteAfterReadNotAllowed");
}
}
int spinCount = 0;
Label_00BC:
if (this.IsWriterAcquired())
{
this.SetWriterAcquired();
}
else
{
if (flag)
{
uint numReaders = this.GetNumReaders();
if (numReaders == 1)
{
this.SetWriterAcquired();
goto Label_01A5;
}
if ((numReaders == 2) && (lrwc != null))
{
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
if (lrwc.readercount > 0)
{
this.SetWriterAcquired();
goto Label_01A5;
}
}
}
if (spinCount < 20)
{
this.ExitMyLock();
if (millisecondsTimeout == 0)
{
return false;
}
spinCount++;
SpinWait(spinCount);
this.EnterMyLock();
goto Label_00BC;
}
if (flag)
{
if (this.waitUpgradeEvent != null)
{
if (!this.WaitOnEvent(this.waitUpgradeEvent, ref this.numWriteUpgradeWaiters, millisecondsTimeout))
{
return false;
}
}
else
{
this.LazyCreateEvent(ref this.waitUpgradeEvent, true);
}
goto Label_00BC;
}
if (this.writeEvent == null)
{
this.LazyCreateEvent(ref this.writeEvent, true);
goto Label_00BC;
}
if (this.WaitOnEvent(this.writeEvent, ref this.numWriteWaiters, millisecondsTimeout))
{
goto Label_00BC;
}
return false;
}
Label_01A5:
if (this.fIsReentrant)
{
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
lrwc.rc.writercount++;
}
this.ExitMyLock();
this.writeLockOwnerId = id;
return true;
}
public bool TryEnterWriteLock(TimeSpan timeout)
{
int millisecondsTimeout = (int) timeout.TotalMilliseconds;
if ((millisecondsTimeout < -1) || (millisecondsTimeout > 0x7fffffff))
{
throw new ArgumentOutOfRangeException("timeout");
}
return this.TryEnterWriteLock(millisecondsTimeout);
}
private bool WaitOnEvent(EventWaitHandle waitEvent, ref uint numWaiters, int millisecondsTimeout)
{
waitEvent.Reset();
numWaiters++;
this.fNoWaiters = false;
if (this.numWriteWaiters == 1)
{
this.SetWritersWaiting();
}
if (this.numWriteUpgradeWaiters == 1)
{
this.SetUpgraderWaiting();
}
bool flag = false;
this.ExitMyLock();
try
{
flag = waitEvent.WaitOne(millisecondsTimeout, false);
}
finally
{
this.EnterMyLock();
numWaiters--;
if (((this.numWriteWaiters == 0) && (this.numWriteUpgradeWaiters == 0)) && ((this.numUpgradeWaiters == 0) && (this.numReadWaiters == 0)))
{
this.fNoWaiters = true;
}
if (this.numWriteWaiters == 0)
{
this.ClearWritersWaiting();
}
if (this.numWriteUpgradeWaiters == 0)
{
this.ClearUpgraderWaiting();
}
if (!flag)
{
this.ExitMyLock();
}
}
return flag;
}
public int CurrentReadCount
{
get
{
int numReaders = (int) this.GetNumReaders();
if (this.upgradeLockOwnerId != -1)
{
return (numReaders - 1);
}
return numReaders;
}
}
public bool IsReadLockHeld
{
get
{
return (this.RecursiveReadCount > 0);
}
}
public bool IsUpgradeableReadLockHeld
{
get
{
return (this.RecursiveUpgradeCount > 0);
}
}
public bool IsWriteLockHeld
{
get
{
return (this.RecursiveWriteCount > 0);
}
}
private bool MyLockHeld
{
get
{
return (this.myLock != 0);
}
}
public LockRecursionPolicy RecursionPolicy
{
get
{
if (this.fIsReentrant)
{
return LockRecursionPolicy.SupportsRecursion;
}
return LockRecursionPolicy.NoRecursion;
}
}
public int RecursiveReadCount
{
get
{
int id = Thread.CurrentThread.get_ManagedThreadId();
int readercount = 0;
this.EnterMyLock();
ReaderWriterCount threadRWCount = this.GetThreadRWCount(id, true);
if (threadRWCount != null)
{
readercount = threadRWCount.readercount;
}
this.ExitMyLock();
return readercount;
}
}
public int RecursiveUpgradeCount
{
get
{
int id = Thread.CurrentThread.get_ManagedThreadId();
if (this.fIsReentrant)
{
int upgradecount = 0;
this.EnterMyLock();
ReaderWriterCount threadRWCount = this.GetThreadRWCount(id, true);
if (threadRWCount != null)
{
upgradecount = threadRWCount.rc.upgradecount;
}
this.ExitMyLock();
return upgradecount;
}
if (id == this.upgradeLockOwnerId)
{
return 1;
}
return 0;
}
}
public int RecursiveWriteCount
{
get
{
int id = Thread.CurrentThread.get_ManagedThreadId();
int writercount = 0;
if (this.fIsReentrant)
{
this.EnterMyLock();
ReaderWriterCount threadRWCount = this.GetThreadRWCount(id, true);
if (threadRWCount != null)
{
writercount = threadRWCount.rc.writercount;
}
this.ExitMyLock();
return writercount;
}
if (id == this.writeLockOwnerId)
{
return 1;
}
return 0;
}
}
public int WaitingReadCount
{
get
{
return (int) this.numReadWaiters;
}
}
public int WaitingUpgradeCount
{
get
{
return (int) this.numUpgradeWaiters;
}
}
public int WaitingWriteCount
{
get
{
return (int) this.numWriteWaiters;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -