[ltt-dev] [PATCH 09/11] move whether atomic byte/short exists to uatomic_arch_*.h
Paolo Bonzini
pbonzini at redhat.com
Sat Feb 13 12:16:29 EST 2010
Just a small cleanup that could be used in the future, for example
if i386 was separated from i486+ and x86_64.
Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
tests/test_uatomic.c | 13 +-------
urcu/uatomic_arch_x86.h | 3 ++
urcu/uatomic_gcc.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+), 11 deletions(-)
diff --git a/tests/test_uatomic.c b/tests/test_uatomic.c
index c0f36fe..2e04c43 100644
--- a/tests/test_uatomic.c
+++ b/tests/test_uatomic.c
@@ -1,21 +4,10 @@
#include <stdio.h>
#include <assert.h>
-
-#define UATOMIC_NO_LINK_ERROR
#include <urcu/uatomic_arch.h>
-#if (defined(__i386__) || defined(__x86_64__))
-#define HAS_ATOMIC_BYTE
-#define HAS_ATOMIC_SHORT
-#endif
-
struct testvals {
-#ifdef HAS_ATOMIC_BYTE
unsigned char c;
-#endif
-#ifdef HAS_ATOMIC_SHORT
unsigned short s;
-#endif
unsigned int i;
unsigned long l;
};
@@ -54,10 +45,10 @@ do { \
int main(int argc, char **argv)
{
-#ifdef HAS_ATOMIC_BYTE
+#ifdef UATOMIC_HAS_ATOMIC_BYTE
do_test(&vals.c);
#endif
-#ifdef HAS_ATOMIC_SHORT
+#ifdef UATOMIC_HAS_ATOMIC_SHORT
do_test(&vals.s);
#endif
do_test(&vals.i);
diff --git a/urcu/uatomic_arch_x86.h b/urcu/uatomic_arch_x86.h
index 0aadbd5..aea207e 100644
--- a/urcu/uatomic_arch_x86.h
+++ b/urcu/uatomic_arch_x86.h
@@ -23,6 +23,9 @@
#include <urcu/compiler.h>
#include <urcu/system.h>
+#define UATOMIC_HAS_ATOMIC_CHAR
+#define UATOMIC_HAS_ATOMIC_SHORT
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/urcu/uatomic_gcc.h b/urcu/uatomic_gcc.h
index 28f6d71..5a4cc40 100644
--- a/urcu/uatomic_gcc.h
+++ b/urcu/uatomic_gcc.h
@@ -64,6 +64,14 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
unsigned long _new, int len)
{
switch (len) {
+#ifdef UATOMIC_HAS_ATOMIC_CHAR
+ case 1:
+ return __sync_val_compare_and_swap_1(addr, old, _new);
+#endif
+#ifdef UATOMIC_HAS_ATOMIC_SHORT
+ case 2:
+ return __sync_val_compare_and_swap_2(addr, old, _new);
+#endif
case 4:
return __sync_val_compare_and_swap_4(addr, old, _new);
#if (BITS_PER_LONG == 64)
@@ -90,6 +98,14 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val,
int len)
{
switch (len) {
+#ifdef UATOMIC_HAS_ATOMIC_CHAR
+ case 1:
+ return __sync_add_and_fetch_1(addr, val);
+#endif
+#ifdef UATOMIC_HAS_ATOMIC_SHORT
+ case 2:
+ return __sync_add_and_fetch_2(addr, val);
+#endif
case 4:
return __sync_add_and_fetch_4(addr, val);
#if (BITS_PER_LONG == 64)
@@ -115,6 +131,34 @@ static inline __attribute__((always_inline))
unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
{
switch (len) {
+#ifdef UATOMIC_HAS_ATOMIC_CHAR
+ case 1:
+ {
+ unsigned char old, oldt;
+
+ oldt = uatomic_read((unsigned char *)addr);
+ do {
+ old = oldt;
+ oldt = _uatomic_cmpxchg(addr, old, old + val, 1);
+ } while (oldt != old);
+
+ return old + val;
+ }
+#endif
+#ifdef UATOMIC_HAS_ATOMIC_SHORT
+ case 2:
+ {
+ unsigned short old, oldt;
+
+ oldt = uatomic_read((unsigned short *)addr);
+ do {
+ old = oldt;
+ oldt = _uatomic_cmpxchg(addr, old, old + val, 2);
+ } while (oldt != old);
+
+ return old + val;
+ }
+#endif
case 4:
{
unsigned int old, oldt;
@@ -161,6 +205,34 @@ static inline __attribute__((always_inline))
unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
{
switch (len) {
+#ifdef UATOMIC_HAS_ATOMIC_CHAR
+ case 1:
+ {
+ unsigned char old, oldt;
+
+ oldt = uatomic_read((unsigned char *)addr);
+ do {
+ old = oldt;
+ oldt = _uatomic_cmpxchg(addr, old, val, 1);
+ } while (oldt != old);
+
+ return old;
+ }
+#endif
+#ifdef UATOMIC_HAS_ATOMIC_SHORT
+ case 2:
+ {
+ unsigned short old, oldt;
+
+ oldt = uatomic_read((unsigned short *)addr);
+ do {
+ old = oldt;
+ oldt = _uatomic_cmpxchg(addr, old, val, 2);
+ } while (oldt != old);
+
+ return old;
+ }
+#endif
case 4:
{
unsigned int old, oldt;
--
1.6.6
More information about the lttng-dev
mailing list