Ejemplo n.º 1
0
        ///<summary>Derived class overrides <c>OnEnter</c> to provide specific reader locking semantics.</summary>
        protected override void OnEnter(Boolean exclusive)
        {
            if (exclusive)
            {
                Interlocked.Increment(ref m_WriteRequests);
                Interlocked.Increment(ref m_WritersWaiting);
                Int64 startTime = Environment.TickCount;
                InnerLock.Enter(exclusive);

                // Only 1 thread is writing, so no thread safety is required here
                m_WriterMaxWaitTime = Math.Max(m_WriterMaxWaitTime, checked ((Int64)(Environment.TickCount - startTime)));
                Interlocked.Decrement(ref m_WritersWaiting);
                Interlocked.Increment(ref m_WritersWriting);
                m_WriterStartHoldTime = Environment.TickCount;
            }
            else
            {
                Interlocked.Increment(ref m_ReadRequests);
                Interlocked.Increment(ref m_ReadersWaiting);
                Int64 startTime = Environment.TickCount;
                InnerLock.Enter(exclusive);

                InterlockedEx.Max(ref m_ReaderMaxWaitTime, checked ((Int64)(Environment.TickCount - startTime)));
                Interlocked.Decrement(ref m_ReadersWaiting);
                Interlocked.Increment(ref m_ReadersReading);
                Monitor.Enter(m_ReaderStartHoldTime);
                m_ReaderStartHoldTime.Add(Thread.CurrentThread.ManagedThreadId, Environment.TickCount);
                Monitor.Exit(m_ReaderStartHoldTime);
            }
        }
Ejemplo n.º 2
0
        /// <summary>Implements the ResourceLock's WaitToWrite behavior.</summary>
        protected override void OnEnter(Boolean exclusive)
        {
            Int32 CallingThreadId = Thread.CurrentThread.ManagedThreadId;

            if (exclusive)
            {
                // If the calling thread already owns the lock, add 1 to the recursion count and return
                if (CallingThreadId == m_WriterThreadIdAndRecurseCount.m_Id)
                {
                    m_WriterThreadIdAndRecurseCount.m_Count++;
                    return;
                }
                InnerLock.Enter(exclusive);
                Interlocked.Exchange(ref m_WriterThreadIdAndRecurseCount.m_Id, CallingThreadId);
                m_WriterThreadIdAndRecurseCount.m_Count = 0;
            }
            else
            {
                Int32 index;
                if (TryFindThreadIdIndex(CallingThreadId, out index))
                {
                    // This thread has the reader lock, increment the count and return
                    m_ReaderThreadIdsAndRecurseCounts[index].m_Count++;
                    return;
                }

                // This thread doesn't have the lock, wait for it
                InnerLock.Enter(exclusive);

                // Record that this thread has the reader lock once
                AddThreadIdWithRecurseCountOf1(CallingThreadId);
            }
        }
        /// <summary>Implements the ResourceLock's Enter behavior.</summary>
        protected override void OnEnter(Boolean exclusive)
        {
            StackTrace st = new System.Diagnostics.StackTrace(0, true);

            using (new Timer(OnTimeout, st, m_timeout, -1)) {
                if (exclusive)
                {
                    InnerLock.Enter(exclusive);
                }
            }
        }
Ejemplo n.º 4
0
 protected override void OnEnter(Boolean exclusive)
 {
     if (exclusive)
     {
         InnerLock.Enter(exclusive);
         VerifyNoWriters("Writing while already writing!");
         VerifyNoReaders("Writing while already reading!");
         InterlockedEx.BitTestAndSet(ref m_LockState, 31);       // Add the writer
     }
     else
     {
         InnerLock.Enter(exclusive);
         VerifyNoWriters("Reading while already writing!"); // Sanity check for no writers
         Interlocked.Increment(ref m_LockState);            // Add a reader
     }
 }