1 /*
2 * Copyright (c) 2003-2011 Hewlett-Packard Development Company, L.P.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 /* This file contains AO primitives based on VC++ built-in intrinsic */
24 /* functions commonly available across 32-bit architectures. */
25
26 /* This file should be included from arch-specific header files. */
27 /* Define AO_USE_INTERLOCKED_INTRINSICS if _Interlocked primitives */
28 /* (used below) are available as intrinsic ones for a target arch */
29 /* (otherwise "Interlocked" functions family is used instead). */
30 /* Define AO_ASSUME_WINDOWS98 if CAS is available. */
31
32 #include <windows.h>
33 /* Seems like over-kill, but that's what MSDN recommends. */
34 /* And apparently winbase.h is not always self-contained. */
35
36 #if _MSC_VER < 1310 || !defined(AO_USE_INTERLOCKED_INTRINSICS)
37
38 # define _InterlockedIncrement InterlockedIncrement
39 # define _InterlockedDecrement InterlockedDecrement
40 # define _InterlockedExchangeAdd InterlockedExchangeAdd
41 # define _InterlockedCompareExchange InterlockedCompareExchange
42
43 # define AO_INTERLOCKED_VOLATILE /**/
44
45 #else /* elif _MSC_VER >= 1310 */
46
47 # if _MSC_VER >= 1400
48 # ifndef _WIN32_WCE
49 # include <intrin.h>
50 # endif
51
52 # else /* elif _MSC_VER < 1400 */
53 # ifdef __cplusplus
54 extern "C" {
55 # endif
56 LONG __cdecl _InterlockedIncrement(LONG volatile *);
57 LONG __cdecl _InterlockedDecrement(LONG volatile *);
58 LONG __cdecl _InterlockedExchangeAdd(LONG volatile *, LONG);
59 LONG __cdecl _InterlockedCompareExchange(LONG volatile *,
60 LONG /* Exchange */, LONG /* Comp */);
61 # ifdef __cplusplus
62 }
63 # endif
64 # endif /* _MSC_VER < 1400 */
65
66 # if !defined(AO_PREFER_GENERALIZED) || !defined(AO_ASSUME_WINDOWS98)
67 # pragma intrinsic (_InterlockedIncrement)
68 # pragma intrinsic (_InterlockedDecrement)
69 # pragma intrinsic (_InterlockedExchangeAdd)
70 # endif /* !AO_PREFER_GENERALIZED */
71 # pragma intrinsic (_InterlockedCompareExchange)
72
73 # define AO_INTERLOCKED_VOLATILE volatile
74
75 #endif /* _MSC_VER >= 1310 */
76
77 #if !defined(AO_PREFER_GENERALIZED) || !defined(AO_ASSUME_WINDOWS98)
78 AO_INLINE AO_t
AO_fetch_and_add_full(volatile AO_t * p,AO_t incr)79 AO_fetch_and_add_full(volatile AO_t *p, AO_t incr)
80 {
81 return _InterlockedExchangeAdd((LONG AO_INTERLOCKED_VOLATILE *)p,
82 (LONG)incr);
83 }
84 #define AO_HAVE_fetch_and_add_full
85
86 AO_INLINE AO_t
AO_fetch_and_add1_full(volatile AO_t * p)87 AO_fetch_and_add1_full(volatile AO_t *p)
88 {
89 return _InterlockedIncrement((LONG AO_INTERLOCKED_VOLATILE *)p) - 1;
90 }
91 #define AO_HAVE_fetch_and_add1_full
92
93 AO_INLINE AO_t
AO_fetch_and_sub1_full(volatile AO_t * p)94 AO_fetch_and_sub1_full(volatile AO_t *p)
95 {
96 return _InterlockedDecrement((LONG AO_INTERLOCKED_VOLATILE *)p) + 1;
97 }
98 #define AO_HAVE_fetch_and_sub1_full
99 #endif /* !AO_PREFER_GENERALIZED */
100
101 #ifdef AO_ASSUME_WINDOWS98
102 AO_INLINE AO_t
AO_fetch_compare_and_swap_full(volatile AO_t * addr,AO_t old_val,AO_t new_val)103 AO_fetch_compare_and_swap_full(volatile AO_t *addr, AO_t old_val,
104 AO_t new_val)
105 {
106 # ifdef AO_OLD_STYLE_INTERLOCKED_COMPARE_EXCHANGE
107 return (AO_t)_InterlockedCompareExchange(
108 (PVOID AO_INTERLOCKED_VOLATILE *)addr,
109 (PVOID)new_val, (PVOID)old_val);
110 # else
111 return (AO_t)_InterlockedCompareExchange(
112 (LONG AO_INTERLOCKED_VOLATILE *)addr,
113 (LONG)new_val, (LONG)old_val);
114 # endif
115 }
116 # define AO_HAVE_fetch_compare_and_swap_full
117 #endif /* AO_ASSUME_WINDOWS98 */
118