From: Bart Van Assche Date: Mon, 7 Jul 2008 08:04:08 +0000 (+0000) Subject: Added regression test for POSIX spinlocks. X-Git-Tag: svn/VALGRIND_3_4_0~366 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53a4604803746812554ab8071ed18b9c66450845;p=thirdparty%2Fvalgrind.git Added regression test for POSIX spinlocks. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8370 --- diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am index cafb77cc16..2a89af68c2 100644 --- a/drd/tests/Makefile.am +++ b/drd/tests/Makefile.am @@ -90,6 +90,8 @@ EXTRA_DIST = \ pth_detached_sem.vgtest \ pth_inconsistent_cond_wait.stderr.exp \ pth_inconsistent_cond_wait.vgtest \ + pth_spinlock.stderr.exp \ + pth_spinlock.vgtest \ recursive_mutex.stderr.exp \ recursive_mutex.stdout.exp \ recursive_mutex.vgtest \ @@ -196,6 +198,7 @@ check_PROGRAMS_COMMON = \ pth_detached \ pth_detached_sem \ pth_inconsistent_cond_wait \ + pth_spinlock \ recursive_mutex \ rwlock_race \ rwlock_test \ @@ -300,6 +303,9 @@ pth_detached_sem_LDADD = -lpthread pth_inconsistent_cond_wait_SOURCES = pth_inconsistent_cond_wait.c pth_inconsistent_cond_wait_LDADD = -lpthread +pth_spinlock_SOURCES = pth_spinlock.c +pth_spinlock_LDADD = -lpthread + recursive_mutex_SOURCES = recursive_mutex.c recursive_mutex_LDADD = -lpthread diff --git a/drd/tests/pth_spinlock.c b/drd/tests/pth_spinlock.c new file mode 100644 index 0000000000..0f56a0ba42 --- /dev/null +++ b/drd/tests/pth_spinlock.c @@ -0,0 +1,57 @@ +/** pthread_spinloc_t test. */ + + +/* Make sure pthread_spinlock_t is available when compiling with older glibc + * versions (2.3 or before). + */ +#define _GNU_SOURCE + +#include +#include /* fprintf() */ +#include /* atoi() */ + + +static pthread_barrier_t s_barrier; +static pthread_spinlock_t s_spinlock; +static int s_iterations; +static int s_counter; + + +static void* thread_func(void* arg) +{ + int i; + + pthread_barrier_wait(&s_barrier); + for (i = s_iterations; i > 0; i--) + { + pthread_spin_lock(&s_spinlock); + s_counter++; + pthread_spin_unlock(&s_spinlock); + } + return 0; +} + +int main(int argc, char** argv) +{ + int i; + const int n_threads = 10; + pthread_t tid[n_threads]; + + s_iterations = argc > 1 ? atoi(argv[1]) : 1000; + + fprintf(stderr, "Start of test.\n"); + pthread_barrier_init(&s_barrier, 0, n_threads); + pthread_spin_init(&s_spinlock, 0); + for (i = 0; i < n_threads; i++) + pthread_create(&tid[i], 0, thread_func, 0); + for (i = 0; i < n_threads; i++) + pthread_join(tid[i], 0); + pthread_spin_destroy(&s_spinlock); + 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/pth_spinlock.stderr.exp b/drd/tests/pth_spinlock.stderr.exp new file mode 100644 index 0000000000..21dba22019 --- /dev/null +++ b/drd/tests/pth_spinlock.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/pth_spinlock.vgtest b/drd/tests/pth_spinlock.vgtest new file mode 100644 index 0000000000..9b9333ffb1 --- /dev/null +++ b/drd/tests/pth_spinlock.vgtest @@ -0,0 +1,3 @@ +prereq: ./supported_libpthread +vgopts: --var-info=yes --check-stack-var=yes +prog: pth_spinlock