ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * IPRT - Event Semaphore, Windows.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2010 Oracle Corporation
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * available from http://www.virtualbox.org. This file is free software;
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * General Public License (GPL) as published by the Free Software
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * The contents of this file may alternatively be used under the terms
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * of the Common Development and Distribution License Version 1.0
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * CDDL are applicable instead of those of the GPL.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * You may elect to license modified versions of this file under the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * terms and conditions of either the GPL or the CDDL or both.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync/*******************************************************************************
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync* Header Files *
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync*******************************************************************************/
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync/*******************************************************************************
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync* Defined Constants And Macros *
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync*******************************************************************************/
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync /** Magic value (RTSEMEVENT_MAGIC). */
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync /** The event handle. */
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync /** Signallers. */
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync /** Indicates that lock validation should be performed. */
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync /** The creation flags. */
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(*pThis));
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync pThis = (struct RTSEMEVENTINTERNAL *)rtMemBaseAlloc(sizeof(*pThis));
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Create the semaphore.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * (Auto reset, not signaled, private event object.)
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync pThis->hev = CreateEvent(NULL, FALSE, FALSE, NULL);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync if (pThis->hev != NULL) /* not INVALID_HANDLE_VALUE */
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync RTLockValidatorRecSharedInit(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync "RTSemEvent-%u", ASMAtomicIncU32(&s_iSemEventAnon) - 1);
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync RTLockValidatorRecSharedInitV(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync * Invalidate the handle and close the semaphore.
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC, RTSEMEVENT_MAGIC), VERR_INVALID_HANDLE);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync RTLockValidatorRecSharedDelete(&pThis->Signallers);
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync if (!(pThis->fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK))
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgFailed(("Destroy hEventSem %p failed, lasterr=%u (%Rrc)\n", pThis, dwErr, rc));
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync /* Leak it. */
0af12d8c60220c658a59ecf229fc65cdf8db2558vboxsync * Validate input.
0af12d8c60220c658a59ecf229fc65cdf8db2558vboxsync AssertReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
0af12d8c60220c658a59ecf229fc65cdf8db2558vboxsync int rc9 = RTLockValidatorRecSharedCheckSignaller(&pThis->Signallers, NIL_RTTHREAD);
0af12d8c60220c658a59ecf229fc65cdf8db2558vboxsync * Signal the object.
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgFailed(("Signaling hEventSem %p failed, lasterr=%d\n", pThis, dwErr));
3e39de9ed8b88fa8907468a77bef44d10049b12avboxsync/** Goto avoidance. */
3e39de9ed8b88fa8907468a77bef44d10049b12avboxsyncDECL_FORCE_INLINE(int) rtSemEventWaitHandleStatus(struct RTSEMEVENTINTERNAL *pThis, DWORD rc)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgFailed(("Wait on hEventSem %p failed, rc=%d lasterr=%d\n", pThis, rc, GetLastError()));
3e39de9ed8b88fa8907468a77bef44d10049b12avboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync * Validate input.
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Wait for condition.
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync RTTHREAD hThreadSelf = !(pThis->fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)
3e39de9ed8b88fa8907468a77bef44d10049b12avboxsync 0 /*Timeout*/,
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync int rc9 = RTLockValidatorRecSharedCheckBlocking(&pThis->Signallers, hThreadSelf, pSrcPos, false,
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_EVENT, true);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_EVENT);
0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7vboxsyncRTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturnVoid(pThis->u32Magic == RTSEMEVENT_MAGIC);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync RTLockValidatorRecSharedResetOwner(&pThis->Signallers, hThread, NULL);
0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7vboxsyncRTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturnVoid(pThis->u32Magic == RTSEMEVENT_MAGIC);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync RTLockValidatorRecSharedAddOwner(&pThis->Signallers, hThread, NULL);
333e7ab9cd83b32080826d06ac7b1951c684ccb5vboxsyncRTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
a1e3db9f69723048cb20f6bc815ec5fb706d8705vboxsync AssertReturnVoid(pThis->u32Magic == RTSEMEVENT_MAGIC);