Changeset 25717 in vbox for trunk/src/VBox/Runtime/r0drv/freebsd
- Timestamp:
- Jan 11, 2010 1:24:09 PM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 56457
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r0drv/freebsd/semevent-r0drv-freebsd.c
r22819 r25717 65 65 66 66 67 RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem) 68 { 69 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *)); 70 AssertPtrReturn(pEventSem, VERR_INVALID_POINTER); 71 72 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pEventInt)); 73 if (pEventInt) 74 { 75 pEventInt->u32Magic = RTSEMEVENT_MAGIC; 76 pEventInt->cWaiters = 0; 77 pEventInt->cWaking = 0; 78 pEventInt->fSignaled = 0; 79 int rc = RTSpinlockCreate(&pEventInt->hSpinLock); 80 if (RT_SUCCESS(rc)) 81 { 82 *pEventSem = pEventInt; 83 return VINF_SUCCESS; 84 } 85 86 RTMemFree(pEventInt); 87 return rc; 88 } 89 return VERR_NO_MEMORY; 90 } 91 92 93 RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem) 94 { 95 if (EventSem == NIL_RTSEMEVENT) /* don't bitch */ 96 return VERR_INVALID_HANDLE; 97 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem; 98 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER; 99 100 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE); 101 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC, 102 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic), 67 RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem) 68 { 69 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL); 70 } 71 72 73 RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...) 74 { 75 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *)); 76 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER); 77 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER); 78 79 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pThis)); 80 if (!pThis) 81 return VERR_NO_MEMORY; 82 83 pThis->u32Magic = RTSEMEVENT_MAGIC; 84 pThis->cWaiters = 0; 85 pThis->cWaking = 0; 86 pThis->fSignaled = 0; 87 int rc = RTSpinlockCreate(&pThis->hSpinLock); 88 if (RT_SUCCESS(rc)) 89 { 90 *phEventSem = pThis; 91 return VINF_SUCCESS; 92 } 93 94 RTMemFree(pThis); 95 return rc; 96 } 97 98 99 RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem) 100 { 101 PRTSEMEVENTINTERNAL pThis = hEventSem; 102 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER; 103 104 if (pThis == NIL_RTSEMEVENT) 105 return VINF_SUCCESS; 106 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 107 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE); 108 109 RTSpinlockAcquire(pThis->hSpinLock, &Tmp); 110 111 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */ 112 if (pThis->cWaiters > 0) 113 { 114 /* abort waiting thread, last man cleans up. */ 115 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters); 116 sleepq_lock(pThis); 117 sleepq_broadcast(pThis, SLEEPQ_CONDVAR, 0, 0); 118 sleepq_release(pThis); 119 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 120 } 121 else if (pThis->cWaking) 122 /* the last waking thread is gonna do the cleanup */ 123 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 124 else 125 { 126 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 127 RTSpinlockDestroy(pThis->hSpinLock); 128 RTMemFree(pThis); 129 } 130 131 return VINF_SUCCESS; 132 } 133 134 135 RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem) 136 { 137 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER; 138 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem; 139 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 140 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, 141 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), 103 142 VERR_INVALID_HANDLE); 104 143 105 RTSpinlockAcquire(pEventInt->hSpinLock, &Tmp); 106 107 ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */ 108 if (pEventInt->cWaiters > 0) 109 { 110 /* abort waiting thread, last man cleans up. */ 111 ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters); 112 sleepq_lock(pEventInt); 113 sleepq_broadcast(pEventInt, SLEEPQ_CONDVAR, 0, 0); 114 sleepq_release(pEventInt); 115 RTSpinlockRelease(pEventInt->hSpinLock, &Tmp); 116 } 117 else if (pEventInt->cWaking) 118 /* the last waking thread is gonna do the cleanup */ 119 RTSpinlockRelease(pEventInt->hSpinLock, &Tmp); 120 else 121 { 122 RTSpinlockRelease(pEventInt->hSpinLock, &Tmp); 123 RTSpinlockDestroy(pEventInt->hSpinLock); 124 RTMemFree(pEventInt); 125 } 126 127 return VINF_SUCCESS; 128 } 129 130 131 RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem) 132 { 133 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER; 134 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem; 135 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE); 136 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC, 137 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic), 138 VERR_INVALID_HANDLE); 139 140 RTSpinlockAcquire(pEventInt->hSpinLock, &Tmp); 141 142 if (pEventInt->cWaiters > 0) 143 { 144 ASMAtomicDecU32(&pEventInt->cWaiters); 145 ASMAtomicIncU32(&pEventInt->cWaking); 146 sleepq_lock(pEventInt); 147 int fWakeupSwapProc = sleepq_signal(pEventInt, SLEEPQ_CONDVAR, 0, 0); 148 sleepq_release(pEventInt); 144 RTSpinlockAcquire(pThis->hSpinLock, &Tmp); 145 146 if (pThis->cWaiters > 0) 147 { 148 ASMAtomicDecU32(&pThis->cWaiters); 149 ASMAtomicIncU32(&pThis->cWaking); 150 sleepq_lock(pThis); 151 int fWakeupSwapProc = sleepq_signal(pThis, SLEEPQ_CONDVAR, 0, 0); 152 sleepq_release(pThis); 149 153 if (fWakeupSwapProc) 150 154 kick_proc0(); 151 155 } 152 156 else 153 ASMAtomicXchgU8(&p EventInt->fSignaled, true);154 155 RTSpinlockRelease(p EventInt->hSpinLock, &Tmp);157 ASMAtomicXchgU8(&pThis->fSignaled, true); 158 159 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 156 160 return VINF_SUCCESS; 157 161 } 158 162 159 163 160 static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)164 static int rtSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies, bool fInterruptible) 161 165 { 162 166 int rc; 163 167 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER; 164 PRTSEMEVENTINTERNAL p EventInt = (PRTSEMEVENTINTERNAL)EventSem;165 AssertPtrReturn(p EventInt, VERR_INVALID_HANDLE);166 AssertMsgReturn(p EventInt->u32Magic == RTSEMEVENT_MAGIC,167 ("p EventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),168 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem; 169 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 170 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, 171 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), 168 172 VERR_INVALID_HANDLE); 169 173 170 RTSpinlockAcquire(p EventInt->hSpinLock, &Tmp);171 172 if (p EventInt->fSignaled)173 { 174 Assert(!p EventInt->cWaiters);175 ASMAtomicXchgU8(&p EventInt->fSignaled, false);174 RTSpinlockAcquire(pThis->hSpinLock, &Tmp); 175 176 if (pThis->fSignaled) 177 { 178 Assert(!pThis->cWaiters); 179 ASMAtomicXchgU8(&pThis->fSignaled, false); 176 180 rc = VINF_SUCCESS; 177 181 } … … 182 186 else 183 187 { 184 ASMAtomicIncU32(&p EventInt->cWaiters);188 ASMAtomicIncU32(&pThis->cWaiters); 185 189 186 190 int fFlags = SLEEPQ_CONDVAR; … … 189 193 fFlags |= SLEEPQ_INTERRUPTIBLE; 190 194 191 sleepq_lock(p EventInt);192 sleepq_add(p EventInt, NULL, "IPRT Event Semaphore", fFlags, 0);195 sleepq_lock(pThis); 196 sleepq_add(pThis, NULL, "IPRT Event Semaphore", fFlags, 0); 193 197 194 198 if (cMillies != RT_INDEFINITE_WAIT) … … 202 206 tv.tv_usec = (cMillies % 1000) * 1000; 203 207 204 sleepq_set_timeout(p EventInt, tvtohz(&tv));205 206 RTSpinlockRelease(p EventInt->hSpinLock, &Tmp);208 sleepq_set_timeout(pThis, tvtohz(&tv)); 209 210 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 207 211 208 212 if (fInterruptible) 209 rc = SLEEPQ_TIMEDWAIT_SIG(p EventInt);213 rc = SLEEPQ_TIMEDWAIT_SIG(pThis); 210 214 else 211 rc = SLEEPQ_TIMEDWAIT(p EventInt);215 rc = SLEEPQ_TIMEDWAIT(pThis); 212 216 } 213 217 else 214 218 { 215 RTSpinlockRelease(p EventInt->hSpinLock, &Tmp);219 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 216 220 217 221 if (fInterruptible) 218 rc = SLEEPQ_WAIT_SIG(p EventInt);222 rc = SLEEPQ_WAIT_SIG(pThis); 219 223 else 220 224 { 221 225 rc = 0; 222 SLEEPQ_WAIT(p EventInt);226 SLEEPQ_WAIT(pThis); 223 227 } 224 228 } 225 229 226 RTSpinlockAcquire(p EventInt->hSpinLock, &Tmp);230 RTSpinlockAcquire(pThis->hSpinLock, &Tmp); 227 231 228 232 switch (rc) 229 233 { 230 234 case 0: 231 if (p EventInt->u32Magic == RTSEMEVENT_MAGIC)235 if (pThis->u32Magic == RTSEMEVENT_MAGIC) 232 236 { 233 ASMAtomicDecU32(&p EventInt->cWaking);237 ASMAtomicDecU32(&pThis->cWaking); 234 238 rc = VINF_SUCCESS; 235 239 } … … 238 242 rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've 239 243 * could've woken up just before destruction... */ 240 if (!ASMAtomicDecU32(&p EventInt->cWaking))244 if (!ASMAtomicDecU32(&pThis->cWaking)) 241 245 { 242 246 /* The event was destroyed, as the last thread do the cleanup. 243 247 we don't actually know whether */ 244 RTSpinlockRelease(p EventInt->hSpinLock, &Tmp);245 RTSpinlockDestroy(p EventInt->hSpinLock);246 RTMemFree(p EventInt);248 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 249 RTSpinlockDestroy(pThis->hSpinLock); 250 RTMemFree(pThis); 247 251 return rc; 248 252 } … … 252 256 case EWOULDBLOCK: 253 257 Assert(cMillies != RT_INDEFINITE_WAIT); 254 if (p EventInt->cWaiters > 0)255 ASMAtomicDecU32(&p EventInt->cWaiters);258 if (pThis->cWaiters > 0) 259 ASMAtomicDecU32(&pThis->cWaiters); 256 260 rc = VERR_TIMEOUT; 257 261 break; … … 260 264 case ERESTART: 261 265 Assert(fInterruptible); 262 if (p EventInt->cWaiters > 0)263 ASMAtomicDecU32(&p EventInt->cWaiters);266 if (pThis->cWaiters > 0) 267 ASMAtomicDecU32(&pThis->cWaiters); 264 268 rc = VERR_INTERRUPTED; 265 269 break; … … 273 277 } 274 278 275 RTSpinlockRelease(p EventInt->hSpinLock, &Tmp);279 RTSpinlockRelease(pThis->hSpinLock, &Tmp); 276 280 277 281 return rc; … … 279 283 280 284 281 RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)282 { 283 return rtSemEventWait( EventSem, cMillies, false /* not interruptible */);284 } 285 286 287 RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)288 { 289 return rtSemEventWait( EventSem, cMillies, true /* interruptible */);290 } 291 285 RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies) 286 { 287 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */); 288 } 289 290 291 RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, unsigned cMillies) 292 { 293 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */); 294 } 295
Note:
See TracChangeset
for help on using the changeset viewer.