atomic_ops.h revision 137283
1129206Scognet/*-
2129206Scognet * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>
3129206Scognet * All rights reserved.
4129206Scognet *
5129206Scognet * Redistribution and use in source and binary forms, with or without
6129206Scognet * modification, are permitted provided that the following conditions
7129206Scognet * are met:
8129206Scognet * 1. Redistributions of source code must retain the above copyright
9129206Scognet *    notice, this list of conditions and the following disclaimer.
10129206Scognet * 2. Neither the name of the author nor the names of its contributors
11129206Scognet *    may be used to endorse or promote products derived from this software
12129206Scognet *    without specific prior written permission.
13129206Scognet *
14129206Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15129206Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16129206Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17129206Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18129206Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129206Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129206Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21129206Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22129206Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23129206Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24129206Scognet * SUCH DAMAGE.
25129206Scognet *
26129206Scognet * $FreeBSD: head/lib/libkse/arch/arm/include/atomic_ops.h 137283 2004-11-05 23:49:21Z cognet $
27129206Scognet */
28129206Scognet
29129206Scognet#ifndef	_ATOMIC_OPS_H_
30129206Scognet#define	_ATOMIC_OPS_H_
31129206Scognet
32137283Scognet#include <machine/atomic.h>
33137283Scognet#include "thr_private.h"
34137283Scognet
35129206Scognet/*
36129206Scognet * Atomic swap:
37129206Scognet *   Atomic (tmp = *dst, *dst = val), then *res = tmp
38129206Scognet *
39129206Scognet * void atomic_swap32(intptr_t *dst, intptr_t val, intptr_t *res);
40129206Scognet */
41129206Scognetstatic inline void
42129206Scognetatomic_swap32(intptr_t *dst, intptr_t val, intptr_t *res)
43129206Scognet{
44137283Scognet	*res = __swp(val, dst);
45129206Scognet}
46129206Scognet
47129206Scognet#define	atomic_swap_ptr(d, v, r) \
48129206Scognet	atomic_swap32((intptr_t *)d, (intptr_t)v, (intptr_t *)r)
49129206Scognet
50129206Scognet#define	atomic_swap_int(d, v, r) \
51129206Scognet	atomic_swap32((intptr_t *)d, (intptr_t)v, (intptr_t *)r)
52129206Scognet#endif
53137283Scognet
54137283Scognetstatic inline u_int32_t
55137283Scognetatomic_cmpset_32(volatile u_int32_t *p, u_int32_t cmpval, u_int32_t newval)
56137283Scognet{
57137283Scognet	kse_critical_t crit = _kse_critical_enter();
58137283Scognet	int ret;
59137283Scognet
60137283Scognet	if (*p == cmpval) {
61137283Scognet		*p = newval;
62137283Scognet		ret = 1;
63137283Scognet	} else
64137283Scognet		ret = 0;
65137283Scognet	_kse_critical_leave(crit);
66137283Scognet	return (ret);
67137283Scognet}
68137283Scognet
69