public UncheckedReaderWriterLock() { this.sentinelToken = new WaitingToken(); this.innerLock = new LightweightSpinLock(); this.id = Interlocked.Increment(ref counter); this.readerCount = 0; this.writerPending = 0; }
internal Transaction(System.Transactions.Transaction transaction, bool isAmbient) { this.internalTransaction = transaction; this.isAmbient = isAmbient; this.transactionId = Interlocked.Increment(ref transactionCounter); this.atomicSectionLock = new LightweightSpinLock(); this.isolationLevel = this.MapIsolationLevel(transaction.IsolationLevel); this.aborted = false; this.registeredHandlers = new HashSet<ITransactionHandler>(); this.internalTransaction.EnlistVolatile(this, System.Transactions.EnlistmentOptions.EnlistDuringPrepareRequired); }
private void WaitForWrite(WaitingToken wait) { int retry = 0; while (true) { // Check if out token is the next and all readers has left if (Interlocked.CompareExchange(ref this.sentinelToken.Next, wait, wait) == wait && Interlocked.CompareExchange(ref this.readerCount, 0, 0) == 0) { return; } LightweightSpinLock.SpinWait(ref retry); } }
public void Enter() { if (Interlocked.CompareExchange(ref this.held, 1, 0) == 0) { return; } int retry = 0; while (true) { LightweightSpinLock.SpinWait(ref retry); if ((this.held == 0) && (Interlocked.CompareExchange(ref this.held, 1, 0) == 0)) { return; } } }
public void EnterRead() { int retry = 0; while (true) { if (Interlocked.CompareExchange(ref this.writerPending, 0, 0) == 0) { this.innerLock.Enter(); if (Interlocked.CompareExchange(ref this.writerPending, 0, 0) == 0) { Interlocked.Increment(ref this.readerCount); } this.innerLock.Exit(); break; } LightweightSpinLock.SpinWait(ref retry); } }