VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstSemMutex.cpp@ 13068

Last change on this file since 13068 was 11822, checked in by vboxsync, 16 years ago

IPRT: RTR3Init cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1/* $Id: tstSemMutex.cpp 11822 2008-08-29 14:21:03Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Simple Semaphore Smoke Test.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/semaphore.h>
35#include <iprt/string.h>
36#include <iprt/thread.h>
37#include <iprt/stream.h>
38#include <iprt/time.h>
39#include <iprt/initterm.h>
40#include <iprt/asm.h>
41#include <iprt/assert.h>
42
43
44/*******************************************************************************
45* Global Variables *
46*******************************************************************************/
47static RTSEMMUTEX g_hMutex = NIL_RTSEMMUTEX;
48static bool volatile g_fTerminate;
49static bool g_fYield;
50static bool g_fQuiet;
51static uint32_t volatile g_cbConcurrent;
52static uint32_t volatile g_cErrors;
53
54
55int PrintError(const char *pszFormat, ...)
56{
57 ASMAtomicIncU32(&g_cErrors);
58
59 RTPrintf("tstSemMutex: FAILURE - ");
60 va_list va;
61 va_start(va, pszFormat);
62 RTPrintfV(pszFormat, va);
63 va_end(va);
64
65 return 1;
66}
67
68
69int ThreadTest1(RTTHREAD ThreadSelf, void *pvUser)
70{
71 uint64_t *pu64 = (uint64_t *)pvUser;
72 for (;;)
73 {
74 int rc = RTSemMutexRequestNoResume(g_hMutex, RT_INDEFINITE_WAIT);
75 if (RT_FAILURE(rc))
76 {
77 PrintError("%x: RTSemMutexRequestNoResume failed with %Rrc\n", rc);
78 break;
79 }
80 if (ASMAtomicIncU32(&g_cbConcurrent) != 1)
81 {
82 PrintError("g_cbConcurrent=%d after request!\n", g_cbConcurrent);
83 break;
84 }
85
86 /*
87 * Check for fairness: The values of the threads should not differ too much
88 */
89 (*pu64)++;
90
91 /*
92 * Check for correctness: Give other threads a chance. If the implementation is
93 * correct, no other thread will be able to enter this lock now.
94 */
95 if (g_fYield)
96 RTThreadYield();
97 if (ASMAtomicDecU32(&g_cbConcurrent) != 0)
98 {
99 PrintError("g_cbConcurrent=%d before release!\n", g_cbConcurrent);
100 break;
101 }
102 rc = RTSemMutexRelease(g_hMutex);
103 if (RT_FAILURE(rc))
104 {
105 PrintError("%x: RTSemMutexRelease failed with %Rrc\n", rc);
106 break;
107 }
108 if (g_fTerminate)
109 break;
110 }
111 if (!g_fQuiet)
112 RTPrintf("tstSemMutex: Thread %08x exited with %lld\n", ThreadSelf, *pu64);
113 return VINF_SUCCESS;
114}
115
116
117static int Test1(unsigned cThreads, unsigned cSeconds, bool fYield, bool fQuiet)
118{
119 int rc;
120 unsigned i;
121 uint64_t g_au64[32];
122 RTTHREAD aThreads[RT_ELEMENTS(g_au64)];
123 AssertRelease(cThreads <= RT_ELEMENTS(g_au64));
124
125 /*
126 * Init globals.
127 */
128 g_fYield = fYield;
129 g_fQuiet = fQuiet;
130 g_fTerminate = false;
131
132 rc = RTSemMutexCreate(&g_hMutex);
133 if (RT_FAILURE(rc))
134 return PrintError("RTSemMutexCreate failed (rc=%Rrc)\n", rc);
135
136 /*
137 * Create the threads and let them block on the mutex.
138 */
139 rc = RTSemMutexRequest(g_hMutex, RT_INDEFINITE_WAIT);
140 if (RT_FAILURE(rc))
141 return PrintError("RTSemMutexRequest failed (rc=%Rrc)\n", rc);
142
143 for (i = 0; i < cThreads; i++)
144 {
145 g_au64[i] = 0;
146 rc = RTThreadCreate(&aThreads[i], ThreadTest1, &g_au64[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test");
147 if (RT_FAILURE(rc))
148 return PrintError("RTThreadCreate failed for thread %u (rc=%Rrc)\n", i, rc);
149 }
150
151 if (!fQuiet)
152 RTPrintf("tstSemMutex: %zu Threads created. Racing them for %u seconds (%s) ...\n",
153 cThreads, cSeconds, g_fYield ? "yielding" : "no yielding");
154
155 uint64_t u64StartTS = RTTimeNanoTS();
156 rc = RTSemMutexRelease(g_hMutex);
157 if (RT_FAILURE(rc))
158 PrintError("RTSemMutexRelease failed (rc=%Rrc)\n", rc);
159 RTThreadSleep(cSeconds * 1000);
160 ASMAtomicXchgBool(&g_fTerminate, true);
161 uint64_t ElapsedNS = RTTimeNanoTS() - u64StartTS;
162
163 for (i = 0; i < cThreads; i++)
164 {
165 rc = RTThreadWait(aThreads[i], 5000, NULL);
166 if (RT_FAILURE(rc))
167 PrintError("RTThreadWait failed for thread %u (rc=%Rrc)\n", i, rc);
168 }
169
170 rc = RTSemMutexDestroy(g_hMutex);
171 if (RT_FAILURE(rc))
172 PrintError("RTSemMutexDestroy failed - %Rrc\n", rc);
173 g_hMutex = NIL_RTSEMMUTEX;
174 if (g_cErrors)
175 RTThreadSleep(100);
176
177 /*
178 * Collect and display the results.
179 */
180 uint64_t Total = g_au64[0];
181 for (i = 1; i < cThreads; i++)
182 Total += g_au64[i];
183
184 uint64_t Normal = Total / cThreads;
185 uint64_t MaxDiviation = 0;
186 for (i = 0; i < cThreads; i++)
187 {
188 uint64_t Delta = RT_ABS((int64_t)(g_au64[i] - Normal));
189 if (Delta > Normal / 2)
190 RTPrintf("tstSemMutex: Warning! Thread %d diviates by more than 50%% - %llu (it) vs. %llu (avg)\n",
191 i, g_au64[i], Normal);
192 if (Delta > MaxDiviation)
193 MaxDiviation = Delta;
194
195 }
196
197 RTPrintf("tstSemMutex: Threads: %u Total: %llu Per Sec: %llu Avg: %llu ns Max div: %llu%%\n",
198 cThreads,
199 Total,
200 Total / cSeconds,
201 ElapsedNS / Total,
202 MaxDiviation * 100 / Normal
203 );
204 return 0;
205}
206
207
208int main(int argc, char **argv)
209{
210 int rc = RTR3Init();
211 if (RT_FAILURE(rc))
212 {
213 RTPrintf("tstSemMutex: RTR3Init failed (rc=%Rrc)\n", rc);
214 return 1;
215 }
216 RTPrintf("tstSemMutex: TESTING...\n");
217
218 if (argc == 1)
219 {
220 /* threads, seconds, yield, quiet */
221 Test1( 1, 1, true, false);
222 Test1( 2, 1, true, false);
223 Test1( 10, 1, true, false);
224 Test1( 10, 10, false, false);
225
226 RTPrintf("tstSemMutex: benchmarking...\n");
227 for (unsigned cThreads = 1; cThreads < 32; cThreads++)
228 Test1(cThreads, 2, false, true);
229
230 /** @todo add a testcase where some stuff times out. */
231 }
232 else
233 {
234 /* threads, seconds, yield, quiet */
235 RTPrintf("tstSemMutex: benchmarking...\n");
236 Test1( 1, 3, false, true);
237 Test1( 1, 3, false, true);
238 Test1( 1, 3, false, true);
239 Test1( 2, 3, false, true);
240 Test1( 2, 3, false, true);
241 Test1( 2, 3, false, true);
242 Test1( 3, 3, false, true);
243 Test1( 3, 3, false, true);
244 Test1( 3, 3, false, true);
245 }
246
247 if (!g_cErrors)
248 RTPrintf("tstSemMutex: SUCCESS\n");
249 else
250 RTPrintf("tstSemMutex: FAILURE - %u errors\n", g_cErrors);
251 return g_cErrors != 0;
252}
253
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette