Fast and Fair ReaderWriter lock.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace WJS.Threading
{
    /// <summary>
    /// Fast and fair ReaderWriter lock.
    /// </summary>
    public class RW
    {
        private int nr = 0;
        private int nw = 0;
        private object writerLock = new object();
        private object readerLock = new object();
        public RW()
        {
        }
        public int ReaderCount
        {
            get
            {
                return Interlocked.CompareExchange(ref nr, 0, 0);
            }
        }
        public int WriterCount
        {
            get
            {
                return Interlocked.CompareExchange(ref nw, 0, 0);
            }
        }
        public bool HasReaders
        {
            get
            {
                return ReaderCount > 0;
            }
        }
        public bool HasWriters
        {
            get
            {
                return WriterCount > 0;
            }
        }
        public bool AcquireWriterLock(int timeout)
        {
            if (!Monitor.TryEnter(writerLock, timeout))
                return false;
            lock (readerLock)
            {
                while (nr > 0)
                {
                    if (!Monitor.Wait(readerLock, timeout))
                        return false;
                }
            }
            // We now have the writer lock.
            Interlocked.Increment(ref nw);
            return true;
        }
        public void AcquireReaderLock()
        {
            bool result = AcquireReaderLock(-1);
            if (!result)
                throw new ApplicationException("Timeout waiting for reader lock.");
        }
        public bool AcquireReaderLock(int millisecondsTimeout)
        {
            if (!Monitor.TryEnter(writerLock, millisecondsTimeout))
                return false;
            try
            {
                if (!Monitor.TryEnter(readerLock, millisecondsTimeout))
                    return false;
                try
                {
                    nr++;
                    return true;
                }
                finally
                {
                    Monitor.Exit(readerLock);
                }
            }
            finally
            {
                Monitor.Exit(writerLock);
            }
        }
        public void ReleaseWriterLock()
        {
            try
            {
                Interlocked.Decrement(ref nw);
                Monitor.Exit(writerLock);
            }
            catch(SynchronizationLockException)
            {
                Interlocked.Increment(ref nw);
                throw new ApplicationException("The thread does not have the writer lock.");
            }
        }
        public void ReleaseReaderLock()
        {
            lock (readerLock)
            {
                if (nr == 0)
                    throw new ApplicationException("The thread does not have a reader lock.");
                nr–;
                if (nr == 0)
                    Monitor.Pulse(readerLock);
            }
        }
    }
    //// Andrew Birrell’s original RW lock.
    //public class RW2
    //{
    //    int i = 0;
    //    public void AcquireWriteLock()
    //    {
    //        lock (this)
    //        {
    //            while (i != 0)
    //                Monitor.Wait(this);
    //            i = -1;
    //        }
    //    }
    //    public void AcquireReadLock()
    //    {
    //        lock (this)
    //        {
    //            while (i < 0)
    //                Monitor.Wait(this);
    //            i++;
    //        }
    //    }
    //    public void ReleaseWriteLock()
    //    {
    //        lock (this)
    //        {
    //            i = 0;
    //            Monitor.PulseAll(this);
    //        }
    //    }
    //    public void ReleaseReadLock()
    //    {
    //        lock (this)
    //        {
    //            i–;
    //            if (i == 0)
    //                Monitor.Pulse(this);
    //        }
    //    }
    //}
}
Advertisements
This entry was posted in C#. Bookmark the permalink.

3 Responses to Fast and Fair ReaderWriter lock.

  1. Unknown says:

    http://www.batterylaptoppower.com/mitac/bp-8050.htm mitac bp-8050 battery http://www.batterylaptoppower.com/mitac/8375.htm mitac 8375 battery http://www.batterylaptoppower.com/mitac/e2000.htm mitac e2000 battery http://www.batterylaptoppower.com/mitac/8×75.htm mitac 8×75 battery http://www.batterylaptoppower.com/mitac/8575.htm mitac 8575 battery http://www.batterylaptoppower.com/mitac/bp-8089.htm mitac bp-8089 battery http://www.batterylaptoppower.com/mitac/bp-8089x.htm mitac bp-8089x battery http://www.batterylaptoppower.com/mitac/bp-8389.htm mitac bp-8389 battery http://www.batterylaptoppower.com/mitac/bp-8089p.htm mitac bp-8089p battery http://www.batterylaptoppower.com/mitac/e680.htm mitac e680 battery http://www.batterylaptoppower.com/mitac/bp-8×99.htm mitac bp-8×99 battery http://www.batterylaptoppower.com/mitac/bp-8599.htm mitac bp-8599 battery http://www.batterylaptoppower.com/nec/op-570-75303.htm nec op-570-75303 battery http://www.batterylaptoppower.com/nec/pc-vp-wp25.htm nec pc-vp-wp25 battery http://www.batterylaptoppower.com/nec/pc-vp-wp44.htm nec pc-vp-wp44 battery http://www.batterylaptoppower.com/nec/op-570-75901.htm nec op-570-75901 battery http://www.batterylaptoppower.com/nec/m300.htm nec m300 battery http://www.batterylaptoppower.com/nec/m500.htm nec m500 battery http://www.batterylaptoppower.com/sony/pcga-bp2s.htm sony pcga-bp2s battery http://www.batterylaptoppower.com/sony/pcga-bp2t.htm sony pcga-bp2t battery http://www.batterylaptoppower.com/sony/pcga-bp3t.htm sony pcga-bp3t battery http://www.batterylaptoppower.com/sony/pcg-tr3.htm sony pcg-tr3 battery http://www.batterylaptoppower.com/sony/pcg-tr2.htm sony pcg-tr2 battery http://www.batterylaptoppower.com/sony/pcg-tr1.htm sony pcg-tr1 battery http://www.batterylaptoppower.com/sony/pcga-bp2v.htm sony pcga-bp2v battery http://www.batterylaptoppower.com/sony/pcg-z1.htm sony pcg-z1 battery http://www.batterylaptoppower.com/sony/pcg-v505.htm sony pcg-v505 battery http://www.batterylaptoppower.com/sony/pcga-bp2e.htm sony pcga-bp2e battery http://www.batterylaptoppower.com/sony/vgp-bp2ea.htm sony vgp-bp2ea battery http://www.batterylaptoppower.com/sony/pcg-gr100.htm sony pcg-gr100 battery http://www.batterylaptoppower.com/sony/pcg-gr150.htm sony pcg-gr150 battery http://www.batterylaptoppower.com/sony/pcg-gr250.htm sony pcg-gr250 battery http://www.batterylaptoppower.com/sony/pcg-gr270.htm sony pcg-gr270 battery http://www.batterylaptoppower.com/sony/vgp-bps2a.htm sony vgp-bps2a battery http://www.batterylaptoppower.com/sony/vgp-bps2b.htm sony vgp-bps2b battery http://www.batterylaptoppower.com/sony/vgp-bps2.htm sony vgp-bps2 battery http://www.batterylaptoppower.com/sony/vgp-bps2b.htm sony vgp-bps2b battery http://www.batterylaptoppower.com/sony/vgp-bps2c.htm sony vgp-bps2c battery http://www.batterylaptoppower.com/sony/vgp-bpl2c.htm sony vgp-bpl2c battery http://www.batterylaptoppower.com/toshiba/b404.htm toshiba b404 battery http://www.batterylaptoppower.com/toshiba/ba1405.htm toshiba ba1405 battery http://www.batterylaptoppower.com/toshiba/pa2487.htm toshiba pa2487 battery http://www.batterylaptoppower.com/toshiba/pa2487urg.htm toshiba pa2487urg battery http://www.batterylaptoppower.com/toshiba/pa3107u-1brs.htm toshiba pa3107u-1brs battery http://www.batterylaptoppower.com/toshiba/1800.htm toshiba 1800 battery

  2. Unknown says:

    http://www.batteryfast.com.au/hp/nc8230.htm hp nc8230 battery http://www.batteryfast.com.au/hp/nc8200.htm hp nc8200 battery http://www.batteryfast.com.au/hp/nw8200.htm hp nw8200 battery http://www.batteryfast.com.au/hp/nx8200.htm hp nx8200 battery http://www.batteryfast.com.au/hp/nc8430.htm hp nc8430 battery http://www.batteryfast.com.au/hp/pb992a.htm hp pb992a battery http://www.batteryfast.com.au/hp/nx7000.htm hp nx7000 battery http://www.batteryfast.com.au/hp/presario-x1000.htm hp presario x1000 battery http://www.batteryfast.com.au/hp/presario-x1100.htm hp presario x1100 battery http://www.batteryfast.com.au/hp/presario-x1200.htm hp presario x1200 battery http://www.batteryfast.com.au/hp/presario-x1300.htm hp presario x1300 battery http://www.batteryfast.com.au/hp/presario-x1400.htm hp presario x1400 battery http://www.batteryfast.com.au/hp/zt3000.htm hp zt3000 battery http://www.batteryfast.com.au/hp/zt3100.htm hp zt3100 battery http://www.batteryfast.com.au/hp/zt3200.htm hp zt3200 battery http://www.batteryfast.com.au/hp/zt3300.htm hp zt3300 battery http://www.batteryfast.com.au/hp/zt3400.htm hp zt3400 battery http://www.batteryfast.com.au/hp/nx6120.htm hp nx6120 battery http://www.batteryfast.com.au/hp/pb994a.htm hp pb994a battery http://www.batteryfast.com.au/hp/nc6100.htm hp nc6100 battery http://www.batteryfast.com.au/hp/nc6120.htm hp nc6120 battery http://www.batteryfast.com.au/hp/nx6100.htm hp nx6100 battery http://www.batteryfast.com.au/hp/nc6400.htm hp nc6400 battery http://www.batteryfast.com.au/hp/nc6230.htm hp nc6230 battery http://www.batteryfast.com.au/hp/nc6110.htm hp nc6110 battery http://www.batteryfast.com.au/hp/dv2000.htm hp dv2000 battery http://www.batteryfast.com.au/hp/dv2100.htm hp dv2100 battery http://www.batteryfast.com.au/hp/dv2200.htm hp dv2200 battery http://www.batteryfast.com.au/hp/dv6000.htm hp dv6000 battery http://www.batteryfast.com.au/hp/dv9000.htm hp dv9000 battery http://www.batteryfast.com.au/hp/dv9200.htm hp dv9200 battery http://www.batteryfast.com.au/hp/dv9100.htm hp dv9100 battery http://www.batteryfast.com.au/hp/dv9500.htm hp dv9500 battery http://www.batteryfast.com.au/hp/dv9600.htm hp dv9600 battery http://www.batteryfast.com.au/hp/dv9700.htm hp dv9700 battery http://www.batteryfast.com.au/hp/hstnn-ib44.htm hp hstnn-ib44 battery http://www.batteryfast.com.au/hp/510.htm hp 510 battery http://www.batteryfast.com.au/hp/530.htm hp 530 battery http://www.batteryfast.com.au/hp/hstnn-fb44.htm hp hstnn-fb44 battery http://www.batteryfast.com.au/hp/hstnn-c29c.htm hp hstnn-c29c battery http://www.batteryfast.com.au/hp/520.htm hp 520 battery http://www.batteryfast.com.au/hp/500.htm hp 500 battery http://www.batteryfast.com.au/hp/dv8000.htm hp dv8000 battery http://www.batteryfast.com.au/hp/dv8100.htm hp dv8100 battery http://www.batteryfast.com.au/hp/dv8200.htm hp dv8200 battery http://www.batteryfast.com.au/hp/dv8300.htm hp dv8300 battery http://www.batteryfast.com.au/hp/hstnn-db20.htm hp hstnn-db20 battery

  3. Unknown says:

    http://www.akkuliste.com/compaq/nc4400.htm compaq nc4400 akku http://www.akkuliste.com/compaq/tc4200.htm compaq tc4200 akku http://www.akkuliste.com/compaq/tc4400.htm compaq tc4400 akku http://www.akkuliste.com/compaq/hstnnib12.htm compaq hstnnib12 akku http://www.akkuliste.com/dell/d400.htm dell d400 akku http://www.akkuliste.com/dell/latitude-d400.htm dell latitude d400 akku http://www.akkuliste.com/dell/original-inspiron-1525.htm dell original inspiron 1525 akku http://www.akkuliste.com/dell/original-inspiron-1526.htm dell original inspiron 1526 akku http://www.akkuliste.com/dell/latitude-d810.htm dell latitude d810 akku http://www.akkuliste.com/dell/precision-m70.htm dell precision m70 akku http://www.akkuliste.com/dell/y4367.htm dell y4367 akku http://www.akkuliste.com/dell/inspiron-1100-series.htm dell inspiron 1100 series akku http://www.akkuliste.com/dell/inspiron-1150.htm dell inspiron 1150 akku http://www.akkuliste.com/dell/inspiron-5150.htm dell inspiron 5150 akku http://www.akkuliste.com/dell/inspiron-5100.htm dell inspiron 5100 akku http://www.akkuliste.com/dell/inspiron-1000.htm dell inspiron 1000 akku http://www.akkuliste.com/dell/inspiron-1200.htm dell inspiron 1200 akku http://www.akkuliste.com/dell/inspiron-2200.htm dell inspiron 2200 akku http://www.akkuliste.com/dell/e1405.htm dell e1405 akku http://www.akkuliste.com/dell/inspiron-630m.htm dell inspiron 630m akku http://www.akkuliste.com/dell/inspiron-640m.htm dell inspiron 640m akku http://www.akkuliste.com/dell/xps-m140.htm dell xps m140 akku http://www.akkuliste.com/dell/xps-1330.htm dell xps 1330 akku http://www.akkuliste.com/dell/xps-m1330.htm dell xps m1330 akku http://www.akkuliste.com/dell/inspiron-6400.htm dell inspiron 6400 akku http://www.akkuliste.com/dell/latitude-131l.htm dell latitude 131l akku http://www.akkuliste.com/dell/vostro-1000.htm dell vostro 1000 akku http://www.akkuliste.com/dell/1501.htm dell 1501 akku http://www.akkuliste.com/dell/e1505.htm dell e1505 akku http://www.akkuliste.com/dell/kd476.htm dell kd476 akku http://www.akkuliste.com/dell/gd761.htm dell gd761 akku http://www.akkuliste.com/dell/312-0273.htm dell 312-0273 akku http://www.akkuliste.com/dell/312-0417.htm dell 312-0417 akku http://www.akkuliste.com/dell/451-10180.htm dell 451-10180 akku http://www.akkuliste.com/dell/c2174.htm dell c2174 akku http://www.akkuliste.com/dell/f1244.htm dell f1244 akku http://www.akkuliste.com/dell/g1947.htm dell g1947 akku http://www.akkuliste.com/dell/h5559.htm dell h5559 akku http://www.akkuliste.com/dell/hj424.htm dell hj424 akku http://www.akkuliste.com/dell/inspiron-9100.htm dell inspiron 9100 akku http://www.akkuliste.com/dell/dell-inspiron-xps.htm dell dell inspiron xps akku http://www.akkuliste.com/dell/inspiron-2000.htm dell inspiron 2000 akku http://www.akkuliste.com/dell/inspiron-2100.htm dell inspiron 2100 akku http://www.akkuliste.com/dell/inspiron-2800.htm dell inspiron 2800 akku

Comments are closed.