diff --git a/sys/amd64/include/atomic.h b/sys/amd64/include/atomic.h index 4c415deee3d2..7d7199c9d7e0 100644 --- a/sys/amd64/include/atomic.h +++ b/sys/amd64/include/atomic.h @@ -32,9 +32,9 @@ #error this file needs sys/cdefs.h as a prerequisite #endif -#define mb() __asm__ __volatile__ ("mfence;": : :"memory") -#define wmb() __asm__ __volatile__ ("sfence;": : :"memory") -#define rmb() __asm__ __volatile__ ("lfence;": : :"memory") +#define mb() __asm __volatile("mfence;" : : : "memory") +#define wmb() __asm __volatile("sfence;" : : : "memory") +#define rmb() __asm __volatile("lfence;" : : : "memory") /* * Various simple operations on memory, each of which is atomic in the @@ -131,50 +131,33 @@ struct __hack * Returns 0 on failure, non-zero on success */ -static __inline int -atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src) -{ - u_char res; +#define DEFINE_CMPSET_GEN(NAME, TYPE, OP) \ +static __inline int \ +atomic_cmpset_##NAME(volatile u_##TYPE *dst, u_##TYPE exp, u_##TYPE src)\ +{ \ + u_char res; \ + \ + __asm __volatile( \ + " " MPLOCKED " " \ + " " OP " %2,%1 ; " \ + " sete %0 ; " \ + "1: " \ + "# atomic_cmpset_##NAME" \ + : "=a" (res), /* 0 */ \ + "=m" (*dst) /* 1 */ \ + : "r" (src), /* 2 */ \ + "a" (exp), /* 3 */ \ + "m" (*dst) /* 4 */ \ + : "memory"); \ + \ + return (res); \ +} \ +struct __hack - __asm __volatile( - " " MPLOCKED " " - " cmpxchgl %2,%1 ; " - " sete %0 ; " - "1: " - "# atomic_cmpset_int" - : "=a" (res), /* 0 */ - "=m" (*dst) /* 1 */ - : "r" (src), /* 2 */ - "a" (exp), /* 3 */ - "m" (*dst) /* 4 */ - : "memory"); - - return (res); -} - -static __inline int -atomic_cmpset_long(volatile u_long *dst, u_long exp, u_long src) -{ - u_char res; - - __asm __volatile( - " " MPLOCKED " " - " cmpxchgq %2,%1 ; " - " sete %0 ; " - "1: " - "# atomic_cmpset_long" - : "=a" (res), /* 0 */ - "=m" (*dst) /* 1 */ - : "r" (src), /* 2 */ - "a" (exp), /* 3 */ - "m" (*dst) /* 4 */ - : "memory"); - - return (res); -} - -#define atomic_cmpset_barr_int atomic_cmpset_int -#define atomic_cmpset_barr_long atomic_cmpset_long +DEFINE_CMPSET_GEN(int, int, "cmpxchgl"); +DEFINE_CMPSET_GEN(long, long, "cmpxchgq"); +DEFINE_CMPSET_GEN(barr_int, int, "cmpxchgl"); +DEFINE_CMPSET_GEN(barr_long, long, "cmpxchgq"); /* * Atomically add the value of v to the integer pointed to by p and return diff --git a/sys/i386/include/atomic.h b/sys/i386/include/atomic.h index e19ed7890b07..b60f2a14ea91 100644 --- a/sys/i386/include/atomic.h +++ b/sys/i386/include/atomic.h @@ -130,57 +130,62 @@ struct __hack #ifdef CPU_DISABLE_CMPXCHG -static __inline int -atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src) -{ - u_char res; - - __asm __volatile( - " pushfl ; " - " cli ; " - " cmpl %3,%4 ; " - " jne 1f ; " - " movl %2,%1 ; " - "1: " - " sete %0 ; " - " popfl ; " - "# atomic_cmpset_int" - : "=q" (res), /* 0 */ - "=m" (*dst) /* 1 */ - : "r" (src), /* 2 */ - "r" (exp), /* 3 */ - "m" (*dst) /* 4 */ - : "memory"); - - return (res); -} +#define DEFINE_CMPSET_GEN(NAME) \ +static __inline int \ +atomic_cmpset_##NAME(volatile u_int *dst, u_int exp, u_int src)\ +{ \ + u_char res; \ + \ + __asm __volatile( \ + " pushfl ; " \ + " cli ; " \ + " cmpl %3,%4 ; " \ + " jne 1f ; " \ + " movl %2,%1 ; " \ + "1: " \ + " sete %0 ; " \ + " popfl ; " \ + "# atomic_cmpset_##NAME" \ + : "=q" (res), /* 0 */ \ + "=m" (*dst) /* 1 */ \ + : "r" (src), /* 2 */ \ + "r" (exp), /* 3 */ \ + "m" (*dst) /* 4 */ \ + : "memory"); \ + \ + return (res); \ +} \ +struct __hack #else /* !CPU_DISABLE_CMPXCHG */ -static __inline int -atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src) -{ - u_char res; - - __asm __volatile( - " " MPLOCKED " " - " cmpxchgl %2,%1 ; " - " sete %0 ; " - "1: " - "# atomic_cmpset_int" - : "=a" (res), /* 0 */ - "=m" (*dst) /* 1 */ - : "r" (src), /* 2 */ - "a" (exp), /* 3 */ - "m" (*dst) /* 4 */ - : "memory"); - - return (res); -} +#define DEFINE_CMPSET_GEN(NAME) \ +static __inline int \ +atomic_cmpset_##NAME(volatile u_int *dst, u_int exp, u_int src)\ +{ \ + u_char res; \ + \ + __asm __volatile( \ + " " MPLOCKED " " \ + " cmpxchgl %2,%1 ; " \ + " sete %0 ; " \ + "1: " \ + "# atomic_cmpset_##NAME" \ + : "=a" (res), /* 0 */ \ + "=m" (*dst) /* 1 */ \ + : "r" (src), /* 2 */ \ + "a" (exp), /* 3 */ \ + "m" (*dst) /* 4 */ \ + : "memory"); \ + \ + return (res); \ +} \ +struct __hack #endif /* CPU_DISABLE_CMPXCHG */ -#define atomic_cmpset_barr_int atomic_cmpset_int +DEFINE_CMPSET_GEN(int); +DEFINE_CMPSET_GEN(barr_int); /* * Atomically add the value of v to the integer pointed to by p and return