VirtualBox

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

Last change on this file since 16684 was 12874, checked in by vboxsync, 16 years ago

IPRT: added a testcase for RTSemPingPong and some state inspection methods.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* $Id: tstSemPingPong.cpp 12874 2008-10-01 20:09:09Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSemPing/RTSemPong.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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* Header Files *
33*******************************************************************************/
34#include <iprt/semaphore.h>
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include <iprt/initterm.h>
38#include <iprt/thread.h>
39#include <iprt/asm.h>
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44#define TSTSEMPINGPONG_ITERATIONS 1000000
45
46
47/*******************************************************************************
48* Global Variables *
49*******************************************************************************/
50static volatile uint32_t g_cErrors = 0;
51
52static DECLCALLBACK(int) tstSemPingPongThread(RTTHREAD hThread, void *pvPP)
53{
54 int rc;
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
90int main()
91{
92 RTR3Init();
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
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