semeventmulti-r0drv-os2.cpp revision c97989161fbe75bc14cea477a5443bbf474dd3ad
c97989161fbe75bc14cea477a5443bbf474dd3advboxsync * innotek Portable Runtime - Multiple Release Event Semaphores, Ring-0 Driver, OS/2.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Permission is hereby granted, free of charge, to any person
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * obtaining a copy of this software and associated documentation
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * files (the "Software"), to deal in the Software without
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * restriction, including without limitation the rights to use,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * copies of the Software, and to permit persons to whom the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Software is furnished to do so, subject to the following
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * conditions:
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * The above copyright notice and this permission notice shall be
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * included in all copies or substantial portions of the Software.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * OTHER DEALINGS IN THE SOFTWARE.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Header Files *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Structures and Typedefs *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * OS/2 multiple release event semaphore.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Magic value (RTSEMEVENTMULTI_MAGIC). */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** The number of waiting threads. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Set if the event object is signaled. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** The number of threads in the process of waking up. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** The OS/2 spinlock protecting this structure. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pEventMultiInt));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ASMAtomicIncU32(&pEventMultiInt->u32Magic); /* make the handle invalid */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* abort waiting thread, last man cleans up. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* the last waking thread is gonna do the cleanup */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync BLOCK_SPINLOCK | (fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync && pEventMultiInt->u32Magic != RTSEMEVENTMULTI_MAGIC)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync the last thread do the cleanup. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptable */);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncRTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptable */);