VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSemEventMulti.cpp@ 96781

Last change on this file since 96781 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.0 KB
Line 
1/* $Id: tstRTSemEventMulti.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Multiple Release Event Semaphores.
4 */
5
6/*
7 * Copyright (C) 2009-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/semaphore.h>
42
43#include <iprt/asm.h>
44#include <iprt/assert.h>
45#include <iprt/errcore.h>
46#include <iprt/rand.h>
47#include <iprt/stream.h>
48#include <iprt/string.h>
49#include <iprt/test.h>
50#include <iprt/thread.h>
51#include <iprt/time.h>
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57/** The test handle. */
58static RTTEST g_hTest;
59
60
61static DECLCALLBACK(int) test1Thread1(RTTHREAD ThreadSelf, void *pvUser)
62{
63 RTSEMEVENTMULTI hSem = *(PRTSEMEVENTMULTI)pvUser;
64 RT_NOREF_PV(ThreadSelf);
65
66 uint64_t u64 = RTTimeSystemMilliTS();
67 RTTEST_CHECK_RC(g_hTest, RTSemEventMultiWait(hSem, 1000), VERR_TIMEOUT);
68 u64 = RTTimeSystemMilliTS() - u64;
69 RTTEST_CHECK_MSG(g_hTest, u64 < 1500 && u64 > 950, (g_hTest, "u64=%llu\n", u64));
70
71 RTTEST_CHECK_RC(g_hTest, RTSemEventMultiWait(hSem, 2000), VINF_SUCCESS);
72 return VINF_SUCCESS;
73}
74
75
76static DECLCALLBACK(int) test1Thread2(RTTHREAD ThreadSelf, void *pvUser)
77{
78 RTSEMEVENTMULTI hSem = *(PRTSEMEVENTMULTI)pvUser;
79 RT_NOREF_PV(ThreadSelf);
80
81 RTTEST_CHECK_RC(g_hTest, RTSemEventMultiWait(hSem, RT_INDEFINITE_WAIT), VINF_SUCCESS);
82 return VINF_SUCCESS;
83}
84
85
86static void test1(void)
87{
88 RTTestISub("Three threads");
89
90 /*
91 * Create the threads and let them block on the event multi semaphore.
92 */
93 RTSEMEVENTMULTI hSem;
94 RTTESTI_CHECK_RC_RETV(RTSemEventMultiCreate(&hSem), VINF_SUCCESS);
95
96 RTTHREAD hThread2;
97 RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread2, test1Thread2, &hSem, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2"), VINF_SUCCESS);
98 RTThreadSleep(100);
99
100 RTTHREAD hThread1;
101 RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread1, test1Thread1, &hSem, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1"), VINF_SUCCESS);
102
103 /* Force first thread (which has a timeout of 1 second) to timeout in the
104 * first wait, and the second wait will succeed. */
105 RTTESTI_CHECK_RC(RTThreadSleep(1500), VINF_SUCCESS);
106 RTTESTI_CHECK_RC(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
107 RTTESTI_CHECK_RC(RTThreadWait(hThread1, 5000, NULL), VINF_SUCCESS);
108 RTTESTI_CHECK_RC(RTThreadWait(hThread2, 5000, NULL), VINF_SUCCESS);
109 RTTESTI_CHECK_RC(RTSemEventMultiDestroy(hSem), VINF_SUCCESS);
110}
111
112
113static void testBasicsWaitTimeout(RTSEMEVENTMULTI hSem, unsigned i)
114{
115 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWait(hSem, 0), VERR_TIMEOUT);
116#if 0
117 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitNoResume(hSem, 0), VERR_TIMEOUT);
118#else
119 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
120 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_RELATIVE,
121 0),
122 VERR_TIMEOUT);
123 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
124 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
125 RTTimeSystemNanoTS() + 1000*i),
126 VERR_TIMEOUT);
127 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
128 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
129 RTTimeNanoTS() + 1000*i),
130 VERR_TIMEOUT);
131
132 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
133 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_RELATIVE,
134 0),
135 VERR_TIMEOUT);
136#endif
137}
138
139
140static void testBasicsWaitSuccess(RTSEMEVENTMULTI hSem, unsigned i)
141{
142 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWait(hSem, 0), VINF_SUCCESS);
143 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWait(hSem, RT_INDEFINITE_WAIT), VINF_SUCCESS);
144#if 0
145 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitNoResume(hSem, 0), VINF_SUCCESS);
146 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitNoResume(hSem, RT_INDEFINITE_WAIT), VINF_SUCCESS);
147#else
148 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
149 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_RELATIVE,
150 0),
151 VINF_SUCCESS);
152 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem, RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_INDEFINITE, 0), VINF_SUCCESS);
153 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem, RTSEMWAIT_FLAGS_NORESUME | RTSEMWAIT_FLAGS_INDEFINITE, 0), VINF_SUCCESS);
154 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
155 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
156 RTTimeSystemNanoTS() + 1000*i),
157 VINF_SUCCESS);
158 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
159 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
160 RTTimeNanoTS() + 1000*i),
161 VINF_SUCCESS);
162 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
163 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
164 0),
165 VINF_SUCCESS);
166 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
167 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
168 _1G),
169 VINF_SUCCESS);
170 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
171 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE,
172 UINT64_MAX),
173 VINF_SUCCESS);
174
175
176 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
177 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE,
178 RTTimeSystemMilliTS() + 1000*i),
179 VINF_SUCCESS);
180 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
181 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE,
182 RTTimeMilliTS() + 1000*i),
183 VINF_SUCCESS);
184 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
185 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE,
186 0),
187 VINF_SUCCESS);
188 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
189 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE,
190 _1M),
191 VINF_SUCCESS);
192 RTTESTI_CHECK_RC_RETV(RTSemEventMultiWaitEx(hSem,
193 RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE,
194 UINT64_MAX),
195 VINF_SUCCESS);
196#endif
197}
198
199
200static void testBasics(void)
201{
202 RTTestISub("Basics");
203
204 RTSEMEVENTMULTI hSem;
205 RTTESTI_CHECK_RC_RETV(RTSemEventMultiCreate(&hSem), VINF_SUCCESS);
206
207 /* The semaphore is created in a reset state, calling reset explicitly
208 shouldn't make any difference. */
209 testBasicsWaitTimeout(hSem, 0);
210 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
211 testBasicsWaitTimeout(hSem, 1);
212 if (RTTestIErrorCount())
213 return;
214
215 /* When signalling the semaphore all successive wait calls shall
216 succeed, signalling it again should make no difference. */
217 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
218 testBasicsWaitSuccess(hSem, 2);
219 if (RTTestIErrorCount())
220 return;
221
222 /* After resetting it we should time out again. */
223 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
224 testBasicsWaitTimeout(hSem, 3);
225 if (RTTestIErrorCount())
226 return;
227
228 /* The number of resets or signal calls shouldn't matter. */
229 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
230 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
231 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
232 testBasicsWaitTimeout(hSem, 4);
233
234 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
235 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
236 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
237 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
238 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
239 testBasicsWaitSuccess(hSem, 5);
240
241 RTTESTI_CHECK_RC_RETV(RTSemEventMultiReset(hSem), VINF_SUCCESS);
242 testBasicsWaitTimeout(hSem, 6);
243
244 /* Destroy it. */
245 RTTESTI_CHECK_RC_RETV(RTSemEventMultiDestroy(hSem), VINF_SUCCESS);
246 RTTESTI_CHECK_RC_RETV(RTSemEventMultiDestroy(NIL_RTSEMEVENTMULTI), VINF_SUCCESS);
247
248 /* Whether it is reset (above), signalled or not used shouldn't matter. */
249 RTTESTI_CHECK_RC_RETV(RTSemEventMultiCreate(&hSem), VINF_SUCCESS);
250 RTTESTI_CHECK_RC_RETV(RTSemEventMultiSignal(hSem), VINF_SUCCESS);
251 RTTESTI_CHECK_RC_RETV(RTSemEventMultiDestroy(hSem), VINF_SUCCESS);
252
253 RTTESTI_CHECK_RC_RETV(RTSemEventMultiCreate(&hSem), VINF_SUCCESS);
254 RTTESTI_CHECK_RC_RETV(RTSemEventMultiDestroy(hSem), VINF_SUCCESS);
255
256 RTTestISubDone();
257}
258
259
260int main(int argc, char **argv)
261{
262 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
263
264 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTSemEventMulti", &g_hTest);
265 if (rcExit != RTEXITCODE_SUCCESS)
266 return rcExit;
267
268 testBasics();
269 if (!RTTestErrorCount(g_hTest))
270 {
271 test1();
272 }
273
274 return RTTestSummaryAndDestroy(g_hTest);
275}
276
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