From: Bart Van Assche Date: Mon, 7 Jul 2008 18:35:13 +0000 (+0000) Subject: Added Qt4 reader-writer lock test. X-Git-Tag: svn/VALGRIND_3_4_0~354 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=230460525ef8ad85ef6a6f812ed2ceaa95353cbe;p=thirdparty%2Fvalgrind.git Added Qt4 reader-writer lock test. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8384 --- diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am index c485f7c28c..1d6715dcb3 100644 --- a/drd/tests/Makefile.am +++ b/drd/tests/Makefile.am @@ -94,6 +94,8 @@ EXTRA_DIST = \ pth_spinlock.vgtest \ qt4_mutex.stderr.exp \ qt4_mutex.vgtest \ + qt4_rwlock.stderr.exp \ + qt4_rwlock.vgtest \ qt4_semaphore.stderr.exp \ qt4_semaphore.vgtest \ recursive_mutex.stderr.exp \ @@ -234,7 +236,7 @@ check_PROGRAMS = \ trylock if HAVE_QTCORE -check_PROGRAMS += qt4_mutex qt4_semaphore +check_PROGRAMS += qt4_mutex qt4_rwlock qt4_semaphore endif if HAVE_OPENMP @@ -312,6 +314,9 @@ if HAVE_QTCORE qt4_mutex_SOURCES = qt4_mutex.cpp qt4_mutex_LDADD = -lQtCore -lpthread +qt4_rwlock_SOURCES = qt4_rwlock.cpp +qt4_rwlock_LDADD = -lQtCore -lpthread + qt4_semaphore_SOURCES = qt4_semaphore.cpp qt4_semaphore_LDADD = -lQtCore -lpthread endif diff --git a/drd/tests/qt4_rwlock.cpp b/drd/tests/qt4_rwlock.cpp new file mode 100644 index 0000000000..e9cc5cafda --- /dev/null +++ b/drd/tests/qt4_rwlock.cpp @@ -0,0 +1,86 @@ +/// Qt4 reader-writer lock test. + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include // class QThread +#include // class QReadWriteLock +#include // fprintf() +#include // atoi() +#include +#include // pthread_barrier_t +#include + + +static pthread_barrier_t s_barrier; +static QReadWriteLock* s_pRWlock; +static int s_iterations; +static int s_counter; + + +class IncThread: public QThread +{ + virtual void run(); +}; + +void IncThread::run() +{ + int i; + + pthread_barrier_wait(&s_barrier); + for (i = s_iterations; i > 0; i--) + { + s_pRWlock->lockForWrite(); + s_counter++; + s_pRWlock->unlock(); + } +} + +int main(int argc, char** argv) +{ + int i; + const int n_threads = 10; + std::vector tid(n_threads); + + s_iterations = argc > 1 ? atoi(argv[1]) : 1000; + + fprintf(stderr, "Start of test.\n"); + + { + // Stack-allocated reader-writer lock. + QReadWriteLock RWL(QReadWriteLock::Recursive); + RWL.lockForRead(); + RWL.lockForRead(); + RWL.unlock(); + RWL.unlock(); + RWL.lockForWrite(); + RWL.lockForWrite(); + RWL.unlock(); + RWL.unlock(); + } + + pthread_barrier_init(&s_barrier, 0, n_threads); + s_pRWlock = new QReadWriteLock(); + for (i = 0; i < n_threads; i++) + { + tid[i] = new IncThread; + tid[i]->start(); + } + for (i = 0; i < n_threads; i++) + { + tid[i]->wait(); + delete tid[i]; + } + delete s_pRWlock; + s_pRWlock = 0; + pthread_barrier_destroy(&s_barrier); + + if (s_counter == n_threads * s_iterations) + fprintf(stderr, "Test successful.\n"); + else + fprintf(stderr, "Test failed: counter = %d, should be %d\n", + s_counter, n_threads * s_iterations); + + return 0; +} diff --git a/drd/tests/qt4_rwlock.stderr.exp b/drd/tests/qt4_rwlock.stderr.exp new file mode 100644 index 0000000000..21dba22019 --- /dev/null +++ b/drd/tests/qt4_rwlock.stderr.exp @@ -0,0 +1,5 @@ + +Start of test. +Test successful. + +ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/drd/tests/qt4_rwlock.vgtest b/drd/tests/qt4_rwlock.vgtest new file mode 100644 index 0000000000..6ca8542bf2 --- /dev/null +++ b/drd/tests/qt4_rwlock.vgtest @@ -0,0 +1,2 @@ +vgopts: --check-stack-var=yes +prog: qt4_rwlock