1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/windows.h>
#include <winpr/interlocked.h>
int TestInterlockedAccess(int argc, char* argv[])
{
LONG* Addend = NULL;
LONG* Target = NULL;
LONG oldValue = 0;
LONG* Destination = NULL;
LONGLONG oldValue64 = 0;
LONGLONG* Destination64 = NULL;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
/* InterlockedIncrement */
Addend = winpr_aligned_malloc(sizeof(LONG), sizeof(LONG));
if (!Addend)
{
printf("Failed to allocate memory\n");
return -1;
}
*Addend = 0;
for (int index = 0; index < 10; index++)
InterlockedIncrement(Addend);
if (*Addend != 10)
{
printf("InterlockedIncrement failure: Actual: %" PRId32 ", Expected: 10\n", *Addend);
return -1;
}
/* InterlockedDecrement */
for (int index = 0; index < 10; index++)
InterlockedDecrement(Addend);
if (*Addend != 0)
{
printf("InterlockedDecrement failure: Actual: %" PRId32 ", Expected: 0\n", *Addend);
return -1;
}
/* InterlockedExchange */
Target = winpr_aligned_malloc(sizeof(LONG), sizeof(LONG));
if (!Target)
{
printf("Failed to allocate memory\n");
return -1;
}
*Target = 0xAA;
oldValue = InterlockedExchange(Target, 0xFF);
if (oldValue != 0xAA)
{
printf("InterlockedExchange failure: Actual: 0x%08" PRIX32 ", Expected: 0xAA\n", oldValue);
return -1;
}
if (*Target != 0xFF)
{
printf("InterlockedExchange failure: Actual: 0x%08" PRIX32 ", Expected: 0xFF\n", *Target);
return -1;
}
/* InterlockedExchangeAdd */
*Addend = 25;
oldValue = InterlockedExchangeAdd(Addend, 100);
if (oldValue != 25)
{
printf("InterlockedExchangeAdd failure: Actual: %" PRId32 ", Expected: 25\n", oldValue);
return -1;
}
if (*Addend != 125)
{
printf("InterlockedExchangeAdd failure: Actual: %" PRId32 ", Expected: 125\n", *Addend);
return -1;
}
/* InterlockedCompareExchange (*Destination == Comparand) */
Destination = winpr_aligned_malloc(sizeof(LONG), sizeof(LONG));
if (!Destination)
{
printf("Failed to allocate memory\n");
return -1;
}
*Destination = (LONG)0xAABBCCDDL;
oldValue = InterlockedCompareExchange(Destination, (LONG)0xCCDDEEFFL, (LONG)0xAABBCCDDL);
if (oldValue != (LONG)0xAABBCCDDL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xAABBCCDD\n",
oldValue);
return -1;
}
if ((*Destination) != (LONG)0xCCDDEEFFL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xCCDDEEFF\n",
*Destination);
return -1;
}
/* InterlockedCompareExchange (*Destination != Comparand) */
*Destination = (LONG)0xAABBCCDDL;
oldValue = InterlockedCompareExchange(Destination, -857870593L, 0x66778899L);
if (oldValue != (LONG)0xAABBCCDDL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xAABBCCDD\n",
oldValue);
return -1;
}
if ((*Destination) != (LONG)0xAABBCCDDL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
", Expected: 0xAABBCCDD\n",
*Destination);
return -1;
}
/* InterlockedCompareExchange64 (*Destination == Comparand) */
Destination64 = winpr_aligned_malloc(sizeof(LONGLONG), sizeof(LONGLONG));
if (!Destination64)
{
printf("Failed to allocate memory\n");
return -1;
}
*Destination64 = 0x66778899AABBCCDD;
oldValue64 =
InterlockedCompareExchange64(Destination64, 0x8899AABBCCDDEEFF, 0x66778899AABBCCDD);
if (oldValue64 != 0x66778899AABBCCDD)
{
printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
", Expected: 0x66778899AABBCCDD\n",
oldValue64);
return -1;
}
if ((*Destination64) != (LONGLONG)0x8899AABBCCDDEEFFLL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
", Expected: 0x8899AABBCCDDEEFF\n",
*Destination64);
return -1;
}
/* InterlockedCompareExchange64 (*Destination != Comparand) */
*Destination64 = 0x66778899AABBCCDDLL;
oldValue64 = InterlockedCompareExchange64(Destination64, 0x8899AABBCCDDEEFFLL, 12345);
if (oldValue64 != 0x66778899AABBCCDDLL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
", Expected: 0x66778899AABBCCDD\n",
oldValue64);
return -1;
}
if (*Destination64 != 0x66778899AABBCCDDLL)
{
printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
", Expected: 0x66778899AABBCCDD\n",
*Destination64);
return -1;
}
winpr_aligned_free(Addend);
winpr_aligned_free(Target);
winpr_aligned_free(Destination);
winpr_aligned_free(Destination64);
return 0;
}
|