1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* $Id: tstRTR0SemMutex.cpp $ */
/** @file
* IPRT R0 Testcase - Mutex Semaphores.
*/
/*
* Copyright (C) 2009-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/semaphore.h>
#include <iprt/errcore.h>
#include <VBox/sup.h>
#include <iprt/string.h>
#include <iprt/time.h>
#include <iprt/thread.h>
#include "tstRTR0SemMutex.h"
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** The mutex used in test #2. */
static RTSEMMUTEX g_hMtxTest2 = NIL_RTSEMMUTEX;
/**
* Service request callback function.
*
* @returns VBox status code.
* @param pSession The caller's session.
* @param u64Arg 64-bit integer argument.
* @param pReqHdr The request header. Input / Output. Optional.
*/
DECLEXPORT(int) TSTRTR0SemMutexSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
{
NOREF(pSession);
if (!VALID_PTR(pReqHdr))
return VERR_INVALID_PARAMETER;
char *pszErr = (char *)(pReqHdr + 1);
size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
if (cchErr < 32 || cchErr >= 0x10000)
return VERR_INVALID_PARAMETER;
*pszErr = '\0';
#define SET_ERROR(szFmt) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt); } while (0)
#define SET_ERROR1(szFmt, a1) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1); } while (0)
#define SET_ERROR2(szFmt, a1, a2) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1, a2); } while (0)
#define SET_ERROR3(szFmt, a1, a2, a3) do { if (!*pszErr) RTStrPrintf(pszErr, cchErr, "!" szFmt, a1, a2, a3); } while (0)
#define CHECK_RC_BREAK(rc, rcExpect, szOp) \
if ((rc) != (rcExpect)) \
{ \
RTStrPrintf(pszErr, cchErr, "!%s -> %Rrc, expected %Rrc. line %u", szOp, rc, rcExpect, __LINE__); \
SUPR0Printf("%s -> %d, expected %d. line %u", szOp, rc, rcExpect, __LINE__); \
break; \
}
/*
* Set up test timeout (when applicable).
*/
if (u64Arg > 120)
{
SET_ERROR1("Timeout is too large (max 120): %lld", u64Arg);
return VINF_SUCCESS;
}
uint64_t const StartTS = RTTimeSystemMilliTS();
uint32_t const cMsMax = (uint32_t)u64Arg * 1000;
/*
* The big switch.
*/
RTSEMMUTEX hMtx;
int rc;
switch (uOperation)
{
case TSTRTR0SEMMUTEX_SANITY_OK:
break;
case TSTRTR0SEMMUTEX_SANITY_FAILURE:
SET_ERROR1("42failure42%1024s", "");
break;
case TSTRTR0SEMMUTEX_BASIC:
rc = RTSemMutexCreate(&hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
do
{
/*
* The interruptible version first.
*/
/* simple request and release, polling. */
rc = RTSemMutexRequestNoResume(hMtx, 0);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* simple request and release, wait for ever. */
rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(indef_wait)");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* simple request and release, wait a tiny while. */
rc = RTSemMutexRequestNoResume(hMtx, 133);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(133)");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* nested request and release. */
rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#1");
rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#2");
rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#3");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3");
rc = RTSemMutexRequestNoResume(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume#3b");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3b");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#2");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#1");
/*
* The uninterruptible variant.
*/
/* simple request and release, polling. */
rc = RTSemMutexRequest(hMtx, 0);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* simple request and release, wait for ever. */
rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest(indef_wait)");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* simple request and release, wait a tiny while. */
rc = RTSemMutexRequest(hMtx, 133);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest(133)");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
/* nested request and release. */
rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#1");
rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#2");
rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#3");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3");
rc = RTSemMutexRequest(hMtx, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequest#3b");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#3b");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#2");
rc = RTSemMutexRelease(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease#1");
} while (false);
rc = RTSemMutexDestroy(hMtx);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexDestroy");
break;
case TSTRTR0SEMMUTEX_TEST2_SETUP:
case TSTRTR0SEMMUTEX_TEST3_SETUP:
case TSTRTR0SEMMUTEX_TEST4_SETUP:
rc = RTSemMutexCreate(&g_hMtxTest2);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
break;
case TSTRTR0SEMMUTEX_TEST2_DO:
for (unsigned i = 0; i < 200; i++)
{
if (i & 1)
{
rc = RTSemMutexRequestNoResume(g_hMtxTest2, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,indef_wait)");
}
else
{
rc = RTSemMutexRequestNoResume(g_hMtxTest2, 30000);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,30000)");
}
RTThreadSleep(1);
rc = RTSemMutexRelease(g_hMtxTest2);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
if ((i % 16) == 15 && RTTimeSystemMilliTS() - StartTS >= cMsMax)
break;
}
break;
case TSTRTR0SEMMUTEX_TEST3_DO:
for (unsigned i = 0; i < 1000000; i++)
{
if (i & 1)
{
rc = RTSemMutexRequestNoResume(g_hMtxTest2, RT_INDEFINITE_WAIT);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,indef_wait)");
}
else
{
rc = RTSemMutexRequestNoResume(g_hMtxTest2, 30000);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume(,30000)");
}
rc = RTSemMutexRelease(g_hMtxTest2);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
if ((i % 256) == 255 && RTTimeSystemMilliTS() - StartTS >= cMsMax)
break;
}
break;
case TSTRTR0SEMMUTEX_TEST4_DO:
for (unsigned i = 0; i < 1024; i++)
{
rc = RTSemMutexRequestNoResume(g_hMtxTest2, (i % 32));
if (rc != VERR_TIMEOUT)
{
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRequestNoResume");
RTThreadSleep(1000);
rc = RTSemMutexRelease(g_hMtxTest2);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexRelease");
}
if (RTTimeSystemMilliTS() - StartTS >= cMsMax)
break;
}
break;
case TSTRTR0SEMMUTEX_TEST2_CLEANUP:
case TSTRTR0SEMMUTEX_TEST3_CLEANUP:
case TSTRTR0SEMMUTEX_TEST4_CLEANUP:
rc = RTSemMutexDestroy(g_hMtxTest2);
CHECK_RC_BREAK(rc, VINF_SUCCESS, "RTSemMutexCreate");
g_hMtxTest2 = NIL_RTSEMMUTEX;
break;
default:
SET_ERROR1("Unknown test #%d", uOperation);
break;
}
/* The error indicator is the '!' in the message buffer. */
return VINF_SUCCESS;
}
|