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