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