2 * Copyright (C) 2005 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: atomic.h,v 1.2.2.2 2005/06/16 22:01:02 jinmei Exp $ */
20 * This code was written based on FreeBSD's kernel source whose copyright
25 * Copyright (c) 1998 Doug Rabson.
26 * Copyright (c) 2001 Jake Burkholder.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * from: FreeBSD: src/sys/i386/include/atomic.h,v 1.20 2001/02/11
55 #define ISC_ATOMIC_H 1
57 #include <isc/platform.h>
58 #include <isc/types.h>
60 #define ASI_P 0x80 /* Primary Address Space Identifier */
62 #ifdef ISC_PLATFORM_USEGCCASM
65 * This routine atomically increments the value stored in 'p' by 'val', and
66 * returns the previous value.
68 static inline isc_int32_t
69 isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
70 isc_int32_t prev, swapped;
72 for (prev = *(volatile isc_int32_t *)p; ; prev = swapped) {
75 "casa [%1] %2, %3, %0"
77 : "r"(p), "n"(ASI_P), "r"(prev));
86 * This routine atomically stores the value 'val' in 'p'.
89 isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
90 isc_int32_t prev, swapped;
92 for (prev = *(volatile isc_int32_t *)p; ; prev = swapped) {
95 "casa [%1] %2, %3, %0"
97 : "r"(p), "n"(ASI_P), "r"(prev)
105 * This routine atomically replaces the value in 'p' with 'val', if the
106 * original value is equal to 'cmpval'. The original value is returned in any
109 static inline isc_int32_t
110 isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
111 isc_int32_t temp = val;
114 "casa [%1] %2, %3, %0"
116 : "r"(p), "n"(ASI_P), "r"(cmpval));
121 #else /* ISC_PLATFORM_USEGCCASM */
123 #error "unsupported compiler. disable atomic ops by --disable-atomic"
125 #endif /* ISC_PLATFORM_USEGCCASM */
127 #endif /* ISC_ATOMIC_H */