System.Transactions.CheapUnfairReaderWriterLock.EnterReadLock C# (CSharp) Method

EnterReadLock() public method

public EnterReadLock ( ) : int
return int
        public int EnterReadLock()
        {
            int readerIndex = 0;
            do
            {
                if (_writerPresent)
                {
                    WriterFinishedEvent.WaitOne();
                }

                readerIndex = Interlocked.Increment(ref _readersIn);

                if (!_writerPresent)
                {
                    break;
                }

                Interlocked.Decrement(ref _readersIn);
            }
            while (true);

            return readerIndex;
        }

Usage Example

Example #1
0
        // Add a transaction to the table.  Transactions are added to the end of the list in sorted order based on their
        // absolute timeout.
        internal int Add(InternalTransaction txNew)
        {
            // Tell the runtime that we are modifying global state.
            int readerIndex = 0;

            readerIndex = _rwLock.EnterReadLock();
            try
            {
                // Start the timer if needed before checking the current time since the current
                // time can be more efficient with a running timer.
                if (txNew.AbsoluteTimeout != long.MaxValue)
                {
                    if (!_timerEnabled)
                    {
                        if (!_timer.Change(_timerInterval, _timerInterval))
                        {
                            throw TransactionException.CreateInvalidOperationException(
                                      SR.TraceSourceLtm,
                                      SR.UnexpectedTimerFailure,
                                      null
                                      );
                        }
                        _lastTimerTime = DateTime.UtcNow.Ticks;
                        _timerEnabled  = true;
                    }
                }
                txNew.CreationTime = CurrentTime;

                AddIter(txNew);
            }
            finally
            {
                _rwLock.ExitReadLock();
            }

            return(readerIndex);
        }