VirtualBox

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

Last change on this file since 46474 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

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