From: Bart Van Assche Date: Sun, 15 Feb 2009 10:19:35 +0000 (+0000) Subject: Cleaned up the source code of the atomic_var regression test, without changing the... X-Git-Tag: svn/VALGRIND_3_5_0~985 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6dacd9c6ffc17de5bcdd20101b5dfd0c3f2f0bda;p=thirdparty%2Fvalgrind.git Cleaned up the source code of the atomic_var regression test, without changing the actual test. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9161 --- diff --git a/drd/tests/atomic_var.c b/drd/tests/atomic_var.c index a4e6ab2f37..ebad1ee6c0 100644 --- a/drd/tests/atomic_var.c +++ b/drd/tests/atomic_var.c @@ -1,8 +1,13 @@ -/** Race condition around use of atomic variable. - * Note: for the i386 and x86_64 memory models, thread 2 must print y = 1. - * On PPC however, both y = 0 and y = 1 are legal results. This is because - * the PPC memory model allows different CPU's to observe stores to variables - * in different cache lines in a different order. +/** + * This test program triggers a single race condition on variable s_y. + * Although another variable (s_x) is also modified by both threads, no race + * condition must be reported on this variable since it is only accessed via + * atomic instructions. + * + * Note: for the i386 and x86_64 memory models, thread 2 must print y = 1. + * On PPC however, both y = 0 and y = 1 are legal results. This is because + * the PPC memory model allows different CPU's to observe stores to variables + * in different cache lines in a different order. */ @@ -14,28 +19,19 @@ #include "../../config.h" -/** Only gcc 4.1.0 and later have atomic builtins. */ +/* Atomic builtins are only supported by gcc 4.1.0 and later. */ + #if defined(HAVE_BUILTIN_ATOMIC) + static __inline__ int sync_add_and_fetch(int* p, int i) { return __sync_add_and_fetch(p, i); } -#else -static __inline__ -int sync_add_and_fetch(int* p, int i) -{ - if (i == 0) - return *p; - return (*p += i); -} -#endif - -#ifdef HAVE_BUILTIN_ATOMIC static int s_x = 0; -/* s_dummy[] ensures that s_x and s_y are not in the same cache line. */ -static char s_dummy[512]; +/* g_dummy[] ensures that s_x and s_y are not in the same cache line. */ +char g_dummy[512]; static int s_y = 0; static void* thread_func_1(void* arg) @@ -52,11 +48,9 @@ static void* thread_func_2(void* arg) fprintf(stderr, "y = %d\n", s_y); return 0; } -#endif int main(int argc, char** argv) { -#ifdef HAVE_BUILTIN_ATOMIC int i; const int n_threads = 2; pthread_t tid[n_threads]; @@ -68,13 +62,17 @@ int main(int argc, char** argv) pthread_join(tid[i], 0); fprintf(stderr, "Test finished.\n"); - /* Suppress the compiler warning about s_dummy not being used. */ - s_dummy[0]++; + return 0; +} + #else + +int main(int argc, char** argv) +{ fprintf(stderr, "Sorry, but your compiler does not have built-in support for atomic" " operations.\n"); -#endif - return 0; } + +#endif diff --git a/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-1 b/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-1 index 09948055f1..859f3649c8 100644 --- a/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-1 +++ b/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-1 @@ -7,7 +7,7 @@ Conflicting load by thread 2/3 at 0x........ size 4 by 0x........: (within libpthread-?.?.so) by 0x........: clone (in /...libc...) Location 0x........ is 0 bytes inside local var "s_y" -declared at atomic_var.c:39, in frame #? of thread 2 +declared at atomic_var.c:35, in frame #? of thread 2 y = 1 Test finished. diff --git a/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-2 b/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-2 index 2f82e10f87..45bf438166 100644 --- a/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-2 +++ b/drd/tests/atomic_var.stderr.exp-with-atomic-builtins-2 @@ -7,7 +7,7 @@ Conflicting load by thread 3/3 at 0x........ size 4 by 0x........: (within libpthread-?.?.so) by 0x........: clone (in /...libc...) Location 0x........ is 0 bytes inside local var "s_y" -declared at atomic_var.c:39, in frame #? of thread 3 +declared at atomic_var.c:35, in frame #? of thread 3 y = 1 Test finished.