1117756Sdeischen/*-
2117756Sdeischen * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>
3117756Sdeischen * All rights reserved.
4117756Sdeischen *
5117756Sdeischen * Redistribution and use in source and binary forms, with or without
6117756Sdeischen * modification, are permitted provided that the following conditions
7117756Sdeischen * are met:
8117756Sdeischen * 1. Redistributions of source code must retain the above copyright
9117756Sdeischen *    notice, this list of conditions and the following disclaimer.
10117756Sdeischen * 2. Neither the name of the author nor the names of its contributors
11117756Sdeischen *    may be used to endorse or promote products derived from this software
12117756Sdeischen *    without specific prior written permission.
13117756Sdeischen *
14117756Sdeischen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15117756Sdeischen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16117756Sdeischen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17117756Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18117756Sdeischen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19117756Sdeischen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20117756Sdeischen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21117756Sdeischen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22117756Sdeischen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23117756Sdeischen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24117756Sdeischen * SUCH DAMAGE.
25117756Sdeischen *
26117756Sdeischen * $FreeBSD: releng/10.2/lib/libkse/arch/amd64/include/atomic_ops.h 174112 2007-11-30 17:20:29Z deischen $
27117756Sdeischen */
28117756Sdeischen
29117756Sdeischen#ifndef	_ATOMIC_OPS_H_
30117756Sdeischen#define	_ATOMIC_OPS_H_
31117756Sdeischen
32117756Sdeischen/*
33117756Sdeischen * Atomic swap:
34117756Sdeischen *   Atomic (tmp = *dst, *dst = val), then *res = tmp
35117756Sdeischen *
36119723Sdeischen * void atomic_swap64(intptr_t *dst, intptr_t val, intptr_t *res);
37117756Sdeischen */
38117756Sdeischenstatic inline void
39174112Sdeischenatomic_swap64(volatile intptr_t *dst, intptr_t val, intptr_t *res)
40117756Sdeischen{
41117756Sdeischen	__asm __volatile(
42117756Sdeischen	"xchgq %2, %1; movq %2, %0"
43117756Sdeischen	 : "=m" (*res) : "m" (*dst), "r" (val) : "memory");
44117756Sdeischen}
45117756Sdeischen
46119723Sdeischenstatic inline void
47174112Sdeischenatomic_swap_int(volatile int *dst, int val, int *res)
48119723Sdeischen{
49119723Sdeischen	__asm __volatile(
50119723Sdeischen	"xchgl %2, %1; movl %2, %0"
51119723Sdeischen	 : "=m" (*res) : "m" (*dst), "r" (val) : "memory");
52119723Sdeischen}
53119723Sdeischen
54117756Sdeischen#define	atomic_swap_ptr(d, v, r) \
55174112Sdeischen	atomic_swap64((volatile intptr_t *)(d), (intptr_t)(v), (intptr_t *)(r))
56117756Sdeischen
57117756Sdeischen#endif
58