📄 readerwriterlockslim.cs
字号:
if (this.rwc[index].threadid == id)
{
return this.rwc[index];
}
if (IsRWEntryEmpty(this.rwc[index]) && !DontAllocate)
{
if (this.rwc[index].next == null)
{
this.rwc[index].threadid = id;
return this.rwc[index];
}
count2 = this.rwc[index];
}
for (rwc = this.rwc[index].next; rwc != null; rwc = rwc.next)
{
if (rwc.threadid == id)
{
return rwc;
}
if ((count2 == null) && IsRWEntryEmpty(rwc))
{
count2 = rwc;
}
}
if (DontAllocate)
{
return null;
}
if (count2 == null)
{
rwc = new ReaderWriterCount(this.fIsReentrant);
rwc.threadid = id;
rwc.next = this.rwc[index].next;
this.rwc[index].next = rwc;
return rwc;
}
count2.threadid = id;
return count2;
}
private void InitializeThreadCounts()
{
this.rwc = new ReaderWriterCount[0x100];
for (int i = 0; i < this.rwc.Length; i++)
{
this.rwc[i] = new ReaderWriterCount(this.fIsReentrant);
}
this.upgradeLockOwnerId = -1;
this.writeLockOwnerId = -1;
}
private static bool IsRWEntryEmpty(ReaderWriterCount rwc)
{
if (rwc.threadid == -1)
{
return true;
}
if ((rwc.readercount == 0) && (rwc.rc == null))
{
return true;
}
if ((rwc.readercount == 0) && (rwc.rc.writercount == 0))
{
return (rwc.rc.upgradecount == 0);
}
return false;
}
private static bool IsRwHashEntryChanged(ReaderWriterCount lrwc, int id)
{
return (lrwc.threadid != id);
}
private bool IsWriterAcquired()
{
return ((this.owners & 0xbfffffff) == 0);
}
private void LazyCreateEvent(ref EventWaitHandle waitEvent, bool makeAutoResetEvent)
{
EventWaitHandle handle;
this.ExitMyLock();
if (makeAutoResetEvent)
{
handle = (EventWaitHandle) new AutoResetEvent(false);
}
else
{
handle = (EventWaitHandle) new ManualResetEvent(false);
}
this.EnterMyLock();
if (waitEvent == null)
{
waitEvent = handle;
}
else
{
handle.Close();
}
}
private void SetUpgraderWaiting()
{
this.owners |= 0x20000000;
}
private void SetWriterAcquired()
{
this.owners |= 0x80000000;
}
private void SetWritersWaiting()
{
this.owners |= 0x40000000;
}
private static void SpinWait(int SpinCount)
{
if ((SpinCount < 5) && (Environment.get_ProcessorCount() > 1))
{
Thread.SpinWait(20 * SpinCount);
}
else if (SpinCount < 0x11)
{
Thread.Sleep(0);
}
else
{
Thread.Sleep(1);
}
}
public bool TryEnterReadLock(int millisecondsTimeout)
{
ReaderWriterCount lrwc = null;
int id = Thread.CurrentThread.get_ManagedThreadId();
if (!this.fIsReentrant)
{
if (id == this.writeLockOwnerId)
{
throw new LockRecursionException("LockRecursionException_ReadAfterWriteNotAllowed");
}
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, false);
if (lrwc.readercount > 0)
{
this.ExitMyLock();
throw new LockRecursionException("LockRecursionException_RecursiveReadNotAllowed");
}
if (id == this.upgradeLockOwnerId)
{
lrwc.readercount++;
this.owners++;
this.ExitMyLock();
return true;
}
}
else
{
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, false);
if (lrwc.readercount > 0)
{
lrwc.readercount++;
this.ExitMyLock();
return true;
}
if (id == this.upgradeLockOwnerId)
{
lrwc.readercount++;
this.owners++;
this.ExitMyLock();
this.fUpgradeThreadHoldingRead = true;
return true;
}
if (id == this.writeLockOwnerId)
{
lrwc.readercount++;
this.owners++;
this.ExitMyLock();
return true;
}
}
bool flag = true;
int spinCount = 0;
Label_0115:
if (this.owners < 0xffffffe)
{
this.owners++;
lrwc.readercount++;
}
else
{
if (spinCount < 20)
{
this.ExitMyLock();
if (millisecondsTimeout == 0)
{
return false;
}
spinCount++;
SpinWait(spinCount);
this.EnterMyLock();
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
}
else if (this.readEvent == null)
{
this.LazyCreateEvent(ref this.readEvent, false);
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
}
else
{
flag = this.WaitOnEvent(this.readEvent, ref this.numReadWaiters, millisecondsTimeout);
if (!flag)
{
return false;
}
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
}
goto Label_0115;
}
this.ExitMyLock();
return flag;
}
public bool TryEnterReadLock(TimeSpan timeout)
{
int millisecondsTimeout = (int) timeout.TotalMilliseconds;
if ((millisecondsTimeout < -1) || (millisecondsTimeout > 0x7fffffff))
{
throw new ArgumentOutOfRangeException("timeout");
}
return this.TryEnterReadLock(millisecondsTimeout);
}
public bool TryEnterUpgradeableReadLock(int millisecondsTimeout)
{
ReaderWriterCount lrwc;
int id = Thread.CurrentThread.get_ManagedThreadId();
if (!this.fIsReentrant)
{
if (id == this.upgradeLockOwnerId)
{
throw new LockRecursionException("LockRecursionException_RecursiveUpgradeNotAllowed");
}
if (id == this.writeLockOwnerId)
{
throw new LockRecursionException("LockRecursionException_UpgradeAfterWriteNotAllowed");
}
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, true);
if ((lrwc != null) && (lrwc.readercount > 0))
{
this.ExitMyLock();
throw new LockRecursionException("LockRecursionException_UpgradeAfterReadNotAllowed");
}
}
else
{
this.EnterMyLock();
lrwc = this.GetThreadRWCount(id, false);
if (id == this.upgradeLockOwnerId)
{
lrwc.rc.upgradecount++;
this.ExitMyLock();
return true;
}
if (id == this.writeLockOwnerId)
{
this.owners++;
this.upgradeLockOwnerId = id;
lrwc.rc.upgradecount++;
if (lrwc.readercount > 0)
{
this.fUpgradeThreadHoldingRead = true;
}
this.ExitMyLock();
return true;
}
if (lrwc.readercount > 0)
{
this.ExitMyLock();
throw new LockRecursionException("LockRecursionException_UpgradeAfterReadNotAllowed");
}
}
int spinCount = 0;
Label_0105:
if ((this.upgradeLockOwnerId == -1) && (this.owners < 0xffffffe))
{
this.owners++;
this.upgradeLockOwnerId = id;
}
else
{
if (spinCount < 20)
{
this.ExitMyLock();
if (millisecondsTimeout == 0)
{
return false;
}
spinCount++;
SpinWait(spinCount);
this.EnterMyLock();
goto Label_0105;
}
if (this.upgradeEvent == null)
{
this.LazyCreateEvent(ref this.upgradeEvent, true);
goto Label_0105;
}
if (this.WaitOnEvent(this.upgradeEvent, ref this.numUpgradeWaiters, millisecondsTimeout))
{
goto Label_0105;
}
return false;
}
if (this.fIsReentrant)
{
if (IsRwHashEntryChanged(lrwc, id))
{
lrwc = this.GetThreadRWCount(id, false);
}
lrwc.rc.upgradecount++;
}
this.ExitMyLock();
return true;
}
public bool TryEnterUpgradeableReadLock(TimeSpan timeout)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -