3 #include <winpr/windows.h>
4 #include <winpr/interlocked.h>
6 int TestInterlockedAccess(
int argc,
char* argv[])
11 LONG* Destination = NULL;
12 LONGLONG oldValue64 = 0;
13 LONGLONG* Destination64 = NULL;
18 Addend = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
21 printf(
"Failed to allocate memory\n");
27 for (
int index = 0; index < 10; index++)
28 InterlockedIncrement(Addend);
32 printf(
"InterlockedIncrement failure: Actual: %" PRId32
", Expected: 10\n", *Addend);
38 for (
int index = 0; index < 10; index++)
39 (
void)InterlockedDecrement(Addend);
43 printf(
"InterlockedDecrement failure: Actual: %" PRId32
", Expected: 0\n", *Addend);
49 Target = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
53 printf(
"Failed to allocate memory\n");
59 oldValue = InterlockedExchange(Target, 0xFF);
63 printf(
"InterlockedExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xAA\n", oldValue);
69 printf(
"InterlockedExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xFF\n", *Target);
77 oldValue = InterlockedExchangeAdd(Addend, 100);
81 printf(
"InterlockedExchangeAdd failure: Actual: %" PRId32
", Expected: 25\n", oldValue);
87 printf(
"InterlockedExchangeAdd failure: Actual: %" PRId32
", Expected: 125\n", *Addend);
93 Destination = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
96 printf(
"Failed to allocate memory\n");
100 *Destination = (LONG)0xAABBCCDDL;
102 oldValue = InterlockedCompareExchange(Destination, (LONG)0xCCDDEEFFL, (LONG)0xAABBCCDDL);
104 if (oldValue != (LONG)0xAABBCCDDL)
106 printf(
"InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
107 ", Expected: 0xAABBCCDD\n",
112 if ((*Destination) != (LONG)0xCCDDEEFFL)
114 printf(
"InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
115 ", Expected: 0xCCDDEEFF\n",
122 *Destination = (LONG)0xAABBCCDDL;
124 oldValue = InterlockedCompareExchange(Destination, -857870593L, 0x66778899L);
126 if (oldValue != (LONG)0xAABBCCDDL)
128 printf(
"InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
129 ", Expected: 0xAABBCCDD\n",
134 if ((*Destination) != (LONG)0xAABBCCDDL)
136 printf(
"InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
137 ", Expected: 0xAABBCCDD\n",
144 Destination64 = winpr_aligned_malloc(
sizeof(LONGLONG),
sizeof(LONGLONG));
147 printf(
"Failed to allocate memory\n");
151 *Destination64 = 0x66778899AABBCCDD;
154 InterlockedCompareExchange64(Destination64, 0x0899AABBCCDDEEFFLL, 0x66778899AABBCCDD);
156 if (oldValue64 != 0x66778899AABBCCDD)
158 printf(
"InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
159 ", Expected: 0x66778899AABBCCDD\n",
164 if ((*Destination64) != 0x0899AABBCCDDEEFFLL)
166 printf(
"InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
167 ", Expected: 0x0899AABBCCDDEEFFLL\n",
174 *Destination64 = 0x66778899AABBCCDDLL;
176 oldValue64 = InterlockedCompareExchange64(Destination64, 0x0899AABBCCDDEEFFLL, 12345);
178 if (oldValue64 != 0x66778899AABBCCDDLL)
180 printf(
"InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
181 ", Expected: 0x66778899AABBCCDD\n",
186 if (*Destination64 != 0x66778899AABBCCDDLL)
188 printf(
"InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
189 ", Expected: 0x66778899AABBCCDD\n",
194 winpr_aligned_free(Addend);
195 winpr_aligned_free(Target);
196 winpr_aligned_free(Destination);
197 winpr_aligned_free(Destination64);