Beispiel #1
0
        public void AwaitUninterruptibly()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition            c      = myLock.WriterLock.NewCondition();
            UninterruptableThread thread = new UninterruptableThread(myLock.WriterLock, c);

            thread.InternalThread.Start();

            while (!thread.lockStarted)
            {
                Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100));
            }

            myLock.WriterLock.Lock();
            try
            {
                thread.InternalThread.Interrupt();
                thread.canAwake = true;
                c.Signal();
            }
            finally
            {
                myLock.WriterLock.Unlock();
            }

            thread.InternalThread.Join();
            Assert.IsTrue(thread.interrupted);
            Assert.IsFalse(thread.InternalThread.IsAlive);
        }
Beispiel #2
0
        public void Lock()
        {
            ReentrantReadWriteLock rl = new ReentrantReadWriteLock();

            rl.WriterLock.Lock();
            Assert.IsTrue(rl.IsWriteLockHeld);
            Assert.IsTrue(rl.WriterLockedByCurrentThread);
//        assertTrue(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
            Assert.AreEqual(0, rl.ReadLockCount);
            rl.WriterLock.Unlock();
            Assert.IsFalse(rl.IsWriteLockHeld);
            Assert.IsFalse(rl.WriterLockedByCurrentThread);
//        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
            Assert.AreEqual(0, rl.ReadLockCount);
            rl.ReaderLock.Lock();
            Assert.IsFalse(rl.IsWriteLockHeld);
            Assert.IsFalse(rl.WriterLockedByCurrentThread);
//        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
            Assert.AreEqual(1, rl.ReadLockCount);
            rl.ReaderLock.Unlock();
            Assert.IsFalse(rl.IsWriteLockHeld);
            Assert.IsFalse(rl.WriterLockedByCurrentThread);
//        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
            Assert.AreEqual(0, rl.ReadLockCount);
        }
        public void AwaitUninterruptibly()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            UninterruptableThread thread = new UninterruptableThread(myLock.WriterLock, c);

            thread.InternalThread.Start();

            while (!thread.lockStarted)
            {
                Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100));
            }

            myLock.WriterLock.Lock();
            try
            {
                thread.InternalThread.Interrupt();
                thread.canAwake = true;
                c.Signal();
            }
            finally
            {
                myLock.WriterLock.Unlock();
            }

            thread.InternalThread.Join();
            Assert.IsTrue(thread.interrupted);
            Assert.IsFalse(thread.InternalThread.IsAlive);
        }
Beispiel #4
0
        public void GetQueueLength()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
            Thread t1 = new Thread(new InterruptedLockRunnable(myLock).Run);
            Thread t2 = new Thread(new InterruptibleLockRunnable(myLock).Run);

            Assert.AreEqual(0, myLock.QueueLength);
            myLock.WriterLock.Lock();
            t1.Start();

            Thread.Sleep(new TimeSpan(10000 * Delays.Short.Milliseconds));
            Assert.AreEqual(1, myLock.QueueLength);
            t2.Start();

            Thread.Sleep(new TimeSpan(10000 * Delays.Short.Milliseconds));
            Assert.AreEqual(2, myLock.QueueLength);
            t1.Interrupt();

            Thread.Sleep(new TimeSpan(10000 * Delays.Short.Milliseconds));
            Assert.AreEqual(1, myLock.QueueLength);
            myLock.WriterLock.Unlock();

            Thread.Sleep(new TimeSpan(10000 * Delays.Short.Milliseconds));
            Assert.AreEqual(0, myLock.QueueLength);
            t1.Join();
            t2.Join();
        }
Beispiel #5
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 #6
0
        public void Constructor()
        {
            ReentrantReadWriteLock rl = new ReentrantReadWriteLock();

            Assert.IsFalse(rl.IsFair);
            Assert.IsFalse(rl.IsWriteLockHeld);
            Assert.AreEqual(0, rl.ReadLockCount);
        }
Beispiel #7
0
        public void Await_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();

            myLock.WriterLock.Lock();
            myLock.WriterLock.Unlock();
        }
Beispiel #8
0
        /// <summary>
        /// Acquires the write lock if it 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>
        /// Acquires the write lock if neither the read nor write lock
        /// are held by another thread
        /// and returns immediately with the value <see lang="true"/>,
        /// setting the write lock hold count to one. 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 write lock. This is in contrast to the <see cref="Spring.Threading.Locks.WriterLock.TryLock()"/>
        /// If you want a timed <see cref="Spring.Threading.Locks.WriterLock.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 current thread already holds this lock then the
        /// hold count is incremented by one and the method returns
        /// <see lang="true"/>.
        ///
        /// <p/>
        /// If the 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 write 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 specified <see cref="System.TimeSpan"/> elapses</li>
        /// </ul>
        ///
        /// <p/>
        /// If the write lock is acquired then the value <see lang="true"/> is
        /// returned and the write lock hold count is set to one.
        ///
        /// <p/>
        /// If the current thread has <see cref="System.Threading.Thread.Interrupt()"/> called on it while acquiring
        /// the write lock, then a <see cref="System.Threading.ThreadInterruptedException"/> is thrown.
        ///
        /// <p/>
        /// If the specified <see cref="System.TimeSpan"/> 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 time to wait for the write lock</param>
        /// <returns> <see lang="true"/> if the lock was free and was acquired
        /// by the current thread, or the write lock was already held by the
        /// current thread; and <see lang="false"/> if the waiting time
        /// elapsed before the lock could be acquired.
        /// </returns>
        ///
        /// <exception cref="System.Threading.ThreadInterruptedException">if the current thread is interrupted.</exception>
        public override bool TryLock(TimeSpan durationToWait)
        {
            ThreadInterruptedException ie = null;

            lock (this)
            {
                if (durationToWait.TotalMilliseconds <= 0)
                {
                    return(ReentrantReadWriteLock.StartWrite());
                }
                else if (ReentrantReadWriteLock.StartWriteFromNewWriter())
                {
                    return(true);
                }
                else
                {
                    DateTime deadline = DateTime.Now.Add(durationToWait);
                    for (;;)
                    {
                        try
                        {
                            Monitor.Wait(this, durationToWait);
                        }
                        catch (ThreadInterruptedException ex)
                        {
                            ReentrantReadWriteLock.CancelWaitingWriter();
                            Monitor.Pulse(this);
                            ie = ex;
                            break;
                        }
                        if (ReentrantReadWriteLock.StartWriteFromWaitingWriter())
                        {
                            return(true);
                        }
                        else
                        {
                            if (deadline.Subtract(DateTime.Now).TotalMilliseconds <= 0)
                            {
                                ReentrantReadWriteLock.CancelWaitingWriter();
                                Monitor.Pulse(this);
                                break;
                            }
                        }
                    }
                }
            }

            ReentrantReadWriteLock.SignallerReaderLock.SignalWaiters();
            if (ie != null)
            {
                throw ie;
            }
            else
            {
                return(false);
            }
        }
Beispiel #9
0
        public void ReadHoldingWriterLock()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.WriterLock.Lock();
            Assert.IsTrue(myLock.ReaderLock.TryLock());
            myLock.ReaderLock.Unlock();
            myLock.WriterLock.Unlock();
        }
        public void AwaitNanos_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            myLock.WriterLock.Lock();
            Assert.IsFalse(c.Await(new TimeSpan(1)));
            myLock.WriterLock.Unlock();
        }
Beispiel #11
0
        public void AwaitNanos_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();

            myLock.WriterLock.Lock();
            Assert.IsFalse(c.Await(new TimeSpan(1)));
            myLock.WriterLock.Unlock();
        }
Beispiel #12
0
        public void WriteTryLockWhenReadLocked()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.ReaderLock.Lock();
            Thread t = new Thread(new AnonymousClassRunnable14(myLock).Run);

            t.Start();
            t.Join();
            myLock.ReaderLock.Unlock();
        }
Beispiel #13
0
        public void WriteTryLock_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.WriterLock.Lock();
            Thread t = new Thread(new AnonymousClassRunnable15(myLock).Run);

            t.Start();
            t.Join();
            myLock.WriterLock.Unlock();
        }
Beispiel #14
0
        public void ReadTryLock_Interrupted()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.WriterLock.Lock();
            Thread t = new Thread(new AnonymousClassRunnable3(myLock).Run);

            t.Start();
            t.Interrupt();
            t.Join();
        }
Beispiel #15
0
        public void MultipleReadLocks()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.ReaderLock.Lock();
            Thread t = new Thread(new AnonymousClassRunnable6(myLock).Run);

            t.Start();
            t.Join();
            myLock.ReaderLock.Unlock();
        }
Beispiel #16
0
        public void AwaitUntil_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();

            myLock.WriterLock.Lock();
            DateTime d = DateTime.Now;

            myLock.WriterLock.Unlock();
        }
Beispiel #17
0
        public void Unlock_IllegalMonitorStateException()
        {
            ReentrantReadWriteLock rl = new ReentrantReadWriteLock();

            try
            {
                rl.WriterLock.Unlock();
                Assert.Fail("Should throw an exception.");
            }
            catch (SynchronizationLockException)
            {
            }
        }
Beispiel #18
0
        public void WriteLockInterruptibly_Interrupted()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
            Thread t = new Thread(new AnonymousClassRunnable(myLock).Run);

            myLock.WriterLock.Lock();
            t.Start();
            Thread.Sleep(Delays.Short);
            t.Interrupt();
            Thread.Sleep(Delays.Short);
            myLock.WriterLock.Unlock();
            t.Join();
        }
Beispiel #19
0
        public void WriteLockToString()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            String us = myLock.WriterLock.ToString();

            Assert.IsTrue(us.IndexOf("Unlocked") >= 0);
            myLock.WriterLock.Lock();

            String ls = myLock.WriterLock.ToString();

            Assert.IsTrue(ls.IndexOf("Locked") >= 0);
        }
Beispiel #20
0
        public void ReadLockToString()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            String us = myLock.ReaderLock.ToString();

            Assert.IsTrue(us.IndexOf("Read locks = 0") >= 0);
            myLock.ReaderLock.Lock();
            myLock.ReaderLock.Lock();

            String rs = myLock.ReaderLock.ToString();

            Assert.IsTrue(rs.IndexOf("Read locks = 2") >= 0);
        }
Beispiel #21
0
        public void AwaitUntil_Interrupt()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            Thread     t = new Thread(new AnonymousClassRunnable22(myLock, c).Run);

            t.Start();

            Thread.Sleep(new TimeSpan(Delays.Short.Milliseconds));
            t.Interrupt();
            t.Join(Delays.Short);
            Assert.IsFalse(t.IsAlive);
        }
        public void AwaitNanos_Interrupt()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            Thread t = new Thread(new AnonymousClassRunnable21(myLock, c).Run);

            t.Start();

            Thread.Sleep(new TimeSpan((Int64) 10000*Delays.Short.Milliseconds));
            t.Interrupt();
            t.Join(Delays.Short);
            Assert.IsFalse(t.IsAlive);
        }
Beispiel #23
0
        public void GetWriteHoldCount()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            for (int i = 1; i <= DEFAULT_COLLECTION_SIZE; i++)
            {
                myLock.WriterLock.Lock();
                Assert.AreEqual(i, myLock.WriteHoldCount);
            }
            for (int i = DEFAULT_COLLECTION_SIZE; i > 0; i--)
            {
                myLock.WriterLock.Unlock();
                Assert.AreEqual(i - 1, myLock.WriteHoldCount);
            }
        }
Beispiel #24
0
        public void Await()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            Thread     t = new Thread(new AnonymousClassRunnable19(myLock, c).Run);

            t.Start();

            Thread.Sleep(new TimeSpan(10000 * Delays.Short.Milliseconds));
            myLock.WriterLock.Lock();
            c.Signal();
            myLock.WriterLock.Unlock();
            t.Join(Delays.Short);
            Assert.IsFalse(t.IsAlive);
        }
Beispiel #25
0
        public void Await_IllegalMonitor()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();

            try
            {
                c.Await();
                Assert.Fail("Should throw an exception.");
            }

            catch (SynchronizationLockException)
            {
            }
        }
Beispiel #26
0
        /// <summary> Attempts to release this lock.</summary>
        /// <remarks>
        /// <p/> If the number of readers is now zero then the lock
        /// is made available for write lock attempts.
        /// </remarks>
        public override void Unlock()
        {
            switch (ReentrantReadWriteLock.EndRead())
            {
            case ReentrantReadWriteLock.Signaller.READER:
                ReentrantReadWriteLock.SignallerReaderLock.SignalWaiters();
                break;

            case ReentrantReadWriteLock.Signaller.WRITER:
                ReentrantReadWriteLock.SignallerWriterLock.SignalWaiters();
                break;

            default:
                break;
            }
        }
        public void Await()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            Thread t = new Thread(new AnonymousClassRunnable19(myLock, c).Run);

            t.Start();

            Thread.Sleep(new TimeSpan(10000*Delays.Short.Milliseconds));
            myLock.WriterLock.Lock();
            c.Signal();
            myLock.WriterLock.Unlock();
            t.Join(Delays.Short);
            Assert.IsFalse(t.IsAlive);
        }
Beispiel #28
0
        public void ReadAfterWriterLock()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            myLock.WriterLock.Lock();
            Thread t1 = new Thread(new AnonymousClassRunnable9(myLock).Run);
            Thread t2 = new Thread(new AnonymousClassRunnable10(myLock).Run);

            t1.Start();
            t2.Start();
            Thread.Sleep(new TimeSpan(Delays.Short.Milliseconds * 10000));
            myLock.WriterLock.Unlock();
            t1.Join(Delays.Medium);
            t2.Join(Delays.Medium);
            Assert.IsTrue(!t1.IsAlive);
            Assert.IsTrue(!t2.IsAlive);
        }
Beispiel #29
0
        public void ToStringTest()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
            String us = myLock.ToString();

            Assert.IsTrue(us.IndexOf("Write locks = 0") >= 0);
            Assert.IsTrue(us.IndexOf("Read locks = 0") >= 0);
            myLock.WriterLock.Lock();
            String ws = myLock.ToString();

            Assert.IsTrue(ws.IndexOf("Write locks = 1") >= 0);
            Assert.IsTrue(ws.IndexOf("Read locks = 0") >= 0);
            myLock.WriterLock.Unlock();
            myLock.ReaderLock.Lock();
            myLock.ReaderLock.Lock();
            String rs = myLock.ToString();

            Assert.IsTrue(rs.IndexOf("Write locks = 0") >= 0);
            Assert.IsTrue(rs.IndexOf("Read locks = 2") >= 0);
        }
Beispiel #30
0
        /// <summary>
        /// Attempts to release this lock.
        /// </summary>
        /// <remarks>
        /// If the current thread is the holder of this lock then
        /// the hold count is decremented. If the hold count is now
        /// zero, the lock is released.  If the current thread is
        /// not the holder of this lock then <see cref="System.Threading.SynchronizationLockException"/>
        /// is thrown.
        /// </remarks>
        /// <exception cref="System.Threading.SynchronizationLockException">if the current thread is not the holder of this lock.</exception>
        public override void Unlock()
        {
            if (!IsHeldByCurrentThread)
            {
                throw new SynchronizationLockException("Current thread does not hold this lock.");
            }
            switch (ReentrantReadWriteLock.EndWrite())
            {
            case ReentrantReadWriteLock.Signaller.READER:
                ReentrantReadWriteLock.SignallerReaderLock.SignalWaiters();
                break;

            case ReentrantReadWriteLock.Signaller.WRITER:
                ReentrantReadWriteLock.SignallerWriterLock.SignalWaiters();
                break;

            default:
                break;
            }
        }
Beispiel #31
0
        public void Serialization()
        {
            ReentrantReadWriteLock l = new ReentrantReadWriteLock();

            l.ReaderLock.Lock();
            l.ReaderLock.Unlock();

            MemoryStream bout = new MemoryStream(10000);

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(bout, l);

            MemoryStream           bin        = new MemoryStream(bout.ToArray());
            BinaryFormatter        formatter2 = new BinaryFormatter();
            ReentrantReadWriteLock r          = (ReentrantReadWriteLock)formatter2.Deserialize(bin);

            r.ReaderLock.Lock();
            r.ReaderLock.Unlock();
        }
Beispiel #32
0
        /// <summary>
        ///	Acquires the write lock unless <see cref="System.Threading.Thread.Interrupt()"/> is called on the current thread
        /// </summary>
        /// <remarks>
        /// Acquires the write lock if neither the read nor write locks
        /// are held by another thread
        /// and returns immediately, setting the write lock hold count to
        /// one.
        ///
        /// <p/>
        /// If the current thread already holds this lock then the
        /// hold count is incremented by one and the method returns
        /// immediately.
        ///
        /// <p/>
        /// If the 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 write 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 the write lock is acquired by the current thread then the
        /// lock hold count is set to one.
        ///
        /// <p/>
        /// If the current thread:
        /// <ul>
        /// <li>has its interrupted status set on entry to this method</li>
        /// <li><see cref="System.Threading.Thread.Interrupt()"/> is called on the thread while acquiring the write lock.</li>
        /// </ul>
        ///
        /// then a <see cref="System.Threading.ThreadInterruptedException"/> is thrown and the current
        /// thread's interrupted status is cleared.
        ///
        /// <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.StartWriteFromNewWriter())
                {
                    for (;;)
                    {
                        try
                        {
                            Monitor.Wait(this);
                            if (ReentrantReadWriteLock.StartWriteFromWaitingWriter())
                            {
                                return(this);
                            }
                        }
                        catch (ThreadInterruptedException ex)
                        {
                            ReentrantReadWriteLock.CancelWaitingWriter();
                            Monitor.Pulse(this);
                            ie = ex;
                            break;
                        }
                    }
                }
            }
            if (ie != null)
            {
                // Fall through outside synch on interrupt.
                //  On exception, we may need to signal readers.
                //  It is not worth checking here whether it is strictly necessary.
                ReentrantReadWriteLock.SignallerReaderLock.SignalWaiters();
                throw ie;
            }
            return(this);
        }
        public void Signal_IllegalMonitor()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            try
            {
                c.Signal();
                Assert.Fail("Should throw an exception.");
            }

            catch (SynchronizationLockException)
            {
            }
        }
 public AnonymousClassRunnable24(ReentrantReadWriteLock myLock, ICondition c)
 {
     this.myLock = myLock;
     this.c = c;
 }
        public void Await_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            myLock.WriterLock.Lock();
            myLock.WriterLock.Unlock();
        }
 public void Constructor()
 {
     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
     Assert.IsFalse(rl.IsFair);
     Assert.IsFalse(rl.IsWriteLockHeld);
     Assert.AreEqual(0, rl.ReadLockCount);
 }
 public void GetWriteHoldCount()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     for (int i = 1; i <= DEFAULT_COLLECTION_SIZE; i++)
     {
         myLock.WriterLock.Lock();
         Assert.AreEqual(i, myLock.WriteHoldCount);
     }
     for (int i = DEFAULT_COLLECTION_SIZE; i > 0; i--)
     {
         myLock.WriterLock.Unlock();
         Assert.AreEqual(i - 1, myLock.WriteHoldCount);
     }
 }
 public void MultipleReadLocks()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     myLock.ReaderLock.Lock();
     Thread t = new Thread(new AnonymousClassRunnable6(myLock).Run);
     t.Start();
     t.Join();
     myLock.ReaderLock.Unlock();
 }
        public void ReadLockToString()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            String us = myLock.ReaderLock.ToString();
            Assert.IsTrue(us.IndexOf("Read locks = 0") >= 0);
            myLock.ReaderLock.Lock();
            myLock.ReaderLock.Lock();

            String rs = myLock.ReaderLock.ToString();
            Assert.IsTrue(rs.IndexOf("Read locks = 2") >= 0);
        }
 public void WriteTryLockWhenReadLocked()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     myLock.ReaderLock.Lock();
     Thread t = new Thread(new AnonymousClassRunnable14(myLock).Run);
     t.Start();
     t.Join();
     myLock.ReaderLock.Unlock();
 }
 public void WriteTryLock_Timeout()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     myLock.WriterLock.Lock();
     Thread t = new Thread(new AnonymousClassRunnable15(myLock).Run);
     t.Start();
     t.Join();
     myLock.WriterLock.Unlock();
 }
 internal InterruptibleLockRunnable(ReentrantReadWriteLock l)
 {
     myLock = l;
 }
 public AnonymousClassRunnable16(ReentrantReadWriteLock myLock)
 {
     this.myLock = myLock;
 }
        public void Serialization()
        {
            ReentrantReadWriteLock l = new ReentrantReadWriteLock();
            l.ReaderLock.Lock();
            l.ReaderLock.Unlock();

            MemoryStream bout = new MemoryStream(10000);

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(bout, l);

            MemoryStream bin = new MemoryStream(bout.ToArray());
            BinaryFormatter formatter2 = new BinaryFormatter();
            ReentrantReadWriteLock r = (ReentrantReadWriteLock) formatter2.Deserialize(bin);
            r.ReaderLock.Lock();
            r.ReaderLock.Unlock();
        }
 public void ReadTryLock_Interrupted()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     myLock.WriterLock.Lock();
     Thread t = new Thread(new AnonymousClassRunnable3(myLock).Run);
     t.Start();
     t.Interrupt();
     t.Join();
 }
        public void WriteLockToString()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            String us = myLock.WriterLock.ToString();
            Assert.IsTrue(us.IndexOf("Unlocked") >= 0);
            myLock.WriterLock.Lock();

            String ls = myLock.WriterLock.ToString();
            Assert.IsTrue(ls.IndexOf("Locked") >= 0);
        }
 public void ReadHoldingWriterLock()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     myLock.WriterLock.Lock();
     Assert.IsTrue(myLock.ReaderLock.TryLock());
     myLock.ReaderLock.Unlock();
     myLock.WriterLock.Unlock();
 }
 public void WriteLockInterruptibly_Interrupted()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     Thread t = new Thread(new AnonymousClassRunnable(myLock).Run);
     myLock.WriterLock.Lock();
     t.Start();
     Thread.Sleep(Delays.Short);
     t.Interrupt();
     Thread.Sleep(Delays.Short);
     myLock.WriterLock.Unlock();
     t.Join();
 }
 public void Lock()
 {
     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
     rl.WriterLock.Lock();
     Assert.IsTrue(rl.IsWriteLockHeld);
     Assert.IsTrue(rl.WriterLockedByCurrentThread);
     //        assertTrue(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
     Assert.AreEqual(0, rl.ReadLockCount);
     rl.WriterLock.Unlock();
     Assert.IsFalse(rl.IsWriteLockHeld);
     Assert.IsFalse(rl.WriterLockedByCurrentThread);
     //        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
     Assert.AreEqual(0, rl.ReadLockCount);
     rl.ReaderLock.Lock();
     Assert.IsFalse(rl.IsWriteLockHeld);
     Assert.IsFalse(rl.WriterLockedByCurrentThread);
     //        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
     Assert.AreEqual(1, rl.ReadLockCount);
     rl.ReaderLock.Unlock();
     Assert.IsFalse(rl.IsWriteLockHeld);
     Assert.IsFalse(rl.WriterLockedByCurrentThread);
     //        assertFalse(((ReentrantReadWriteLock.WriteLock)rl.writeLock()).isHeldByCurrentThread());
     Assert.AreEqual(0, rl.ReadLockCount);
 }
        public void WriteAfterMultipleReadLocks()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
            myLock.ReaderLock.Lock();
            Thread t1 = new Thread(new AnonymousClassRunnable7(myLock).Run);
            Thread t2 = new Thread(new AnonymousClassRunnable8(myLock).Run);

            t1.Start();
            t2.Start();
            Thread.Sleep(new TimeSpan(Delays.Short.Milliseconds*10000));
            myLock.ReaderLock.Unlock();
            t1.Join(Delays.Medium);
            t2.Join(Delays.Medium);
            Assert.IsTrue(!t1.IsAlive);
            Assert.IsTrue(!t2.IsAlive);
        }
        public void GetQueueLength()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
            Thread t1 = new Thread(new InterruptedLockRunnable(myLock).Run);
            Thread t2 = new Thread(new InterruptibleLockRunnable(myLock).Run);
            Assert.AreEqual(0, myLock.QueueLength);
            myLock.WriterLock.Lock();
            t1.Start();

            Thread.Sleep(new TimeSpan(10000*Delays.Short.Milliseconds));
            Assert.AreEqual(1, myLock.QueueLength);
            t2.Start();

            Thread.Sleep(new TimeSpan(10000*Delays.Short.Milliseconds));
            Assert.AreEqual(2, myLock.QueueLength);
            t1.Interrupt();

            Thread.Sleep(new TimeSpan(10000*Delays.Short.Milliseconds));
            Assert.AreEqual(1, myLock.QueueLength);
            myLock.WriterLock.Unlock();

            Thread.Sleep(new TimeSpan(10000*Delays.Short.Milliseconds));
            Assert.AreEqual(0, myLock.QueueLength);
            t1.Join();
            t2.Join();
        }
 public void Unlock_IllegalMonitorStateException()
 {
     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
     try
     {
         rl.WriterLock.Unlock();
         Assert.Fail("Should throw an exception.");
     }
     catch (SynchronizationLockException)
     {
     }
 }
Beispiel #53
0
 public AnonymousClassRunnable3(ReentrantReadWriteLock myLock)
 {
     this.myLock = myLock;
 }
Beispiel #54
0
 internal InterruptedLockRunnable(ReentrantReadWriteLock l)
 {
     myLock = l;
 }
 public void ToStringTest()
 {
     ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
     String us = myLock.ToString();
     Assert.IsTrue(us.IndexOf("Write locks = 0") >= 0);
     Assert.IsTrue(us.IndexOf("Read locks = 0") >= 0);
     myLock.WriterLock.Lock();
     String ws = myLock.ToString();
     Assert.IsTrue(ws.IndexOf("Write locks = 1") >= 0);
     Assert.IsTrue(ws.IndexOf("Read locks = 0") >= 0);
     myLock.WriterLock.Unlock();
     myLock.ReaderLock.Lock();
     myLock.ReaderLock.Lock();
     String rs = myLock.ToString();
     Assert.IsTrue(rs.IndexOf("Write locks = 0") >= 0);
     Assert.IsTrue(rs.IndexOf("Read locks = 2") >= 0);
 }
Beispiel #56
0
 /// <summary>
 /// Constructs a <see cref="Spring.Threading.Locks.WriterLock"/>, using the given <see cref="Spring.Threading.Locks.ReentrantReadWriteLock"/>
 /// </summary>
 /// <param name="reentrantReadWriteLock"><see cref="Spring.Threading.Locks.ReentrantReadWriteLock"/> to use for this lock.</param>
 public WriterLock(ReentrantReadWriteLock reentrantReadWriteLock)
     : base(reentrantReadWriteLock)
 {
 }
 protected AbstractSignallerLock(ReentrantReadWriteLock reentrantReadWriteLock)
 {
     _reentrantReadWriteLock = reentrantReadWriteLock;
 }
        public void AwaitUntil_Timeout()
        {
            ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();

            ICondition c = myLock.WriterLock.NewCondition();
            myLock.WriterLock.Lock();
            DateTime d = DateTime.Now;
            myLock.WriterLock.Unlock();
        }