2 * Generic UP xchg and cmpxchg using interrupt disablement. Does not
6 #ifndef __ASM_GENERIC_CMPXCHG_H
7 #define __ASM_GENERIC_CMPXCHG_H
10 #error "Cannot use generic cmpxchg on SMP"
13 #include <linux/irqflags.h>
18 * This function doesn't exist, so you'll get a linker error if
19 * something tries to do an invalidly-sized xchg().
21 extern void __xchg_called_with_bad_pointer(void);
24 unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
26 unsigned long ret, flags;
31 return __xchg_u8(x, ptr);
33 local_irq_save(flags);
34 ret = *(volatile u8 *)ptr;
35 *(volatile u8 *)ptr = x;
36 local_irq_restore(flags);
38 #endif /* __xchg_u8 */
42 return __xchg_u16(x, ptr);
44 local_irq_save(flags);
45 ret = *(volatile u16 *)ptr;
46 *(volatile u16 *)ptr = x;
47 local_irq_restore(flags);
49 #endif /* __xchg_u16 */
53 return __xchg_u32(x, ptr);
55 local_irq_save(flags);
56 ret = *(volatile u32 *)ptr;
57 *(volatile u32 *)ptr = x;
58 local_irq_restore(flags);
60 #endif /* __xchg_u32 */
65 return __xchg_u64(x, ptr);
67 local_irq_save(flags);
68 ret = *(volatile u64 *)ptr;
69 *(volatile u64 *)ptr = x;
70 local_irq_restore(flags);
72 #endif /* __xchg_u64 */
73 #endif /* CONFIG_64BIT */
76 __xchg_called_with_bad_pointer();
81 #define xchg(ptr, x) \
82 ((__typeof__(*(ptr))) __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))
87 * Atomic compare and exchange.
89 * Do not define __HAVE_ARCH_CMPXCHG because we want to use it to check whether
90 * a cmpxchg primitive faster than repeated local irq save/restore exists.
92 #include <asm-generic/cmpxchg-local.h>
94 #define cmpxchg(ptr, o, n) cmpxchg_local((ptr), (o), (n))
95 #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
97 #endif /* __ASM_GENERIC_CMPXCHG_H */