1 | /* $Id: tstHandleTable.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Handle Tables.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/handletable.h>
|
---|
42 | #include <iprt/stream.h>
|
---|
43 | #include <iprt/initterm.h>
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/getopt.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/alloca.h>
|
---|
48 | #include <iprt/thread.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Global Variables *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | static unsigned g_cErrors;
|
---|
56 |
|
---|
57 | static DECLCALLBACK(void) tstHandleTableTest1Delete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
|
---|
58 | {
|
---|
59 | uint32_t *pcCalls = (uint32_t *)pvUser;
|
---|
60 | (*pcCalls)++;
|
---|
61 | RT_NOREF_PV(hHandleTable); RT_NOREF_PV(h); RT_NOREF_PV(pvCtx); RT_NOREF_PV(pvObj);
|
---|
62 | }
|
---|
63 |
|
---|
64 | static DECLCALLBACK(int) tstHandleTableTest1Retain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
|
---|
65 | {
|
---|
66 | uint32_t *pcCalls = (uint32_t *)pvUser;
|
---|
67 | (*pcCalls)++;
|
---|
68 | RT_NOREF_PV(hHandleTable); RT_NOREF_PV(pvCtx); RT_NOREF_PV(pvObj);
|
---|
69 | return VINF_SUCCESS;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static int tstHandleTableTest1(uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks, uint32_t fFlags)
|
---|
73 | {
|
---|
74 | const char *pszWithCtx = fFlags & RTHANDLETABLE_FLAGS_CONTEXT ? "WithCtx" : "";
|
---|
75 | uint32_t cRetainerCalls = 0;
|
---|
76 | int rc;
|
---|
77 |
|
---|
78 | RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
|
---|
79 | if (fFlags & RTHANDLETABLE_FLAGS_LOCKED) RTPrintf(" | LOCKED");
|
---|
80 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT) RTPrintf(" | CONTEXT");
|
---|
81 | RTPrintf(", %#x, %#x,,)...\n", uBase, cMax);
|
---|
82 |
|
---|
83 | RTHANDLETABLE hHT;
|
---|
84 | rc = RTHandleTableCreateEx(&hHT, fFlags, uBase, cMax,
|
---|
85 | fCallbacks ? tstHandleTableTest1Retain : NULL,
|
---|
86 | fCallbacks ? &cRetainerCalls : NULL);
|
---|
87 | if (RT_FAILURE(rc))
|
---|
88 | {
|
---|
89 | RTPrintf("\ntstHandleTable: FAILURE - RTHandleTableCreateEx failed, %Rrc!\n", rc);
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* fill it */
|
---|
94 | RTPrintf("tstHandleTable: TESTING RTHandleTableAlloc%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
95 | uint32_t i = uBase;
|
---|
96 | for (;; i++)
|
---|
97 | {
|
---|
98 | uint32_t h;
|
---|
99 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
100 | rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), NULL, &h);
|
---|
101 | else
|
---|
102 | rc = RTHandleTableAlloc(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), &h);
|
---|
103 | if (RT_SUCCESS(rc))
|
---|
104 | {
|
---|
105 | if (h != i)
|
---|
106 | {
|
---|
107 | RTPrintf("\ntstHandleTable: FAILURE (%d) - h=%d, expected %d!\n", __LINE__, h, i);
|
---|
108 | g_cErrors++;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else if (rc == VERR_NO_MORE_HANDLES)
|
---|
112 | {
|
---|
113 | if (i < cMax)
|
---|
114 | {
|
---|
115 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, expected > 65534!\n", __LINE__, i);
|
---|
116 | g_cErrors++;
|
---|
117 | }
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, rc=%Rrc!\n", __LINE__, i, rc);
|
---|
123 | g_cErrors++;
|
---|
124 | }
|
---|
125 | if (!(i % cUnitsPerDot))
|
---|
126 | {
|
---|
127 | RTPrintf(".");
|
---|
128 | RTStrmFlush(g_pStdOut);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | uint32_t const c = i;
|
---|
132 | RTPrintf(" c=%#x\n", c);
|
---|
133 | if (fCallbacks && cRetainerCalls != 0)
|
---|
134 | {
|
---|
135 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, cRetainerCalls);
|
---|
136 | g_cErrors++;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* look up all the entries */
|
---|
140 | RTPrintf("tstHandleTable: TESTING RTHandleTableLookup%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
141 | cRetainerCalls = 0;
|
---|
142 | for (i = uBase; i < c; i++)
|
---|
143 | {
|
---|
144 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
|
---|
145 | void *pvObj;
|
---|
146 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
147 | pvObj = RTHandleTableLookupWithCtx(hHT, i, NULL);
|
---|
148 | else
|
---|
149 | pvObj = RTHandleTableLookup(hHT, i);
|
---|
150 | if (!pvObj)
|
---|
151 | {
|
---|
152 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
|
---|
153 | g_cErrors++;
|
---|
154 | }
|
---|
155 | else if (pvObj != pvExpect)
|
---|
156 | {
|
---|
157 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
|
---|
158 | g_cErrors++;
|
---|
159 | }
|
---|
160 | if (!(i % cUnitsPerDot))
|
---|
161 | {
|
---|
162 | RTPrintf(".");
|
---|
163 | RTStrmFlush(g_pStdOut);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | RTPrintf("\n");
|
---|
167 | if (fCallbacks && cRetainerCalls != c - uBase)
|
---|
168 | {
|
---|
169 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
|
---|
170 | g_cErrors++;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* remove all the entries (in order) */
|
---|
174 | RTPrintf("tstHandleTable: TESTING RTHandleTableFree%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
175 | cRetainerCalls = 0;
|
---|
176 | for (i = uBase; i < c; i++)
|
---|
177 | {
|
---|
178 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
|
---|
179 | void *pvObj;
|
---|
180 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
181 | pvObj = RTHandleTableFreeWithCtx(hHT, i, NULL);
|
---|
182 | else
|
---|
183 | pvObj = RTHandleTableFree(hHT, i);
|
---|
184 | if (!pvObj)
|
---|
185 | {
|
---|
186 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
|
---|
187 | g_cErrors++;
|
---|
188 | }
|
---|
189 | else if (pvObj != pvExpect)
|
---|
190 | {
|
---|
191 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
|
---|
192 | g_cErrors++;
|
---|
193 | }
|
---|
194 | else if ( fFlags & RTHANDLETABLE_FLAGS_CONTEXT
|
---|
195 | ? RTHandleTableLookupWithCtx(hHT, i, NULL)
|
---|
196 | : RTHandleTableLookup(hHT, i))
|
---|
197 | {
|
---|
198 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s succeeded after free!\n", __LINE__, i, pszWithCtx);
|
---|
199 | g_cErrors++;
|
---|
200 | }
|
---|
201 | if (!(i % cUnitsPerDot))
|
---|
202 | {
|
---|
203 | RTPrintf(".");
|
---|
204 | RTStrmFlush(g_pStdOut);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | RTPrintf("\n");
|
---|
208 | if (fCallbacks && cRetainerCalls != c - uBase)
|
---|
209 | {
|
---|
210 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
|
---|
211 | g_cErrors++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
|
---|
215 | RTPrintf("tstHandleTable: TESTING Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
|
---|
216 | for (i = uBase; i < c * 2; i++)
|
---|
217 | {
|
---|
218 | /* alloc */
|
---|
219 | uint32_t hExpect = ((i - uBase) % (c - uBase)) + uBase;
|
---|
220 | uint32_t h;
|
---|
221 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
222 | rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), NULL, &h);
|
---|
223 | else
|
---|
224 | rc = RTHandleTableAlloc(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), &h);
|
---|
225 | if (RT_FAILURE(rc))
|
---|
226 | {
|
---|
227 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: rc=%Rrc!\n", __LINE__, i, pszWithCtx, rc);
|
---|
228 | g_cErrors++;
|
---|
229 | }
|
---|
230 | else if (h != hExpect)
|
---|
231 | {
|
---|
232 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: h=%u hExpect=%u! - abort sub-test\n", __LINE__, i, pszWithCtx, h, hExpect);
|
---|
233 | g_cErrors++;
|
---|
234 | break;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (i >= cDelta + uBase)
|
---|
238 | {
|
---|
239 | /* lookup */
|
---|
240 | for (uint32_t j = i - cDelta; j <= i; j++)
|
---|
241 | {
|
---|
242 | uint32_t hLookup = ((j - uBase) % (c - uBase)) + uBase;
|
---|
243 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hLookup * 4);
|
---|
244 | void *pvObj;
|
---|
245 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
246 | pvObj = RTHandleTableLookupWithCtx(hHT, hLookup, NULL);
|
---|
247 | else
|
---|
248 | pvObj = RTHandleTableLookup(hHT, hLookup);
|
---|
249 | if (pvObj != pvExpect)
|
---|
250 | {
|
---|
251 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookup%s(,%u,): pvObj=%p expected %p!\n",
|
---|
252 | __LINE__, i, j, pszWithCtx, hLookup, pvObj, pvExpect);
|
---|
253 | g_cErrors++;
|
---|
254 | }
|
---|
255 | else if ( (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
256 | && RTHandleTableLookupWithCtx(hHT, hLookup, &i))
|
---|
257 | {
|
---|
258 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
|
---|
259 | __LINE__, i, j);
|
---|
260 | g_cErrors++;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* free */
|
---|
265 | uint32_t hFree = ((i - uBase - cDelta) % (c - uBase)) + uBase;
|
---|
266 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hFree * 4);
|
---|
267 | void *pvObj;
|
---|
268 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
269 | pvObj = RTHandleTableFreeWithCtx(hHT, hFree, NULL);
|
---|
270 | else
|
---|
271 | pvObj = RTHandleTableFree(hHT, hFree);
|
---|
272 | if (pvObj != pvExpect)
|
---|
273 | {
|
---|
274 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFree%s: pvObj=%p expected %p!\n",
|
---|
275 | __LINE__, i, pszWithCtx, pvObj, pvExpect);
|
---|
276 | g_cErrors++;
|
---|
277 | }
|
---|
278 | else if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT
|
---|
279 | ? RTHandleTableLookupWithCtx(hHT, hFree, NULL)
|
---|
280 | || RTHandleTableFreeWithCtx(hHT, hFree, NULL)
|
---|
281 | : RTHandleTableLookup(hHT, hFree)
|
---|
282 | || RTHandleTableFree(hHT, hFree))
|
---|
283 | {
|
---|
284 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/Free%s: succeeded after free\n",
|
---|
285 | __LINE__, i, pszWithCtx);
|
---|
286 | g_cErrors++;
|
---|
287 | }
|
---|
288 | }
|
---|
289 | if (!(i % (cUnitsPerDot * 2)))
|
---|
290 | {
|
---|
291 | RTPrintf(".");
|
---|
292 | RTStrmFlush(g_pStdOut);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | RTPrintf("\n");
|
---|
296 |
|
---|
297 | /* finally, destroy the table (note that there are 128 entries in it). */
|
---|
298 | cRetainerCalls = 0;
|
---|
299 | uint32_t cDeleteCalls = 0;
|
---|
300 | rc = RTHandleTableDestroy(hHT,
|
---|
301 | fCallbacks ? tstHandleTableTest1Delete : NULL,
|
---|
302 | fCallbacks ? &cDeleteCalls : NULL);
|
---|
303 | if (RT_FAILURE(rc))
|
---|
304 | {
|
---|
305 | RTPrintf("tstHandleTable: FAILURE (%d) - RTHandleTableDestroy failed, %Rrc!\n", __LINE__, rc);
|
---|
306 | g_cErrors++;
|
---|
307 | }
|
---|
308 |
|
---|
309 | return 0;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | typedef struct TSTHTTEST2ARGS
|
---|
314 | {
|
---|
315 | /** The handle table. */
|
---|
316 | RTHANDLETABLE hHT;
|
---|
317 | /** The thread handle. */
|
---|
318 | RTTHREAD hThread;
|
---|
319 | /** Thread index. */
|
---|
320 | uint32_t iThread;
|
---|
321 | /** the max number of handles the thread should allocate. */
|
---|
322 | uint32_t cMax;
|
---|
323 | } TSTHTTEST2ARGS, *PTSTHTTEST2ARGS;
|
---|
324 |
|
---|
325 |
|
---|
326 | static DECLCALLBACK(int) tstHandleTableTest2Thread(RTTHREAD hThread, void *pvUser)
|
---|
327 | {
|
---|
328 | RTHANDLETABLE const hHT = ((PTSTHTTEST2ARGS)pvUser)->hHT;
|
---|
329 | uint32_t const iThread = ((PTSTHTTEST2ARGS)pvUser)->iThread;
|
---|
330 | uint32_t const cMax = ((PTSTHTTEST2ARGS)pvUser)->cMax;
|
---|
331 | uint32_t *pah = (uint32_t *)RTMemAllocZ(sizeof(uint32_t) * cMax);
|
---|
332 | if (!pah)
|
---|
333 | {
|
---|
334 | RTPrintf("tstHandleTable: FAILURE (%d) - failed to allocate %zu bytes\n", __LINE__, sizeof(uint32_t) * cMax);
|
---|
335 | return VERR_NO_MEMORY;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * Allocate our quota.
|
---|
340 | */
|
---|
341 | for (uint32_t i = 0; i < cMax; i++)
|
---|
342 | {
|
---|
343 | int rc = RTHandleTableAllocWithCtx(hHT, pvUser, hThread, &pah[i]);
|
---|
344 | if (RT_FAILURE(rc))
|
---|
345 | {
|
---|
346 | RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableAllocWithCtx failed, rc=%Rrc\n",
|
---|
347 | __LINE__, iThread, i, rc);
|
---|
348 | return rc;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Look them up.
|
---|
354 | */
|
---|
355 | for (uint32_t i = 0; i < cMax; i++)
|
---|
356 | {
|
---|
357 | void *pvObj = RTHandleTableLookupWithCtx(hHT, pah[i], hThread);
|
---|
358 | if (pvObj != pvUser)
|
---|
359 | {
|
---|
360 | RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableLookupWithCtx failed, pvObj=%p\n",
|
---|
361 | __LINE__, iThread, i, pvObj);
|
---|
362 | return VERR_INTERNAL_ERROR;
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Free them all.
|
---|
368 | */
|
---|
369 | for (uint32_t i = 0; i < cMax; i++)
|
---|
370 | {
|
---|
371 | void *pvObj = RTHandleTableFreeWithCtx(hHT, pah[i], hThread);
|
---|
372 | if (pvObj != pvUser)
|
---|
373 | {
|
---|
374 | RTPrintf("tstHandleTable: FAILURE (%d) - t=%d i=%d: RTHandleTableFreeWithCtx failed, pvObj=%p\n",
|
---|
375 | __LINE__, iThread, i, pvObj);
|
---|
376 | return VERR_INTERNAL_ERROR;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | RTMemFree(pah);
|
---|
381 | return VINF_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static int tstHandleTableTest2(uint32_t uBase, uint32_t cMax, uint32_t cThreads)
|
---|
385 | {
|
---|
386 | /*
|
---|
387 | * Create the table.
|
---|
388 | */
|
---|
389 | RTPrintf("tstHandleTable: TESTING %u threads: uBase=%u, cMax=%u\n", cThreads, uBase, cMax);
|
---|
390 | RTHANDLETABLE hHT;
|
---|
391 | int rc = RTHandleTableCreateEx(&hHT, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT, uBase, cMax, NULL, NULL);
|
---|
392 | if (RT_FAILURE(rc))
|
---|
393 | {
|
---|
394 | RTPrintf("tstHandleTable: FAILURE - RTHandleTableCreateEx failed, %Rrc!\n", rc);
|
---|
395 | return 1;
|
---|
396 | }
|
---|
397 | /// @todo there must be a race somewhere in the thread code, I keep hitting a duplicate insert id here...
|
---|
398 | // Or perhaps it just barcelona B2 bugs?
|
---|
399 | RTThreadSleep(50);
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * Spawn the threads.
|
---|
403 | */
|
---|
404 | PTSTHTTEST2ARGS paThread = (PTSTHTTEST2ARGS)alloca(sizeof(*paThread) * cThreads);
|
---|
405 | for (uint32_t i = 0; i < cThreads; i++)
|
---|
406 | {
|
---|
407 | paThread[i].hHT = hHT;
|
---|
408 | paThread[i].hThread = NIL_RTTHREAD;
|
---|
409 | paThread[i].iThread = i;
|
---|
410 | paThread[i].cMax = cMax / cThreads;
|
---|
411 | }
|
---|
412 | for (uint32_t i = 0; i < cThreads; i++)
|
---|
413 | {
|
---|
414 | char szName[32];
|
---|
415 | RTStrPrintf(szName, sizeof(szName), "TEST2-%x/%x", i, cMax);
|
---|
416 | rc = RTThreadCreate(&paThread[i].hThread, tstHandleTableTest2Thread, &paThread[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, szName);
|
---|
417 | if (RT_FAILURE(rc))
|
---|
418 | {
|
---|
419 | RTPrintf("tstHandleTable: FAILURE - RTThreadCreate failed, %Rrc!\n", rc);
|
---|
420 | g_cErrors++;
|
---|
421 | break;
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Wait for them to complete.
|
---|
427 | */
|
---|
428 | uint32_t cRunning = cThreads;
|
---|
429 | do /** @todo Remove when RTSemEventWait (linux) has been fixed. */
|
---|
430 | {
|
---|
431 | if (cRunning != cThreads)
|
---|
432 | RTThreadSleep(10);
|
---|
433 | cRunning = 0;
|
---|
434 | for (uint32_t i = 0; i < cThreads; i++)
|
---|
435 | if (paThread[i].hThread != NIL_RTTHREAD)
|
---|
436 | {
|
---|
437 | rc = RTThreadWait(paThread[i].hThread, RT_INDEFINITE_WAIT, NULL);
|
---|
438 | if (RT_SUCCESS(rc))
|
---|
439 | paThread[i].hThread = NIL_RTTHREAD;
|
---|
440 | else
|
---|
441 | cRunning++;
|
---|
442 | }
|
---|
443 | } while (cRunning);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Destroy the handle table.
|
---|
447 | */
|
---|
448 | rc = RTHandleTableDestroy(hHT, NULL, NULL);
|
---|
449 | if (RT_FAILURE(rc))
|
---|
450 | {
|
---|
451 | RTPrintf("tstHandleTable: FAILURE (%d) - RTHandleTableDestroy failed, %Rrc!\n", __LINE__, rc);
|
---|
452 | g_cErrors++;
|
---|
453 | }
|
---|
454 |
|
---|
455 | return 0;
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | int main(int argc, char **argv)
|
---|
460 | {
|
---|
461 | /*
|
---|
462 | * Init the runtime and parse the arguments.
|
---|
463 | */
|
---|
464 | RTR3InitExe(argc, &argv, 0);
|
---|
465 |
|
---|
466 | static RTGETOPTDEF const s_aOptions[] =
|
---|
467 | {
|
---|
468 | { "--base", 'b', RTGETOPT_REQ_UINT32 },
|
---|
469 | { "--max", 'm', RTGETOPT_REQ_UINT32 },
|
---|
470 | { "--threads", 't', RTGETOPT_REQ_UINT32 },
|
---|
471 | };
|
---|
472 |
|
---|
473 | uint32_t uBase = 0;
|
---|
474 | uint32_t cMax = 0;
|
---|
475 | uint32_t cThreads = 0;
|
---|
476 |
|
---|
477 | int ch;
|
---|
478 | RTGETOPTUNION Value;
|
---|
479 | RTGETOPTSTATE GetState;
|
---|
480 | RTGetOptInit(&GetState, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
|
---|
481 | while ((ch = RTGetOpt(&GetState, &Value)))
|
---|
482 | switch (ch)
|
---|
483 | {
|
---|
484 | case 'b':
|
---|
485 | uBase = Value.u32;
|
---|
486 | break;
|
---|
487 |
|
---|
488 | case 'm':
|
---|
489 | cMax = Value.u32;
|
---|
490 | break;
|
---|
491 |
|
---|
492 | case 't':
|
---|
493 | cThreads = Value.u32;
|
---|
494 | if (!cThreads)
|
---|
495 | cThreads = 1;
|
---|
496 | break;
|
---|
497 |
|
---|
498 | case 'h':
|
---|
499 | RTPrintf("syntax: tstHandleTable [-b <base>] [-m <max>] [-t <threads>]\n");
|
---|
500 | return 1;
|
---|
501 |
|
---|
502 | case 'V':
|
---|
503 | RTPrintf("$Revision: 98103 $\n");
|
---|
504 | return 0;
|
---|
505 |
|
---|
506 | default:
|
---|
507 | return RTGetOptPrintError(ch, &Value);
|
---|
508 | }
|
---|
509 |
|
---|
510 | /*
|
---|
511 | * If any argument was specified, run the requested test setup.
|
---|
512 | * Otherwise run a bunch of default tests.
|
---|
513 | */
|
---|
514 | if (cThreads || cMax || uBase)
|
---|
515 | {
|
---|
516 | if (!cMax)
|
---|
517 | cMax = 65535;
|
---|
518 | if (!cThreads)
|
---|
519 | tstHandleTableTest1(uBase, cMax, 128, cMax / 32, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
520 | else
|
---|
521 | tstHandleTableTest2(uBase, cMax, 128);
|
---|
522 | }
|
---|
523 | else
|
---|
524 | {
|
---|
525 | /*
|
---|
526 | * Do a simple warmup / smoke test first.
|
---|
527 | */
|
---|
528 | tstHandleTableTest1(1, 65534, 128, 2048, false, 0);
|
---|
529 | tstHandleTableTest1(1, 65534, 128, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT);
|
---|
530 | tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_LOCKED);
|
---|
531 | tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
532 | /* Test that the retain and delete functions work. */
|
---|
533 | tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_LOCKED);
|
---|
534 | tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
535 | /* check that the base works. */
|
---|
536 | tstHandleTableTest1(0x7ffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
537 | tstHandleTableTest1(0xeffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
538 | tstHandleTableTest1(0, 4097, 4, 256, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
539 | tstHandleTableTest1(0, 1024, 4, 128, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
540 | /* For testing 1st level expansion / reallocation. */
|
---|
541 | tstHandleTableTest1(1, 1024*1024*8, 3, 150000, false, 0);
|
---|
542 | tstHandleTableTest1(1, 1024*1024*8, 3, 150000, false, RTHANDLETABLE_FLAGS_CONTEXT);
|
---|
543 |
|
---|
544 | /*
|
---|
545 | * Threaded tests.
|
---|
546 | */
|
---|
547 | tstHandleTableTest2(0x80000000, 32768, 2);
|
---|
548 | tstHandleTableTest2(0x00010000, 2048, 4);
|
---|
549 | tstHandleTableTest2(0x00010000, 3072, 8);
|
---|
550 | tstHandleTableTest2(0x00000000, 1024*1024*8, 3);
|
---|
551 | }
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * Summary.
|
---|
555 | */
|
---|
556 | if (!g_cErrors)
|
---|
557 | RTPrintf("tstHandleTable: SUCCESS\n");
|
---|
558 | else
|
---|
559 | RTPrintf("tstHandleTable: FAILURE - %d errors\n", g_cErrors);
|
---|
560 |
|
---|
561 | return !!g_cErrors;
|
---|
562 | }
|
---|