VirtualBox

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

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

RTLockValidator,RTThread: Serialize deadlock detection and RTTHREAD/RTLOCKVALIDATORREC destruction.

  • 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 25436 2009-12-16 15:22:40Z 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
82
83static DECLCALLBACK(int) tstTrafficNSThread(RTTHREAD hSelf, void *pvUser)
84{
85 uintptr_t iThread = (uintptr_t)pvUser;
86 return tstTrafficThreadCommon(iThread, true);
87}
88
89
90static DECLCALLBACK(int) tstTrafficEWThread(RTTHREAD hSelf, void *pvUser)
91{
92 uintptr_t iThread = (uintptr_t)pvUser;
93 return tstTrafficThreadCommon(iThread, false);
94}
95
96
97static void tstTraffic(void)
98{
99 RTTestSub(g_hTest, "Traffic");
100
101 /*
102 * Create X worker threads which drives in the south/north direction and Y
103 * worker threads which drives in the west/east direction. Let them drive
104 * in a loop for 15 seconds with slight delays between some of the runs and
105 * then check the numbers.
106 */
107
108 /* init */
109 RTTHREAD ahThreadsX[4];
110 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsX); i++)
111 ahThreadsX[i] = NIL_RTTHREAD;
112
113 RTTHREAD ahThreadsY[4];
114 for (unsigned i = 0; i < RT_ELEMENTS(ahThreadsY); i++)
115 ahThreadsY[i] = NIL_RTTHREAD;
116
117 g_cNSCrossings = 0;
118 g_cEWCrossings = 0;
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 < RT_ELEMENTS(ahThreadsX) && RT_SUCCESS(rc); i++)
126 {
127 rc = RTThreadCreateF(&ahThreadsX[i], tstTrafficNSThread, (void *)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 < RT_ELEMENTS(ahThreadsY) && RT_SUCCESS(rc); i++)
132 {
133 rc = RTThreadCreateF(&ahThreadsX[i], tstTrafficEWThread, (void *)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*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*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("tstRTCidr", &g_hTest);
190 if (rc)
191 return rc;
192 RTTestBanner(g_hTest);
193
194 if (tstBasics())
195 tstTraffic();
196
197 return RTTestSummaryAndDestroy(g_hTest);
198}
199
200
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