Changed the implementation of the primitives to guarantee atomicity

with respect to interrupts on UP systems.  (The upgrade from gcc 2.7.x
to egcs 1.1.2 produced at least one non-atomic code sequence in
swap_pager_getpages.)

In addition, the primitives are now SMP-safe, but only on SMPs.  (For
portability between SMPs and UPs, modules are compiled with the SMP-safe
versions.)

Submitted by:	dillon and myself
Reviewed by:	bde
This commit is contained in:
Alan Cox 1999-07-13 03:32:17 +00:00
parent 5bf6ab8369
commit e58bb1c453
2 changed files with 78 additions and 38 deletions

View File

@ -23,36 +23,56 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id$
* $Id: atomic.h,v 1.1 1998/08/24 08:39:36 dfr Exp $
*/
#ifndef _MACHINE_ATOMIC_H_
#define _MACHINE_ATOMIC_H_
/*
* Various simple arithmetic on memory which is atomic in the presence
* of interrupts.
* of interrupts. This code is now SMP safe as well.
*
* Note: these versions are not SMP safe.
* The assembly is volatilized to demark potential before-and-after side
* effects if an interrupt or SMP collision were to occurs.
*/
#define atomic_set_char(P, V) (*(u_char*)(P) |= (V))
#define atomic_clear_char(P, V) (*(u_char*)(P) &= ~(V))
#define atomic_add_char(P, V) (*(u_char*)(P) += (V))
#define atomic_subtract_char(P, V) (*(u_char*)(P) -= (V))
#ifdef SMP
#define ATOMIC_ASM(NAME, TYPE, OP, V) \
static __inline void \
NAME(void *p, TYPE v) \
{ \
__asm __volatile("lock; " \
OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
}
#else
#define ATOMIC_ASM(NAME, TYPE, OP, V) \
static __inline void \
NAME(void *p, TYPE v) \
{ \
__asm __volatile(OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
}
#endif
#define atomic_set_short(P, V) (*(u_short*)(P) |= (V))
#define atomic_clear_short(P, V) (*(u_short*)(P) &= ~(V))
#define atomic_add_short(P, V) (*(u_short*)(P) += (V))
#define atomic_subtract_short(P, V) (*(u_short*)(P) -= (V))
ATOMIC_ASM(atomic_set_char, u_char, "orb %1,%0", v)
ATOMIC_ASM(atomic_clear_char, u_char, "andb %1,%0", ~v)
ATOMIC_ASM(atomic_add_char, u_char, "addb %1,%0", v)
ATOMIC_ASM(atomic_subtract_char,u_char, "subb %1,%0", v)
#define atomic_set_int(P, V) (*(u_int*)(P) |= (V))
#define atomic_clear_int(P, V) (*(u_int*)(P) &= ~(V))
#define atomic_add_int(P, V) (*(u_int*)(P) += (V))
#define atomic_subtract_int(P, V) (*(u_int*)(P) -= (V))
ATOMIC_ASM(atomic_set_short, u_short,"orw %1,%0", v)
ATOMIC_ASM(atomic_clear_short, u_short,"andw %1,%0", ~v)
ATOMIC_ASM(atomic_add_short, u_short,"addw %1,%0", v)
ATOMIC_ASM(atomic_subtract_short,u_short,"subw %1,%0", v)
#define atomic_set_long(P, V) (*(u_long*)(P) |= (V))
#define atomic_clear_long(P, V) (*(u_long*)(P) &= ~(V))
#define atomic_add_long(P, V) (*(u_long*)(P) += (V))
#define atomic_subtract_long(P, V) (*(u_long*)(P) -= (V))
ATOMIC_ASM(atomic_set_int, u_int, "orl %1,%0", v)
ATOMIC_ASM(atomic_clear_int, u_int, "andl %1,%0", ~v)
ATOMIC_ASM(atomic_add_int, u_int, "addl %1,%0", v)
ATOMIC_ASM(atomic_subtract_int, u_int, "subl %1,%0", v)
ATOMIC_ASM(atomic_set_long, u_long, "orl %1,%0", v)
ATOMIC_ASM(atomic_clear_long, u_long, "andl %1,%0", ~v)
ATOMIC_ASM(atomic_add_long, u_long, "addl %1,%0", v)
ATOMIC_ASM(atomic_subtract_long,u_long, "subl %1,%0", v)
#undef ATOMIC_ASM
#endif /* ! _MACHINE_ATOMIC_H_ */

View File

@ -23,36 +23,56 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id$
* $Id: atomic.h,v 1.1 1998/08/24 08:39:36 dfr Exp $
*/
#ifndef _MACHINE_ATOMIC_H_
#define _MACHINE_ATOMIC_H_
/*
* Various simple arithmetic on memory which is atomic in the presence
* of interrupts.
* of interrupts. This code is now SMP safe as well.
*
* Note: these versions are not SMP safe.
* The assembly is volatilized to demark potential before-and-after side
* effects if an interrupt or SMP collision were to occurs.
*/
#define atomic_set_char(P, V) (*(u_char*)(P) |= (V))
#define atomic_clear_char(P, V) (*(u_char*)(P) &= ~(V))
#define atomic_add_char(P, V) (*(u_char*)(P) += (V))
#define atomic_subtract_char(P, V) (*(u_char*)(P) -= (V))
#ifdef SMP
#define ATOMIC_ASM(NAME, TYPE, OP, V) \
static __inline void \
NAME(void *p, TYPE v) \
{ \
__asm __volatile("lock; " \
OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
}
#else
#define ATOMIC_ASM(NAME, TYPE, OP, V) \
static __inline void \
NAME(void *p, TYPE v) \
{ \
__asm __volatile(OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
}
#endif
#define atomic_set_short(P, V) (*(u_short*)(P) |= (V))
#define atomic_clear_short(P, V) (*(u_short*)(P) &= ~(V))
#define atomic_add_short(P, V) (*(u_short*)(P) += (V))
#define atomic_subtract_short(P, V) (*(u_short*)(P) -= (V))
ATOMIC_ASM(atomic_set_char, u_char, "orb %1,%0", v)
ATOMIC_ASM(atomic_clear_char, u_char, "andb %1,%0", ~v)
ATOMIC_ASM(atomic_add_char, u_char, "addb %1,%0", v)
ATOMIC_ASM(atomic_subtract_char,u_char, "subb %1,%0", v)
#define atomic_set_int(P, V) (*(u_int*)(P) |= (V))
#define atomic_clear_int(P, V) (*(u_int*)(P) &= ~(V))
#define atomic_add_int(P, V) (*(u_int*)(P) += (V))
#define atomic_subtract_int(P, V) (*(u_int*)(P) -= (V))
ATOMIC_ASM(atomic_set_short, u_short,"orw %1,%0", v)
ATOMIC_ASM(atomic_clear_short, u_short,"andw %1,%0", ~v)
ATOMIC_ASM(atomic_add_short, u_short,"addw %1,%0", v)
ATOMIC_ASM(atomic_subtract_short,u_short,"subw %1,%0", v)
#define atomic_set_long(P, V) (*(u_long*)(P) |= (V))
#define atomic_clear_long(P, V) (*(u_long*)(P) &= ~(V))
#define atomic_add_long(P, V) (*(u_long*)(P) += (V))
#define atomic_subtract_long(P, V) (*(u_long*)(P) -= (V))
ATOMIC_ASM(atomic_set_int, u_int, "orl %1,%0", v)
ATOMIC_ASM(atomic_clear_int, u_int, "andl %1,%0", ~v)
ATOMIC_ASM(atomic_add_int, u_int, "addl %1,%0", v)
ATOMIC_ASM(atomic_subtract_int, u_int, "subl %1,%0", v)
ATOMIC_ASM(atomic_set_long, u_long, "orl %1,%0", v)
ATOMIC_ASM(atomic_clear_long, u_long, "andl %1,%0", ~v)
ATOMIC_ASM(atomic_add_long, u_long, "addl %1,%0", v)
ATOMIC_ASM(atomic_subtract_long,u_long, "subl %1,%0", v)
#undef ATOMIC_ASM
#endif /* ! _MACHINE_ATOMIC_H_ */