1 | /* $Id: tstRTSemRW.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Reader/Writer Semaphore.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2022 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/semaphore.h>
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/lockvalidator.h>
|
---|
38 | #include <iprt/mp.h>
|
---|
39 | #include <iprt/rand.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/stream.h>
|
---|
42 | #include <iprt/test.h>
|
---|
43 | #include <iprt/time.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | static RTTEST g_hTest;
|
---|
51 | static RTSEMRW g_hSemRW = NIL_RTSEMRW;
|
---|
52 | static bool volatile g_fTerminate;
|
---|
53 | static bool g_fYield;
|
---|
54 | static bool g_fQuiet;
|
---|
55 | static unsigned g_uWritePercent;
|
---|
56 | static uint32_t volatile g_cConcurrentWriters;
|
---|
57 | static uint32_t volatile g_cConcurrentReaders;
|
---|
58 |
|
---|
59 |
|
---|
60 | static DECLCALLBACK(int) Test4Thread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
61 | {
|
---|
62 | /* Use randomization to get a little more variation of the sync pattern.
|
---|
63 | We use a pseudo random generator here so that we don't end up testing
|
---|
64 | the speed of the /dev/urandom implementation, but rather the read-write
|
---|
65 | semaphores. */
|
---|
66 | int rc;
|
---|
67 | RTRAND hRand;
|
---|
68 | RTTEST_CHECK_RC_OK_RET(g_hTest, rc = RTRandAdvCreateParkMiller(&hRand), rc);
|
---|
69 | RTTEST_CHECK_RC_OK_RET(g_hTest, rc = RTRandAdvSeed(hRand, (uintptr_t)ThreadSelf), rc);
|
---|
70 | unsigned c100 = RTRandAdvU32Ex(hRand, 0, 99);
|
---|
71 |
|
---|
72 | uint64_t *pcItr = (uint64_t *)pvUser;
|
---|
73 | bool fWrite;
|
---|
74 | for (;;)
|
---|
75 | {
|
---|
76 | unsigned readrec = RTRandAdvU32Ex(hRand, 0, 3);
|
---|
77 | unsigned writerec = RTRandAdvU32Ex(hRand, 0, 3);
|
---|
78 | /* Don't overdo recursion testing. */
|
---|
79 | if (readrec > 1)
|
---|
80 | readrec--;
|
---|
81 | if (writerec > 1)
|
---|
82 | writerec--;
|
---|
83 |
|
---|
84 | fWrite = (c100 < g_uWritePercent);
|
---|
85 | if (fWrite)
|
---|
86 | {
|
---|
87 | for (unsigned i = 0; i <= writerec; i++)
|
---|
88 | {
|
---|
89 | rc = RTSemRWRequestWriteNoResume(g_hSemRW, RT_INDEFINITE_WAIT);
|
---|
90 | if (RT_FAILURE(rc))
|
---|
91 | {
|
---|
92 | RTTestFailed(g_hTest, "Write recursion %u on %s failed with rc=%Rrc", i, RTThreadSelfName(), rc);
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | if (RT_FAILURE(rc))
|
---|
97 | break;
|
---|
98 | if (ASMAtomicIncU32(&g_cConcurrentWriters) != 1)
|
---|
99 | {
|
---|
100 | RTTestFailed(g_hTest, "g_cConcurrentWriters=%u on %s after write locking it",
|
---|
101 | g_cConcurrentWriters, RTThreadSelfName());
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | if (g_cConcurrentReaders != 0)
|
---|
105 | {
|
---|
106 | RTTestFailed(g_hTest, "g_cConcurrentReaders=%u on %s after write locking it",
|
---|
107 | g_cConcurrentReaders, RTThreadSelfName());
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | rc = RTSemRWRequestReadNoResume(g_hSemRW, RT_INDEFINITE_WAIT);
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | {
|
---|
116 | RTTestFailed(g_hTest, "Read locking on %s failed with rc=%Rrc", RTThreadSelfName(), rc);
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | ASMAtomicIncU32(&g_cConcurrentReaders);
|
---|
120 | if (g_cConcurrentWriters != 0)
|
---|
121 | {
|
---|
122 | RTTestFailed(g_hTest, "g_cConcurrentWriters=%u on %s after read locking it",
|
---|
123 | g_cConcurrentWriters, RTThreadSelfName());
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | for (unsigned i = 0; i < readrec; i++)
|
---|
128 | {
|
---|
129 | rc = RTSemRWRequestReadNoResume(g_hSemRW, RT_INDEFINITE_WAIT);
|
---|
130 | if (RT_FAILURE(rc))
|
---|
131 | {
|
---|
132 | RTTestFailed(g_hTest, "Read recursion %u on %s failed with rc=%Rrc", i, RTThreadSelfName(), rc);
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (RT_FAILURE(rc))
|
---|
137 | break;
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Check for fairness: The values of the threads should not differ too much
|
---|
141 | */
|
---|
142 | (*pcItr)++;
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Check for correctness: Give other threads a chance. If the implementation is
|
---|
146 | * correct, no other thread will be able to enter this lock now.
|
---|
147 | */
|
---|
148 | if (g_fYield)
|
---|
149 | RTThreadYield();
|
---|
150 |
|
---|
151 | for (unsigned i = 0; i < readrec; i++)
|
---|
152 | {
|
---|
153 | rc = RTSemRWReleaseRead(g_hSemRW);
|
---|
154 | if (RT_FAILURE(rc))
|
---|
155 | {
|
---|
156 | RTTestFailed(g_hTest, "Read release %u on %s failed with rc=%Rrc", i, RTThreadSelfName(), rc);
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | if (RT_FAILURE(rc))
|
---|
161 | break;
|
---|
162 |
|
---|
163 | if (fWrite)
|
---|
164 | {
|
---|
165 | if (ASMAtomicDecU32(&g_cConcurrentWriters) != 0)
|
---|
166 | {
|
---|
167 | RTTestFailed(g_hTest, "g_cConcurrentWriters=%u on %s before write release",
|
---|
168 | g_cConcurrentWriters, RTThreadSelfName());
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | if (g_cConcurrentReaders != 0)
|
---|
172 | {
|
---|
173 | RTTestFailed(g_hTest, "g_cConcurrentReaders=%u on %s before write release",
|
---|
174 | g_cConcurrentReaders, RTThreadSelfName());
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | for (unsigned i = 0; i <= writerec; i++)
|
---|
178 | {
|
---|
179 | rc = RTSemRWReleaseWrite(g_hSemRW);
|
---|
180 | if (RT_FAILURE(rc))
|
---|
181 | {
|
---|
182 | RTTestFailed(g_hTest, "Write release %u on %s failed with rc=%Rrc", i, RTThreadSelfName(), rc);
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | if (g_cConcurrentWriters != 0)
|
---|
190 | {
|
---|
191 | RTTestFailed(g_hTest, "g_cConcurrentWriters=%u on %s before read release",
|
---|
192 | g_cConcurrentWriters, RTThreadSelfName());
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | ASMAtomicDecU32(&g_cConcurrentReaders);
|
---|
196 | rc = RTSemRWReleaseRead(g_hSemRW);
|
---|
197 | if (RT_FAILURE(rc))
|
---|
198 | {
|
---|
199 | RTTestFailed(g_hTest, "Read release on %s failed with rc=%Rrc", RTThreadSelfName(), rc);
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (g_fTerminate)
|
---|
205 | break;
|
---|
206 |
|
---|
207 | c100++;
|
---|
208 | c100 %= 100;
|
---|
209 | }
|
---|
210 | if (!g_fQuiet)
|
---|
211 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Thread %s exited with %lld\n", RTThreadSelfName(), *pcItr);
|
---|
212 | RTRandAdvDestroy(hRand);
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | static void Test4(unsigned cThreads, unsigned cSeconds, unsigned uWritePercent, bool fYield, bool fQuiet)
|
---|
218 | {
|
---|
219 | unsigned i;
|
---|
220 | uint64_t acIterations[32];
|
---|
221 | RTTHREAD aThreads[RT_ELEMENTS(acIterations)];
|
---|
222 | AssertRelease(cThreads <= RT_ELEMENTS(acIterations));
|
---|
223 |
|
---|
224 | RTTestSubF(g_hTest, "Test4 - %u threads, %u sec, %u%% writes, %syielding",
|
---|
225 | cThreads, cSeconds, uWritePercent, fYield ? "" : "non-");
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Init globals.
|
---|
229 | */
|
---|
230 | g_fYield = fYield;
|
---|
231 | g_fQuiet = fQuiet;
|
---|
232 | g_fTerminate = false;
|
---|
233 | g_uWritePercent = uWritePercent;
|
---|
234 | g_cConcurrentWriters = 0;
|
---|
235 | g_cConcurrentReaders = 0;
|
---|
236 |
|
---|
237 | RTTEST_CHECK_RC_RETV(g_hTest, RTSemRWCreate(&g_hSemRW), VINF_SUCCESS);
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Create the threads and let them block on the semrw.
|
---|
241 | */
|
---|
242 | RTTEST_CHECK_RC_RETV(g_hTest, RTSemRWRequestWrite(g_hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS);
|
---|
243 |
|
---|
244 | for (i = 0; i < cThreads; i++)
|
---|
245 | {
|
---|
246 | acIterations[i] = 0;
|
---|
247 | RTTEST_CHECK_RC_RETV(g_hTest, RTThreadCreateF(&aThreads[i], Test4Thread, &acIterations[i], 0,
|
---|
248 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
|
---|
249 | "test-%u", i), VINF_SUCCESS);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * Do the test run.
|
---|
254 | */
|
---|
255 | uint32_t cErrorsBefore = RTTestErrorCount(g_hTest);
|
---|
256 | uint64_t u64StartTS = RTTimeNanoTS();
|
---|
257 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_hSemRW), VINF_SUCCESS);
|
---|
258 | RTThreadSleep(cSeconds * 1000);
|
---|
259 | ASMAtomicWriteBool(&g_fTerminate, true);
|
---|
260 | uint64_t ElapsedNS = RTTimeNanoTS() - u64StartTS;
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Clean up the threads and semaphore.
|
---|
264 | */
|
---|
265 | for (i = 0; i < cThreads; i++)
|
---|
266 | RTTEST_CHECK_RC(g_hTest, RTThreadWait(aThreads[i], 5000, NULL), VINF_SUCCESS);
|
---|
267 |
|
---|
268 | RTTEST_CHECK_MSG(g_hTest, g_cConcurrentWriters == 0, (g_hTest, "g_cConcurrentWriters=%u at end of test\n", g_cConcurrentWriters));
|
---|
269 | RTTEST_CHECK_MSG(g_hTest, g_cConcurrentReaders == 0, (g_hTest, "g_cConcurrentReaders=%u at end of test\n", g_cConcurrentReaders));
|
---|
270 |
|
---|
271 | RTTEST_CHECK_RC(g_hTest, RTSemRWDestroy(g_hSemRW), VINF_SUCCESS);
|
---|
272 | g_hSemRW = NIL_RTSEMRW;
|
---|
273 |
|
---|
274 | if (RTTestErrorCount(g_hTest) != cErrorsBefore)
|
---|
275 | RTThreadSleep(100);
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Collect and display the results.
|
---|
279 | */
|
---|
280 | uint64_t cItrTotal = acIterations[0];
|
---|
281 | for (i = 1; i < cThreads; i++)
|
---|
282 | cItrTotal += acIterations[i];
|
---|
283 |
|
---|
284 | uint64_t cItrNormal = cItrTotal / cThreads;
|
---|
285 | uint64_t cItrMinOK = cItrNormal / 20; /* 5% */
|
---|
286 | uint64_t cItrMaxDeviation = 0;
|
---|
287 | for (i = 0; i < cThreads; i++)
|
---|
288 | {
|
---|
289 | uint64_t cItrDelta = RT_ABS((int64_t)(acIterations[i] - cItrNormal));
|
---|
290 | if (acIterations[i] < cItrMinOK)
|
---|
291 | RTTestFailed(g_hTest, "Thread %u did less than 5%% of the iterations - %llu (it) vs. %llu (5%%) - %llu%%\n",
|
---|
292 | i, acIterations[i], cItrMinOK, cItrDelta * 100 / cItrNormal);
|
---|
293 | else if (cItrDelta > cItrNormal / 2)
|
---|
294 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
|
---|
295 | "Warning! Thread %u deviates by more than 50%% - %llu (it) vs. %llu (avg) - %llu%%\n",
|
---|
296 | i, acIterations[i], cItrNormal, cItrDelta * 100 / cItrNormal);
|
---|
297 | if (cItrDelta > cItrMaxDeviation)
|
---|
298 | cItrMaxDeviation = cItrDelta;
|
---|
299 |
|
---|
300 | }
|
---|
301 |
|
---|
302 | //RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
|
---|
303 | // "Threads: %u Total: %llu Per Sec: %llu Avg: %llu ns Max dev: %llu%%\n",
|
---|
304 | // cThreads,
|
---|
305 | // cItrTotal,
|
---|
306 | // cItrTotal / cSeconds,
|
---|
307 | // ElapsedNS / cItrTotal,
|
---|
308 | // cItrMaxDeviation * 100 / cItrNormal
|
---|
309 | // );
|
---|
310 | //
|
---|
311 | RTTestValue(g_hTest, "Thruput", cItrTotal * UINT32_C(1000000000) / ElapsedNS, RTTESTUNIT_CALLS_PER_SEC);
|
---|
312 | RTTestValue(g_hTest, "Max diviation", cItrMaxDeviation * 100 / cItrNormal, RTTESTUNIT_PCT);
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | static DECLCALLBACK(int) Test2Thread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
317 | {
|
---|
318 | RTSEMRW hSemRW = (RTSEMRW)pvUser;
|
---|
319 | RT_NOREF_PV(hThreadSelf);
|
---|
320 |
|
---|
321 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead(hSemRW, 0), VERR_TIMEOUT);
|
---|
322 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(hSemRW, 0), VERR_TIMEOUT);
|
---|
323 |
|
---|
324 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead(hSemRW, 1), VERR_TIMEOUT);
|
---|
325 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(hSemRW, 1), VERR_TIMEOUT);
|
---|
326 |
|
---|
327 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead(hSemRW, 50), VERR_TIMEOUT);
|
---|
328 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(hSemRW, 50), VERR_TIMEOUT);
|
---|
329 |
|
---|
330 | return VINF_SUCCESS;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | static void Test3(void)
|
---|
335 | {
|
---|
336 | RTTestSub(g_hTest, "Negative");
|
---|
337 | bool fSavedAssertQuiet = RTAssertSetQuiet(true);
|
---|
338 | bool fSavedAssertMayPanic = RTAssertSetMayPanic(false);
|
---|
339 | bool fSavedLckValEnabled = RTLockValidatorSetEnabled(false);
|
---|
340 |
|
---|
341 | RTSEMRW hSemRW;
|
---|
342 | RTTEST_CHECK_RC_RETV(g_hTest, RTSemRWCreate(&hSemRW), VINF_SUCCESS);
|
---|
343 |
|
---|
344 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(hSemRW), VERR_NOT_OWNER);
|
---|
345 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(hSemRW), VERR_NOT_OWNER);
|
---|
346 |
|
---|
347 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS);
|
---|
348 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(hSemRW), VERR_NOT_OWNER);
|
---|
349 |
|
---|
350 | RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS);
|
---|
351 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(hSemRW), VERR_WRONG_ORDER); /* cannot release the final write before the reads. */
|
---|
352 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS);
|
---|
353 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS);
|
---|
354 |
|
---|
355 | RTTEST_CHECK_RC(g_hTest, RTSemRWDestroy(hSemRW), VINF_SUCCESS);
|
---|
356 |
|
---|
357 | RTLockValidatorSetEnabled(fSavedLckValEnabled);
|
---|
358 | RTAssertSetMayPanic(fSavedAssertMayPanic);
|
---|
359 | RTAssertSetQuiet(fSavedAssertQuiet);
|
---|
360 | }
|
---|
361 |
|
---|
362 | static void Test2(void)
|
---|
363 | {
|
---|
364 | RTTestSub(g_hTest, "Timeout");
|
---|
365 |
|
---|
366 | RTSEMRW hSemRW = NIL_RTSEMRW;
|
---|
367 | RTTEST_CHECK_RC_RETV(g_hTest, RTSemRWCreate(&hSemRW), VINF_SUCCESS);
|
---|
368 |
|
---|
369 | /* Lock it for writing and let the thread do the remainder of the test. */
|
---|
370 | RTTEST_CHECK_RC_RETV(g_hTest, RTSemRWRequestWrite(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS);
|
---|
371 |
|
---|
372 | RTTHREAD hThread;
|
---|
373 | RTTEST_CHECK_RC_RETV(g_hTest, RTThreadCreate(&hThread, Test2Thread, hSemRW, 0,
|
---|
374 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2"),
|
---|
375 | VINF_SUCCESS);
|
---|
376 | RTTEST_CHECK_RC(g_hTest, RTThreadWait(hThread, 15000, NULL), VINF_SUCCESS);
|
---|
377 | RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS);
|
---|
378 |
|
---|
379 | RTTEST_CHECK_RC(g_hTest, RTSemRWDestroy(hSemRW), VINF_SUCCESS);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | static bool Test1(void)
|
---|
384 | {
|
---|
385 | RTTestSub(g_hTest, "Basics");
|
---|
386 |
|
---|
387 | RTSEMRW hSemRW = NIL_RTSEMRW;
|
---|
388 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWCreate(&hSemRW), VINF_SUCCESS, false);
|
---|
389 | RTTEST_CHECK_RET(g_hTest, hSemRW != NIL_RTSEMRW, false);
|
---|
390 |
|
---|
391 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS, false);
|
---|
392 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS, false);
|
---|
393 |
|
---|
394 | for (unsigned cMs = 0; cMs < 50; cMs++)
|
---|
395 | {
|
---|
396 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
397 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
398 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS, false);
|
---|
399 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS, false);
|
---|
400 | }
|
---|
401 |
|
---|
402 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS, false);
|
---|
403 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS, false);
|
---|
404 |
|
---|
405 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS, false);
|
---|
406 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hSemRW, RT_INDEFINITE_WAIT), VINF_SUCCESS, false);
|
---|
407 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS, false);
|
---|
408 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS, false);
|
---|
409 |
|
---|
410 | for (unsigned cMs = 0; cMs < 50; cMs++)
|
---|
411 | {
|
---|
412 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
413 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 1, false);
|
---|
414 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 0, false);
|
---|
415 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
416 |
|
---|
417 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
418 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 2, false);
|
---|
419 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 0, false);
|
---|
420 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
421 |
|
---|
422 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
423 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 2, false);
|
---|
424 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 1, false);
|
---|
425 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
426 |
|
---|
427 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hSemRW, cMs), VINF_SUCCESS, false);
|
---|
428 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 3, false);
|
---|
429 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 1, false);
|
---|
430 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
431 |
|
---|
432 | /* midway */
|
---|
433 |
|
---|
434 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS, false);
|
---|
435 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 2, false);
|
---|
436 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 1, false);
|
---|
437 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
438 |
|
---|
439 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseRead(hSemRW), VINF_SUCCESS, false);
|
---|
440 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 2, false);
|
---|
441 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 0, false);
|
---|
442 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
443 |
|
---|
444 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS, false);
|
---|
445 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 1, false);
|
---|
446 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 0, false);
|
---|
447 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == true, false);
|
---|
448 |
|
---|
449 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWReleaseWrite(hSemRW), VINF_SUCCESS, false);
|
---|
450 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriteRecursion(hSemRW) == 0, false);
|
---|
451 | RTTEST_CHECK_RET(g_hTest, RTSemRWGetWriterReadRecursion(hSemRW) == 0, false);
|
---|
452 | RTTEST_CHECK_RET(g_hTest, RTSemRWIsWriteOwner(hSemRW) == false, false);
|
---|
453 | }
|
---|
454 |
|
---|
455 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWDestroy(hSemRW), VINF_SUCCESS, false);
|
---|
456 | RTTEST_CHECK_RC_RET(g_hTest, RTSemRWDestroy(NIL_RTSEMRW), VINF_SUCCESS, false);
|
---|
457 |
|
---|
458 | return true;
|
---|
459 | }
|
---|
460 |
|
---|
461 | int main(int argc, char **argv)
|
---|
462 | {
|
---|
463 | RT_NOREF_PV(argv);
|
---|
464 | int rc = RTTestInitAndCreate("tstRTSemRW", &g_hTest);
|
---|
465 | if (rc)
|
---|
466 | return 1;
|
---|
467 | RTTestBanner(g_hTest);
|
---|
468 |
|
---|
469 | if (Test1())
|
---|
470 | {
|
---|
471 | RTCPUID cCores = RTMpGetOnlineCoreCount();
|
---|
472 | if (argc == 1)
|
---|
473 | {
|
---|
474 | Test2();
|
---|
475 | Test3();
|
---|
476 |
|
---|
477 | /* threads, seconds, writePercent, yield, quiet */
|
---|
478 | Test4( 1, 1, 0, true, false);
|
---|
479 | Test4( 1, 1, 1, true, false);
|
---|
480 | Test4( 1, 1, 5, true, false);
|
---|
481 | Test4( 2, 1, 3, true, false);
|
---|
482 | Test4( 10, 1, 5, true, false);
|
---|
483 | Test4( 10, 10, 10, false, false);
|
---|
484 |
|
---|
485 | if (cCores > 1)
|
---|
486 | {
|
---|
487 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "benchmarking (%u CPU cores)...\n", cCores);
|
---|
488 | for (unsigned cThreads = 1; cThreads < 32; cThreads++)
|
---|
489 | Test4(cThreads, 2, 1, false, true);
|
---|
490 | }
|
---|
491 | else
|
---|
492 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "skipping benchmarking (only %u CPU core available)\n", cCores);
|
---|
493 |
|
---|
494 | /** @todo add a testcase where some stuff times out. */
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | if (cCores > 1)
|
---|
499 | {
|
---|
500 | /* threads, seconds, writePercent, yield, quiet */
|
---|
501 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "benchmarking...\n");
|
---|
502 | Test4( 1, 3, 1, false, true);
|
---|
503 | Test4( 1, 3, 1, false, true);
|
---|
504 | Test4( 1, 3, 1, false, true);
|
---|
505 | Test4( 2, 3, 1, false, true);
|
---|
506 | Test4( 2, 3, 1, false, true);
|
---|
507 | Test4( 2, 3, 1, false, true);
|
---|
508 | Test4( 3, 3, 1, false, true);
|
---|
509 | Test4( 3, 3, 1, false, true);
|
---|
510 | Test4( 3, 3, 1, false, true);
|
---|
511 | }
|
---|
512 | else
|
---|
513 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "skipping benchmarking (only %u CPU core available)\n", cCores);
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
518 | }
|
---|
519 |
|
---|