Add inc/dec x86 atomics
authorMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Tue, 22 Sep 2009 21:09:55 +0000 (17:09 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Tue, 22 Sep 2009 21:09:55 +0000 (17:09 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
arch_atomic_x86.h
tests/test_atomic.c

index 3422cb4734a0a5e2dd57ff2147f86cd783b58c97..fdd3d6b58ecd28eaf620e10001920eae0193a073 100644 (file)
@@ -167,7 +167,8 @@ void _atomic_add(volatile void *addr, unsigned long val, int len)
                __asm__ __volatile__(
                "lock; addb %1, %0"
                        : "=m"(*__hp(addr))
-                       : "q" ((unsigned char)val));
+                       : "iq" ((unsigned char)val)
+                       : "memory");
                return;
        }
        case 2:
@@ -175,7 +176,8 @@ void _atomic_add(volatile void *addr, unsigned long val, int len)
                __asm__ __volatile__(
                "lock; addw %1, %0"
                        : "=m"(*__hp(addr))
-                       : "r" ((unsigned short)val));
+                       : "ir" ((unsigned short)val)
+                       : "memory");
                return;
        }
        case 4:
@@ -183,7 +185,8 @@ void _atomic_add(volatile void *addr, unsigned long val, int len)
                __asm__ __volatile__(
                "lock; addl %1, %0"
                        : "=m"(*__hp(addr))
-                       : "r" ((unsigned int)val));
+                       : "ir" ((unsigned int)val)
+                       : "memory");
                return;
        }
 #if (BITS_PER_LONG == 64)
@@ -192,7 +195,8 @@ void _atomic_add(volatile void *addr, unsigned long val, int len)
                __asm__ __volatile__(
                "lock; addq %1, %0"
                        : "=m"(*__hp(addr))
-                       : "r" ((unsigned long)val));
+                       : "er" ((unsigned long)val)
+                       : "memory");
                return;
        }
 #endif
index 47cc319f9164381a92c6bb1e0ee374d8378bcd48..bf847572815012be866ff9b921c85b97ace1aa0d 100644 (file)
@@ -11,10 +11,32 @@ struct testvals {
 
 static struct testvals vals;
 
-int main(int argc, void **argv)
+#define do_test(ptr)                           \
+do {                                           \
+       __typeof__(*ptr) v;                     \
+                                               \
+       atomic_add(ptr, 10);                    \
+       assert(*ptr == 10);                     \
+       atomic_add(ptr, -11);                   \
+       assert(*ptr == (__typeof__(*ptr))-1U);  \
+       v = cmpxchg(ptr, -1, 22);               \
+       assert(*ptr == 22);                     \
+       assert(v == (__typeof__(*ptr))-1U);     \
+       v = cmpxchg(ptr, 33, 44);               \
+       assert(*ptr == 22);                     \
+       assert(v == 22);                        \
+       v = xchg(ptr, 55);                      \
+       assert(*ptr == 55);                     \
+       assert(v == 22);                        \
+} while (0)
+
+int main(int argc, char **argv)
 {
-       atomic_add(&vals.c, 10);
-       assert(vals.c == 10);
-       atomic_add(&vals.c, -11);
-       assert((char)vals.c == -1);
+       do_test(&vals.c);
+       do_test(&vals.s);
+       do_test(&vals.i);
+       do_test(&vals.l);
+       printf("Atomic ops test OK\n");
+
+       return 0;
 }
This page took 0.026091 seconds and 4 git commands to generate.