1 /*	$NetBSD: atomic.h,v 1.6 2014/12/10 04:38:00 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id: atomic.h,v 1.4 2007/06/19 23:47:18 tbox Exp  */
20 
21 #ifndef ISC_ATOMIC_H
22 #define ISC_ATOMIC_H 1
23 
24 #include <isc/platform.h>
25 #include <isc/types.h>
26 
27 /* XXX: These are not atomic! */
28 /*
29  * This routine atomically increments the value stored in 'p' by 'val', and
30  * returns the previous value.
31  */
32 static __inline isc_int32_t
33 isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
34 	isc_int32_t prev = *p;
35 
36 	*p += val;
37 
38 	return prev;
39 }
40 
41 /*
42  * This routine atomically stores the value 'val' in 'p'.
43  */
44 static __inline void
45 isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
46 	*p = val;
47 }
48 
49 /*
50  * This routine atomically replaces the value in 'p' with 'val', if the
51  * original value is equal to 'cmpval'.  The original value is returned in any
52  * case.
53  */
54 static __inline isc_int32_t
55 isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
56 	isc_int32_t prev = *p;
57 
58 	if (*p == cmpval)
59 		*p = val;
60 
61 	return prev;
62 }
63 
64 #endif /* ISC_ATOMIC_H */
65