From f8fe689a81f906d1b91bb3220acde2a4ecb14c5b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 May 2024 05:01:46 +0200 Subject: Adding upstream version 6.0.4-dfsg. Signed-off-by: Daniel Baumann --- .../Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp | 281 +++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp (limited to 'src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp') diff --git a/src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp b/src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp new file mode 100644 index 00000000..c7f8bfbd --- /dev/null +++ b/src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp @@ -0,0 +1,281 @@ +/* $Id: semeventmulti-r0drv-os2.cpp $ */ +/** @file + * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, OS/2. + */ + +/* + * Copyright (c) 2007 knut st. osmundsen + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include "the-os2-kernel.h" +#include "internal/iprt.h" + +#include +#include +#include +#include +#include +#include +#include "internal/magics.h" + + +/********************************************************************************************************************************* +* Structures and Typedefs * +*********************************************************************************************************************************/ +/** + * OS/2 multiple release event semaphore. + */ +typedef struct RTSEMEVENTMULTIINTERNAL +{ + /** Magic value (RTSEMEVENTMULTI_MAGIC). */ + uint32_t volatile u32Magic; + /** The number of waiting threads. */ + uint32_t volatile cWaiters; + /** Set if the event object is signaled. */ + uint8_t volatile fSignaled; + /** The number of threads in the process of waking up. */ + uint32_t volatile cWaking; + /** The OS/2 spinlock protecting this structure. */ + SpinLock_t Spinlock; +} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL; + + +RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem) +{ + return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL); +} + + +RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass, + const char *pszNameFmt, ...) +{ + AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER); + AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER); + + AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *)); + PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis)); + if (pThis) + { + pThis->u32Magic = RTSEMEVENTMULTI_MAGIC; + pThis->cWaiters = 0; + pThis->cWaking = 0; + pThis->fSignaled = 0; + KernAllocSpinLock(&pThis->Spinlock); + + *phEventMultiSem = pThis; + return VINF_SUCCESS; + } + RT_NOREF(hClass, pszNameFmt); + return VERR_NO_MEMORY; +} + + +RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem) +{ + PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem; + if (pThis == NIL_RTSEMEVENTMULTI) + return VINF_SUCCESS; + AssertPtrReturn(pThis, VERR_INVALID_HANDLE); + AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE); + + KernAcquireSpinLock(&pThis->Spinlock); + ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */ + if (pThis->cWaiters > 0) + { + /* abort waiting thread, last man cleans up. */ + ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters); + ULONG cThreads; + KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED); + KernReleaseSpinLock(&pThis->Spinlock); + } + else if (pThis->cWaking) + /* the last waking thread is gonna do the cleanup */ + KernReleaseSpinLock(&pThis->Spinlock); + else + { + KernReleaseSpinLock(&pThis->Spinlock); + KernFreeSpinLock(&pThis->Spinlock); + RTMemFree(pThis); + } + + return VINF_SUCCESS; +} + + +RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem) +{ + PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem; + AssertPtrReturn(pThis, VERR_INVALID_HANDLE); + AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, + ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), + VERR_INVALID_HANDLE); + + KernAcquireSpinLock(&pThis->Spinlock); + + ASMAtomicXchgU8(&pThis->fSignaled, true); + if (pThis->cWaiters > 0) + { + ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters); + ASMAtomicXchgU32(&pThis->cWaiters, 0); + ULONG cThreads; + KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS); + } + + KernReleaseSpinLock(&pThis->Spinlock); + return VINF_SUCCESS; +} + + +RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem) +{ + PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem; + AssertPtrReturn(pThis, VERR_INVALID_HANDLE); + AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, + ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), + VERR_INVALID_HANDLE); + + KernAcquireSpinLock(&pThis->Spinlock); + ASMAtomicXchgU8(&pThis->fSignaled, false); + KernReleaseSpinLock(&pThis->Spinlock); + return VINF_SUCCESS; +} + + +/** + * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug. + * + * @returns VBox status code. + * @param pThis The event semaphore. + * @param fFlags See RTSemEventWaitEx. + * @param uTimeout See RTSemEventWaitEx. + * @param pSrcPos The source code position of the wait. + */ +static int rtR0SemEventMultiOs2Wait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout, + PCRTLOCKVALSRCPOS pSrcPos) +{ + RT_NOREF(pSrcPos); + + /* + * Validate and convert the input. + */ + if (!pThis) + return VERR_INVALID_HANDLE; + AssertPtrReturn(pThis, VERR_INVALID_HANDLE); + AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, + ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), + VERR_INVALID_HANDLE); + AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER); + + ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout); + ULONG fBlock = BLOCK_SPINLOCK; + if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)) + fBlock |= BLOCK_UNINTERRUPTABLE; + + /* + * Do the job. + */ + KernAcquireSpinLock(&pThis->Spinlock); + + int rc; + if (pThis->fSignaled) + rc = VINF_SUCCESS; + else + { + ASMAtomicIncU32(&pThis->cWaiters); + + ULONG ulData = (ULONG)VERR_INTERNAL_ERROR; + rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock, + &pThis->Spinlock, + &ulData); + switch (rc) + { + case NO_ERROR: + rc = (int)ulData; + Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED); + Assert(pThis->cWaking > 0); + if ( !ASMAtomicDecU32(&pThis->cWaking) + && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC) + { + /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as + the last thread do the cleanup. */ + KernReleaseSpinLock(&pThis->Spinlock); + KernFreeSpinLock(&pThis->Spinlock); + RTMemFree(pThis); + return VINF_SUCCESS; + } + rc = VINF_SUCCESS; + break; + + case ERROR_TIMEOUT: + Assert(cMsTimeout != SEM_INDEFINITE_WAIT); + ASMAtomicDecU32(&pThis->cWaiters); + rc = VERR_TIMEOUT; + break; + + case ERROR_INTERRUPT: + Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE); + ASMAtomicDecU32(&pThis->cWaiters); + rc = VERR_INTERRUPTED; + break; + + default: + AssertMsgFailed(("rc=%d\n", rc)); + rc = VERR_GENERAL_FAILURE; + break; + } + } + + KernReleaseSpinLock(&pThis->Spinlock); + return rc; +} + + +RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout) +{ +#ifndef RTSEMEVENT_STRICT + return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, NULL); +#else + RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API(); + return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos); +#endif +} + + +RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout, + RTHCUINTPTR uId, RT_SRC_POS_DECL) +{ + RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API(); + return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos); +} + + +RTDECL(uint32_t) RTSemEventMultiGetResolution(void) +{ + return 32000000; /* 32ms */ +} + -- cgit v1.2.3