2 * Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 /* This file contains AO primitives based on VC++ built-in intrinsic */
24 /* functions commonly available across 32-bit architectures. */
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. */
33 /* Seems like over-kill, but that's what MSDN recommends. */
34 /* And apparently winbase.h is not always self-contained. */
36 #if _MSC_VER < 1310 || !defined(AO_USE_INTERLOCKED_INTRINSICS)
38 # define _InterlockedIncrement InterlockedIncrement
39 # define _InterlockedDecrement InterlockedDecrement
40 # define _InterlockedExchange InterlockedExchange
41 # define _InterlockedExchangeAdd InterlockedExchangeAdd
42 # define _InterlockedCompareExchange InterlockedCompareExchange
44 # define AO_INTERLOCKED_VOLATILE /**/
46 #else /* elif _MSC_VER >= 1310 */
53 # pragma intrinsic (_ReadWriteBarrier)
55 # else /* elif _MSC_VER < 1400 */
59 LONG __cdecl _InterlockedIncrement(LONG volatile *);
60 LONG __cdecl _InterlockedDecrement(LONG volatile *);
61 LONG __cdecl _InterlockedExchangeAdd(LONG volatile *, LONG);
62 LONG __cdecl _InterlockedExchange(LONG volatile *, LONG);
63 LONG __cdecl _InterlockedCompareExchange(LONG volatile *,
64 LONG /* Exchange */, LONG /* Comp */);
68 # endif /* _MSC_VER < 1400 */
70 # pragma intrinsic (_InterlockedIncrement)
71 # pragma intrinsic (_InterlockedDecrement)
72 # pragma intrinsic (_InterlockedExchange)
73 # pragma intrinsic (_InterlockedExchangeAdd)
74 # pragma intrinsic (_InterlockedCompareExchange)
76 # define AO_INTERLOCKED_VOLATILE volatile
78 #endif /* _MSC_VER >= 1310 */
81 AO_fetch_and_add_full(volatile AO_t *p, AO_t incr)
83 return _InterlockedExchangeAdd((LONG AO_INTERLOCKED_VOLATILE *)p,
86 #define AO_HAVE_fetch_and_add_full
89 AO_fetch_and_add1_full(volatile AO_t *p)
91 return _InterlockedIncrement((LONG AO_INTERLOCKED_VOLATILE *)p) - 1;
93 #define AO_HAVE_fetch_and_add1_full
96 AO_fetch_and_sub1_full(volatile AO_t *p)
98 return _InterlockedDecrement((LONG AO_INTERLOCKED_VOLATILE *)p) + 1;
100 #define AO_HAVE_fetch_and_sub1_full
102 #ifdef AO_ASSUME_WINDOWS98
103 /* Returns nonzero if the comparison succeeded. */
105 AO_compare_and_swap_full(volatile AO_t *addr, AO_t old, AO_t new_val)
107 # ifdef AO_OLD_STYLE_INTERLOCKED_COMPARE_EXCHANGE
108 return _InterlockedCompareExchange((PVOID AO_INTERLOCKED_VOLATILE *)addr,
109 (PVOID)new_val, (PVOID)old)
112 return _InterlockedCompareExchange((LONG AO_INTERLOCKED_VOLATILE *)addr,
113 (LONG)new_val, (LONG)old)
117 # define AO_HAVE_compare_and_swap_full
118 #endif /* AO_ASSUME_WINDOWS98 */