/*
   Writer1 acquires a write-lock
   Readers try to acquire read-locks but are blocked
   Writer2 tries to acquire a write-lock, but is blocked
   Writer1 releases the lock, writer2 gets the lock, readers still blocked
   Writer2 releases the lock, readers are unblocked and get their read-locks
*/
void tst_QSystemReadWriteLock::writerPrecedence()
{
    bool print = false;
    writeThreadStarted = false;
    writeThreadDone = false;
    release = false;

    QSystemReadWriteLock testRwLock("Viper", QSystemReadWriteLock::Create);
    if (print)
        qDebug() << "Main Thread: About to lock for writing";
    testRwLock.lockForWrite();
    if (print)
        qDebug() << "Main Thread: After lock for writing";

    const int numReaders = 5;
    ReadLockThread *readers[numReaders];
    for (int i = 0; i < numReaders; ++i) {
        readers[i] = new ReadLockThread(print);
        readers[i]->start();
    }

    //check that the readers are still running
    //and so are likely to be correctly blocking
    QTest::qSleep(1000);
    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(!readers[i]->isFinished());
    }

    // Start the writer2 thread and ensure it blocks
    WriteLockReleaseableThread *writer = new WriteLockReleaseableThread(print);
    writer->start();
    QTest::qSleep(1000);
    QVERIFY(writeThreadStarted == true);
    QVERIFY(writeThreadDone == false);
    QVERIFY(!writer->isFinished());

    // Unlock writer1 and verify the readers are still blocked
    testRwLock.unlock();
    if (print)
        qDebug() << "Main Thread: After unlock(write)";
    QTest::qSleep(1000);
    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(!readers[i]->isFinished());
    }

    //Release writer2 and verify that it and the readers
    //finish executing.
    QVERIFY(!writer->isFinished());
    release = true;
    QTest::qSleep(1000);
    QVERIFY(writeThreadDone == true);
    QVERIFY(writer->isFinished());
    for (int i = 0; i < numReaders; ++i ) {
        QVERIFY(readers[i]->isFinished());
        delete readers[i];
    }
    delete writer;
}
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     t.start();
     while (t.elapsed() < runTime)  {
         testRwLock.lockForRead();
         if(count)
             qFatal("Non-zero count at Read! (%d)",count );
         testRwLock.unlock();
         msleep(waitTime);
     }
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     t.start();
     while (t.elapsed()<runTime)  {
         testRwLock.lockForRead();
         //if (print) printf("reading\n");
         if (holdTime) msleep(holdTime);
         testRwLock.unlock();
         if (waitTime) msleep(waitTime);
     }
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     readThreadStarted = true;
     if (print)
         qDebug() << qPrintable(BeforeLockForRead);
     testRwLock.lockForRead();
     if (print)
         qDebug() << qPrintable(AfterLockForRead);
     testRwLock.unlock();
     if (print)
         qDebug() << qPrintable(AfterUnlockForRead);
     readThreadDone = true;
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     t.start();
     while (t.elapsed() < runTime)  {
         if (print) qDebug() << "About to lock for write";
         testRwLock.lockForWrite();
         if (print) qDebug() << "Locked for Write";
         if (holdTime) msleep(holdTime);
         testRwLock.unlock();
         if (print) qDebug() << "unlocked";
         if (waitTime) msleep(waitTime);
     }
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
 if (print)
         qDebug() << qPrintable(BeforeLockForRead);
     testRwLock.lockForRead();
     if (print)
         qDebug() << qPrintable(AfterLockForRead);
     while (release == false) {
         usleep(1);
     }
     testRwLock.unlock();
     if (print)
         qDebug() << qPrintable(AfterUnlockForRead);
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     writeThreadStarted = true;
     if (print)
         qDebug() << BeforeLockForWrite;
     testRwLock.lockForWrite();
     if (print)
         qDebug() << AfterLockForWrite;
     while(release == false) {
         usleep(1);
     }
     testRwLock.unlock();
     if (print)
         qDebug() << AfterUnlockForWrite;
     writeThreadDone = true;
 }
 void run()
 {
     QSystemReadWriteLock testRwLock("Viper");
     t.start();
     while (t.elapsed() < runTime)  {
         testRwLock.lockForWrite();
         if(count)
             qFatal("Non-zero count at start of write! (%d)",count );
         for(int i = 0; i < maxval; ++i) {
             volatile int lc=count;
             ++lc;
             count=lc;
         }
         count=0;
         testRwLock.unlock();
         msleep(waitTime);
     }
 }
Ejemplo n.º 9
0
int main(int argc, char * argv[])
{
    QCoreApplication app(argc, argv);

    QStringList args;
    for (int i = 1; i < argc; ++i)
        args << QString(argv[i]);

    if (args.count() == 0 )
        qFatal("No connection name supplied to lackey process");

    // This is a workaround for the fact that we cannot use stdin/stdout/stderr
    // to communicate with a process on WinCE or Symbian systems.
    // We use sockets instead, and the first argument given is the port number.

    QString connectionName = args.takeFirst();
    QLocalSocket oopSocket;

    if (connectionName != QString("NoComms")) {
        oopSocket.connectToServer(connectionName);
        oopSocket.waitForConnected(-1);
    }

    if (args.count() == 0 )
        qFatal("No arguments supplied to lackey process");

    int retVal = 0;

    if (args[0] == "ReadLock") {
        // 1)read-lock
        // 2)unlock
        QSystemReadWriteLock testRwLock("Viper");
        write(&oopSocket, qPrintable(Lackey::BeforeLockForRead));
        testRwLock.lockForRead();
        write(&oopSocket, qPrintable(Lackey::AfterLockForRead));

        testRwLock.unlock();
        QTest::qSleep(1000);
        write(&oopSocket, qPrintable(Lackey::AfterUnlockForRead));
    } else if (args[0] == "WriteLock") {
        // 1) write-lock
        // 2) unlock
        QSystemReadWriteLock testRwLock("Viper");
        write(&oopSocket, qPrintable(Lackey::BeforeLockForWrite));
        testRwLock.lockForWrite();
        write(&oopSocket, qPrintable(Lackey::AfterLockForWrite));

        testRwLock.unlock();
        QTest::qSleep(1000);
        write(&oopSocket, qPrintable(Lackey::AfterUnlockForWrite));
    } else if (args[0] == "ReadLockReleaseable") {
        // 1) read-lock
        // 2) wait for input on stdin
        // 3) unlock
        QSystemReadWriteLock testRwLock("Viper");
        write(&oopSocket, qPrintable(Lackey::BeforeLockForRead));
        testRwLock.lockForRead();
        QTest::qSleep(1000);
        write(&oopSocket, qPrintable(Lackey::AfterLockForRead));

        readLine(&oopSocket);

        testRwLock.unlock();
        write(&oopSocket, qPrintable(Lackey::AfterUnlockForRead));
    } else if (args[0] == "WriteLockReleaseable") {
        // 1) write-lock
        // 2) wait for input on stdin
        // 3) unlock
        QSystemReadWriteLock testRwLock("Viper");
        write(&oopSocket, qPrintable(Lackey::BeforeLockForWrite));
        testRwLock.lockForWrite();
        write(&oopSocket, qPrintable(Lackey::AfterLockForWrite));

        readLine(&oopSocket);

        testRwLock.unlock();
        write(&oopSocket, qPrintable(Lackey::AfterUnlockForWrite));
    } else if (args[0] == "ReadLockLoop") {
        // for(runTime msecs):
        // 1) read-lock
        // 2) sleep(holdTime msecs)
        // 3) unlock
        // 4) sleep(waitTime msecs)
        Q_ASSERT(args.count() == 4);
        int runTime = args[1].toInt();
        int holdTime = args[2].toInt();
        int waitTime = args[3].toInt();

        QSystemReadWriteLock testRwLock("Viper");
        QTime t;
        t.start();
        while(t.elapsed() < runTime) {
            testRwLock.lockForRead();
            if (holdTime)
                QTest::qSleep(holdTime);
            testRwLock.unlock();
            if(waitTime)
                QTest::qSleep(waitTime);
        }
    } else if (args[0]  == "WriteLockLoop") {
        // for(runTime msecs):
        // 1) read-lock
        // 2) sleep(holdTime msecs)
        // 3) unlock
        // 4) sleep(waitTime msecs)
        Q_ASSERT(args.count() == 4);
        int runTime = args[1].toInt();
        int holdTime = args[2].toInt();
        int waitTime = args[3].toInt();

        QSystemReadWriteLock testRwLock("Viper");
        QTime t;
        t.start();
        while (t.elapsed() < runTime) {
            testRwLock.lockForWrite();
            if (holdTime)
                QTest::qSleep(holdTime);
            testRwLock.unlock();
            if (waitTime)
                QTest::qSleep(waitTime);
        }
    } else if (args[0] == "ReadLockExcl") {
        // for (runTime msecs):
        // 1) read-lock
        // 2) check that the exclusive file does not
        //    exist, if it does qFatal
        // 3) sleep(holdTime msecs)
        // 4) unlock
        // 5) sleep(waitTime msecs)
        Q_ASSERT(args.count() == 4);
        int runTime = args[1].toInt();
        int holdTime = args[2].toInt();
        int waitTime = args[3].toInt();

        QSystemReadWriteLock testRwLock("Viper");
        QTime t;
        t.start();
        while (t.elapsed() < runTime) {
            testRwLock.lockForRead();
            QDir cwd;
            if (cwd.exists("writeLockExcl.tmp"))
                qFatal("writeLockExcl.tmp file found during read!");
            if (holdTime)
                QTest::qSleep(holdTime);
            testRwLock.unlock();
            if (waitTime)
                QTest::qSleep(waitTime);
        }
    } else if (args[0] == "WriteLockExcl") {
        // for(runTime msecs)
        // 1) write-lock
        // 2) check that exclusive file does not
        //      exist, if it does qFatal
        // 3) create the exclusive file,
        //    sleep(holdTime msecs), delete the file
        // 4) unlock
        // 5) sleep(waitTime msecs)
        Q_ASSERT(args.count() == 4);
        int runTime = args[1].toInt();
        int holdTime = args[2].toInt();
        int waitTime = args[3].toInt();

        QSystemReadWriteLock testRwLock("Viper");
        QTime t;
        t.start();
        while(t.elapsed() < runTime) {
            testRwLock.lockForWrite();
            QDir cwd;
            if (cwd.exists("writeLockExcl.tmp"))
                qFatal("writeLockExcl.tmp file found during write!");
            QFile file("writeLockExcl.tmp");
            file.open(QIODevice::ReadWrite);
            if (holdTime)
                QTest::qSleep(holdTime);
            file.close();
            cwd.remove("writeLockExcl.tmp");
            testRwLock.unlock();
            if(waitTime)
                QTest::qSleep(waitTime);
        }
    } else {
        retVal = 1;
    }

    if (connectionName != QString("NoComms")) {
        oopSocket.disconnectFromServer();
        oopSocket.waitForDisconnected(-1);
    }

    return retVal;
}