1 /*	$NetBSD: atomic.h,v 1.3 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2013  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 */
20 
21 #ifndef ISC_ATOMIC_H
22 #define ISC_ATOMIC_H 1
23 
24 #include <config.h>
25 #include <isc/platform.h>
26 #include <isc/types.h>
27 
28 /*
29  * This routine atomically increments the value stored in 'p' by 'val', and
30  * returns the previous value.
31  */
32 #ifdef ISC_PLATFORM_HAVEXADD
33 static __inline isc_int32_t
34 isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
35 	return (isc_int32_t) _InterlockedExchangeAdd((long *)p, (long)val);
36 }
37 #endif
38 
39 #ifdef ISC_PLATFORM_HAVEXADDQ
40 static __inline isc_int64_t
41 isc_atomic_xaddq(isc_int64_t *p, isc_int64_t val) {
42 	return (isc_int64_t) _InterlockedExchangeAdd64((__int64 *)p,
43 						       (__int64) val);
44 }
45 #endif
46 
47 /*
48  * This routine atomically stores the value 'val' in 'p'.
49  */
50 #ifdef ISC_PLATFORM_HAVEATOMICSTORE
51 static __inline void
52 isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
53 	(void) _InterlockedExchange((long *)p, (long)val);
54 }
55 #endif
56 
57 /*
58  * This routine atomically replaces the value in 'p' with 'val', if the
59  * original value is equal to 'cmpval'.  The original value is returned in any
60  * case.
61  */
62 #ifdef ISC_PLATFORM_HAVECMPXCHG
63 static __inline isc_int32_t
64 isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
65 	/* beware: swap arguments */
66 	return (isc_int32_t) _InterlockedCompareExchange((long *)p,
67 							 (long)val,
68 							 (long)cmpval);
69 }
70 #endif
71 
72 #endif /* ISC_ATOMIC_H */
73