tst_atomic: make tst_atomic_inc take a signed integer
PATCH v3 1/4 tst_atomic: make tst_atomic_inc take a signed integer
This is a preparation for upcoming patches, which add atomic_add_return(), based on kernel function of same name. Kernel's atomic_t is int based, so let's match that.
--- doc/test-writing-guidelines.txt | 2 +- include/tst_atomic.h | 2 +- lib/newlib_tests/test08.c | 2 +- lib/tst_test.c | 16 ++++++++-------- 4 files changed, 11 insertions(+), 11 deletions(-)
Changes in v3:
- no changes
doc/test-writing-guidelines.txt +1 -1
@@ -780,7 +780,7 @@ the user supplied cleanup to the test library. static void cleanup(void) { - static unsigned int flag; + static int flag; if (tst_atomic_inc(&flag) != 1) pthread_exit(NULL);
include/tst_atomic.h +1 -1
@@ -18,7 +18,7 @@#ifndef TST_ATOMIC_H__ #define TST_ATOMIC_H__ -static inline unsigned int tst_atomic_inc (unsigned int *v) +static inline int tst_atomic_inc (int *v) {return __sync_add_and_fetch (v,1 ); }
lib/newlib_tests/test08.c +1 -1
@@ -35,7 +35,7 @@ static void setup(void)static void cleanup (void ) { -static unsigned int flag; +static int flag;/* Avoid subsequent threads to enter the cleanup */ if (tst_atomic_inc (&flag) !=1 )
lib/tst_test.c +8 -8
@@ -43,10 +43,10 @@ static float duration = -1;static pid_t main_pid;struct results { -unsigned int passed; -unsigned int skipped; -unsigned int failed; -unsigned int warnings; +int passed; +int skipped; +int failed; +int warnings; };static struct results *results;
@@ -442,10 +442,10 @@ static void do_exit(void)int ret =0 ;printf ("\nSummary:\n" ); -printf ("passed %u\n" , results->passed); -printf ("failed %u\n" , results->failed); -printf ("skipped %u\n" , results->skipped); -printf ("warnings %u\n" , results->warnings); +printf ("passed %d\n" , results->passed); +printf ("failed %d\n" , results->failed); +printf ("skipped %d\n" , results->skipped); +printf ("warnings %d\n" , results->warnings);if (results->failed) ret |=TFAIL ;
Signature · 1 line
1.8.3.1
PATCH v3 2/4 m4: add a check for __sync_add_and_fetch
Compilers older than gcc 4.1.2 do not provide __sync_add_and_fetch, newer ones usually do, but it still may be missing for some targets.
--- configure.ac | 1 + include/tst_atomic.h | 14 +++++++++++++- m4/ltp-sync_add_and_fetch.m4 | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 m4/ltp-sync_add_and_fetch.m4
Changes in v3:
- added tst_ prefix to atomic_add_return
- dropped always_inline attribute, kept just inline
configure.ac +1 -0
@@ -186,5 +186,6 @@ LTP_CHECK_PREADV LTP_CHECK_PWRITEV LTP_CHECK_EPOLL_PWAIT LTP_CHECK_KEYUTILS_SUPPORT +LTP_CHECK_SYNC_ADD_AND_FETCH AC_OUTPUT
include/tst_atomic.h +13 -1
@@ -18,9 +18,21 @@#ifndef TST_ATOMIC_H__ #define TST_ATOMIC_H__ +#include "config.h" + +#if HAVE_SYNC_ADD_AND_FETCH ==1 +static inline int tst_atomic_add_return (int i,int *v) +{ +return __sync_add_and_fetch (v, i); +} +#else +# error Your compiler does not provide __sync_add_and_fetch and LTP\ + implementation is missing for your architecture. +#endif +static inline int tst_atomic_inc (int *v) { -return __sync_add_and_fetch (v,1 ); +return tst_atomic_add_return (1 , v); }#endif /* TST_ATOMIC_H__ */
m4/ltp-sync_add_and_fetch.m4 new file +29 -0
@@ -0,0 +1,29 @@ +dnl +dnl Copyright (c) Linux Test Project, 2016 +dnl +dnl This program is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 2 of the License, or +dnl (at your option) any later version. +dnl +dnl This program is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +dnl the GNU General Public License for more details. +dnl + +AC_DEFUN([LTP_CHECK_SYNC_ADD_AND_FETCH],[dnl + AC_MSG_CHECKING([for __sync_add_and_fetch]) + AC_LINK_IFELSE([AC_LANG_SOURCE([ +int main(void) { + int i = 0; + return __sync_add_and_fetch(&i, 1); +}])],[has_saac="yes"]) + +if test "x$has_saac" = xyes; then + AC_DEFINE(HAVE_SYNC_ADD_AND_FETCH,1,[Define to 1 if you have __sync_add_and_fetch]) + AC_MSG_RESULT(yes) +else + AC_MSG_RESULT(no) +fi +])
Signature · 1 line
1.8.3.1
PATCH v3 3/4 tst_atomic: add atomic_add_return for x86/64, ppc/64 and s390/x
In reply to Jan StancekIn case __sync_add_and_fetch is not provided by compiler, we try to supply our own implementation.
It has been taken from kernel 4.5 sources, by compiling a small piece of code (atomic_add_return()) with "gcc -E" and applying some formatting to make it more readable/pretty.
--- include/tst_atomic.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
Changes in v3:
- eliminated some preprocessor nesting by using #elif
- dropped HAVE_ATOMIC_ADD_RETURN define
include/tst_atomic.h +77 -1
@@ -25,7 +25,83 @@ static inline int tst_atomic_add_return(int i, int *v) {return __sync_add_and_fetch (v, i); } -#else + +#elif defined(__i386__) || defined(__x86_64__) +static inline int tst_atomic_add_return (int i,int *v) +{ +int __ret = i; + +/* +* taken from arch/x86/include/asm/cmpxchg.h +* Since we always pass int sized parameter, we can simplify it +* and cherry-pick only that specific case. +* +switch (sizeof(*v)) { +case 1: +asm volatile ("lock; xaddb %b0, %1\n" +: "+q" (__ret), "+m" (*v) : : "memory", "cc"); +break; +case 2: +asm volatile ("lock; xaddw %w0, %1\n" +: "+r" (__ret), "+m" (*v) : : "memory", "cc"); +break; +case 4: +asm volatile ("lock; xaddl %0, %1\n" +: "+r" (__ret), "+m" (*v) : : "memory", "cc"); +break; +case 8: +asm volatile ("lock; xaddq %q0, %1\n" +: "+r" (__ret), "+m" (*v) : : "memory", "cc"); +break; +default: +__xadd_wrong_size(); +} +*/ + asmvolatile ("lock; xaddl %0, %1\n" + :"+r" (__ret),"+m" (*v) : :"memory" ,"cc" ); + +return i + __ret; +} + +#elif defined(__powerpc__) || defined(__powerpc64__) +static inline int tst_atomic_add_return (int i,int *v) +{ +int t; + +/* taken from arch/powerpc/include/asm/atomic.h */ + asmvolatile ( +" sync\n" +"1: lwarx %0,0,%2 # atomic_add_return\n" +" add %0,%1,%0\n" +" stwcx. %0,0,%2 \n" +" bne- 1b\n" +" sync\n" + :"=&r" (t) + :"r" (i),"r" (v) + :"cc" ,"memory" ); + +return t; +} + +#elif defined(__s390__) || defined(__s390x__) +static inline int tst_atomic_add_return (int i,int *v) +{ +int old_val, new_val; + +/* taken from arch/s390/include/asm/atomic.h */ + asmvolatile ( +" l %0,%2\n" +"0: lr %1,%0\n" +" ar %1,%3\n" +" cs %0,%1,%2\n" +" jl 0b" + :"=&d" (old_val),"=&d" (new_val),"+Q" (*v) + :"d" (i) + :"cc" ,"memory" ); + +return old_val + i; +} +#else /* HAVE_SYNC_ADD_AND_FETCH == 1 */ # error Your compiler does not provide __sync_add_and_fetch and LTP\ implementation is missing for your architecture.#endif
Signature · 1 line
1.8.3.1
PATCH v3 4/4 tst_atomic: add test for tst_atomic_inc
In reply to Jan StancekSpawn a lot of threads and run tst_atomic_inc in a loop. At the end we expect value to be THREADS * ITERATIONS.
--- lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/Makefile | 1 + lib/newlib_tests/test09.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 lib/newlib_tests/test09.c
Changes in v3:
- added test09 to .gitignore
lib/newlib_tests/.gitignore +1 -0
@@ -6,3 +6,4 @@ test05 test06 test07 test08 +test09
lib/newlib_tests/Makefile +1 -0
@@ -6,5 +6,6 @@ CFLAGS += -W -Wall LDLIBS += -lltp test08: CFLAGS+=-pthread +test09: CFLAGS+=-pthread include $(top_srcdir)/include/mk/generic_leaf_target.mk
lib/newlib_tests/test09.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v3 4/4 tst_atomic: add test for tst_atomic_inc
Hi!
This version looks good, acked.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 4/4 tst_atomic: add test for tst_atomic_inc
----- Original Message -----
Quoted 8 lines
From: "Cyril Hrubis" <[email protected]>
To: "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Monday, 18 April, 2016 2:40:57 PM
Subject: Re: [LTP] [PATCH v3 4/4]tst_atomic: add test fortst_atomic_incHi!
This version looks good, acked.
Series pushed.
Regards,
Jan