VirtualBox

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

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

RTSemXRoads: initial implementation.

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