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