sems-win.cpp revision f53321a9e9d78b7745c5792e7f6e7058eb6efd5e
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/* $Id$ */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** @file
0f70ed40798198e1d9099c6ae3bdb239d2b8cf0dvboxsync * IPRT - Semaphores, implementation for Windows host platform.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
0f70ed40798198e1d9099c6ae3bdb239d2b8cf0dvboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
a9749534ba173982f6c3bafe8d51ccd22960e493vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * available from http://www.virtualbox.org. This file is free software;
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * General Public License (GPL) as published by the Free Software
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The contents of this file may alternatively be used under the terms
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * of the Common Development and Distribution License Version 1.0
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * VirtualBox OSE distribution, in which case the provisions of the
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * CDDL are applicable instead of those of the GPL.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * You may elect to license modified versions of this file under the
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * terms and conditions of either the GPL or the CDDL or both.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync *
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * additional information or have any questions.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync */
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync/*******************************************************************************
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync* Header Files *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#define LOG_GROUP RTLOGGROUP_SEMAPHORE
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <Windows.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/semaphore.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/thread.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/assert.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/err.h>
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Defined Constants And Macros *
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync*******************************************************************************/
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync/** @def RTSEMMUTEX_STRICT
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Enables strictness checks and lock accounting.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef RTSEMMUTEX_STRICT
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync# if defined(RT_STRICT) || defined(RT_LOCK_STRICT) || defined(RTSEM_STRICT) || defined(DOXYGEN_RUNNING)
500aaaf3dc1d98456808e7618db3fb2e7c8fb8e0vboxsync# define RTSEMMUTEX_STRICT
8ffcab9595cc0d56977968cd496363502fd814aevboxsync# endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** Converts semaphore to win32 handle. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#define SEM2HND(Sem) ((HANDLE)(uintptr_t)Sem)
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Create the semaphore.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * (Auto reset, not signaled, private event object.)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync HANDLE hev = CreateEvent(NULL, FALSE, FALSE, NULL);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (hev)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *pEventSem = (RTSEMEVENT)(void *)hev;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync return RTErrConvertFromWin32(GetLastError());
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync}
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync{
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync /*
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * Close semaphore handle.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync */
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync if (CloseHandle(SEM2HND(EventSem)))
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync return VINF_SUCCESS;
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync AssertMsgFailed(("Destroy EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync return RTErrConvertFromWin32(GetLastError());
77da7a074c86956d36759983037056c00cb87535vboxsync}
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Wait for condition.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync */
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync int rc = WaitForSingleObjectEx(SEM2HND(EventSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync switch (rc)
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync {
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync case WAIT_OBJECT_0: return VINF_SUCCESS;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync case WAIT_TIMEOUT: return VERR_TIMEOUT;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync default:
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync {
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync AssertMsgFailed(("Wait on EventSem %p failed, rc=%d lasterr=%d\n", EventSem, rc, GetLastError()));
d5b5f09d8841828e647de9da5003fda55ca4cd5evboxsync int rc2 = RTErrConvertFromWin32(GetLastError());
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync if (rc2)
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync return rc2;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync return VERR_INTERNAL_ERROR;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync}
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Signal the object.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (SetEvent(SEM2HND(EventSem)))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Signaling EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Create the semaphore.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * (Manual reset, not signaled, private event object.)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync HANDLE hev = CreateEvent(NULL, TRUE, FALSE, NULL);
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync if (hev)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync *pEventMultiSem = (RTSEMEVENTMULTI)(void *)hev;
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync return VINF_SUCCESS;
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync }
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync return RTErrConvertFromWin32(GetLastError());
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync}
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsyncRTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Close semaphore handle.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (CloseHandle(SEM2HND(EventMultiSem)))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Destroy EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync{
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Signal the object.
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync */
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync if (SetEvent(SEM2HND(EventMultiSem)))
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync return VINF_SUCCESS;
2622c26c6b4105d944a29c5e2c77b6ef26e10101vboxsync AssertMsgFailed(("Signaling EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Reset the object.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (ResetEvent(SEM2HND(EventMultiSem)))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Resetting EventMultiSem %p failed, lasterr=%d\n", EventMultiSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Wait for condition.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc = WaitForSingleObjectEx(SEM2HND(EventMultiSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync switch (rc)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_OBJECT_0: return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_TIMEOUT: return VERR_TIMEOUT;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync default:
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Wait on EventMultiSem %p failed, rc=%d lasterr=%d\n", EventMultiSem, rc, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc2 = RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (rc2)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return rc2;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VERR_INTERNAL_ERROR;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
77da7a074c86956d36759983037056c00cb87535vboxsync
77da7a074c86956d36759983037056c00cb87535vboxsync
77da7a074c86956d36759983037056c00cb87535vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Create the semaphore.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync HANDLE hmtx = CreateMutex(NULL, FALSE, NULL);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (hmtx)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
8ffcab9595cc0d56977968cd496363502fd814aevboxsync *pMutexSem = (RTSEMMUTEX)(void *)hmtx;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
7f67048412d241d45c0835b9c403a5bb1c879030vboxsync
8ffcab9595cc0d56977968cd496363502fd814aevboxsync return RTErrConvertFromWin32(GetLastError());
8ffcab9595cc0d56977968cd496363502fd814aevboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync /*
7f67048412d241d45c0835b9c403a5bb1c879030vboxsync * Close semaphore handle.
7f67048412d241d45c0835b9c403a5bb1c879030vboxsync */
7f67048412d241d45c0835b9c403a5bb1c879030vboxsync if (CloseHandle(SEM2HND(MutexSem)))
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync return VINF_SUCCESS;
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync AssertMsgFailed(("Destroy MutexSem %p failed, lasterr=%d\n", MutexSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
e2843ed205192b88e54eef60ad541d00bbbc932avboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync{
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync /*
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * Lock mutex semaphore.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync */
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync int rc = WaitForSingleObjectEx(SEM2HND(MutexSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync switch (rc)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_OBJECT_0:
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync#ifdef RTSEMMUTEX_STRICT
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTTHREAD Thread = RTThreadSelf();
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (Thread != NIL_RTTHREAD)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadWriteLockInc(Thread);
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_TIMEOUT: return VERR_TIMEOUT;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync default:
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc2 = RTErrConvertFromWin32(GetLastError());
8a8d7629deae8875b70c6899e8b0f683b2a543e1vboxsync if (rc2 != 0)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return rc2;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VERR_INTERNAL_ERROR;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
8a8d7629deae8875b70c6899e8b0f683b2a543e1vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncRTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Unlock mutex semaphore.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef RTSEMMUTEX_STRICT
8a8d7629deae8875b70c6899e8b0f683b2a543e1vboxsync RTTHREAD Thread = RTThreadSelf();
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (Thread != NIL_RTTHREAD)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadWriteLockDec(Thread);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (ReleaseMutex(SEM2HND(MutexSem)))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return VINF_SUCCESS;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef RTSEMMUTEX_STRICT
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (Thread != NIL_RTTHREAD)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadWriteLockInc(Thread);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("Release MutexSem %p failed, lasterr=%d\n", MutexSem, GetLastError()));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTErrConvertFromWin32(GetLastError());
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync