rtSemWaitOs2ConvertTimeout.cpp revision 4eb60291a3736df8e5c706848a0c5dfa75a5814f
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/* $Id$ */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/** @file
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * IPRT - RTSemEventMultiWait, implementation based on RTSemEventMultiWaitEx.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/*
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * Copyright (C) 2010 Oracle Corporation
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * This file is part of VirtualBox Open Source Edition (OSE), as
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * available from http://www.virtualbox.org. This file is free software;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * you can redistribute it and/or modify it under the terms of the GNU
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * General Public License (GPL) as published by the Free Software
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * Foundation, in version 2 as it comes in the "COPYING" file of the
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * The contents of this file may alternatively be used under the terms
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * of the Common Development and Distribution License Version 1.0
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * VirtualBox OSE distribution, in which case the provisions of the
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * CDDL are applicable instead of those of the GPL.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * You may elect to license modified versions of this file under the
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * terms and conditions of either the GPL or the CDDL or both.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/*******************************************************************************
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw* Header Files *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw*******************************************************************************/
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw#include "internal/iprt.h"
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw#include <iprt/semaphore.h>
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw#include <iprt/time.h>
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/*******************************************************************************
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw* Defined Constants And Macros *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw*******************************************************************************/
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/** Too lazy to include the right OS/2 header, duplicating the define we
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * need here. */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw#define MY_SEM_INDEFINITE_WAIT UINT32_MAX
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw/**
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * Converts the timeout to a millisecond value that can be fed to KernBlock.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw *
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * @returns Relative timeout in milliseconds.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * @param fFlags The semaphore wait flags.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * @param uTimeout The timeout value.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xwuint32_t rtR0SemWaitOs2ConvertTimeout(uint32_t fFlags, uint64_t uTimeout)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw{
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw /*
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * Simple & common cases.
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return MY_SEM_INDEFINITE_WAIT;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if ( (fFlags & (RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE))
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw == RTSEMWAIT_FLAGS_MILLISECS)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw {
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (uTimeout < UINT32_MAX)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return (uint32_t)uTimeout;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return MY_SEM_INDEFINITE_WAIT;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw }
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (!uTimeout)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return 0;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (uTimeout == UINT64_MAX)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return MY_SEM_INDEFINITE_WAIT;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw /*
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw * For the more complicated cases (nano or/and abs), convert thru nano (lazy bird).
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw */
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw {
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (uTimeout >= UINT64_MAX / RT_NS_1MS * RT_NS_1MS)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return MY_SEM_INDEFINITE_WAIT;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw uTimeout = uTimeout * RT_NS_1MS;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw }
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw {
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw uint64_t u64Now = RTTimeSystemNanoTS();
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (u64Now >= uTimeout)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return 0;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw uTimeout = uTimeout - u64Now;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw }
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw uTimeout /= RT_NS_1MS;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw if (uTimeout >= UINT32_MAX)
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return MY_SEM_INDEFINITE_WAIT;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw return (uint32_t)uTimeout;
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw}
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw
d39a76e7b087a3d0927cbe6898dc0a6770fa6c68xw