92251c342dae9be5a42e3add768dea1b923643ba
[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 InterlockedBitTestAndSet64 _interlockedbittestandset64
61 #define InterlockedBitTestAndReset64 _interlockedbittestandreset64
62 #endif
63
64 #if !defined(__INTERLOCKED_DECLARED)
65 #define __INTERLOCKED_DECLARED
66
67 #if defined (_X86_)
68 #if defined(NO_INTERLOCKED_INTRINSICS)
69 NTKERNELAPI
70 LONG
71 FASTCALL
72 InterlockedIncrement(
73 IN OUT LONG volatile *Addend);
74
75 NTKERNELAPI
76 LONG
77 FASTCALL
78 InterlockedDecrement(
79 IN OUT LONG volatile *Addend);
80
81 NTKERNELAPI
82 LONG
83 FASTCALL
84 InterlockedCompareExchange(
85 IN OUT LONG volatile *Destination,
86 IN LONG Exchange,
87 IN LONG Comparand);
88
89 NTKERNELAPI
90 LONG
91 FASTCALL
92 InterlockedExchange(
93 IN OUT LONG volatile *Destination,
94 IN LONG Value);
95
96 NTKERNELAPI
97 LONG
98 FASTCALL
99 InterlockedExchangeAdd(
100 IN OUT LONG volatile *Addend,
101 IN LONG Value);
102
103 #else /* !defined(NO_INTERLOCKED_INTRINSICS) */
104
105 #define InterlockedExchange _InterlockedExchange
106 #define InterlockedIncrement _InterlockedIncrement
107 #define InterlockedDecrement _InterlockedDecrement
108 #define InterlockedExchangeAdd _InterlockedExchangeAdd
109 #define InterlockedCompareExchange _InterlockedCompareExchange
110 #define InterlockedOr _InterlockedOr
111 #define InterlockedAnd _InterlockedAnd
112 #define InterlockedXor _InterlockedXor
113
114 #endif /* !defined(NO_INTERLOCKED_INTRINSICS) */
115
116 #endif /* defined (_X86_) */
117
118 #if !defined (_WIN64)
119 /*
120 * PVOID
121 * InterlockedExchangePointer(
122 * IN OUT PVOID volatile *Target,
123 * IN PVOID Value)
124 */
125 #define InterlockedExchangePointer(Target, Value) \
126 ((PVOID) InterlockedExchange((PLONG) Target, (LONG) Value))
127
128 /*
129 * PVOID
130 * InterlockedCompareExchangePointer(
131 * IN OUT PVOID *Destination,
132 * IN PVOID Exchange,
133 * IN PVOID Comparand)
134 */
135 #define InterlockedCompareExchangePointer(Destination, Exchange, Comparand) \
136 ((PVOID) InterlockedCompareExchange((PLONG) Destination, (LONG) Exchange, (LONG) Comparand))
137
138 #define InterlockedExchangeAddSizeT(a, b) InterlockedExchangeAdd((LONG *)a, b)
139 #define InterlockedIncrementSizeT(a) InterlockedIncrement((LONG *)a)
140 #define InterlockedDecrementSizeT(a) InterlockedDecrement((LONG *)a)
141
142 #endif // !defined (_WIN64)
143
144 #if defined (_M_AMD64)
145
146 #define InterlockedExchangeAddSizeT(a, b) InterlockedExchangeAdd64((LONGLONG *)a, (LONGLONG)b)
147 #define InterlockedIncrementSizeT(a) InterlockedIncrement64((LONGLONG *)a)
148 #define InterlockedDecrementSizeT(a) InterlockedDecrement64((LONGLONG *)a)
149 #define InterlockedAnd _InterlockedAnd
150 #define InterlockedOr _InterlockedOr
151 #define InterlockedXor _InterlockedXor
152 #define InterlockedIncrement _InterlockedIncrement
153 #define InterlockedDecrement _InterlockedDecrement
154 #define InterlockedAdd _InterlockedAdd
155 #define InterlockedExchange _InterlockedExchange
156 #define InterlockedExchangeAdd _InterlockedExchangeAdd
157 #define InterlockedCompareExchange _InterlockedCompareExchange
158 #define InterlockedAnd64 _InterlockedAnd64
159 #define InterlockedOr64 _InterlockedOr64
160 #define InterlockedXor64 _InterlockedXor64
161 #define InterlockedIncrement64 _InterlockedIncrement64
162 #define InterlockedDecrement64 _InterlockedDecrement64
163 #define InterlockedAdd64 _InterlockedAdd64
164 #define InterlockedExchange64 _InterlockedExchange64
165 #define InterlockedExchangeAdd64 _InterlockedExchangeAdd64
166 #define InterlockedCompareExchange64 _InterlockedCompareExchange64
167 #define InterlockedCompareExchangePointer _InterlockedCompareExchangePointer
168 #define InterlockedExchangePointer _InterlockedExchangePointer
169 #define InterlockedBitTestAndSet64 _interlockedbittestandset64
170 #define InterlockedBitTestAndReset64 _interlockedbittestandreset64
171
172 #endif // _M_AMD64
173
174 #if defined(_M_AMD64) && !defined(RC_INVOKED) && !defined(MIDL_PASS)
175 //#if !defined(_X86AMD64_) // FIXME: what's _X86AMD64_ used for?
176 FORCEINLINE
177 LONG64
178 InterlockedAdd64(
179 IN OUT LONG64 volatile *Addend,
180 IN LONG64 Value)
181 {
182 return InterlockedExchangeAdd64(Addend, Value) + Value;
183 }
184 //#endif
185 #endif
186
187 #endif /* !__INTERLOCKED_DECLARED */
188
189