VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSemXRoads.cpp@ 25596

Last change on this file since 25596 was 25549, checked in by vboxsync, 15 years ago

semxroads-generic.cpp: bugfix - reset race (of course).

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