1 | /* $Id: tstSemPingPong.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTSemPing/RTSemPong.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | #include <iprt/string.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/initterm.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #define TSTSEMPINGPONG_ITERATIONS 1000000
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | static volatile uint32_t g_cErrors = 0;
|
---|
49 |
|
---|
50 | static DECLCALLBACK(int) tstSemPingPongThread(RTTHREAD hThread, void *pvPP)
|
---|
51 | {
|
---|
52 | RT_NOREF_PV(hThread);
|
---|
53 |
|
---|
54 | int rc = VINF_SUCCESS; /* (MSC powers of deduction are rather weak. sigh) */
|
---|
55 | PRTPINGPONG pPP = (PRTPINGPONG)pvPP;
|
---|
56 | for (uint32_t i = 0; i < TSTSEMPINGPONG_ITERATIONS; i++)
|
---|
57 | {
|
---|
58 | if (!RTSemPongShouldWait(pPP))
|
---|
59 | {
|
---|
60 | ASMAtomicIncU32(&g_cErrors);
|
---|
61 | RTPrintf("tstSemPingPong: ERROR - RTSemPongShouldWait returned false before RTSemPongWait.\n");
|
---|
62 | }
|
---|
63 |
|
---|
64 | rc = RTSemPongWait(pPP, RT_INDEFINITE_WAIT);
|
---|
65 | if (RT_FAILURE(rc))
|
---|
66 | {
|
---|
67 | ASMAtomicIncU32(&g_cErrors);
|
---|
68 | RTPrintf("tstSemPingPong: ERROR - RTSemPongWait -> %Rrc\n", rc);
|
---|
69 | break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (!RTSemPongIsSpeaker(pPP))
|
---|
73 | {
|
---|
74 | ASMAtomicIncU32(&g_cErrors);
|
---|
75 | RTPrintf("tstSemPingPong: ERROR - RTSemPongIsSpeaker returned false before RTSemPong.\n");
|
---|
76 | }
|
---|
77 |
|
---|
78 | rc = RTSemPong(pPP);
|
---|
79 | if (RT_FAILURE(rc))
|
---|
80 | {
|
---|
81 | ASMAtomicIncU32(&g_cErrors);
|
---|
82 | RTPrintf("tstSemPingPong: ERROR - RTSemPong -> %Rrc\n", rc);
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return rc;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | int main()
|
---|
91 | {
|
---|
92 | RTR3InitExeNoArguments(0);
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Create a ping pong and kick off a second thread which we'll
|
---|
96 | * exchange TSTSEMPINGPONG_ITERATIONS messages with.
|
---|
97 | */
|
---|
98 | RTPINGPONG PingPong;
|
---|
99 | int rc = RTSemPingPongInit(&PingPong);
|
---|
100 | if (RT_FAILURE(rc))
|
---|
101 | {
|
---|
102 | RTPrintf("tstSemPingPong: FATAL ERROR - RTSemPingPongInit -> %Rrc\n", rc);
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | RTTHREAD hThread;
|
---|
107 | rc = RTThreadCreate(&hThread, tstSemPingPongThread, &PingPong, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "PONG");
|
---|
108 | if (RT_FAILURE(rc))
|
---|
109 | {
|
---|
110 | RTPrintf("tstSemPingPong: FATAL ERROR - RTSemPingPongInit -> %Rrc\n", rc);
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | RTPrintf("tstSemPingPong: TESTING - %u iterations...\n", TSTSEMPINGPONG_ITERATIONS);
|
---|
115 | for (uint32_t i = 0; i < TSTSEMPINGPONG_ITERATIONS; i++)
|
---|
116 | {
|
---|
117 | if (!RTSemPingIsSpeaker(&PingPong))
|
---|
118 | {
|
---|
119 | ASMAtomicIncU32(&g_cErrors);
|
---|
120 | RTPrintf("tstSemPingPong: ERROR - RTSemPingIsSpeaker returned false before RTSemPing.\n");
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = RTSemPing(&PingPong);
|
---|
124 | if (RT_FAILURE(rc))
|
---|
125 | {
|
---|
126 | ASMAtomicIncU32(&g_cErrors);
|
---|
127 | RTPrintf("tstSemPingPong: ERROR - RTSemPing -> %Rrc\n", rc);
|
---|
128 | break;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!RTSemPingShouldWait(&PingPong))
|
---|
132 | {
|
---|
133 | ASMAtomicIncU32(&g_cErrors);
|
---|
134 | RTPrintf("tstSemPingPong: ERROR - RTSemPingShouldWait returned false before RTSemPingWait.\n");
|
---|
135 | }
|
---|
136 |
|
---|
137 | rc = RTSemPingWait(&PingPong, RT_INDEFINITE_WAIT);
|
---|
138 | if (RT_FAILURE(rc))
|
---|
139 | {
|
---|
140 | ASMAtomicIncU32(&g_cErrors);
|
---|
141 | RTPrintf("tstSemPingPong: ERROR - RTSemPingWait -> %Rrc\n", rc);
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | int rcThread;
|
---|
147 | rc = RTThreadWait(hThread, 5000, &rcThread);
|
---|
148 | if (RT_FAILURE(rc))
|
---|
149 | {
|
---|
150 | ASMAtomicIncU32(&g_cErrors);
|
---|
151 | RTPrintf("tstSemPingPong: ERROR - RTSemPingWait -> %Rrc\n", rc);
|
---|
152 | }
|
---|
153 |
|
---|
154 | rc = RTSemPingPongDelete(&PingPong);
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | {
|
---|
157 | ASMAtomicIncU32(&g_cErrors);
|
---|
158 | RTPrintf("tstSemPingPong: ERROR - RTSemPingPongDestroy -> %Rrc\n", rc);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Summary.
|
---|
163 | */
|
---|
164 | uint32_t cErrors = ASMAtomicUoReadU32(&g_cErrors);
|
---|
165 | if (cErrors)
|
---|
166 | RTPrintf("tstSemPingPong: FAILED - %d errors\n", cErrors);
|
---|
167 | else
|
---|
168 | RTPrintf("tstSemPingPong: SUCCESS\n");
|
---|
169 | return cErrors ? 1 : 0;
|
---|
170 | }
|
---|
171 |
|
---|