1258945Sroberto/*
2258945Sroberto * Copyright (C) 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto *
4258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
5258945Sroberto * purpose with or without fee is hereby granted, provided that the above
6258945Sroberto * copyright notice and this permission notice appear in all copies.
7258945Sroberto *
8258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
15258945Sroberto */
16258945Sroberto
17258945Sroberto/* $Id: atomic.h,v 1.3 2007/06/19 23:47:18 tbox Exp $ */
18258945Sroberto
19258945Sroberto#ifndef ISC_ATOMIC_H
20258945Sroberto#define ISC_ATOMIC_H 1
21258945Sroberto
22258945Sroberto#include <isc/platform.h>
23258945Sroberto#include <isc/types.h>
24258945Sroberto
25258945Sroberto#ifdef ISC_PLATFORM_USEGCCASM
26258945Sroberto/*
27258945Sroberto * This routine atomically increments the value stored in 'p' by 'val', and
28258945Sroberto * returns the previous value.
29258945Sroberto */
30258945Srobertostatic inline isc_int32_t
31258945Srobertoisc_atomic_xadd(isc_int32_t *p, int val) {
32258945Sroberto	isc_int32_t orig;
33258945Sroberto
34258945Sroberto	/* add is a cheat, since MIPS has no mov instruction */
35258945Sroberto	__asm__ volatile (
36258945Sroberto	    "1:"
37258945Sroberto	    "ll $3, %1\n"
38258945Sroberto	    "add %0, $0, $3\n"
39258945Sroberto	    "add $3, $3, %2\n"
40258945Sroberto	    "sc $3, %1\n"
41258945Sroberto	    "beq $3, 0, 1b"
42258945Sroberto	    : "=&r"(orig)
43258945Sroberto	    : "m"(*p), "r"(val)
44258945Sroberto	    : "memory", "$3"
45258945Sroberto		);
46258945Sroberto
47258945Sroberto	return (orig);
48258945Sroberto}
49258945Sroberto
50258945Sroberto/*
51258945Sroberto * This routine atomically stores the value 'val' in 'p'.
52258945Sroberto */
53258945Srobertostatic inline void
54258945Srobertoisc_atomic_store(isc_int32_t *p, isc_int32_t val) {
55258945Sroberto	__asm__ volatile (
56258945Sroberto	    "1:"
57258945Sroberto	    "ll $3, %0\n"
58258945Sroberto	    "add $3, $0, %1\n"
59258945Sroberto	    "sc $3, %0\n"
60258945Sroberto	    "beq $3, 0, 1b"
61258945Sroberto	    :
62258945Sroberto	    : "m"(*p), "r"(val)
63258945Sroberto	    : "memory", "$3"
64258945Sroberto		);
65258945Sroberto}
66258945Sroberto
67258945Sroberto/*
68258945Sroberto * This routine atomically replaces the value in 'p' with 'val', if the
69258945Sroberto * original value is equal to 'cmpval'.  The original value is returned in any
70258945Sroberto * case.
71258945Sroberto */
72258945Srobertostatic inline isc_int32_t
73258945Srobertoisc_atomic_cmpxchg(isc_int32_t *p, int cmpval, int val) {
74258945Sroberto	isc_int32_t orig;
75258945Sroberto
76258945Sroberto	__asm__ volatile(
77258945Sroberto	    "1:"
78258945Sroberto	    "ll $3, %1\n"
79258945Sroberto	    "add %0, $0, $3\n"
80258945Sroberto	    "bne $3, %2, 2f\n"
81258945Sroberto	    "add $3, $0, %3\n"
82258945Sroberto	    "sc $3, %1\n"
83258945Sroberto	    "beq $3, 0, 1b\n"
84258945Sroberto	    "2:"
85258945Sroberto	    : "=&r"(orig)
86258945Sroberto	    : "m"(*p), "r"(cmpval), "r"(val)
87258945Sroberto	    : "memory", "$3"
88258945Sroberto		);
89258945Sroberto
90258945Sroberto	return (orig);
91258945Sroberto}
92258945Sroberto
93258945Sroberto#else /* !ISC_PLATFORM_USEGCCASM */
94258945Sroberto
95258945Sroberto#error "unsupported compiler.  disable atomic ops by --disable-atomic"
96258945Sroberto
97258945Sroberto#endif
98258945Sroberto#endif /* ISC_ATOMIC_H */
99