VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/semspingpong.cpp@ 14398

Last change on this file since 14398 was 14057, checked in by vboxsync, 16 years ago

semspingpong.cpp: shut up 64-bit MSC warning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/* $Id: semspingpong.cpp 14057 2008-11-10 23:00:21Z vboxsync $ */
2/** @file
3 * IPRT - Thread Ping-Pong Construct.
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/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/semaphore.h>
36#include <iprt/thread.h>
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/err.h>
40
41
42/*******************************************************************************
43* Defined Constants And Macros *
44*******************************************************************************/
45/**
46 * Validation macro returns if invalid parameter.
47 *
48 * Expects a enmSpeaker variable to be handy and will set it to the current
49 * enmSpeaker value.
50 */
51#define RTSEMPP_VALIDATE_RETURN(pPP) \
52 do { \
53 AssertPtrReturn(pPP, VERR_INVALID_PARAMETER); \
54 AssertCompileSize(pPP->enmSpeaker, 4); \
55 enmSpeaker = (RTPINGPONGSPEAKER)ASMAtomicUoReadU32((volatile uint32_t *)&pPP->enmSpeaker); \
56 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING \
57 || enmSpeaker == RTPINGPONGSPEAKER_PONG \
58 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED \
59 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED, \
60 ("enmSpeaker=%d\n", enmSpeaker), \
61 VERR_INVALID_PARAMETER); \
62 } while (0)
63
64
65/**
66 * Init a Ping-Pong construct.
67 *
68 * @returns iprt status code.
69 * @param pPP Pointer to the ping-pong structure which needs initialization.
70 */
71RTR3DECL(int) RTSemPingPongInit(PRTPINGPONG pPP)
72{
73 /*
74 * Init the structure.
75 */
76 pPP->enmSpeaker = RTPINGPONGSPEAKER_PING;
77
78 int rc = RTSemEventCreate(&pPP->Ping);
79 if (RT_SUCCESS(rc))
80 {
81 rc = RTSemEventCreate(&pPP->Pong);
82 if (RT_SUCCESS(rc))
83 return VINF_SUCCESS;
84 RTSemEventDestroy(pPP->Ping);
85 }
86
87 return rc;
88}
89
90
91/**
92 * Destroys a Ping-Pong construct.
93 *
94 * @returns iprt status code.
95 * @param pPP Pointer to the ping-pong structure which is to be destroyed.
96 * (I.e. put into uninitialized state.)
97 */
98RTR3DECL(int) RTSemPingPongDelete(PRTPINGPONG pPP)
99{
100 /*
101 * Validate input
102 */
103 if (!pPP)
104 return VINF_SUCCESS;
105 RTPINGPONGSPEAKER enmSpeaker;
106 RTSEMPP_VALIDATE_RETURN(pPP);
107
108 /*
109 * Invalidate the ping pong handle and destroy the event semaphores.
110 */
111 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_UNINITIALIZE);
112 int rc = RTSemEventDestroy(pPP->Ping);
113 int rc2 = RTSemEventDestroy(pPP->Pong);
114 AssertRC(rc);
115 AssertRC(rc2);
116
117 return VINF_SUCCESS;
118}
119
120
121/**
122 * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
123 * This is called by the ping thread.
124 *
125 * @returns iprt status code.
126 * @param pPP Pointer to the ping-pong structure to ping.
127 */
128RTR3DECL(int) RTSemPing(PRTPINGPONG pPP)
129{
130 /*
131 * Validate input
132 */
133 RTPINGPONGSPEAKER enmSpeaker;
134 RTSEMPP_VALIDATE_RETURN(pPP);
135 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PING,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
136 VERR_SEM_OUT_OF_TURN);
137
138 /*
139 * Signal the other thread.
140 */
141 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG_SIGNALED);
142 int rc = RTSemEventSignal(pPP->Pong);
143 if (RT_SUCCESS(rc))
144 return rc;
145
146 /* restore the state. */
147 AssertMsgFailed(("Failed to signal pong sem %x. rc=%Rrc\n", pPP->Pong, rc));
148 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
149 return rc;
150}
151
152
153/**
154 * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
155 * This is called by the pong thread.
156 *
157 * @returns iprt status code.
158 * @param pPP Pointer to the ping-pong structure to pong.
159 */
160RTR3DECL(int) RTSemPong(PRTPINGPONG pPP)
161{
162 /*
163 * Validate input
164 */
165 RTPINGPONGSPEAKER enmSpeaker;
166 RTSEMPP_VALIDATE_RETURN(pPP);
167 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PONG,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
168 VERR_SEM_OUT_OF_TURN);
169
170 /*
171 * Signal the other thread.
172 */
173 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING_SIGNALED);
174 int rc = RTSemEventSignal(pPP->Ping);
175 if (RT_SUCCESS(rc))
176 return rc;
177
178 /* restore the state. */
179 AssertMsgFailed(("Failed to signal ping sem %x. rc=%Rrc\n", pPP->Ping, rc));
180 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
181 return rc;
182}
183
184
185/**
186 * Wait function for the ping thread.
187 *
188 * @returns iprt status code.
189 * Will not return VERR_INTERRUPTED.
190 * @param pPP Pointer to the ping-pong structure to wait on.
191 * @param cMillies Number of milliseconds to wait.
192 */
193RTR3DECL(int) RTSemPingWait(PRTPINGPONG pPP, unsigned cMillies)
194{
195 /*
196 * Validate input
197 */
198 RTPINGPONGSPEAKER enmSpeaker;
199 RTSEMPP_VALIDATE_RETURN(pPP);
200 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PONG
201 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
202 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED,
203 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
204 VERR_SEM_OUT_OF_TURN);
205
206 /*
207 * Wait.
208 */
209 int rc = RTSemEventWait(pPP->Ping, cMillies);
210 if (RT_SUCCESS(rc))
211 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
212 Assert(rc != VERR_INTERRUPTED);
213 return rc;
214}
215
216
217/**
218 * Wait function for the pong thread.
219 *
220 * @returns iprt status code.
221 * Will not return VERR_INTERRUPTED.
222 * @param pPP Pointer to the ping-pong structure to wait on.
223 * @param cMillies Number of milliseconds to wait.
224 */
225RTR3DECL(int) RTSemPongWait(PRTPINGPONG pPP, unsigned cMillies)
226{
227 /*
228 * Validate input
229 */
230 RTPINGPONGSPEAKER enmSpeaker;
231 RTSEMPP_VALIDATE_RETURN(pPP);
232 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING
233 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
234 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED,
235 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
236 VERR_SEM_OUT_OF_TURN);
237
238 /*
239 * Wait.
240 */
241 int rc = RTSemEventWait(pPP->Pong, cMillies);
242 if (RT_SUCCESS(rc))
243 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
244 Assert(rc != VERR_INTERRUPTED);
245 return rc;
246}
247
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