1 | /* $Id: semspingpong.cpp 98103 2023-01-17 14:15:46Z 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 |
|
---|
73 | /**
|
---|
74 | * Init a Ping-Pong construct.
|
---|
75 | *
|
---|
76 | * @returns iprt status code.
|
---|
77 | * @param pPP Pointer to the ping-pong structure which needs initialization.
|
---|
78 | */
|
---|
79 | RTDECL(int) RTSemPingPongInit(PRTPINGPONG pPP)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Init the structure.
|
---|
83 | */
|
---|
84 | pPP->enmSpeaker = RTPINGPONGSPEAKER_PING;
|
---|
85 |
|
---|
86 | int rc = RTSemEventCreate(&pPP->Ping);
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | rc = RTSemEventCreate(&pPP->Pong);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | RTSemEventDestroy(pPP->Ping);
|
---|
93 | }
|
---|
94 |
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 | RT_EXPORT_SYMBOL(RTSemPingPongInit);
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Destroys a Ping-Pong construct.
|
---|
102 | *
|
---|
103 | * @returns iprt status code.
|
---|
104 | * @param pPP Pointer to the ping-pong structure which is to be destroyed.
|
---|
105 | * (I.e. put into uninitialized state.)
|
---|
106 | */
|
---|
107 | RTDECL(int) RTSemPingPongDelete(PRTPINGPONG pPP)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Validate input
|
---|
111 | */
|
---|
112 | if (!pPP)
|
---|
113 | return VINF_SUCCESS;
|
---|
114 | RTPINGPONGSPEAKER enmSpeaker;
|
---|
115 | RTSEMPP_VALIDATE_RETURN(pPP);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Invalidate the ping pong handle and destroy the event semaphores.
|
---|
119 | */
|
---|
120 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_UNINITIALIZE);
|
---|
121 | int rc = RTSemEventDestroy(pPP->Ping);
|
---|
122 | int rc2 = RTSemEventDestroy(pPP->Pong);
|
---|
123 | AssertRC(rc);
|
---|
124 | AssertRC(rc2);
|
---|
125 |
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 | RT_EXPORT_SYMBOL(RTSemPingPongDelete);
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
|
---|
133 | * This is called by the ping thread.
|
---|
134 | *
|
---|
135 | * @returns iprt status code.
|
---|
136 | * @param pPP Pointer to the ping-pong structure to ping.
|
---|
137 | */
|
---|
138 | RTDECL(int) RTSemPing(PRTPINGPONG pPP)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Validate input
|
---|
142 | */
|
---|
143 | RTPINGPONGSPEAKER enmSpeaker;
|
---|
144 | RTSEMPP_VALIDATE_RETURN(pPP);
|
---|
145 | AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PING,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
|
---|
146 | VERR_SEM_OUT_OF_TURN);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Signal the other thread.
|
---|
150 | */
|
---|
151 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG_SIGNALED);
|
---|
152 | int rc = RTSemEventSignal(pPP->Pong);
|
---|
153 | if (RT_SUCCESS(rc))
|
---|
154 | return rc;
|
---|
155 |
|
---|
156 | /* restore the state. */
|
---|
157 | AssertMsgFailed(("Failed to signal pong sem %x. rc=%Rrc\n", pPP->Pong, rc));
|
---|
158 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 | RT_EXPORT_SYMBOL(RTSemPing);
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
|
---|
166 | * This is called by the pong thread.
|
---|
167 | *
|
---|
168 | * @returns iprt status code.
|
---|
169 | * @param pPP Pointer to the ping-pong structure to pong.
|
---|
170 | */
|
---|
171 | RTDECL(int) RTSemPong(PRTPINGPONG pPP)
|
---|
172 | {
|
---|
173 | /*
|
---|
174 | * Validate input
|
---|
175 | */
|
---|
176 | RTPINGPONGSPEAKER enmSpeaker;
|
---|
177 | RTSEMPP_VALIDATE_RETURN(pPP);
|
---|
178 | AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PONG,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
|
---|
179 | VERR_SEM_OUT_OF_TURN);
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Signal the other thread.
|
---|
183 | */
|
---|
184 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING_SIGNALED);
|
---|
185 | int rc = RTSemEventSignal(pPP->Ping);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | return rc;
|
---|
188 |
|
---|
189 | /* restore the state. */
|
---|
190 | AssertMsgFailed(("Failed to signal ping sem %x. rc=%Rrc\n", pPP->Ping, rc));
|
---|
191 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 | RT_EXPORT_SYMBOL(RTSemPong);
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Wait function for the ping thread.
|
---|
199 | *
|
---|
200 | * @returns iprt status code.
|
---|
201 | * Will not return VERR_INTERRUPTED.
|
---|
202 | * @param pPP Pointer to the ping-pong structure to wait on.
|
---|
203 | * @param cMillies Number of milliseconds to wait.
|
---|
204 | */
|
---|
205 | RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies)
|
---|
206 | {
|
---|
207 | /*
|
---|
208 | * Validate input
|
---|
209 | */
|
---|
210 | RTPINGPONGSPEAKER enmSpeaker;
|
---|
211 | RTSEMPP_VALIDATE_RETURN(pPP);
|
---|
212 | AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PONG
|
---|
213 | || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
|
---|
214 | || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED,
|
---|
215 | ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
|
---|
216 | VERR_SEM_OUT_OF_TURN);
|
---|
217 |
|
---|
218 | /*
|
---|
219 | * Wait.
|
---|
220 | */
|
---|
221 | int rc = RTSemEventWait(pPP->Ping, cMillies);
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);
|
---|
224 | Assert(rc != VERR_INTERRUPTED);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 | RT_EXPORT_SYMBOL(RTSemPingWait);
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Wait function for the pong thread.
|
---|
232 | *
|
---|
233 | * @returns iprt status code.
|
---|
234 | * Will not return VERR_INTERRUPTED.
|
---|
235 | * @param pPP Pointer to the ping-pong structure to wait on.
|
---|
236 | * @param cMillies Number of milliseconds to wait.
|
---|
237 | */
|
---|
238 | RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies)
|
---|
239 | {
|
---|
240 | /*
|
---|
241 | * Validate input
|
---|
242 | */
|
---|
243 | RTPINGPONGSPEAKER enmSpeaker;
|
---|
244 | RTSEMPP_VALIDATE_RETURN(pPP);
|
---|
245 | AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING
|
---|
246 | || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
|
---|
247 | || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED,
|
---|
248 | ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker),
|
---|
249 | VERR_SEM_OUT_OF_TURN);
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Wait.
|
---|
253 | */
|
---|
254 | int rc = RTSemEventWait(pPP->Pong, cMillies);
|
---|
255 | if (RT_SUCCESS(rc))
|
---|
256 | ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);
|
---|
257 | Assert(rc != VERR_INTERRUPTED);
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 | RT_EXPORT_SYMBOL(RTSemPongWait);
|
---|
261 |
|
---|