upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/api_ref_state_test.cpp
diff options
context:
space:
mode:
authorRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
committerRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
commit63f0e0dea3a9d44201db408c4e2c3f2f5b2b94d6 (patch)
tree86f0dcad8070e2a3a693ef4897aa83950cfeb224 /runtime/test/api_ref_state_test.cpp
parentf78f7d6a2a9364cb128c03ac539fe540d5c70df1 (diff)
parent6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff)
Merge main into fix/walk-bink_names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/api_ref_state_test.cpp')
-rw-r--r--runtime/test/api_ref_state_test.cpp315
1 files changed, 315 insertions, 0 deletions
diff --git a/runtime/test/api_ref_state_test.cpp b/runtime/test/api_ref_state_test.cpp
new file mode 100644
index 000000000..b9ecf36b8
--- /dev/null
+++ b/runtime/test/api_ref_state_test.cpp
@@ -0,0 +1,315 @@
1#include <windows.h>
2#include <ApiRefState.h>
3
4#include <cstdio>
5#include <atomic>
6#include <thread>
7
8static int failures = 0;
9
10void Check(bool condition, const char* name)
11{
12 std::printf("%s %s\n",
13 condition ? "PASS" : "FAIL", name);
14
15 if (!condition)
16 ++failures;
17}
18
19struct RaceContext
20{
21 ApiRefState state;
22 HANDLE rundownEntered = nullptr;
23 HANDLE allowRundownExit = nullptr;
24};
25
26DWORD WINAPI RundownThread(void* arg)
27{
28 auto& ctx = *static_cast<RaceContext*>(arg);
29
30 if (ctx.state.AddRef() != 0)
31 return 1;
32
33 auto finalRelease = ctx.state.Release();
34
35 if (!finalRelease.startRundown ||
36 finalRelease.remaining != 0)
37 return 2;
38
39 // Simulate an internal AddRef during RundownObject().
40 if (ctx.state.AddRef() != 0)
41 return 3;
42
43 SetEvent(ctx.rundownEntered);
44
45 if (WaitForSingleObject(
46 ctx.allowRundownExit, 3000) != WAIT_OBJECT_0)
47 return 4;
48
49 auto internalRelease = ctx.state.Release();
50
51 if (internalRelease.startRundown ||
52 internalRelease.remaining != 0)
53 return 5;
54
55 return 0;
56}
57
58void CheckPinWinsBeforeFinalRelease()
59{
60 constexpr int rounds = 200;
61 int failuresLocal = 0;
62
63 for (int i = 0; i < rounds; ++i)
64 {
65 ApiRefState state;
66 state.AddRef();
67
68 std::atomic<bool> pinFinished{ false };
69 std::atomic<bool> allowPinRelease{ false };
70
71 bool acquired = false;
72 ApiRefState::ReleaseResult pinRelease{ 0, false };
73
74 std::thread contender([&]
75 {
76 acquired = state.TryAddRef();
77
78 pinFinished.store(true, std::memory_order_release);
79
80 if (acquired)
81 {
82 while (!allowPinRelease.load(
83 std::memory_order_acquire))
84 {
85 std::this_thread::yield();
86 }
87
88 pinRelease = state.Release();
89 }
90 });
91
92 while (!pinFinished.load(std::memory_order_acquire))
93 {
94 std::this_thread::yield();
95 }
96
97 const auto ownerRelease = state.Release();
98
99 bool valid =
100 acquired &&
101 ownerRelease.remaining == 1 &&
102 !ownerRelease.startRundown &&
103 state.RefCount() == 1 &&
104 !state.IsDying();
105
106 allowPinRelease.store(true, std::memory_order_release);
107
108 contender.join();
109
110 valid =
111 valid &&
112 pinRelease.remaining == 0 &&
113 pinRelease.startRundown &&
114 state.RefCount() == 0 &&
115 state.IsDying() &&
116 !state.TryAddRef();
117
118 if (!valid)
119 {
120 ++failuresLocal;
121 }
122 }
123
124 Check(failuresLocal == 0,
125 "pin acquired before final owner Release stays valid");
126
127 std::printf(
128 "forced pin-first rounds: %d, failures: %d\n",
129 rounds, failuresLocal);
130}
131void CheckFinalReleaseRace()
132{
133 constexpr int rounds = 1000;
134 int badRundown = 0;
135 int badFinalState = 0;
136 int successfulPins = 0;
137
138 for (int i = 0; i < rounds; ++i)
139 {
140 ApiRefState state;
141 state.AddRef();
142
143 std::atomic<bool> ready{ false };
144 std::atomic<bool> go{ false };
145 std::atomic<int> rundownCount{ 0 };
146 std::atomic<bool> pinned{ false };
147
148 std::thread contender([&]
149 {
150 ready.store(true, std::memory_order_release);
151
152 while (!go.load(std::memory_order_acquire))
153 std::this_thread::yield();
154
155 if (state.TryAddRef())
156 {
157 pinned.store(true, std::memory_order_relaxed);
158
159 if (state.Release().startRundown)
160 rundownCount.fetch_add(1);
161 }
162 });
163
164 while (!ready.load(std::memory_order_acquire))
165 std::this_thread::yield();
166
167 go.store(true, std::memory_order_release);
168
169 if (state.Release().startRundown)
170 rundownCount.fetch_add(1);
171
172 contender.join();
173
174 if (pinned.load(std::memory_order_relaxed))
175 ++successfulPins;
176
177 if (rundownCount.load() != 1)
178 ++badRundown;
179
180 bool unexpectedPin = state.TryAddRef();
181
182 if (unexpectedPin)
183 state.Release();
184
185 if (state.RefCount() != 0 ||
186 !state.IsDying() ||
187 unexpectedPin)
188 {
189 ++badFinalState;
190 }
191 }
192
193 Check(badRundown == 0,
194 "racing final Release starts rundown exactly once");
195
196 Check(badFinalState == 0,
197 "racing final Release preserves terminal state");
198
199 std::printf(
200 "race rounds: %d, successful pins: %d, rejected pins: %d\n",
201 rounds, successfulPins, rounds - successfulPins);
202}
203int main()
204{
205 std::setvbuf(stdout, nullptr, _IONBF, 0);
206
207 std::puts("api_ref_state_test");
208
209 ApiRefState state;
210
211 Check(!state.TryAddRef(),
212 "cannot pin zero-ref object");
213
214 Check(state.AddRef() == 0,
215 "initial AddRef");
216
217 Check(state.TryAddRef(),
218 "can pin live object");
219
220 Check(state.RefCount() == 2,
221 "two references held");
222
223 auto first = state.Release();
224
225 Check(first.remaining == 1 &&
226 !first.startRundown,
227 "non-final Release");
228
229 auto last = state.Release();
230
231 Check(last.remaining == 0 &&
232 last.startRundown,
233 "final Release starts rundown");
234
235 Check(state.IsDying(),
236 "dying marker is permanent");
237
238 Check(!state.TryAddRef(),
239 "cannot pin after final Release");
240
241 RaceContext race;
242
243 race.rundownEntered =
244 CreateEventW(nullptr, TRUE, FALSE, nullptr);
245
246 race.allowRundownExit =
247 CreateEventW(nullptr, TRUE, FALSE, nullptr);
248
249 if (!race.rundownEntered || !race.allowRundownExit)
250 return 2;
251
252 HANDLE thread = CreateThread(
253 nullptr, 0, RundownThread, &race, 0, nullptr);
254
255 if (!thread)
256 return 3;
257
258 DWORD entered = WaitForSingleObject(
259 race.rundownEntered, 3000);
260
261 Check(entered == WAIT_OBJECT_0,
262 "rundown thread reached checkpoint");
263
264 if (entered == WAIT_OBJECT_0)
265 {
266 Check(race.state.IsDying(),
267 "dying bit survives internal AddRef");
268
269 Check(race.state.RefCount() == 1,
270 "temporary internal reference exists");
271
272 bool rejected = true;
273
274 for (int i = 0; i < 10000; ++i)
275 {
276 if (race.state.TryAddRef())
277 {
278 rejected = false;
279 race.state.Release();
280 break;
281 }
282 }
283
284 Check(rejected,
285 "concurrent pins rejected during rundown");
286 }
287
288 SetEvent(race.allowRundownExit);
289
290 DWORD joined = WaitForSingleObject(thread, 3000);
291
292 Check(joined == WAIT_OBJECT_0,
293 "rundown thread completed");
294
295 if (joined == WAIT_OBJECT_0)
296 {
297 DWORD exitCode = 1;
298 GetExitCodeThread(thread, &exitCode);
299
300 Check(exitCode == 0,
301 "internal Release did not restart rundown");
302 }
303
304 CloseHandle(thread);
305 CloseHandle(race.rundownEntered);
306 CloseHandle(race.allowRundownExit);
307
308 CheckPinWinsBeforeFinalRelease();
309 CheckFinalReleaseRace();
310
311 std::printf("RESULT: %s (%d failures)\n",
312 failures == 0 ? "PASS" : "FAIL", failures);
313
314 return failures == 0 ? 0 : 1;
315}