[XDK]
[reactos.git] / include / xdk / interlocked.h
1 /******************************************************************************
2 * INTERLOCKED Functions *
3 ******************************************************************************/
4 //
5 // Intrinsics (note: taken from our winnt.h)
6 // FIXME: 64-bit
7 //
8 #if defined(__GNUC__)
9
10 static __inline__ BOOLEAN
11 InterlockedBitTestAndSet(
12 IN LONG volatile *Base,
13 IN LONG Bit)
14 {
15 #if defined(_M_IX86)
16 LONG OldBit;
17 __asm__ __volatile__("lock "
18 "btsl %2,%1\n\t"
19 "sbbl %0,%0\n\t"
20 :"=r" (OldBit),"+m" (*Base)
21 :"Ir" (Bit)
22 : "memory");
23 return OldBit;
24 #else
25 return (_InterlockedOr(Base, 1 << Bit) >> Bit) & 1;
26 #endif
27 }
28
29 static __inline__ BOOLEAN
30 InterlockedBitTestAndReset(
31 IN LONG volatile *Base,
32 IN LONG Bit)
33 {
34 #if defined(_M_IX86)
35 LONG OldBit;
36 __asm__ __volatile__("lock "
37 "btrl %2,%1\n\t"
38 "sbbl %0,%0\n\t"
39 :"=r" (OldBit),"+m" (*Base)
40 :"Ir" (Bit)
41 : "memory");
42 return OldBit;
43 #else
44 return (_InterlockedAnd(Base, ~(1 << Bit)) >> Bit) & 1;
45 #endif
46 }
47
48 #endif /* defined(__GNUC__) */
49
50 #define BitScanForward _BitScanForward
51 #define BitScanReverse _BitScanReverse
52 #define BitTest _bittest
53 #define BitTestAndComplement _bittestandcomplement
54 #define BitTestAndSet _bittestandset
55 #define BitTestAndReset _bittestandreset
56 #define InterlockedBitTestAndSet _interlockedbittestandset
57 #define InterlockedBitTestAndReset _interlockedbittestandreset
58
59 #ifdef _M_AMD64
60 #define BitTest64 _bittest64
61 #define BitTestAndComplement64 _bittestandcomplement64
62 #define BitTestAndSet64 _bittestandset64
63 #define BitTestAndReset64 _bittestandreset64
64 #define InterlockedBitTestAndSet64 _interlockedbittestandset64
65 #define InterlockedBitTestAndReset64 _interlockedbittestandreset64
66 #endif
67
68 #if !defined(__INTERLOCKED_DECLARED)
69 #define __INTERLOCKED_DECLARED
70
71 #if defined (_X86_)
72 #if defined(NO_INTERLOCKED_INTRINSICS)
73 NTKERNELAPI
74 LONG
75 FASTCALL
76 InterlockedIncrement(
77 IN OUT LONG volatile *Addend);
78
79 NTKERNELAPI
80 LONG
81 FASTCALL
82 InterlockedDecrement(
83 IN OUT LONG volatile *Addend);
84
85 NTKERNELAPI
86 LONG
87 FASTCALL
88 InterlockedCompareExchange(
89 IN OUT LONG volatile *Destination,
90 IN LONG Exchange,
91 IN LONG Comparand);
92
93 NTKERNELAPI
94 LONG
95 FASTCALL
96 InterlockedExchange(
97 IN OUT LONG volatile *Destination,
98 IN LONG Value);
99
100 NTKERNELAPI
101 LONG
102 FASTCALL
103 InterlockedExchangeAdd(
104 IN OUT LONG volatile *Addend,
105 IN LONG Value);
106
107 #else /* !defined(NO_INTERLOCKED_INTRINSICS) */
108
109 #define InterlockedExchange _InterlockedExchange
110 #define InterlockedIncrement _InterlockedIncrement
111 #define InterlockedDecrement _InterlockedDecrement
112 #define InterlockedExchangeAdd _InterlockedExchangeAdd
113 #define InterlockedCompareExchange _InterlockedCompareExchange
114 #define InterlockedOr _InterlockedOr
115 #define InterlockedAnd _InterlockedAnd
116 #define InterlockedXor _InterlockedXor
117
118 #endif /* !defined(NO_INTERLOCKED_INTRINSICS) */
119
120 #endif /* defined (_X86_) */
121
122 #if !defined (_WIN64)
123 /*
124 * PVOID
125 * InterlockedExchangePointer(
126 * IN OUT PVOID volatile *Target,
127 * IN PVOID Value)
128 */
129 #define InterlockedExchangePointer(Target, Value) \
130 ((PVOID) InterlockedExchange((PLONG) Target, (LONG) Value))
131
132 /*
133 * PVOID
134 * InterlockedCompareExchangePointer(
135 * IN OUT PVOID *Destination,
136 * IN PVOID Exchange,
137 * IN PVOID Comparand)
138 */
139 #define InterlockedCompareExchangePointer(Destination, Exchange, Comparand) \
140 ((PVOID) InterlockedCompareExchange((PLONG) Destination, (LONG) Exchange, (LONG) Comparand))
141
142 #define InterlockedExchangeAddSizeT(a, b) InterlockedExchangeAdd((LONG *)a, b)
143 #define InterlockedIncrementSizeT(a) InterlockedIncrement((LONG *)a)
144 #define InterlockedDecrementSizeT(a) InterlockedDecrement((LONG *)a)
145
146 #endif // !defined (_WIN64)
147
148 #if defined (_M_AMD64)
149
150 #define InterlockedExchangeAddSizeT(a, b) InterlockedExchangeAdd64((LONGLONG *)a, (LONGLONG)b)
151 #define InterlockedIncrementSizeT(a) InterlockedIncrement64((LONGLONG *)a)
152 #define InterlockedDecrementSizeT(a) InterlockedDecrement64((LONGLONG *)a)
153 #define InterlockedAnd _InterlockedAnd
154 #define InterlockedOr _InterlockedOr
155 #define InterlockedXor _InterlockedXor
156 #define InterlockedIncrement _InterlockedIncrement
157 #define InterlockedDecrement _InterlockedDecrement
158 #define InterlockedAdd _InterlockedAdd
159 #define InterlockedExchange _InterlockedExchange
160 #define InterlockedExchangeAdd _InterlockedExchangeAdd
161 #define InterlockedCompareExchange _InterlockedCompareExchange
162 #define InterlockedAnd64 _InterlockedAnd64
163 #define InterlockedOr64 _InterlockedOr64
164 #define InterlockedXor64 _InterlockedXor64
165 #define InterlockedIncrement64 _InterlockedIncrement64
166 #define InterlockedDecrement64 _InterlockedDecrement64
167 #define InterlockedAdd64 _InterlockedAdd64
168 #define InterlockedExchange64 _InterlockedExchange64
169 #define InterlockedExchangeAdd64 _InterlockedExchangeAdd64
170 #define InterlockedCompareExchange64 _InterlockedCompareExchange64
171 #define InterlockedCompareExchangePointer _InterlockedCompareExchangePointer
172 #define InterlockedExchangePointer _InterlockedExchangePointer
173 #define InterlockedBitTestAndSet64 _interlockedbittestandset64
174 #define InterlockedBitTestAndReset64 _interlockedbittestandreset64
175
176 #endif // _M_AMD64
177
178 #if defined(_M_AMD64) && !defined(RC_INVOKED) && !defined(MIDL_PASS)
179 //#if !defined(_X86AMD64_) // FIXME: what's _X86AMD64_ used for?
180 FORCEINLINE
181 LONG64
182 InterlockedAdd64(
183 IN OUT LONG64 volatile *Addend,
184 IN LONG64 Value)
185 {
186 return InterlockedExchangeAdd64(Addend, Value) + Value;
187 }
188 //#endif
189 #endif
190
191 #endif /* !__INTERLOCKED_DECLARED */
192
193