VirtualBox

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

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

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

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