VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstSemPingPong.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.2 KB
Line 
1/* $Id: tstSemPingPong.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSemPing/RTSemPong.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/semaphore.h>
42#include <iprt/string.h>
43#include <iprt/stream.h>
44#include <iprt/initterm.h>
45#include <iprt/thread.h>
46#include <iprt/asm.h>
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52#define TSTSEMPINGPONG_ITERATIONS 1000000
53
54
55/*********************************************************************************************************************************
56* Global Variables *
57*********************************************************************************************************************************/
58static volatile uint32_t g_cErrors = 0;
59
60static DECLCALLBACK(int) tstSemPingPongThread(RTTHREAD hThread, void *pvPP)
61{
62 RT_NOREF_PV(hThread);
63
64 int rc = VINF_SUCCESS; /* (MSC powers of deduction are rather weak. sigh) */
65 PRTPINGPONG pPP = (PRTPINGPONG)pvPP;
66 for (uint32_t i = 0; i < TSTSEMPINGPONG_ITERATIONS; i++)
67 {
68 if (!RTSemPongShouldWait(pPP))
69 {
70 ASMAtomicIncU32(&g_cErrors);
71 RTPrintf("tstSemPingPong: ERROR - RTSemPongShouldWait returned false before RTSemPongWait.\n");
72 }
73
74 rc = RTSemPongWait(pPP, RT_INDEFINITE_WAIT);
75 if (RT_FAILURE(rc))
76 {
77 ASMAtomicIncU32(&g_cErrors);
78 RTPrintf("tstSemPingPong: ERROR - RTSemPongWait -> %Rrc\n", rc);
79 break;
80 }
81
82 if (!RTSemPongIsSpeaker(pPP))
83 {
84 ASMAtomicIncU32(&g_cErrors);
85 RTPrintf("tstSemPingPong: ERROR - RTSemPongIsSpeaker returned false before RTSemPong.\n");
86 }
87
88 rc = RTSemPong(pPP);
89 if (RT_FAILURE(rc))
90 {
91 ASMAtomicIncU32(&g_cErrors);
92 RTPrintf("tstSemPingPong: ERROR - RTSemPong -> %Rrc\n", rc);
93 break;
94 }
95 }
96 return rc;
97}
98
99
100int main()
101{
102 RTR3InitExeNoArguments(0);
103
104 /*
105 * Create a ping pong and kick off a second thread which we'll
106 * exchange TSTSEMPINGPONG_ITERATIONS messages with.
107 */
108 RTPINGPONG PingPong;
109 int rc = RTSemPingPongInit(&PingPong);
110 if (RT_FAILURE(rc))
111 {
112 RTPrintf("tstSemPingPong: FATAL ERROR - RTSemPingPongInit -> %Rrc\n", rc);
113 return 1;
114 }
115
116 RTTHREAD hThread;
117 rc = RTThreadCreate(&hThread, tstSemPingPongThread, &PingPong, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "PONG");
118 if (RT_FAILURE(rc))
119 {
120 RTPrintf("tstSemPingPong: FATAL ERROR - RTSemPingPongInit -> %Rrc\n", rc);
121 return 1;
122 }
123
124 RTPrintf("tstSemPingPong: TESTING - %u iterations...\n", TSTSEMPINGPONG_ITERATIONS);
125 for (uint32_t i = 0; i < TSTSEMPINGPONG_ITERATIONS; i++)
126 {
127 if (!RTSemPingIsSpeaker(&PingPong))
128 {
129 ASMAtomicIncU32(&g_cErrors);
130 RTPrintf("tstSemPingPong: ERROR - RTSemPingIsSpeaker returned false before RTSemPing.\n");
131 }
132
133 rc = RTSemPing(&PingPong);
134 if (RT_FAILURE(rc))
135 {
136 ASMAtomicIncU32(&g_cErrors);
137 RTPrintf("tstSemPingPong: ERROR - RTSemPing -> %Rrc\n", rc);
138 break;
139 }
140
141 if (!RTSemPingShouldWait(&PingPong))
142 {
143 ASMAtomicIncU32(&g_cErrors);
144 RTPrintf("tstSemPingPong: ERROR - RTSemPingShouldWait returned false before RTSemPingWait.\n");
145 }
146
147 rc = RTSemPingWait(&PingPong, RT_INDEFINITE_WAIT);
148 if (RT_FAILURE(rc))
149 {
150 ASMAtomicIncU32(&g_cErrors);
151 RTPrintf("tstSemPingPong: ERROR - RTSemPingWait -> %Rrc\n", rc);
152 break;
153 }
154 }
155
156 int rcThread;
157 rc = RTThreadWait(hThread, 5000, &rcThread);
158 if (RT_FAILURE(rc))
159 {
160 ASMAtomicIncU32(&g_cErrors);
161 RTPrintf("tstSemPingPong: ERROR - RTSemPingWait -> %Rrc\n", rc);
162 }
163
164 rc = RTSemPingPongDelete(&PingPong);
165 if (RT_FAILURE(rc))
166 {
167 ASMAtomicIncU32(&g_cErrors);
168 RTPrintf("tstSemPingPong: ERROR - RTSemPingPongDestroy -> %Rrc\n", rc);
169 }
170
171 /*
172 * Summary.
173 */
174 uint32_t cErrors = ASMAtomicUoReadU32(&g_cErrors);
175 if (cErrors)
176 RTPrintf("tstSemPingPong: FAILED - %d errors\n", cErrors);
177 else
178 RTPrintf("tstSemPingPong: SUCCESS\n");
179 return cErrors ? 1 : 0;
180}
181
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