1290001Sglebius/*
2290001Sglebius * Copyright (C) 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3290001Sglebius *
4290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any
5290001Sglebius * purpose with or without fee is hereby granted, provided that the above
6290001Sglebius * copyright notice and this permission notice appear in all copies.
7290001Sglebius *
8290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10290001Sglebius * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14290001Sglebius * PERFORMANCE OF THIS SOFTWARE.
15290001Sglebius */
16290001Sglebius
17290001Sglebius/* $Id: atomic.h,v 1.3 2007/06/19 23:47:18 tbox Exp $ */
18290001Sglebius
19290001Sglebius#ifndef ISC_ATOMIC_H
20290001Sglebius#define ISC_ATOMIC_H 1
21290001Sglebius
22290001Sglebius#include <isc/platform.h>
23290001Sglebius#include <isc/types.h>
24290001Sglebius
25290001Sglebius#ifdef ISC_PLATFORM_USEGCCASM
26290001Sglebius/*
27290001Sglebius * This routine atomically increments the value stored in 'p' by 'val', and
28290001Sglebius * returns the previous value.
29290001Sglebius */
30290001Sglebiusstatic inline isc_int32_t
31290001Sglebiusisc_atomic_xadd(isc_int32_t *p, int val) {
32290001Sglebius	isc_int32_t orig;
33290001Sglebius
34290001Sglebius	/* add is a cheat, since MIPS has no mov instruction */
35290001Sglebius	__asm__ volatile (
36290001Sglebius	    "1:"
37290001Sglebius	    "ll $3, %1\n"
38290001Sglebius	    "add %0, $0, $3\n"
39290001Sglebius	    "add $3, $3, %2\n"
40290001Sglebius	    "sc $3, %1\n"
41290001Sglebius	    "beq $3, 0, 1b"
42290001Sglebius	    : "=&r"(orig)
43290001Sglebius	    : "m"(*p), "r"(val)
44290001Sglebius	    : "memory", "$3"
45290001Sglebius		);
46290001Sglebius
47290001Sglebius	return (orig);
48290001Sglebius}
49290001Sglebius
50290001Sglebius/*
51290001Sglebius * This routine atomically stores the value 'val' in 'p'.
52290001Sglebius */
53290001Sglebiusstatic inline void
54290001Sglebiusisc_atomic_store(isc_int32_t *p, isc_int32_t val) {
55290001Sglebius	__asm__ volatile (
56290001Sglebius	    "1:"
57290001Sglebius	    "ll $3, %0\n"
58290001Sglebius	    "add $3, $0, %1\n"
59290001Sglebius	    "sc $3, %0\n"
60290001Sglebius	    "beq $3, 0, 1b"
61290001Sglebius	    :
62290001Sglebius	    : "m"(*p), "r"(val)
63290001Sglebius	    : "memory", "$3"
64290001Sglebius		);
65290001Sglebius}
66290001Sglebius
67290001Sglebius/*
68290001Sglebius * This routine atomically replaces the value in 'p' with 'val', if the
69290001Sglebius * original value is equal to 'cmpval'.  The original value is returned in any
70290001Sglebius * case.
71290001Sglebius */
72290001Sglebiusstatic inline isc_int32_t
73290001Sglebiusisc_atomic_cmpxchg(isc_int32_t *p, int cmpval, int val) {
74290001Sglebius	isc_int32_t orig;
75290001Sglebius
76290001Sglebius	__asm__ volatile(
77290001Sglebius	    "1:"
78290001Sglebius	    "ll $3, %1\n"
79290001Sglebius	    "add %0, $0, $3\n"
80290001Sglebius	    "bne $3, %2, 2f\n"
81290001Sglebius	    "add $3, $0, %3\n"
82290001Sglebius	    "sc $3, %1\n"
83290001Sglebius	    "beq $3, 0, 1b\n"
84290001Sglebius	    "2:"
85290001Sglebius	    : "=&r"(orig)
86290001Sglebius	    : "m"(*p), "r"(cmpval), "r"(val)
87290001Sglebius	    : "memory", "$3"
88290001Sglebius		);
89290001Sglebius
90290001Sglebius	return (orig);
91290001Sglebius}
92290001Sglebius
93290001Sglebius#else /* !ISC_PLATFORM_USEGCCASM */
94290001Sglebius
95290001Sglebius#error "unsupported compiler.  disable atomic ops by --disable-atomic"
96290001Sglebius
97290001Sglebius#endif
98290001Sglebius#endif /* ISC_ATOMIC_H */
99