View source code
Display the source code in core/sync/rwmutex.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Class core.sync.rwmutex.ReadWriteMutex

This class represents a mutex that allows any number of readers to enter, but when a writer enters, all other readers and writers are blocked.

class ReadWriteMutex ;

Please note that this mutex is not recursive and is intended to guard access to data only. Also, no deadlock checking is in place because doing so would require dynamic memory allocation, which would reduce performance by an unacceptable amount. As a result, any attempt to recursively acquire this mutex may well deadlock the caller, particularly if a write lock is acquired while holding a read lock, or vice-versa. In practice, this should not be an issue however, because it is uncommon to call deeply into unknown code while holding a lock that simply protects data.

Constructors

NameDescription
this (policy) Initializes a read/write mutex object with the supplied policy.

Properties

NameTypeDescription
policy[get] ReadWriteMutex.PolicyGets the policy used by this mutex.
reader[get] ReadWriteMutex.ReaderGets an object representing the reader lock for the associated mutex.
writer[get] ReadWriteMutex.WriterGets an object representing the writer lock for the associated mutex.

Methods

NameDescription
factory (classname) Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor.
opCmp (o) Compare with another Object obj.
opEquals (o) Test whether this is equal to o. The default implementation only compares by identity (using the is operator). Generally, overrides and overloads for opEquals should attempt to compare objects by their contents. A class will most likely want to add an overload that takes your specific type as the argument and does the content comparison. Then you can override this and forward it to your specific typed overload with a cast. Remember to check for null on the typed overload.
toHash () Compute hash function for Object.
toString () Convert Object to a human readable string.

Inner classes

NameDescription
Reader This class can be considered a mutex in its own right, and is used to negotiate a read lock for the enclosing mutex.
Writer This class can be considered a mutex in its own right, and is used to negotiate a write lock for the enclosing mutex.

Enums

NameDescription
Policy Defines the policy used by this mutex. Currently, two policies are defined.

Example

import core.atomic, core.thread, core.sync.semaphore;

static void runTest(ReadWriteMutex.Policy policy)
{
    scope mutex = new ReadWriteMutex(policy);
    scope rdSemA = new Semaphore, rdSemB = new Semaphore,
          wrSemA = new Semaphore, wrSemB = new Semaphore;
    shared size_t numReaders, numWriters;

    void readerFn()
    {
        synchronized (mutex.reader)
        {
            atomicOp!"+="(numReaders, 1);
            rdSemA.notify();
            rdSemB.wait();
            atomicOp!"-="(numReaders, 1);
        }
    }

    void writerFn()
    {
        synchronized (mutex.writer)
        {
            atomicOp!"+="(numWriters, 1);
            wrSemA.notify();
            wrSemB.wait();
            atomicOp!"-="(numWriters, 1);
        }
    }

    void waitQueued(size_t queuedReaders, size_t queuedWriters)
    {
        for (;;)
        {
            synchronized (mutex.m_commonMutex)
            {
                if (mutex.m_numQueuedReaders == queuedReaders &&
                    mutex.m_numQueuedWriters == queuedWriters)
                    break;
            }
            Thread.yield();
        }
    }

    scope group = new ThreadGroup;

    // 2 simultaneous readers
    group.create(&readerFn); group.create(&readerFn);
    rdSemA.wait(); rdSemA.wait();
    writeln(numReaders); // 2
    rdSemB.notify(); rdSemB.notify();
    group.joinAll();
    writeln(numReaders); // 0
    foreach (t; group) group.remove(t);

    // 1 writer at a time
    group.create(&writerFn); group.create(&writerFn);
    wrSemA.wait();
    assert(!wrSemA.tryWait());
    writeln(numWriters); // 1
    wrSemB.notify();
    wrSemA.wait();
    writeln(numWriters); // 1
    wrSemB.notify();
    group.joinAll();
    writeln(numWriters); // 0
    foreach (t; group) group.remove(t);

    // reader and writer are mutually exclusive
    group.create(&readerFn);
    rdSemA.wait();
    group.create(&writerFn);
    waitQueued(0, 1);
    assert(!wrSemA.tryWait());
    assert(numReaders == 1 && numWriters == 0);
    rdSemB.notify();
    wrSemA.wait();
    assert(numReaders == 0 && numWriters == 1);
    wrSemB.notify();
    group.joinAll();
    assert(numReaders == 0 && numWriters == 0);
    foreach (t; group) group.remove(t);

    // writer and reader are mutually exclusive
    group.create(&writerFn);
    wrSemA.wait();
    group.create(&readerFn);
    waitQueued(1, 0);
    assert(!rdSemA.tryWait());
    assert(numReaders == 0 && numWriters == 1);
    wrSemB.notify();
    rdSemA.wait();
    assert(numReaders == 1 && numWriters == 0);
    rdSemB.notify();
    group.joinAll();
    assert(numReaders == 0 && numWriters == 0);
    foreach (t; group) group.remove(t);

    // policy determines whether queued reader or writers progress first
    group.create(&writerFn);
    wrSemA.wait();
    group.create(&readerFn);
    group.create(&writerFn);
    waitQueued(1, 1);
    assert(numReaders == 0 && numWriters == 1);
    wrSemB.notify();

    if (policy == ReadWriteMutex.Policy.PREFER_READERS)
    {
        rdSemA.wait();
        assert(numReaders == 1 && numWriters == 0);
        rdSemB.notify();
        wrSemA.wait();
        assert(numReaders == 0 && numWriters == 1);
        wrSemB.notify();
    }
    else if (policy == ReadWriteMutex.Policy.PREFER_WRITERS)
    {
        wrSemA.wait();
        assert(numReaders == 0 && numWriters == 1);
        wrSemB.notify();
        rdSemA.wait();
        assert(numReaders == 1 && numWriters == 0);
        rdSemB.notify();
    }
    group.joinAll();
    assert(numReaders == 0 && numWriters == 0);
    foreach (t; group) group.remove(t);
}
runTest(ReadWriteMutex.Policy.PREFER_READERS);
runTest(ReadWriteMutex.Policy.PREFER_WRITERS);

Authors

Sean Kelly

License

Boost License 1.0