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