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