VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0SemMutexDriver.cpp@ 28514

Last change on this file since 28514 was 28514, checked in by vboxsync, 15 years ago

tstRTR0SemMutex: Added a missing check in the threaded part.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/* $Id: tstRTR0SemMutexDriver.cpp 28514 2010-04-20 11:30:01Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
4 */
5
6/*
7 * Copyright (C) 2009-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/initterm.h>
35
36#include <iprt/err.h>
37#include <iprt/path.h>
38#include <iprt/param.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41#include <iprt/test.h>
42#include <iprt/thread.h>
43#ifdef VBOX
44# include <VBox/sup.h>
45# include "tstRTR0SemMutex.h"
46#endif
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51typedef struct TSTRTR0SEMMUTEXREQ
52{
53 SUPR0SERVICEREQHDR Hdr;
54 char szMsg[256];
55} TSTRTR0SEMMUTEXREQ;
56typedef TSTRTR0SEMMUTEXREQ *PTSTRTR0SEMMUTEXREQ;
57
58
59/*******************************************************************************
60* Global Variables *
61*******************************************************************************/
62static RTTEST g_hTest;
63
64
65/**
66 * Thread function employed by tstDoThreadedTest.
67 *
68 * @returns IPRT status code.
69 * @param hThreadSelf The thread.
70 * @param pvUser The operation to perform.
71 */
72static DECLCALLBACK(int) tstThreadFn(RTTHREAD hThreadSelf, void *pvUser)
73{
74 uint32_t u32 = (uint32_t)(uintptr_t)pvUser;
75 TSTRTR0SEMMUTEX enmDo = (TSTRTR0SEMMUTEX)RT_LOWORD(u32);
76 uint32_t cSecs = RT_HIWORD(u32);
77 TSTRTR0SEMMUTEXREQ Req;
78 RT_ZERO(Req);
79 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
80 Req.Hdr.cbReq = sizeof(Req);
81 Req.szMsg[0] = '\0';
82 RTTEST_CHECK_RC_RET(g_hTest, SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
83 enmDo, cSecs, &Req.Hdr),
84 VINF_SUCCESS,
85 rcCheck);
86 if (Req.szMsg[0] == '!')
87 {
88 RTTestFailed(g_hTest, "%s", &Req.szMsg[1]);
89 return VERR_GENERAL_FAILURE;
90 }
91 if (Req.szMsg[0])
92 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%s", Req.szMsg);
93 return VINF_SUCCESS;
94}
95
96/**
97 * Performs a threaded test.
98 *
99 * @returns true on succes, false on failure.
100 * @param enmSetup The setup operation number.
101 * @param enmDo The do-it operation number.
102 * @param enmCleanup The cleanup operation number.
103 * @param cThreads The number of threads.
104 * @param pReq The request structure.
105 * @param pszTest The test name.
106 */
107static bool tstDoThreadedTest(TSTRTR0SEMMUTEX enmSetup, TSTRTR0SEMMUTEX enmDo, TSTRTR0SEMMUTEX enmCleanup,
108 unsigned cThreads, unsigned cSecs, PTSTRTR0SEMMUTEXREQ pReq, const char *pszTest)
109{
110 RTTestSubF(g_hTest, "%s - %u threads", pszTest, cThreads);
111 RTTHREAD ahThreads[32];
112 RTTESTI_CHECK_RET(cThreads <= RT_ELEMENTS(ahThreads), false);
113
114 /*
115 * Setup.
116 */
117 pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
118 pReq->Hdr.cbReq = sizeof(*pReq);
119 pReq->szMsg[0] = '\0';
120 RTTESTI_CHECK_RC_RET(SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmSetup, 0, &pReq->Hdr),
121 VINF_SUCCESS, false);
122 if (pReq->szMsg[0] == '!')
123 return RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
124 if (pReq->szMsg[0])
125 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
126
127 /*
128 * Test execution.
129 */
130 for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
131 ahThreads[i] = NIL_RTTHREAD;
132
133 int rc = VINF_SUCCESS;
134 for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++)
135 rc = RTThreadCreateF(&ahThreads[i], tstThreadFn, (void *)(uintptr_t)RT_MAKE_U32(enmDo, cSecs), 0, RTTHREADTYPE_DEFAULT,
136 RTTHREADFLAGS_WAITABLE, "test-%u", i);
137
138 for (unsigned i = 0; i < cThreads; i++)
139 if (ahThreads[i] != NIL_RTTHREAD)
140 {
141 int rcThread = VINF_SUCCESS;
142 int rc2 = RTThreadWait(ahThreads[i], 3600*1000, &rcThread);
143 if (RT_SUCCESS(rc2))
144 {
145 ahThreads[i] = NIL_RTTHREAD;
146 if (RT_FAILURE(rcThread) && RT_SUCCESS(rc2))
147 rc = rcThread;
148 }
149 else if (RT_SUCCESS(rc))
150 rc = rc2;
151 }
152
153 /*
154 * Cleanup.
155 */
156 pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
157 pReq->Hdr.cbReq = sizeof(*pReq);
158 pReq->szMsg[0] = '\0';
159 RTTESTI_CHECK_RC_RET(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmCleanup, 0, &pReq->Hdr),
160 VINF_SUCCESS, false);
161 if (pReq->szMsg[0] == '!')
162 return RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
163 if (pReq->szMsg[0])
164 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
165
166 if (RT_FAILURE(rc))
167 for (unsigned i = 0; i < cThreads; i++)
168 if (ahThreads[i] != NIL_RTTHREAD)
169 RTThreadWait(ahThreads[i], 1000, NULL);
170
171 return RT_SUCCESS(rc);
172}
173
174
175int main(int argc, char **argv)
176{
177#ifndef VBOX
178 RTPrintf("tstSup: SKIPPED\n");
179 return 0;
180#else
181 /*
182 * Init.
183 */
184 RTTEST hTest;
185 int rc = RTTestInitAndCreate("tstRTR0SemMutex", &hTest);
186 if (rc)
187 return rc;
188 g_hTest = hTest;
189 RTTestBanner(hTest);
190
191 PSUPDRVSESSION pSession;
192 rc = SUPR3Init(&pSession);
193 if (RT_FAILURE(rc))
194 {
195 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
196 return RTTestSummaryAndDestroy(hTest);
197 }
198
199 char szPath[RTPATH_MAX];
200 rc = RTPathExecDir(szPath, sizeof(szPath));
201 if (RT_SUCCESS(rc))
202 rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0SemMutex.r0");
203 if (RT_FAILURE(rc))
204 {
205 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
206 return RTTestSummaryAndDestroy(hTest);
207 }
208
209 void *pvImageBase;
210 rc = SUPR3LoadServiceModule(szPath, "tstRTR0SemMutex",
211 "TSTRTR0SemMutexSrvReqHandler",
212 &pvImageBase);
213 if (RT_FAILURE(rc))
214 {
215 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
216 return RTTestSummaryAndDestroy(hTest);
217 }
218
219 /* test request */
220 TSTRTR0SEMMUTEXREQ Req;
221
222 /*
223 * Sanity checks.
224 */
225 RTTestSub(hTest, "Sanity");
226 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
227 Req.Hdr.cbReq = sizeof(Req);
228 Req.szMsg[0] = '\0';
229 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
230 TSTRTR0SEMMUTEX_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
231 if (RT_FAILURE(rc))
232 return RTTestSummaryAndDestroy(hTest);
233 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
234 if (Req.szMsg[0] != '\0')
235 return RTTestSummaryAndDestroy(hTest);
236
237 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
238 Req.Hdr.cbReq = sizeof(Req);
239 Req.szMsg[0] = '\0';
240 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
241 TSTRTR0SEMMUTEX_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
242 if (RT_FAILURE(rc))
243 return RTTestSummaryAndDestroy(hTest);
244 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1), ("%s", Req.szMsg));
245 if (strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1))
246 return RTTestSummaryAndDestroy(hTest);
247
248 /*
249 * Basic tests, bail out on failure.
250 */
251 RTTestSub(hTest, "Basics");
252 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
253 Req.Hdr.cbReq = sizeof(Req);
254 Req.szMsg[0] = '\0';
255 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
256 TSTRTR0SEMMUTEX_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
257 if (RT_FAILURE(rc))
258 return RTTestSummaryAndDestroy(hTest);
259 if (Req.szMsg[0] == '!')
260 {
261 RTTestIFailed("%s", &Req.szMsg[1]);
262 return RTTestSummaryAndDestroy(hTest);
263 }
264 if (Req.szMsg[0])
265 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
266
267 /*
268 * Tests with multiple threads for bugs in the contention part of the code.
269 * Test #2: Try to hold the semaphore for 1 ms.
270 * Test #3: Grab and release immediately.
271 * Test #4: Timeout checks. Try grab it for 0-32 ms and hold it for 1 s.
272 */
273 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 1, 1, &Req, "test #2");
274 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 2, 3, &Req, "test #2");
275 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 3, 3, &Req, "test #2");
276 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 9, 3, &Req, "test #2");
277
278 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 1, 1, &Req, "test #3");
279 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 2, 3, &Req, "test #3");
280 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 3, 3, &Req, "test #3");
281 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 9, 3, &Req, "test #3");
282
283 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 1, 1, &Req, "test #4");
284 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 2, 3, &Req, "test #4");
285 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 3, 3, &Req, "test #4");
286 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 9, 3, &Req, "test #4");
287
288 /*
289 * Done.
290 */
291 return RTTestSummaryAndDestroy(hTest);
292#endif
293}
294
Note: See TracBrowser for help on using the repository browser.

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