Beispiel #1
0
        /// <summary> Acquires the read lock unless <see cref="System.Threading.Thread.Interrupt()"/> is called on the current thread</summary>
        /// <remarks>
        /// <p/>
        /// Acquires the read lock if the write lock is not held
        /// by another thread and returns immediately.
        ///
        /// <p/>
        /// If the write lock is held by another thread then the
        /// current thread becomes disabled for thread scheduling
        /// purposes and lies dormant until one of two things happens:
        ///
        /// <ul>
        /// <li>The read lock is acquired by the current thread</li>
        /// <li>Some other thread calls <see cref="System.Threading.Thread.Interrupt()"/> on the current thread.</li>
        /// </ul>
        ///
        /// <p/>If <see cref="System.Threading.Thread.Interrupt()"/> is called on the current thread,
        /// a <see cref="System.Threading.ThreadInterruptedException"/> is thrown
        ///
        /// <p/>
        /// In this implementation, as this method is an explicit
        /// interruption point, preference is given to responding to
        /// the interrupt over normal or reentrant acquisition of the
        /// lock.
        /// </remarks>
        /// <exception cref="System.Threading.ThreadInterruptedException">if the current thread is interrupted.</exception>
        public override IDisposable LockInterruptibly()
        {
            ThreadInterruptedException ie = null;

            lock (this)
            {
                if (!ReentrantReadWriteLock.StartReadFromNewReader())
                {
                    for (;;)
                    {
                        try
                        {
                            Monitor.Wait(this);
                            if (ReentrantReadWriteLock.StartReadFromWaitingReader())
                            {
                                return(this);
                            }
                        }
                        catch (ThreadInterruptedException ex)
                        {
                            ReentrantReadWriteLock.CancelWaitingReader();
                            ie = ex;
                            break;
                        }
                    }
                }
            }
            if (ie != null)
            {
                ReentrantReadWriteLock.SignallerWriterLock.SignalWaiters();
                throw ie;
            }
            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Acquires the read lock if the write lock is not held by
        /// another thread within the given waiting time and <see cref="System.Threading.Thread.Interrupt()"/>
        /// has not been called on  the current thread.
        /// </summary>
        /// <remarks>
        /// <p/>
        /// Acquires the read lock if the write lock is not held by
        /// another thread and returns immediately with the value
        /// <see lang="true"/>. If this lock has been set to use a fair
        /// ordering policy then an available lock <b>will not</b> be
        /// acquired if any other threads are waiting for the
        /// lock. This is in contrast to the <see cref="Spring.Threading.Locks.ReaderLock.TryLock()"/>
        /// method. If you want a timed <see cref="Spring.Threading.Locks.ReaderLock.TryLock()"/> that does
        /// permit barging on a fair lock then combine the timed and un-timed forms together:
        ///
        /// <code>
        /// if (lock.TryLock() || lock.TryLock(timespan) ) { ... }
        /// </code>
        ///
        /// <p/>
        /// If the write lock is held by another thread then the
        /// current thread becomes disabled for thread scheduling
        /// purposes and lies dormant until one of three things happens:
        ///
        /// <ul>
        ///
        /// <li>The read lock is acquired by the current thread</li>
        /// <li>Some other thread calls <see cref="System.Threading.Thread.Interrupt()"/> on the current thread</li>
        /// <li>The <paramref name="durationToWait"/> elapses</li>
        /// </ul>
        ///
        /// <p/>If the read lock is acquired then the value <see lang="true"/> is
        /// returned.
        ///
        /// <p/>
        /// If another thread calls <see cref="System.Threading.Thread.Interrupt()"/> on the current thread
        /// then an <see cref="System.Threading.ThreadInterruptedException"/> is thrown
        ///
        /// <p/>
        /// If the specified waiting time elapses then the value
        /// <see lang="false"/> is returned.  If the time is less than or
        /// equal to zero, the method will not wait at all.
        ///
        /// <p/>
        /// In this implementation, as this method is an explicit
        /// interruption point, preference is given to responding to
        /// the interrupt over normal or reentrant acquisition of the
        /// lock, and over reporting the elapse of the waiting time.
        /// </remarks>
        /// <param name="durationToWait">the <see cref="System.TimeSpan"/> to wait for the read lock</param>
        /// <returns> <see lang="true"/> if the read lock was acquired, <see lang="false"/> otherwise.</returns>
        /// <exception cref="System.Threading.ThreadInterruptedException">if the current thread is interrupted.</exception>
        public override bool TryLock(TimeSpan durationToWait)
        {
            ThreadInterruptedException ie = null;
            TimeSpan duration             = durationToWait;

            lock (this)
            {
                if (duration.TotalMilliseconds <= 0)
                {
                    return(ReentrantReadWriteLock.StartRead());
                }
                else if (ReentrantReadWriteLock.StartReadFromNewReader())
                {
                    return(true);
                }
                else
                {
                    DateTime deadline = DateTime.Now.Add(duration);
                    for (;;)
                    {
                        try
                        {
                            Monitor.Wait(this, durationToWait);
                        }
                        catch (ThreadInterruptedException ex)
                        {
                            ReentrantReadWriteLock.CancelWaitingReader();
                            ie = ex;
                            break;
                        }
                        if (ReentrantReadWriteLock.StartReadFromWaitingReader())
                        {
                            return(true);
                        }
                        else
                        {
                            if (deadline.Subtract(DateTime.Now).TotalMilliseconds <= 0)
                            {
                                ReentrantReadWriteLock.CancelWaitingReader();
                                break;
                            }
                        }
                    }
                }
            }
            ReentrantReadWriteLock.SignallerWriterLock.SignalWaiters();
            if (ie != null)
            {
                throw ie;
            }
            else
            {
                return(false);
            }
        }