提交 31a8394e 编写于 作者: J Jeremy Fitzhardinge

x86: consolidate xchg and xadd macros

They both have a basic "put new value in location, return old value"
pattern, so they can use the same macro easily.
Signed-off-by: NJeremy Fitzhardinge <jeremy@goop.org>
上级 3d94ae0c
...@@ -34,59 +34,46 @@ extern void __add_wrong_size(void) ...@@ -34,59 +34,46 @@ extern void __add_wrong_size(void)
#endif #endif
/* /*
* Note: no "lock" prefix even on SMP: xchg always implies lock anyway. * An exchange-type operation, which takes a value and a pointer, and
* Since this is generally used to protect other memory information, we * returns a the old value.
* use "asm volatile" and "memory" clobbers to prevent gcc from moving
* information around.
*/ */
#define __xchg(x, ptr, size) \ #define __xchg_op(ptr, arg, op, lock) \
({ \ ({ \
__typeof(*(ptr)) __x = (x); \ __typeof__ (*(ptr)) __ret = (arg); \
switch (size) { \ switch (sizeof(*(ptr))) { \
case __X86_CASE_B: \ case __X86_CASE_B: \
{ \ asm volatile (lock #op "b %b0, %1\n" \
volatile u8 *__ptr = (volatile u8 *)(ptr); \ : "+r" (__ret), "+m" (*(ptr)) \
asm volatile("xchgb %0,%1" \ : : "memory", "cc"); \
: "=q" (__x), "+m" (*__ptr) \
: "0" (__x) \
: "memory"); \
break; \ break; \
} \
case __X86_CASE_W: \ case __X86_CASE_W: \
{ \ asm volatile (lock #op "w %w0, %1\n" \
volatile u16 *__ptr = (volatile u16 *)(ptr); \ : "+r" (__ret), "+m" (*(ptr)) \
asm volatile("xchgw %0,%1" \ : : "memory", "cc"); \
: "=r" (__x), "+m" (*__ptr) \
: "0" (__x) \
: "memory"); \
break; \ break; \
} \
case __X86_CASE_L: \ case __X86_CASE_L: \
{ \ asm volatile (lock #op "l %0, %1\n" \
volatile u32 *__ptr = (volatile u32 *)(ptr); \ : "+r" (__ret), "+m" (*(ptr)) \
asm volatile("xchgl %0,%1" \ : : "memory", "cc"); \
: "=r" (__x), "+m" (*__ptr) \
: "0" (__x) \
: "memory"); \
break; \ break; \
} \
case __X86_CASE_Q: \ case __X86_CASE_Q: \
{ \ asm volatile (lock #op "q %q0, %1\n" \
volatile u64 *__ptr = (volatile u64 *)(ptr); \ : "+r" (__ret), "+m" (*(ptr)) \
asm volatile("xchgq %0,%1" \ : : "memory", "cc"); \
: "=r" (__x), "+m" (*__ptr) \
: "0" (__x) \
: "memory"); \
break; \ break; \
} \
default: \ default: \
__xchg_wrong_size(); \ __ ## op ## _wrong_size(); \
} \ } \
__x; \ __ret; \
}) })
#define xchg(ptr, v) \ /*
__xchg((v), (ptr), sizeof(*ptr)) * Note: no "lock" prefix even on SMP: xchg always implies lock anyway.
* Since this is generally used to protect other memory information, we
* use "asm volatile" and "memory" clobbers to prevent gcc from moving
* information around.
*/
#define xchg(ptr, v) __xchg_op((ptr), (v), xchg, "")
/* /*
* Atomic compare and exchange. Compare OLD with MEM, if identical, * Atomic compare and exchange. Compare OLD with MEM, if identical,
...@@ -167,36 +154,6 @@ extern void __add_wrong_size(void) ...@@ -167,36 +154,6 @@ extern void __add_wrong_size(void)
__cmpxchg_local((ptr), (old), (new), sizeof(*ptr)) __cmpxchg_local((ptr), (old), (new), sizeof(*ptr))
#endif #endif
#define __xadd(ptr, inc, lock) \
({ \
__typeof__ (*(ptr)) __ret = (inc); \
switch (sizeof(*(ptr))) { \
case __X86_CASE_B: \
asm volatile (lock "xaddb %b0, %1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
: : "memory", "cc"); \
break; \
case __X86_CASE_W: \
asm volatile (lock "xaddw %w0, %1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
: : "memory", "cc"); \
break; \
case __X86_CASE_L: \
asm volatile (lock "xaddl %0, %1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
: : "memory", "cc"); \
break; \
case __X86_CASE_Q: \
asm volatile (lock "xaddq %q0, %1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
: : "memory", "cc"); \
break; \
default: \
__xadd_wrong_size(); \
} \
__ret; \
})
/* /*
* xadd() adds "inc" to "*ptr" and atomically returns the previous * xadd() adds "inc" to "*ptr" and atomically returns the previous
* value of "*ptr". * value of "*ptr".
...@@ -205,6 +162,7 @@ extern void __add_wrong_size(void) ...@@ -205,6 +162,7 @@ extern void __add_wrong_size(void)
* xadd_sync() is always locked * xadd_sync() is always locked
* xadd_local() is never locked * xadd_local() is never locked
*/ */
#define __xadd(ptr, inc, lock) __xchg_op((ptr), (inc), xadd, lock)
#define xadd(ptr, inc) __xadd((ptr), (inc), LOCK_PREFIX) #define xadd(ptr, inc) __xadd((ptr), (inc), LOCK_PREFIX)
#define xadd_sync(ptr, inc) __xadd((ptr), (inc), "lock; ") #define xadd_sync(ptr, inc) __xadd((ptr), (inc), "lock; ")
#define xadd_local(ptr, inc) __xadd((ptr), (inc), "") #define xadd_local(ptr, inc) __xadd((ptr), (inc), "")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册