Beispiel #1
0
    // Shows how to release all locks and later restore
    // the lock state. Shows how to use sequence numbers
    // to determine whether another thread has obtained
    // a writer lock since this thread last accessed the
    // resource.
    static void ReleaseRestore(int timeOut)
    {
        int lastWriter;

        try
        {
            rwl.AcquireReaderLock(timeOut);
            try
            {
                // It is safe for this thread to read from
                // the shared resource. Cache the value. (You
                // might do this if reading the resource is
                // an expensive operation.)
                int resourceValue = resource;
                Display("reads resource value " + resourceValue);
                Interlocked.Increment(ref reads);

                // Save the current writer sequence number.
                lastWriter = rwl.WriterSeqNum;

                // Release the lock, and save a cookie so the
                // lock can be restored later.
                LockCookie lc = rwl.ReleaseLock();

                // Wait for a random interval (up to a
                // quarter of a second), and then restore
                // the previous state of the lock. Note that
                // there is no time-out on the Restore method.
                Thread.Sleep(rnd.Next(250));
                rwl.RestoreLock(ref lc);

                // Check whether other threads obtained the
                // writer lock in the interval. If not, then
                // the cached value of the resource is still
                // valid.
                if (rwl.AnyWritersSince(lastWriter))
                {
                    resourceValue = resource;
                    Interlocked.Increment(ref reads);
                    Display("resource has changed " + resourceValue);
                }
                else
                {
                    Display("resource has not changed " + resourceValue);
                }
            }
            finally
            {
                // Ensure that the lock is released.
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException)
        {
            // The reader lock request timed out.
            Interlocked.Increment(ref readerTimeouts);
        }
    }
Beispiel #2
0
        /**
         * @brief
         * Release all locks and later restores the lock state. Uses sequence numbers to determine
         * whether another thread has obtained a writer lock since this thread last accessed the
         * resource.
         */
        public void MRelease(int aiTimeout)
        {
            int        kiLastWriter;
            int        kiResource;
            LockCookie koLC;

            try
            {
                /// -# Request reader lock
                voRWL.AcquireReaderLock(aiTimeout);
                try
                {
                    /// -# Read and cache resource value
                    kiResource = viResource;
                    this.MDisplay("Read: " + kiResource.ToString( ));
                    Interlocked.Increment(ref viReads);

                    /// -# Save the current writer sequence number.
                    kiLastWriter = voRWL.WriterSeqNum;

                    /// -# Release the lock and save a cookie so the lock can be restored later.
                    koLC = voRWL.ReleaseLock( );

                    /// -# Wait for a random interval and then restore the previous state of the lock.
                    Thread.Sleep(voRand.Next(250));
                    voRWL.RestoreLock(ref koLC);

                    /// -# Check whether other threads obtained the writer lock in the interval.
                    ///    If not, then the cached value of the resource is still valid.
                    if (voRWL.AnyWritersSince(kiLastWriter))
                    {
                        kiResource = viResource;
                        Interlocked.Increment(ref viReads);
                        this.MDisplay("Change: " + kiResource);
                    }
                    else
                    {
                        this.MDisplay("Change: None");
                    }
                }
                finally
                {
                    /// -# Ensure that the lock is released.
                    voRWL.ReleaseReaderLock( );
                }
            }
            catch (ApplicationException)
            {
                /// -# The reader lock request timed out.
                Interlocked.Increment(ref viTOReader);
            }
        }
    // Release all locks and later restores the lock state.
    // Uses sequence numbers to determine whether another thread has
    // obtained a writer lock since this thread last accessed the resource.
    static void ReleaseRestore(Random rnd, int timeOut)
    {
        int lastWriter;

        try
        {
            rwl.AcquireReaderLock(timeOut);
            try
            {
                // It's safe for this thread to read from the shared resource,
                // so read and cache the resource value.
                int resourceValue = resource;     // Cache the resource value.
                Display("reads resource value " + resourceValue);
                Interlocked.Increment(ref reads);

                // Save the current writer sequence number.
                lastWriter = rwl.WriterSeqNum;

                // Release the lock and save a cookie so the lock can be restored later.
                LockCookie lc = rwl.ReleaseLock();

                // Wait for a random interval and then restore the previous state of the lock.
                Thread.Sleep(rnd.Next(250));
                rwl.RestoreLock(ref lc);

                // Check whether other threads obtained the writer lock in the interval.
                // If not, then the cached value of the resource is still valid.
                if (rwl.AnyWritersSince(lastWriter))
                {
                    resourceValue = resource;
                    Interlocked.Increment(ref reads);
                    Display("resource has changed " + resourceValue);
                }
                else
                {
                    Display("resource has not changed " + resourceValue);
                }
            }
            finally
            {
                // Ensure that the lock is released.
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException)
        {
            // The reader lock request timed out.
            Interlocked.Increment(ref readerTimeouts);
        }
    }
Beispiel #4
0
        public void TestReleaseRestoreWriterLock()
        {
            rwlock = new ReaderWriterLock();
            rwlock.AcquireWriterLock(500);
            rwlock.AcquireWriterLock(500);
            Assert.IsTrue(rwlock.IsWriterLockHeld);

            LockCookie co = rwlock.ReleaseLock();

            RunThread(new ThreadStart(AcquireLock_readerWorks));

            rwlock.RestoreLock(ref co);
            RunThread(new ThreadStart(AcquireLock_readerFails));

            rwlock.ReleaseWriterLock();
            Assert.IsTrue(rwlock.IsWriterLockHeld);
            rwlock.ReleaseWriterLock();
            Assert.IsTrue(!rwlock.IsWriterLockHeld);
        }
Beispiel #5
0
        void ReleaseRestore(int timeout)
        {
            int lastWriter;

            try{
                rwl.AcquireReaderLock(timeout);
                try {
                    Display("read resource:" + resource);
                    Interlocked.Increment(ref reads);

                    int resourceValue = resource;

                    lastWriter = rwl.WriterSeqNum;

                    LockCookie lc = rwl.ReleaseLock();

                    Thread.Sleep(rnd.Next(250));

                    rwl.RestoreLock(ref lc);

                    if (rwl.AnyWritersSince(lastWriter))
                    {
                        resourceValue = resource;
                        Interlocked.Increment(ref reads);
                        Display("resouce has changed:" + resourceValue);
                    }
                    else
                    {
                        Display("resouce has not changed:" + resourceValue);
                    }
                }
                catch (ApplicationException)
                {
                    rwl.ReleaseReaderLock();
                }
            }
            catch (ApplicationException)
            {
                Interlocked.Increment(ref readTimeout);
            }
        }
    static void ReleaseRestore(int timeOut)
    {
        int lastWriter;

        try{
            rwl.AcquireReaderLock(timeOut);
            try{
                int resourceValue = num;
                Console.WriteLine("Leyendo dato" + resourceValue);
                Interlocked.Increment(ref reads);

                lastWriter = rwl.WriterSeqNum;

                LockCookie lc = rwl.ReleaseLock();

                Thread.Sleep(rnd.Next(250));
                rwl.RestoreLock(ref lc);

                if (rwl.AnyWritersSince(lastWriter))
                {
                    resourceValue = num;
                    Interlocked.Increment(ref reads);
                    Console.WriteLine("dato cambiado" + resourceValue);
                }
                else
                {
                    Console.WriteLine("dato no cambiado" + resourceValue);
                }
            }
            finally{
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException) {
            Interlocked.Increment(ref readerTimeouts);
        }
    }
            public void RestoreLock(TestLockCookie tlc, int expectedFailureHResult = 0)
            {
                Assert.NotNull(tlc);
                Assert.NotEqual(TimeoutExceptionHResult, expectedFailureHResult);

                PerformLockAction(
                    expectedFailureHResult,
                    true /* isBlockingOperation */,
                    () => _rwl.RestoreLock(ref tlc._lockCookie),
                    () =>
                {
                    Assert.Equal(0, ThreadReaderLevel);
                    Assert.Equal(InvalidThreadID, _writerThreadID);
                    Assert.Equal(0, _writerLevel);
                    ThreadReaderLevel = tlc._readerLevel;
                    _writerLevel      = tlc._writerLevel;
                    if (_writerLevel != 0)
                    {
                        Assert.Equal(InvalidThreadID, _writerThreadID);
                        _writerThreadID = Environment.CurrentManagedThreadId;
                        ++_writerSeqNum;
                    }
                });
            }
    void WriterFunc( )
    {
        int        WriteL        = Interlocked.Increment(ref WriteX);
        LockCookie releaseCookie = new LockCookie( );
        int        iWriterSeqNum;

        if ((rwLock.IsReaderLockHeld) || (rwLock.IsWriterLockHeld))
        {
            lock (this)
                Console.WriteLine("{0}, Writer -BAD- No Locks should be held now. (I)", WriteL);
        }
        try
        {
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- Before AcquireWriterLock.", WriteL);
            rwLock.AcquireWriterLock(10000);
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- After  AcquireWriterLock.", WriteL);
        }
        catch (Exception e)
        {
            lock (this)
                Console.WriteLine("{0}, Writer -BAD- (AcquireWriterLock) {1}", WriteL, e.ToString());
            failed = 1;
        }
        Thread.Sleep(10);
        try
        {
            if (!(rwLock.IsWriterLockHeld))
            {
                lock (this)
                    Console.WriteLine("{0}, Writer -BAD- WriterLock should be held now. (I)", WriteL);
            }
            iWriterSeqNum = rwLock.WriterSeqNum;
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- Before ReleaseLock.", WriteL);
            releaseCookie = rwLock.ReleaseLock( );
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- After  ReleaseLock.", WriteL);
            if ((rwLock.IsReaderLockHeld) || (rwLock.IsWriterLockHeld))
            {
                lock (this)
                    Console.WriteLine("{0}, Writer -BAD- No Locks should be held now. (II)", WriteL);
            }
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- Before RestoreLock.", WriteL);
            rwLock.RestoreLock(ref releaseCookie);
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- After  RestoreLock.", WriteL);
            if (!(rwLock.IsWriterLockHeld))
            {
                lock (this)
                    Console.WriteLine("{0}, Writer -BAD- WriterLock should be held now. (II)", WriteL);
            }
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- Before ReleaseWriterLock.", WriteL);
            rwLock.ReleaseWriterLock( );
            lock (this)
                Console.WriteLine("{0}, Writer -fyi- After  ReleaseWriterLock.", WriteL);
            bool boX;
            boX           = rwLock.AnyWritersSince(iWriterSeqNum);
            iWriterSeqNum = rwLock.WriterSeqNum;
            boX           = rwLock.AnyWritersSince(iWriterSeqNum);
        }
        catch (Exception e)
        {
            lock (this)
                Console.WriteLine("{0}, Writer - {1}", WriteL, e.ToString());
            failed = 1;
        }
    }