atomic.h revision 48796
1/*-
2 * Copyright (c) 1998 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id: atomic.h,v 1.1 1998/08/24 08:39:36 dfr Exp $
27 */
28#ifndef _MACHINE_ATOMIC_H_
29#define _MACHINE_ATOMIC_H_
30
31/*
32 * Various simple arithmetic on memory which is atomic in the presence
33 * of interrupts.  This code is now SMP safe as well.
34 *
35 * The assembly is volatilized to demark potential before-and-after side
36 * effects if an interrupt or SMP collision were to occurs.
37 */
38
39#ifdef SMP
40#define ATOMIC_ASM(NAME, TYPE, OP, V)	\
41static __inline void			\
42NAME(void *p, TYPE v)			\
43{					\
44	__asm __volatile("lock; "	\
45			 OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
46}
47#else
48#define ATOMIC_ASM(NAME, TYPE, OP, V)	\
49static __inline void			\
50NAME(void *p, TYPE v)			\
51{					\
52	__asm __volatile(OP : "=m" (*(TYPE *)p) : "ir" (V), "0" (*(TYPE *)p)); \
53}
54#endif
55
56ATOMIC_ASM(atomic_set_char,	u_char, "orb %1,%0",   v)
57ATOMIC_ASM(atomic_clear_char,	u_char, "andb %1,%0", ~v)
58ATOMIC_ASM(atomic_add_char,	u_char, "addb %1,%0",  v)
59ATOMIC_ASM(atomic_subtract_char,u_char, "subb %1,%0",  v)
60
61ATOMIC_ASM(atomic_set_short,	u_short,"orw %1,%0",   v)
62ATOMIC_ASM(atomic_clear_short,	u_short,"andw %1,%0", ~v)
63ATOMIC_ASM(atomic_add_short,	u_short,"addw %1,%0",  v)
64ATOMIC_ASM(atomic_subtract_short,u_short,"subw %1,%0", v)
65
66ATOMIC_ASM(atomic_set_int,	u_int,	"orl %1,%0",   v)
67ATOMIC_ASM(atomic_clear_int,	u_int,	"andl %1,%0", ~v)
68ATOMIC_ASM(atomic_add_int,	u_int,	"addl %1,%0",  v)
69ATOMIC_ASM(atomic_subtract_int,	u_int,	"subl %1,%0",  v)
70
71ATOMIC_ASM(atomic_set_long,	u_long,	"orl %1,%0",   v)
72ATOMIC_ASM(atomic_clear_long,	u_long,	"andl %1,%0", ~v)
73ATOMIC_ASM(atomic_add_long,	u_long,	"addl %1,%0",  v)
74ATOMIC_ASM(atomic_subtract_long,u_long,	"subl %1,%0",  v)
75
76#undef ATOMIC_ASM
77
78#endif /* ! _MACHINE_ATOMIC_H_ */
79