VirtualBox

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

Last change on this file was 99758, checked in by vboxsync, 12 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.9 KB
Line 
1/* $Id: semspingpong.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
2/** @file
3 * IPRT - Thread Ping-Pong Construct.
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 "internal/iprt.h"
43
44#include <iprt/thread.h>
45#include <iprt/asm.h>
46#include <iprt/assert.h>
47#include <iprt/err.h>
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53/**
54 * Validation macro returns if invalid parameter.
55 *
56 * Expects a enmSpeaker variable to be handy and will set it to the current
57 * enmSpeaker value.
58 */
59#define RTSEMPP_VALIDATE_RETURN(pPP) \
60 do { \
61 AssertPtrReturn(pPP, VERR_INVALID_PARAMETER); \
62 AssertCompileSize(pPP->enmSpeaker, 4); \
63 enmSpeaker = (RTPINGPONGSPEAKER)ASMAtomicUoReadU32((volatile uint32_t *)&pPP->enmSpeaker); \
64 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING \
65 || enmSpeaker == RTPINGPONGSPEAKER_PONG \
66 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED \
67 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED, \
68 ("enmSpeaker=%d\n", enmSpeaker), \
69 VERR_INVALID_PARAMETER); \
70 } while (0)
71
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
94RTDECL(int) RTSemPingPongDelete(PRTPINGPONG pPP)
95{
96 /*
97 * Validate input
98 */
99 if (!pPP)
100 return VINF_SUCCESS;
101 RTPINGPONGSPEAKER enmSpeaker;
102 RTSEMPP_VALIDATE_RETURN(pPP);
103
104 /*
105 * Invalidate the ping pong handle and destroy the event semaphores.
106 */
107 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_UNINITIALIZE);
108 int rc = RTSemEventDestroy(pPP->Ping);
109 int rc2 = RTSemEventDestroy(pPP->Pong);
110 AssertRC(rc);
111 AssertRC(rc2);
112
113 return VINF_SUCCESS;
114}
115RT_EXPORT_SYMBOL(RTSemPingPongDelete);
116
117
118RTDECL(int) RTSemPing(PRTPINGPONG pPP)
119{
120 /*
121 * Validate input
122 */
123 RTPINGPONGSPEAKER enmSpeaker;
124 RTSEMPP_VALIDATE_RETURN(pPP);
125 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PING,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
126 VERR_SEM_OUT_OF_TURN);
127
128 /*
129 * Signal the other thread.
130 */
131 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG_SIGNALED);
132 int rc = RTSemEventSignal(pPP->Pong);
133 if (RT_SUCCESS(rc))
134 return rc;
135
136 /* restore the state. */
137 AssertMsgFailed(("Failed to signal pong sem %x. rc=%Rrc\n", pPP->Pong, rc));
138 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
139 return rc;
140}
141RT_EXPORT_SYMBOL(RTSemPing);
142
143
144RTDECL(int) RTSemPong(PRTPINGPONG pPP)
145{
146 /*
147 * Validate input
148 */
149 RTPINGPONGSPEAKER enmSpeaker;
150 RTSEMPP_VALIDATE_RETURN(pPP);
151 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PONG,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
152 VERR_SEM_OUT_OF_TURN);
153
154 /*
155 * Signal the other thread.
156 */
157 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING_SIGNALED);
158 int rc = RTSemEventSignal(pPP->Ping);
159 if (RT_SUCCESS(rc))
160 return rc;
161
162 /* restore the state. */
163 AssertMsgFailed(("Failed to signal ping sem %x. rc=%Rrc\n", pPP->Ping, rc));
164 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
165 return rc;
166}
167RT_EXPORT_SYMBOL(RTSemPong);
168
169
170RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies)
171{
172 /*
173 * Validate input
174 */
175 RTPINGPONGSPEAKER enmSpeaker;
176 RTSEMPP_VALIDATE_RETURN(pPP);
177 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PONG
178 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
179 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED,
180 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
181 VERR_SEM_OUT_OF_TURN);
182
183 /*
184 * Wait.
185 */
186 int rc = RTSemEventWait(pPP->Ping, cMillies);
187 if (RT_SUCCESS(rc))
188 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
189 Assert(rc != VERR_INTERRUPTED);
190 return rc;
191}
192RT_EXPORT_SYMBOL(RTSemPingWait);
193
194
195RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies)
196{
197 /*
198 * Validate input
199 */
200 RTPINGPONGSPEAKER enmSpeaker;
201 RTSEMPP_VALIDATE_RETURN(pPP);
202 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING
203 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
204 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED,
205 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
206 VERR_SEM_OUT_OF_TURN);
207
208 /*
209 * Wait.
210 */
211 int rc = RTSemEventWait(pPP->Pong, cMillies);
212 if (RT_SUCCESS(rc))
213 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
214 Assert(rc != VERR_INTERRUPTED);
215 return rc;
216}
217RT_EXPORT_SYMBOL(RTSemPongWait);
218
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use