VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0SemMutex.cpp@ 52320

Last change on this file since 52320 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: tstRTR0SemMutex.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Mutex Semaphores.
4 */
5
6/*
7 * Copyright (C) 2009-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/semaphore.h>
31
32#include <iprt/err.h>
33#include <VBox/sup.h>
34#include <iprt/string.h>
35#include <iprt/time.h>
36#include <iprt/thread.h>
37#include "tstRTR0SemMutex.h"
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43/** The mutex used in test #2. */
44static RTSEMMUTEX g_hMtxTest2 = NIL_RTSEMMUTEX;
45
46
47
48/**
49 * Service request callback function.
50 *
51 * @returns VBox status code.
52 * @param pSession The caller's session.
53 * @param u64Arg 64-bit integer argument.
54 * @param pReqHdr The request header. Input / Output. Optional.
55 */
56DECLEXPORT(int) TSTRTR0SemMutexSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
57 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
58{
59 NOREF(pSession);
60 if (!VALID_PTR(pReqHdr))
61 return VERR_INVALID_PARAMETER;
62 char *pszErr = (char *)(pReqHdr + 1);
63 size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
64 if (cchErr < 32 || cchErr >= 0x10000)
65 return VERR_INVALID_PARAMETER;
66 *pszErr = '\0';
67
68#define SET_ERROR(szFmt) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt); } while (0)
69#define SET_ERROR1(szFmt, a1) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1); } while (0)
70#define SET_ERROR2(szFmt, a1, a2) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1, a2); } while (0)
71#define SET_ERROR3(szFmt, a1, a2, a3) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1, a2, a3); } while (0)
72#define CHECK_RC_BREAK(rc, rcExpect, szOp) \
73 if ((rc) != (rcExpect)) \
74 { \
75 RTStrPrintf(pszErr, cchErr, "!%s -> %Rrc, expected %Rrc. line %u", szOp, rc, rcExpect, __LINE__); \
76 SUPR0Printf("%s -> %d, expected %d. line %u", szOp, rc, rcExpect, __LINE__); \
77 break; \
78 }
79
80 /*
81 * Set up test timeout (when applicable).
82 */
83 if (u64Arg > 120)
84 {
85 SET_ERROR1("Timeout is too large (max 120): %lld", u64Arg);
86 return VINF_SUCCESS;
87 }
88 uint64_t const StartTS = RTTimeSystemMilliTS();
89 uint32_t const cMsMax = (uint32_t)u64Arg * 1000;
90
91 /*
92 * The big switch.
93 */
94 RTSEMMUTEX hMtx;
95 int rc;
96 switch (uOperation)
97 {
98 case TSTRTR0SEMMUTEX_SANITY_OK:
99 break;
100
101 case TSTRTR0SEMMUTEX_SANITY_FAILURE:
102 SET_ERROR1("42failure42%1024s", "");
103 break;
104
105 case TSTRTR0SEMMUTEX_BASIC:
106 rc = RTSemMutexCreate(&hMtx);
107 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
108 do
109 {
110 /*
111 * The interruptible version first.
112 */
113 /* simple request and release, polling. */
114 rc = RTSemMutexRequestNoResume(hMtx, 0);
115 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume");
116 rc = RTSemMutexRelease(hMtx);
117 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
118
119 /* simple request and release, wait for ever. */
120 rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
121 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(indef_wait)");
122 rc = RTSemMutexRelease(hMtx);
123 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
124
125 /* simple request and release, wait a tiny while. */
126 rc = RTSemMutexRequestNoResume(hMtx, 133);
127 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(133)");
128 rc = RTSemMutexRelease(hMtx);
129 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
130
131 /* nested request and release. */
132 rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
133 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#1");
134 rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
135 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#2");
136 rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
137 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#3");
138 rc = RTSemMutexRelease(hMtx);
139 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3");
140 rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
141 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#3b");
142 rc = RTSemMutexRelease(hMtx);
143 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3b");
144 rc = RTSemMutexRelease(hMtx);
145 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#2");
146 rc = RTSemMutexRelease(hMtx);
147 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#1");
148
149 /*
150 * The uninterruptible variant.
151 */
152 /* simple request and release, polling. */
153 rc = RTSemMutexRequest(hMtx, 0);
154 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest");
155 rc = RTSemMutexRelease(hMtx);
156 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
157
158 /* simple request and release, wait for ever. */
159 rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
160 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest(indef_wait)");
161 rc = RTSemMutexRelease(hMtx);
162 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
163
164 /* simple request and release, wait a tiny while. */
165 rc = RTSemMutexRequest(hMtx, 133);
166 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest(133)");
167 rc = RTSemMutexRelease(hMtx);
168 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
169
170 /* nested request and release. */
171 rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
172 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#1");
173 rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
174 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#2");
175 rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
176 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#3");
177 rc = RTSemMutexRelease(hMtx);
178 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3");
179 rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
180 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#3b");
181 rc = RTSemMutexRelease(hMtx);
182 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3b");
183 rc = RTSemMutexRelease(hMtx);
184 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#2");
185 rc = RTSemMutexRelease(hMtx);
186 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#1");
187
188 } while (false);
189
190 rc = RTSemMutexDestroy(hMtx);
191 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexDestroy");
192 break;
193
194 case TSTRTR0SEMMUTEX_TEST2_SETUP:
195 case TSTRTR0SEMMUTEX_TEST3_SETUP:
196 case TSTRTR0SEMMUTEX_TEST4_SETUP:
197 rc = RTSemMutexCreate(&g_hMtxTest2);
198 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
199 break;
200
201 case TSTRTR0SEMMUTEX_TEST2_DO:
202 for (unsigned i = 0; i < 200; i++)
203 {
204 if (i & 1)
205 {
206 rc = RTSemMutexRequestNoResume(g_hMtxTest2, RT_INDEFINITE_WAIT);
207 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,indef_wait)");
208 }
209 else
210 {
211 rc = RTSemMutexRequestNoResume(g_hMtxTest2, 30000);
212 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,30000)");
213 }
214 RTThreadSleep(1);
215 rc = RTSemMutexRelease(g_hMtxTest2);
216 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
217
218 if ((i % 16) == 15 && RTTimeSystemMilliTS() - StartTS >= cMsMax)
219 break;
220 }
221 break;
222
223
224 case TSTRTR0SEMMUTEX_TEST3_DO:
225 for (unsigned i = 0; i < 1000000; i++)
226 {
227 if (i & 1)
228 {
229 rc = RTSemMutexRequestNoResume(g_hMtxTest2, RT_INDEFINITE_WAIT);
230 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,indef_wait)");
231 }
232 else
233 {
234 rc = RTSemMutexRequestNoResume(g_hMtxTest2, 30000);
235 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,30000)");
236 }
237 rc = RTSemMutexRelease(g_hMtxTest2);
238 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
239
240 if ((i % 256) == 255 && RTTimeSystemMilliTS() - StartTS >= cMsMax)
241 break;
242 }
243 break;
244
245 case TSTRTR0SEMMUTEX_TEST4_DO:
246 for (unsigned i = 0; i < 1024; i++)
247 {
248 rc = RTSemMutexRequestNoResume(g_hMtxTest2, (i % 32));
249 if (rc != VERR_TIMEOUT)
250 {
251 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume");
252 RTThreadSleep(1000);
253
254 rc = RTSemMutexRelease(g_hMtxTest2);
255 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
256 }
257
258 if (RTTimeSystemMilliTS() - StartTS >= cMsMax)
259 break;
260 }
261 break;
262
263 case TSTRTR0SEMMUTEX_TEST2_CLEANUP:
264 case TSTRTR0SEMMUTEX_TEST3_CLEANUP:
265 case TSTRTR0SEMMUTEX_TEST4_CLEANUP:
266 rc = RTSemMutexDestroy(g_hMtxTest2);
267 CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
268 g_hMtxTest2 = NIL_RTSEMMUTEX;
269 break;
270
271
272 default:
273 SET_ERROR1("Unknown test #%d", uOperation);
274 break;
275 }
276
277 /* The error indicator is the '!' in the message buffer. */
278 return VINF_SUCCESS;
279}
280
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