1113661Sdeischen/*-
2113656Sdeischen * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>
3113656Sdeischen * All rights reserved.
4113656Sdeischen *
5113656Sdeischen * Redistribution and use in source and binary forms, with or without
6113656Sdeischen * modification, are permitted provided that the following conditions
7113656Sdeischen * are met:
8113656Sdeischen * 1. Redistributions of source code must retain the above copyright
9113656Sdeischen *    notice, this list of conditions and the following disclaimer.
10113661Sdeischen * 2. Neither the name of the author nor the names of its contributors
11113661Sdeischen *    may be used to endorse or promote products derived from this software
12113661Sdeischen *    without specific prior written permission.
13113656Sdeischen *
14113656Sdeischen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15113656Sdeischen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16113656Sdeischen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17113656Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18113656Sdeischen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19113656Sdeischen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20113656Sdeischen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21113656Sdeischen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22113656Sdeischen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23113656Sdeischen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24113656Sdeischen * SUCH DAMAGE.
25113656Sdeischen *
26113656Sdeischen * $FreeBSD$
27113656Sdeischen */
28113656Sdeischen
29113656Sdeischen#ifndef	_ATOMIC_OPS_H_
30113656Sdeischen#define	_ATOMIC_OPS_H_
31113656Sdeischen
32113656Sdeischen/*
33113656Sdeischen * Atomic swap:
34113656Sdeischen *   Atomic (tmp = *dst, *dst = val), then *res = tmp
35113656Sdeischen *
36119723Sdeischen * void atomic_swap32(intptr_t *dst, intptr_t val, intptr_t *res);
37113656Sdeischen */
38113656Sdeischenstatic inline void
39174112Sdeischenatomic_swap32(volatile intptr_t *dst, intptr_t val, intptr_t *res)
40113656Sdeischen{
41113656Sdeischen	__asm __volatile(
42113656Sdeischen	"xchgl %2, %1; movl %2, %0"
43113656Sdeischen	 : "=m" (*res) : "m" (*dst), "r" (val) : "memory");
44113656Sdeischen}
45113656Sdeischen
46119723Sdeischen#define	atomic_swap_ptr(d, v, r) \
47174112Sdeischen	atomic_swap32((volatile intptr_t *)d, (intptr_t)v, (intptr_t *)r)
48119723Sdeischen
49113656Sdeischen#define	atomic_swap_int(d, v, r) \
50174112Sdeischen	atomic_swap32((volatile intptr_t *)d, (intptr_t)v, (intptr_t *)r)
51113656Sdeischen#endif
52