xref: /haiku/headers/libs/zydis/Zycore/Internal/AtomicMSVC.h (revision caed67a8cba83913b9c21ac2b06ebc6bd1cb3111)
1 /***************************************************************************************************
2 
3   Zyan Core Library (Zyan-C)
4 
5   Original Author : Florian Bernd
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
27 #ifndef ZYCORE_ATOMIC_MSVC_H
28 #define ZYCORE_ATOMIC_MSVC_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <Windows.h>
35 
36 #include <Zycore/Defines.h>
37 #include <Zycore/Types.h>
38 
39 /* ============================================================================================== */
40 /* Functions                                                                                      */
41 /* ============================================================================================== */
42 
43 #if defined(ZYAN_MSVC)
44 
45 /* ---------------------------------------------------------------------------------------------- */
46 /* Pointer sized                                                                                  */
47 /* ---------------------------------------------------------------------------------------------- */
48 
49 #if defined(ZYAN_X86)
50 
51 static ZYAN_INLINE ZyanUPointer ZyanAtomicCompareExchange(ZyanAtomicPointer* destination,
52     ZyanUPointer comparand, ZyanUPointer value)
53 {
54     return (ZyanUPointer)ZyanAtomicCompareExchange32((ZyanAtomic32*)destination, comparand, value);
55 }
56 
57 static ZYAN_INLINE ZyanUPointer ZyanAtomicIncrement(ZyanAtomicPointer* destination)
58 {
59     return (ZyanUPointer)ZyanAtomicIncrement32((ZyanAtomic32*)destination);
60 }
61 
62 static ZYAN_INLINE ZyanUPointer ZyanAtomicDecrement(ZyanAtomicPointer* destination)
63 {
64     return (ZyanUPointer)ZyanAtomicDecrement32((ZyanAtomic32*)destination);
65 }
66 
67 #elif defined(ZYAN_X64)
68 
69 static ZYAN_INLINE ZyanUPointer ZyanAtomicCompareExchange(ZyanAtomicPointer* destination,
70     ZyanUPointer comparand, ZyanUPointer value)
71 {
72     return (ZyanUPointer)ZyanAtomicCompareExchange64((ZyanAtomic64*)destination, comparand, value);
73 }
74 
75 static ZYAN_INLINE ZyanUPointer ZyanAtomicIncrement(ZyanAtomicPointer* destination)
76 {
77     return (ZyanUPointer)ZyanAtomicIncrement64((ZyanAtomic64*)destination);
78 }
79 
80 static ZYAN_INLINE ZyanUPointer ZyanAtomicDecrement(ZyanAtomicPointer* destination)
81 {
82     return (ZyanUPointer)ZyanAtomicDecrement64((ZyanAtomic64*)destination);
83 }
84 
85 #else
86 #   error "Unsupported architecture detected"
87 #endif
88 
89 /* ---------------------------------------------------------------------------------------------- */
90 /* 32-bit                                                                                         */
91 /* ---------------------------------------------------------------------------------------------- */
92 
93 static ZYAN_INLINE ZyanU32 ZyanAtomicCompareExchange32(ZyanAtomic32* destination,
94     ZyanU32 comparand, ZyanU32 value)
95 {
96     return (ZyanU32)(_InterlockedCompareExchange((volatile LONG*)&(destination->value),
97         (LONG)value, (LONG)comparand));
98 }
99 
100 static ZYAN_INLINE ZyanU32 ZyanAtomicIncrement32(ZyanAtomic32* destination)
101 {
102     return (ZyanU32)(_InterlockedIncrement((volatile LONG*)&(destination->value)));
103 }
104 
105 static ZYAN_INLINE ZyanU32 ZyanAtomicDecrement32(ZyanAtomic32* destination)
106 {
107     return (ZyanU32)(_InterlockedDecrement((volatile LONG*)&(destination->value)));
108 }
109 
110 /* ---------------------------------------------------------------------------------------------- */
111 /* 64-bit                                                                                         */
112 /* ---------------------------------------------------------------------------------------------- */
113 
114 static ZYAN_INLINE ZyanU64 ZyanAtomicCompareExchange64(ZyanAtomic64* destination,
115     ZyanU64 comparand, ZyanU64 value)
116 {
117     return (ZyanU64)(_InterlockedCompareExchange64((volatile LONG64*)&(destination->value),
118         (LONG64)value, (LONG64)comparand));
119 }
120 
121 static ZYAN_INLINE ZyanU64 ZyanAtomicIncrement64(ZyanAtomic64* destination)
122 {
123     return (ZyanU64)(_InterlockedIncrement64((volatile LONG64*)&(destination->value)));
124 }
125 
126 static ZYAN_INLINE ZyanU64 ZyanAtomicDecrement64(ZyanAtomic64* destination)
127 {
128     return (ZyanU64)(_InterlockedDecrement64((volatile LONG64*)&(destination->value)));
129 }
130 
131 /* ---------------------------------------------------------------------------------------------- */
132 
133 #endif
134 
135 /* ============================================================================================== */
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* ZYCORE_ATOMIC_MSVC_H */
142