VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSemXRoads.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 Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: tstRTSemXRoads.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSemXRoads.
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/errcore.h>
45#include <iprt/initterm.h>
46#include <iprt/test.h>
47#include <iprt/thread.h>
48#include <iprt/time.h>
49
50
51/*********************************************************************************************************************************
52* Global Variables *
53*********************************************************************************************************************************/
54static RTTEST g_hTest;
55
56static uint32_t volatile g_cNSCrossings;
57static uint32_t volatile g_cEWCrossings;
58static uint64_t g_u64StartMilliTS;
59static uint32_t g_cSecs;
60static RTSEMXROADS g_hXRoads;
61
62
63static int tstTrafficThreadCommon(uintptr_t iThread, bool fNS)
64{
65 RT_NOREF_PV(iThread);
66
67 for (uint32_t iLoop = 0; RTTimeMilliTS() - g_u64StartMilliTS < g_cSecs*1000; iLoop++)
68 {
69 /* fudge */
70 if ((iLoop % 223) == 222)
71 RTThreadYield();
72 else if ((iLoop % 16127) == 16126)
73 RTThreadSleep(1);
74
75 if (fNS)
76 {
77 RTTEST_CHECK_RC(g_hTest,RTSemXRoadsNSEnter(g_hXRoads), VINF_SUCCESS);
78 ASMAtomicIncU32(&g_cNSCrossings);
79 RTTEST_CHECK_RC(g_hTest,RTSemXRoadsNSLeave(g_hXRoads), VINF_SUCCESS);
80 }
81 else
82 {
83 RTTEST_CHECK_RC(g_hTest,RTSemXRoadsEWEnter(g_hXRoads), VINF_SUCCESS);
84 ASMAtomicIncU32(&g_cEWCrossings);
85 RTTEST_CHECK_RC(g_hTest,RTSemXRoadsEWLeave(g_hXRoads), VINF_SUCCESS);
86 }
87 }
88 return VINF_SUCCESS;
89}
90
91
92static DECLCALLBACK(int) tstTrafficNSThread(RTTHREAD hSelf, void *pvUser)
93{
94 RT_NOREF_PV(hSelf);
95
96 uintptr_t iThread = (uintptr_t)pvUser;
97 return tstTrafficThreadCommon(iThread, true);
98}
99
100
101static DECLCALLBACK(int) tstTrafficEWThread(RTTHREAD hSelf, void *pvUser)
102{
103 RT_NOREF_PV(hSelf);
104
105 uintptr_t iThread = (uintptr_t)pvUser;
106 return tstTrafficThreadCommon(iThread, false);
107}
108
109
110static void tstTraffic(unsigned cThreads, unsigned cSecs)
111{
112 RTTestSubF(g_hTest, "Traffic - %u threads per direction, %u sec", cThreads, cSecs);
113
114 /*
115 * Create X worker threads which drives in the south/north direction and Y
116 * worker threads which drives in the west/east direction. Let them drive
117 * in a loop for 15 seconds with slight delays between some of the runs and
118 * then check the numbers.
119 */
120
121 /* init */
122 RTTHREAD ahThreadsX[8];
123 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsX); i++)
124 ahThreadsX[i] = NIL_RTTHREAD;
125 AssertRelease(RT_ELEMENTS(ahThreadsX) >= cThreads);
126
127 RTTHREAD ahThreadsY[8];
128 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsY); i++)
129 ahThreadsY[i] = NIL_RTTHREAD;
130 AssertRelease(RT_ELEMENTS(ahThreadsY) >= cThreads);
131
132 g_cNSCrossings = 0;
133 g_cEWCrossings = 0;
134 g_cSecs = cSecs;
135 g_u64StartMilliTS = RTTimeMilliTS();
136
137 /* create */
138 RTTEST_CHECK_RC_RETV(g_hTest, RTSemXRoadsCreate(&g_hXRoads), VINF_SUCCESS);
139
140 int rc = VINF_SUCCESS;
141 for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++)
142 {
143 rc = RTThreadCreateF(&ahThreadsX[i], tstTrafficNSThread, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "NS-%u", i);
144 RTTEST_CHECK_RC_OK(g_hTest, rc);
145 }
146
147 for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++)
148 {
149 rc = RTThreadCreateF(&ahThreadsX[i], tstTrafficEWThread, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "NS-%u", i);
150 RTTEST_CHECK_RC_OK(g_hTest, rc);
151 }
152
153 /* wait */
154 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsX); i++)
155 if (ahThreadsX[i] != NIL_RTTHREAD)
156 {
157 int rc2 = RTThreadWaitNoResume(ahThreadsX[i], (60 + cSecs) * 1000, NULL);
158 RTTEST_CHECK_RC_OK(g_hTest, rc2);
159 }
160
161 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsY); i++)
162 if (ahThreadsY[i] != NIL_RTTHREAD)
163 {
164 int rc2 = RTThreadWaitNoResume(ahThreadsY[i], (60 + cSecs) * 1000, NULL);
165 RTTEST_CHECK_RC_OK(g_hTest, rc2);
166 }
167
168 RTTEST_CHECK_MSG_RETV(g_hTest, g_cEWCrossings > 10 && g_cNSCrossings,
169 (g_hTest, "cEWCrossings=%u g_cNSCrossings=%u\n", g_cEWCrossings, g_cNSCrossings));
170 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "cNSCrossings=%u\n", g_cNSCrossings);
171 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "cEWCrossings=%u\n", g_cEWCrossings);
172}
173
174
175
176static bool tstBasics(void)
177{
178 RTTestSub(g_hTest, "Basics");
179
180 RTSEMXROADS hXRoads;
181 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsCreate(&hXRoads), VINF_SUCCESS, false);
182
183 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSEnter(hXRoads), VINF_SUCCESS, false);
184 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSLeave(hXRoads), VINF_SUCCESS, false);
185 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsEWEnter(hXRoads), VINF_SUCCESS, false);
186 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsEWLeave(hXRoads), VINF_SUCCESS, false);
187
188 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsEWEnter(hXRoads), VINF_SUCCESS, false);
189 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsEWLeave(hXRoads), VINF_SUCCESS, false);
190 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSEnter(hXRoads), VINF_SUCCESS, false);
191 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSLeave(hXRoads), VINF_SUCCESS, false);
192
193 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSEnter(hXRoads), VINF_SUCCESS, false);
194 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsNSLeave(hXRoads), VINF_SUCCESS, false);
195
196 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsDestroy(hXRoads), VINF_SUCCESS, false);
197 RTTEST_CHECK_RC_RET(g_hTest, RTSemXRoadsDestroy(NIL_RTSEMXROADS), VINF_SUCCESS, false);
198
199 return true;
200}
201
202
203int main()
204{
205 int rc = RTTestInitAndCreate("tstRTSemXRoads", &g_hTest);
206 if (rc)
207 return rc;
208 RTTestBanner(g_hTest);
209
210 if (tstBasics())
211 {
212 tstTraffic(1, 5);
213 tstTraffic(2, 5);
214 tstTraffic(4, 15);
215 tstTraffic(8, 10);
216 }
217
218 return RTTestSummaryAndDestroy(g_hTest);
219}
220
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