Changeset 12874 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Oct 1, 2008 8:09:09 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 37272
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/semspingpong.cpp
r8245 r12874 40 40 41 41 42 43 /**44 * Validates a pPP handle passed to one of the PP functions. 45 46 * @returns true if valid, false if invalid.47 * @param pPP Pointe to the ping-pong structure.48 * /49 inline bool rtsemPPValid(PRTPINGPONG pPP) 50 { 51 if (!VALID_PTR(pPP)) 52 return false;53 54 RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;55 if ( enmSpeaker != RTPINGPONGSPEAKER_PING56 && enmSpeaker != RTPINGPONGSPEAKER_PONG57 && enmSpeaker != RTPINGPONGSPEAKER_PONG_SIGNALED58 && enmSpeaker != RTPINGPONGSPEAKER_PING_SIGNALED)59 return false;60 61 return true;62 } 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 ASMAtomicUoReadSize(&pPP->enmSpeaker, &enmSpeaker); \ 55 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING \ 56 || enmSpeaker == RTPINGPONGSPEAKER_PONG \ 57 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED \ 58 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED, \ 59 ("enmSpeaker=%d\n", enmSpeaker), \ 60 VERR_INVALID_PARAMETER); \ 61 } while (0) 62 63 63 64 64 /** … … 95 95 * (I.e. put into uninitialized state.) 96 96 */ 97 RTR3DECL(int) RTSemPingPongDestroy(PRTPINGPONG pPP) 98 { 99 /* 100 * Validate input 101 */ 102 if (!rtsemPPValid(pPP)) 103 { 104 AssertMsgFailed(("Invalid input %p\n", pPP)); 105 return VERR_INVALID_PARAMETER; 106 } 97 RTR3DECL(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); 107 106 108 107 /* 109 108 * Invalidate the ping pong handle and destroy the event semaphores. 110 109 */ 111 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_UNINITIALIZE);110 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_UNINITIALIZE); 112 111 int rc = RTSemEventDestroy(pPP->Ping); 113 112 int rc2 = RTSemEventDestroy(pPP->Pong); … … 131 130 * Validate input 132 131 */ 133 if (!rtsemPPValid(pPP)) 134 { 135 AssertMsgFailed(("Invalid input %p\n", pPP)); 136 return VERR_INVALID_PARAMETER; 137 } 138 139 if (pPP->enmSpeaker != RTPINGPONGSPEAKER_PING) 140 { 141 AssertMsgFailed(("Speaking out of turn!\n")); 142 return VERR_SEM_OUT_OF_TURN; 143 } 132 RTPINGPONGSPEAKER enmSpeaker; 133 RTSEMPP_VALIDATE_RETURN(pPP); 134 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PING,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker), 135 VERR_SEM_OUT_OF_TURN); 144 136 145 137 /* 146 138 * Signal the other thread. 147 139 */ 148 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG_SIGNALED);140 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG_SIGNALED); 149 141 int rc = RTSemEventSignal(pPP->Pong); 150 142 if (RT_SUCCESS(rc)) … … 152 144 153 145 /* restore the state. */ 154 AssertMsgFailed(("Failed to signal pong sem %x. rc=% d\n", pPP->Pong, rc));155 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);146 AssertMsgFailed(("Failed to signal pong sem %x. rc=%Rrc\n", pPP->Pong, rc)); 147 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING); 156 148 return rc; 157 149 } … … 170 162 * Validate input 171 163 */ 172 if (!rtsemPPValid(pPP)) 173 { 174 AssertMsgFailed(("Invalid input %p\n", pPP)); 175 return VERR_INVALID_PARAMETER; 176 } 177 178 if (pPP->enmSpeaker != RTPINGPONGSPEAKER_PONG) 179 { 180 AssertMsgFailed(("Speaking out of turn!\n")); 181 return VERR_SEM_OUT_OF_TURN; 182 } 164 RTPINGPONGSPEAKER enmSpeaker; 165 RTSEMPP_VALIDATE_RETURN(pPP); 166 AssertMsgReturn(enmSpeaker == RTPINGPONGSPEAKER_PONG,("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker), 167 VERR_SEM_OUT_OF_TURN); 183 168 184 169 /* 185 170 * Signal the other thread. 186 171 */ 187 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING_SIGNALED);172 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING_SIGNALED); 188 173 int rc = RTSemEventSignal(pPP->Ping); 189 174 if (RT_SUCCESS(rc)) … … 191 176 192 177 /* restore the state. */ 193 AssertMsgFailed(("Failed to signal ping sem %x. rc=% d\n", pPP->Ping, rc));194 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);178 AssertMsgFailed(("Failed to signal ping sem %x. rc=%Rrc\n", pPP->Ping, rc)); 179 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG); 195 180 return rc; 196 181 } … … 210 195 * Validate input 211 196 */ 212 if (!rtsemPPValid(pPP)) 213 { 214 AssertMsgFailed(("Invalid input %p\n", pPP)); 215 return VERR_INVALID_PARAMETER; 216 } 217 218 if ( pPP->enmSpeaker != RTPINGPONGSPEAKER_PONG 219 && pPP->enmSpeaker != RTPINGPONGSPEAKER_PONG_SIGNALED 220 && pPP->enmSpeaker != RTPINGPONGSPEAKER_PING_SIGNALED) 221 { 222 AssertMsgFailed(("Listening out of turn! enmSpeaker=%d\n", pPP->enmSpeaker)); 223 return VERR_SEM_OUT_OF_TURN; 224 } 197 RTPINGPONGSPEAKER enmSpeaker; 198 RTSEMPP_VALIDATE_RETURN(pPP); 199 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PONG 200 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED 201 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED, 202 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker), 203 VERR_SEM_OUT_OF_TURN); 225 204 226 205 /* … … 229 208 int rc = RTSemEventWait(pPP->Ping, cMillies); 230 209 if (RT_SUCCESS(rc)) 231 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING);210 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PING); 232 211 Assert(rc != VERR_INTERRUPTED); 233 212 return rc; … … 248 227 * Validate input 249 228 */ 250 if (!rtsemPPValid(pPP)) 251 { 252 AssertMsgFailed(("Invalid input %p\n", pPP)); 253 return VERR_INVALID_PARAMETER; 254 } 255 256 if ( pPP->enmSpeaker != RTPINGPONGSPEAKER_PING 257 && pPP->enmSpeaker != RTPINGPONGSPEAKER_PING_SIGNALED 258 && pPP->enmSpeaker != RTPINGPONGSPEAKER_PONG_SIGNALED) 259 { 260 AssertMsgFailed(("Listening out of turn! enmSpeaker=%d\n", pPP->enmSpeaker)); 261 return VERR_SEM_OUT_OF_TURN; 262 } 229 RTPINGPONGSPEAKER enmSpeaker; 230 RTSEMPP_VALIDATE_RETURN(pPP); 231 AssertMsgReturn( enmSpeaker == RTPINGPONGSPEAKER_PING 232 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED 233 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED, 234 ("Speaking out of turn! enmSpeaker=%d\n", enmSpeaker), 235 VERR_SEM_OUT_OF_TURN); 263 236 264 237 /* … … 267 240 int rc = RTSemEventWait(pPP->Pong, cMillies); 268 241 if (RT_SUCCESS(rc)) 269 ASMAtomic XchgSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG);242 ASMAtomicWriteSize(&pPP->enmSpeaker, RTPINGPONGSPEAKER_PONG); 270 243 Assert(rc != VERR_INTERRUPTED); 271 244 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.