void tst_QWaitCondition::wait_QReadWriteLock()
{
    {
        QReadWriteLock readWriteLock(QReadWriteLock::Recursive);
        QWaitCondition waitCondition;

        // ensure that the lockForRead is correctly restored
        readWriteLock.lockForRead();

        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        QVERIFY(!readWriteLock.tryLockForWrite());
        QVERIFY(readWriteLock.tryLockForRead());
        readWriteLock.unlock();
        QVERIFY(!readWriteLock.tryLockForWrite());
        readWriteLock.unlock();

        QVERIFY(readWriteLock.tryLockForWrite());
        readWriteLock.unlock();
    }

    {
        QReadWriteLock readWriteLock(QReadWriteLock::Recursive);
        QWaitCondition waitCondition;

        // ensure that the lockForWrite is correctly restored
        readWriteLock.lockForWrite();

        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        QVERIFY(!readWriteLock.tryLockForRead());
        QVERIFY(readWriteLock.tryLockForWrite());
        readWriteLock.unlock();
        QVERIFY(!readWriteLock.tryLockForRead());
        readWriteLock.unlock();

        QVERIFY(readWriteLock.tryLockForRead());
        readWriteLock.unlock();
    }


    int x;
    for (int i = 0; i < iterations; ++i) {
        {
            QReadWriteLock readWriteLock;
            QWaitCondition waitCondition;

            readWriteLock.lockForRead();

            waitCondition.wakeOne();
            QVERIFY(!waitCondition.wait(&readWriteLock, 1));

            waitCondition.wakeAll();
            QVERIFY(!waitCondition.wait(&readWriteLock, 1));

            readWriteLock.unlock();
        }

        {
            QReadWriteLock readWriteLock;
            QWaitCondition waitCondition;

            readWriteLock.lockForWrite();

            waitCondition.wakeOne();
            QVERIFY(!waitCondition.wait(&readWriteLock, 1));

            waitCondition.wakeAll();
            QVERIFY(!waitCondition.wait(&readWriteLock, 1));

            readWriteLock.unlock();
        }

        {
            // test multiple threads waiting on separate wait conditions
            wait_QReadWriteLock_Thread_1 thread[ThreadCount];

            for (x = 0; x < ThreadCount; ++x) {
                thread[x].readWriteLock.lockForRead();
                thread[x].start();
                // wait for thread to start
                QVERIFY(thread[x].cond.wait(&thread[x].readWriteLock, 1000));
                thread[x].readWriteLock.unlock();
            }

            for (x = 0; x < ThreadCount; ++x) {
                QVERIFY(thread[x].isRunning());
                QVERIFY(!thread[x].isFinished());
            }

            for (x = 0; x < ThreadCount; ++x) {
                thread[x].readWriteLock.lockForRead();
                thread[x].cond.wakeOne();
                thread[x].readWriteLock.unlock();
            }

            for (x = 0; x < ThreadCount; ++x) {
                QVERIFY(thread[x].wait(1000));
            }
        }

        {
            // test multiple threads waiting on a wait condition
            QReadWriteLock readWriteLock;
            QWaitCondition cond1, cond2;
            wait_QReadWriteLock_Thread_2 thread[ThreadCount];

            readWriteLock.lockForWrite();
            for (x = 0; x < ThreadCount; ++x) {
                thread[x].readWriteLock = &readWriteLock;
                thread[x].cond = (x < ThreadCount / 2) ? &cond1 : &cond2;
                thread[x].start();
                // wait for thread to start
                QVERIFY(thread[x].started.wait(&readWriteLock, 1000));
            }
            readWriteLock.unlock();

            for (x = 0; x < ThreadCount; ++x) {
                QVERIFY(thread[x].isRunning());
                QVERIFY(!thread[x].isFinished());
            }

            readWriteLock.lockForWrite();
            cond1.wakeAll();
            cond2.wakeAll();
            readWriteLock.unlock();

            for (x = 0; x < ThreadCount; ++x) {
                QVERIFY(thread[x].wait(1000));
            }
        }
    }
}
示例#2
0
void tst_QWaitCondition::wait_QReadWriteLock()
{
    {
        QReadWriteLock readWriteLock(QReadWriteLock::Recursive);
        QWaitCondition waitCondition;

        // ensure that the lockForRead is correctly restored
        readWriteLock.lockForRead();

        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        QVERIFY(!readWriteLock.tryLockForWrite());
        QVERIFY(readWriteLock.tryLockForRead());
        readWriteLock.unlock();
        QVERIFY(!readWriteLock.tryLockForWrite());
        readWriteLock.unlock();

        QVERIFY(readWriteLock.tryLockForWrite());
        readWriteLock.unlock();
    }

    {
        QReadWriteLock readWriteLock(QReadWriteLock::Recursive);
        QWaitCondition waitCondition;

        // ensure that the lockForWrite is correctly restored
        readWriteLock.lockForWrite();

        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        QVERIFY(!readWriteLock.tryLockForRead());
        QVERIFY(readWriteLock.tryLockForWrite());
        readWriteLock.unlock();
        QVERIFY(!readWriteLock.tryLockForRead());
        readWriteLock.unlock();

        QVERIFY(readWriteLock.tryLockForRead());
        readWriteLock.unlock();
    }


    int x;
    for (int i = 0; i < iterations; ++i) {
    {
        QReadWriteLock readWriteLock;
        QWaitCondition waitCondition;

        readWriteLock.lockForRead();

        waitCondition.wakeOne();
        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        waitCondition.wakeAll();
        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        readWriteLock.unlock();
    }

    {
        QReadWriteLock readWriteLock;
        QWaitCondition waitCondition;

        readWriteLock.lockForWrite();

        waitCondition.wakeOne();
        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        waitCondition.wakeAll();
        QVERIFY(!waitCondition.wait(&readWriteLock, 1));

        readWriteLock.unlock();
    }

    {
        // test multiple threads waiting on separate wait conditions
        wait_QReadWriteLock_Thread_1 thread[ThreadCount];

        for (x = 0; x < ThreadCount; ++x) {
        thread[x].readWriteLock.lockForRead();
        thread[x].start();
        // wait for thread to start
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_WINSCW)
        // Symbian emulator startup simultaneously with this thread causes additional delay
        QVERIFY(thread[x].cond.wait(&thread[x].readWriteLock, 10000));
#else
        QVERIFY(thread[x].cond.wait(&thread[x].readWriteLock, 1000));
#endif
        thread[x].readWriteLock.unlock();
        }

        for (x = 0; x < ThreadCount; ++x) {
        QVERIFY(thread[x].isRunning());
        QVERIFY(!thread[x].isFinished());
        }

        for (x = 0; x < ThreadCount; ++x) {
        thread[x].readWriteLock.lockForRead();
        thread[x].cond.wakeOne();
        thread[x].readWriteLock.unlock();
        }

        for (x = 0; x < ThreadCount; ++x) {
        QVERIFY(thread[x].wait(1000));
        }
    }

    {
        // test multiple threads waiting on a wait condition
        QReadWriteLock readWriteLock;
        QWaitCondition cond1, cond2;
        wait_QReadWriteLock_Thread_2 thread[ThreadCount];

        readWriteLock.lockForWrite();
        for (x = 0; x < ThreadCount; ++x) {
        thread[x].readWriteLock = &readWriteLock;
        thread[x].cond = (x < ThreadCount / 2) ? &cond1 : &cond2;
        thread[x].start();
        // wait for thread to start
        QVERIFY(thread[x].started.wait(&readWriteLock, 1000));
        }
        readWriteLock.unlock();

        for (x = 0; x < ThreadCount; ++x) {
        QVERIFY(thread[x].isRunning());
        QVERIFY(!thread[x].isFinished());
        }

        readWriteLock.lockForWrite();
        cond1.wakeAll();
        cond2.wakeAll();
        readWriteLock.unlock();

        for (x = 0; x < ThreadCount; ++x) {
        QVERIFY(thread[x].wait(1000));
        }
    }
    }

}