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