Define atomic_exchange.
authordrepper <drepper>
Thu, 20 Mar 2003 10:13:00 +0000 (10:13 +0000)
committerdrepper <drepper>
Thu, 20 Mar 2003 10:13:00 +0000 (10:13 +0000)
sysdeps/i386/i486/bits/atomic.h
sysdeps/x86_64/bits/atomic.h

index 87739c6..e22e560 100644 (file)
@@ -119,6 +119,29 @@ typedef uintmax_t uatomic_max_t;
 #endif
 
 
+/* Note that we need no lock prefix.  */
+#define atomic_exchange(mem, newvalue) \
+  ({ __typeof (*mem) result;                                                 \
+     if (sizeof (*mem) == 1)                                                 \
+       __asm __volatile ("xchgb %b0, %1"                                     \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else if (sizeof (*mem) == 2)                                            \
+       __asm __volatile ("xchgw %w0, %1"                                     \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else if (sizeof (*mem) == 4)                                            \
+       __asm __volatile ("xchgl %0, %1"                                              \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else                                                                    \
+       {                                                                     \
+        result = 0;                                                          \
+        abort ();                                                            \
+       }                                                                     \
+     result; })
+
+
 #define atomic_exchange_and_add(mem, value) \
   ({ __typeof (*mem) result;                                                 \
      __typeof (value) addval = (value);                                              \
index f72f16c..22d3ab0 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002 Free Software Foundation, Inc.
+/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
 
@@ -84,6 +84,28 @@ typedef uintmax_t uatomic_max_t;
      ret; })
 
 
+/* Note that we need no lock prefix.  */
+#define atomic_exchange(mem, newvalue) \
+  ({ __typeof (*mem) result;                                                 \
+     if (sizeof (*mem) == 1)                                                 \
+       __asm __volatile ("xchgb %b0, %1"                                     \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else if (sizeof (*mem) == 2)                                            \
+       __asm __volatile ("xchgw %w0, %1"                                     \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else if (sizeof (*mem) == 4)                                            \
+       __asm __volatile ("xchgl %0, %1"                                              \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     else                                                                    \
+       __asm __volatile ("xchgq %q0, %1"                                     \
+                        : "=r" (result), "=m" (*mem)                         \
+                        : "0" (newvalue), "1" (*mem));                       \
+     result; })
+
+
 #define atomic_exchange_and_add(mem, value) \
   ({ __typeof (*mem) result;                                                 \
      if (sizeof (*mem) == 1)                                                 \