Changeset 6747 in vbox
- Timestamp:
- Feb 2, 2008 12:57:41 AM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 27829
- Location:
- trunk/src/VBox/Runtime/r3/linux
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/linux/semevent-linux.cpp
r6738 r6747 90 90 * Allocate semaphore handle. 91 91 */ 92 struct RTSEMEVENTINTERNAL *p IntEventSem= (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));93 if (p IntEventSem)94 { 95 p IntEventSem->iMagic = RTSEMEVENT_MAGIC;96 p IntEventSem->cWaiters = 0;97 *pEventSem = p IntEventSem;92 struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL)); 93 if (pThis) 94 { 95 pThis->iMagic = RTSEMEVENT_MAGIC; 96 pThis->cWaiters = 0; 97 *pEventSem = pThis; 98 98 return VINF_SUCCESS; 99 99 } … … 107 107 * Validate input. 108 108 */ 109 struct RTSEMEVENTINTERNAL *p IntEventSem= EventSem;110 AssertReturn(VALID_PTR(p IntEventSem) && pIntEventSem->iMagic == RTSEMEVENT_MAGIC,109 struct RTSEMEVENTINTERNAL *pThis = EventSem; 110 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC, 111 111 VERR_INVALID_HANDLE); 112 112 … … 114 114 * Invalidate the semaphore and wake up anyone waiting on it. 115 115 */ 116 ASMAtomicXchgSize(&p IntEventSem->iMagic, RTSEMEVENT_MAGIC + 1);117 if (ASMAtomicXchgS32(&p IntEventSem->cWaiters, INT32_MIN / 2) > 0)118 { 119 sys_futex(&p IntEventSem->cWaiters, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);116 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC + 1); 117 if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0) 118 { 119 sys_futex(&pThis->cWaiters, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); 120 120 usleep(1000); 121 121 } … … 124 124 * Free the semaphore memory and be gone. 125 125 */ 126 RTMemFree(p IntEventSem);126 RTMemFree(pThis); 127 127 return VINF_SUCCESS; 128 128 } … … 134 134 * Validate input. 135 135 */ 136 struct RTSEMEVENTINTERNAL *p IntEventSem= EventSem;137 AssertReturn(VALID_PTR(p IntEventSem) && pIntEventSem->iMagic == RTSEMEVENT_MAGIC,136 struct RTSEMEVENTINTERNAL *pThis = EventSem; 137 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC, 138 138 VERR_INVALID_HANDLE); 139 139 /* … … 142 142 for (unsigned i = 0;; i++) 143 143 { 144 int32_t iCur = p IntEventSem->cWaiters;144 int32_t iCur = pThis->cWaiters; 145 145 if (iCur == 0) 146 146 { 147 if (ASMAtomicCmpXchgS32(&p IntEventSem->cWaiters, -1, 0))147 if (ASMAtomicCmpXchgS32(&pThis->cWaiters, -1, 0)) 148 148 break; /* nobody is waiting */ 149 149 } … … 153 153 { 154 154 /* somebody is waiting, try wake up one of them. */ 155 long cWoken = sys_futex(&p IntEventSem->cWaiters, FUTEX_WAKE, 1, NULL, NULL, 0);155 long cWoken = sys_futex(&pThis->cWaiters, FUTEX_WAKE, 1, NULL, NULL, 0); 156 156 if (RT_LIKELY(cWoken == 1)) 157 157 { 158 ASMAtomicDecS32(&p IntEventSem->cWaiters);158 ASMAtomicDecS32(&pThis->cWaiters); 159 159 break; 160 160 } … … 180 180 pthread_yield(); 181 181 else 182 AssertReleaseMsg(i < 4096, ("iCur=%#x p IntEventSem=%p\n", iCur, pIntEventSem));182 AssertReleaseMsg(i < 4096, ("iCur=%#x pThis=%p\n", iCur, pThis)); 183 183 } 184 184 } … … 193 193 * Validate input. 194 194 */ 195 struct RTSEMEVENTINTERNAL *p IntEventSem= EventSem;196 AssertReturn(VALID_PTR(p IntEventSem) && pIntEventSem->iMagic == RTSEMEVENT_MAGIC,195 struct RTSEMEVENTINTERNAL *pThis = EventSem; 196 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC, 197 197 VERR_INVALID_HANDLE); 198 198 … … 200 200 * Quickly check whether it's signaled. 201 201 */ 202 if (ASMAtomicCmpXchgS32(&p IntEventSem->cWaiters, 0, -1))202 if (ASMAtomicCmpXchgS32(&pThis->cWaiters, 0, -1)) 203 203 return VINF_SUCCESS; 204 204 … … 223 223 * Announce that we're among the waiters. 224 224 */ 225 int32_t iNew = ASMAtomicIncS32(&p IntEventSem->cWaiters);225 int32_t iNew = ASMAtomicIncS32(&pThis->cWaiters); 226 226 if (iNew == 0) 227 227 return VINF_SUCCESS; … … 231 231 * Go to sleep. 232 232 */ 233 long rc = sys_futex(&p IntEventSem->cWaiters, FUTEX_WAIT, iNew, pTimeout, NULL, 0);234 if (RT_UNLIKELY(p IntEventSem->iMagic != RTSEMEVENT_MAGIC))233 long rc = sys_futex(&pThis->cWaiters, FUTEX_WAIT, iNew, pTimeout, NULL, 0); 234 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC)) 235 235 return VERR_SEM_DESTROYED; 236 236 … … 240 240 241 241 /* No, then the kernel woke us up or we failed going to sleep. Adjust the accounting. */ 242 iNew = ASMAtomicDecS32(&p IntEventSem->cWaiters);242 iNew = ASMAtomicDecS32(&pThis->cWaiters); 243 243 Assert(iNew >= 0); 244 244 … … 268 268 { 269 269 /* this can't happen. */ 270 if (RT_UNLIKELY(p IntEventSem->iMagic != RTSEMEVENT_MAGIC))270 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC)) 271 271 return VERR_SEM_DESTROYED; 272 272 AssertReleaseMsgFailed(("iNew=%d\n", iNew)); -
trunk/src/VBox/Runtime/r3/linux/semeventmulti-linux.cpp
r6738 r6747 89 89 * Allocate semaphore handle. 90 90 */ 91 struct RTSEMEVENTMULTIINTERNAL *p IntEventMultiSem= (struct RTSEMEVENTMULTIINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTMULTIINTERNAL));92 if (p IntEventMultiSem)93 { 94 p IntEventMultiSem->iMagic = RTSEMEVENTMULTI_MAGIC;95 p IntEventMultiSem->iState = 0;96 *pEventMultiSem = p IntEventMultiSem;91 struct RTSEMEVENTMULTIINTERNAL *pThis = (struct RTSEMEVENTMULTIINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTMULTIINTERNAL)); 92 if (pThis) 93 { 94 pThis->iMagic = RTSEMEVENTMULTI_MAGIC; 95 pThis->iState = 0; 96 *pEventMultiSem = pThis; 97 97 return VINF_SUCCESS; 98 98 } … … 106 106 * Validate input. 107 107 */ 108 struct RTSEMEVENTMULTIINTERNAL *p IntEventMultiSem= EventMultiSem;109 AssertReturn(VALID_PTR(p IntEventMultiSem) && pIntEventMultiSem->iMagic == RTSEMEVENTMULTI_MAGIC,108 struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem; 109 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC, 110 110 VERR_INVALID_HANDLE); 111 111 … … 113 113 * Invalidate the semaphore and wake up anyone waiting on it. 114 114 */ 115 ASMAtomicXchgSize(&p IntEventMultiSem->iMagic, RTSEMEVENTMULTI_MAGIC + 1);116 if (ASMAtomicXchgS32(&p IntEventMultiSem->iState, -1) == 1)117 { 118 sys_futex(&p IntEventMultiSem->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);115 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENTMULTI_MAGIC + 1); 116 if (ASMAtomicXchgS32(&pThis->iState, -1) == 1) 117 { 118 sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); 119 119 usleep(1000); 120 120 } … … 123 123 * Free the semaphore memory and be gone. 124 124 */ 125 RTMemFree(p IntEventMultiSem);125 RTMemFree(pThis); 126 126 return VINF_SUCCESS; 127 127 } … … 133 133 * Validate input. 134 134 */ 135 struct RTSEMEVENTMULTIINTERNAL *p IntEventMultiSem= EventMultiSem;136 AssertReturn(VALID_PTR(p IntEventMultiSem) && pIntEventMultiSem->iMagic == RTSEMEVENTMULTI_MAGIC,135 struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem; 136 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC, 137 137 VERR_INVALID_HANDLE); 138 138 /* 139 139 * Signal it. 140 140 */ 141 int32_t iOld = ASMAtomicXchgS32(&p IntEventMultiSem->iState, -1);141 int32_t iOld = ASMAtomicXchgS32(&pThis->iState, -1); 142 142 if (iOld > 0) 143 143 { 144 144 /* wake up sleeping threads. */ 145 long cWoken = sys_futex(&p IntEventMultiSem->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);145 long cWoken = sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); 146 146 AssertMsg(cWoken >= 0, ("%ld\n", cWoken)); NOREF(cWoken); 147 147 } … … 156 156 * Validate input. 157 157 */ 158 struct RTSEMEVENTMULTIINTERNAL *p IntEventMultiSem= EventMultiSem;159 AssertReturn(VALID_PTR(p IntEventMultiSem) && pIntEventMultiSem->iMagic == RTSEMEVENTMULTI_MAGIC,158 struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem; 159 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC, 160 160 VERR_INVALID_HANDLE); 161 161 #ifdef RT_STRICT 162 int32_t i = p IntEventMultiSem->iState;162 int32_t i = pThis->iState; 163 163 Assert(i == 0 || i == -1 || i == 1); 164 164 #endif … … 167 167 * Reset it. 168 168 */ 169 ASMAtomicCmpXchgS32(&p IntEventMultiSem->iState, 0, -1);169 ASMAtomicCmpXchgS32(&pThis->iState, 0, -1); 170 170 return VINF_SUCCESS; 171 171 } … … 177 177 * Validate input. 178 178 */ 179 struct RTSEMEVENTMULTIINTERNAL *p IntEventMultiSem= EventMultiSem;180 AssertReturn(VALID_PTR(p IntEventMultiSem) && pIntEventMultiSem->iMagic == RTSEMEVENTMULTI_MAGIC,179 struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem; 180 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC, 181 181 VERR_INVALID_HANDLE); 182 182 … … 184 184 * Quickly check whether it's signaled. 185 185 */ 186 int32_t iCur = p IntEventMultiSem->iState;186 int32_t iCur = pThis->iState; 187 187 Assert(iCur == 0 || iCur == -1 || iCur == 1); 188 188 if (iCur == -1) … … 212 212 * threads waiting on the semaphore to keep things simple. 213 213 */ 214 iCur = p IntEventMultiSem->iState;214 iCur = pThis->iState; 215 215 Assert(iCur == 0 || iCur == -1 || iCur == 1); 216 216 if ( iCur == 1 217 || ASMAtomicCmpXchgS32(&p IntEventMultiSem->iState, 1, 0))217 || ASMAtomicCmpXchgS32(&pThis->iState, 1, 0)) 218 218 { 219 long rc = sys_futex(&p IntEventMultiSem->iState, FUTEX_WAIT, 1, pTimeout, NULL, 0);220 if (RT_UNLIKELY(p IntEventMultiSem->iMagic != RTSEMEVENTMULTI_MAGIC))219 long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 1, pTimeout, NULL, 0); 220 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENTMULTI_MAGIC)) 221 221 return VERR_SEM_DESTROYED; 222 222 if (rc == 0) -
trunk/src/VBox/Runtime/r3/linux/semmutex-linux.cpp
r6746 r6747 93 93 * Allocate semaphore handle. 94 94 */ 95 struct RTSEMMUTEXINTERNAL *p IntMutexSem= (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));96 if (p IntMutexSem)97 { 98 p IntMutexSem->iMagic = RTSEMMUTEX_MAGIC;99 p IntMutexSem->iState = 0;100 p IntMutexSem->Owner = (pthread_t)~0;101 p IntMutexSem->cNesting = 0;102 103 *pMutexSem = p IntMutexSem;95 struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL)); 96 if (pThis) 97 { 98 pThis->iMagic = RTSEMMUTEX_MAGIC; 99 pThis->iState = 0; 100 pThis->Owner = (pthread_t)~0; 101 pThis->cNesting = 0; 102 103 *pMutexSem = pThis; 104 104 return VINF_SUCCESS; 105 105 } … … 116 116 if (MutexSem == NIL_RTSEMMUTEX) 117 117 return VERR_INVALID_HANDLE; 118 struct RTSEMMUTEXINTERNAL *p IntMutexSem= MutexSem;119 AssertPtrReturn(p IntMutexSem, VERR_INVALID_HANDLE);120 AssertMsgReturn(p IntMutexSem->iMagic == RTSEMMUTEX_MAGIC,121 ("MutexSem=%p iMagic=%#x\n", p IntMutexSem, pIntMutexSem->iMagic),118 struct RTSEMMUTEXINTERNAL *pThis = MutexSem; 119 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 120 AssertMsgReturn(pThis->iMagic == RTSEMMUTEX_MAGIC, 121 ("MutexSem=%p iMagic=%#x\n", pThis, pThis->iMagic), 122 122 VERR_INVALID_HANDLE); 123 123 … … 125 125 * Invalidate the semaphore and wake up anyone waiting on it. 126 126 */ 127 ASMAtomicXchgSize(&p IntMutexSem->iMagic, RTSEMMUTEX_MAGIC + 1);128 if (ASMAtomicXchgS32(&p IntMutexSem->iState, 0) > 0)129 { 130 sys_futex(&p IntMutexSem->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);127 ASMAtomicXchgSize(&pThis->iMagic, RTSEMMUTEX_MAGIC + 1); 128 if (ASMAtomicXchgS32(&pThis->iState, 0) > 0) 129 { 130 sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); 131 131 usleep(1000); 132 132 } 133 p IntMutexSem->Owner = (pthread_t)~0;134 p IntMutexSem->cNesting = 0;133 pThis->Owner = (pthread_t)~0; 134 pThis->cNesting = 0; 135 135 136 136 /* 137 137 * Free the semaphore memory and be gone. 138 138 */ 139 RTMemFree(p IntMutexSem);139 RTMemFree(pThis); 140 140 return VINF_SUCCESS; 141 141 } … … 147 147 * Validate input. 148 148 */ 149 struct RTSEMMUTEXINTERNAL *p IntMutexSem= MutexSem;150 AssertPtrReturn(p IntMutexSem, VERR_INVALID_HANDLE);151 AssertMsgReturn(p IntMutexSem->iMagic == RTSEMMUTEX_MAGIC,152 ("MutexSem=%p iMagic=%#x\n", p IntMutexSem, pIntMutexSem->iMagic),149 struct RTSEMMUTEXINTERNAL *pThis = MutexSem; 150 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 151 AssertMsgReturn(pThis->iMagic == RTSEMMUTEX_MAGIC, 152 ("MutexSem=%p iMagic=%#x\n", pThis, pThis->iMagic), 153 153 VERR_INVALID_HANDLE); 154 154 … … 157 157 */ 158 158 pthread_t Self = pthread_self(); 159 if ( p IntMutexSem->Owner == Self160 && p IntMutexSem->cNesting > 0)161 { 162 p IntMutexSem->cNesting++;159 if ( pThis->Owner == Self 160 && pThis->cNesting > 0) 161 { 162 pThis->cNesting++; 163 163 return VINF_SUCCESS; 164 164 } … … 179 179 * Lock the mutex. 180 180 */ 181 if (RT_UNLIKELY(!ASMAtomicCmpXchgS32(&p IntMutexSem->iState, 1, 0)))181 if (RT_UNLIKELY(!ASMAtomicCmpXchgS32(&pThis->iState, 1, 0))) 182 182 { 183 183 for (;;) 184 184 { 185 int32_t iOld = ASMAtomicXchgS32(&p IntMutexSem->iState, 2);185 int32_t iOld = ASMAtomicXchgS32(&pThis->iState, 2); 186 186 187 187 /* … … 194 194 * Go to sleep. 195 195 */ 196 long rc = sys_futex(&p IntMutexSem->iState, FUTEX_WAIT, 2, pTimeout, NULL, 0);197 if (RT_UNLIKELY(p IntMutexSem->iMagic != RTSEMMUTEX_MAGIC))196 long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 2, pTimeout, NULL, 0); 197 if (RT_UNLIKELY(pThis->iMagic != RTSEMMUTEX_MAGIC)) 198 198 return VERR_SEM_DESTROYED; 199 199 … … 236 236 * Set the owner and nesting. 237 237 */ 238 p IntMutexSem->Owner = Self;239 ASMAtomicXchgU32(&p IntMutexSem->cNesting, 1);238 pThis->Owner = Self; 239 ASMAtomicXchgU32(&pThis->cNesting, 1); 240 240 return VINF_SUCCESS; 241 241 } … … 261 261 * Validate input. 262 262 */ 263 struct RTSEMMUTEXINTERNAL *p IntMutexSem= MutexSem;264 AssertPtrReturn(p IntMutexSem, VERR_INVALID_HANDLE);265 AssertMsgReturn(p IntMutexSem->iMagic == RTSEMMUTEX_MAGIC,266 ("MutexSem=%p iMagic=%#x\n", p IntMutexSem, pIntMutexSem->iMagic),263 struct RTSEMMUTEXINTERNAL *pThis = MutexSem; 264 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 265 AssertMsgReturn(pThis->iMagic == RTSEMMUTEX_MAGIC, 266 ("MutexSem=%p iMagic=%#x\n", pThis, pThis->iMagic), 267 267 VERR_INVALID_HANDLE); 268 268 … … 271 271 */ 272 272 pthread_t Self = pthread_self(); 273 if (RT_UNLIKELY( p IntMutexSem->Owner != Self274 || p IntMutexSem->cNesting == 0))273 if (RT_UNLIKELY( pThis->Owner != Self 274 || pThis->cNesting == 0)) 275 275 { 276 276 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n", 277 p IntMutexSem, Self, pIntMutexSem->Owner, pIntMutexSem->cNesting));277 pThis, Self, pThis->Owner, pThis->cNesting)); 278 278 return VERR_NOT_OWNER; 279 279 } … … 282 282 * If nested we'll just pop a nesting. 283 283 */ 284 if (p IntMutexSem->cNesting > 1)285 { 286 p IntMutexSem->cNesting--;284 if (pThis->cNesting > 1) 285 { 286 pThis->cNesting--; 287 287 return VINF_SUCCESS; 288 288 } … … 291 291 * Clear the state. (cNesting == 1) 292 292 */ 293 p IntMutexSem->Owner = (pthread_t)~0;294 ASMAtomicXchgU32(&p IntMutexSem->cNesting, 0);293 pThis->Owner = (pthread_t)~0; 294 ASMAtomicXchgU32(&pThis->cNesting, 0); 295 295 296 296 /* 297 297 * Release the mutex. 298 298 */ 299 int32_t iNew = ASMAtomicDecS32(&p IntMutexSem->iState);299 int32_t iNew = ASMAtomicDecS32(&pThis->iState); 300 300 if (iNew != 0) 301 301 { 302 302 /* somebody is waiting, try wake up one of them. */ 303 ASMAtomicXchgS32(&p IntMutexSem->iState, 0);304 (void)sys_futex(&p IntMutexSem->iState, FUTEX_WAKE, 1, NULL, NULL, 0);303 ASMAtomicXchgS32(&pThis->iState, 0); 304 (void)sys_futex(&pThis->iState, FUTEX_WAKE, 1, NULL, NULL, 0); 305 305 } 306 306 return VINF_SUCCESS;
Note:
See TracChangeset
for help on using the changeset viewer.