예제 #1
0
        // NOTE: All locks must implement the WaitToWrite/DoneWriting methods
        ///<summary>Allows the calling thread to acquire the lock for writing or reading.</summary>
        /// <param name="exclusive">true if the thread wishes to acquire the lock for exclusive access.</param>
        public void Enter(Boolean exclusive)
        {
#if DEADLOCK_DETECTION
            if (exclusive)
            {
                if (AcquiringThreadMustRelease)
                {
                    Thread.BeginCriticalRegion();
                }
                using (s_PerformDeadlockDetection ? DeadlockDetector.BlockForLock(this, true) : null) {
                    OnEnter(exclusive);
                }
            }
            else
            {
                // When reading, there is no need to call BeginCriticalRegion since resource is not being modified
                using (s_PerformDeadlockDetection ? DeadlockDetector.BlockForLock(this, IsMutualExclusive) : null) {
                    OnEnter(exclusive);
                }
            }
#else
            OnEnter(exclusive);
#endif
            Interlocked.Add(ref m_readWriteCounts, exclusive ? c_OneWriterCount : c_OneReaderCount);
        }
예제 #2
0
        ///<summary>Allows the calling thread to release the lock.</summary>
        public void Leave()
        {
            Contract.Assume(!CurrentlyFree());
            Boolean exclusive = CurrentReaderCount() == 0;

#if DEADLOCK_DETECTION
            if (s_PerformDeadlockDetection)
            {
                DeadlockDetector.ReleaseLock(this);
            }
#endif
            OnLeave(exclusive);
            if (exclusive)
            {
                Interlocked.Add(ref m_readWriteCounts, -c_OneWriterCount);
                //if (AcquiringThreadMustRelease) Thread.EndCriticalRegion();
            }
            else
            {
                Interlocked.Add(ref m_readWriteCounts, -c_OneReaderCount);
                // When done reading, there is no need to call EndCriticalRegion since resource was not modified
            }
        }