1 | /* $Id: rtSemWaitOs2ConvertTimeout.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSemEventMultiWait, implementation based on RTSemEventMultiWaitEx.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/semaphore.h>
|
---|
43 | #include <iprt/time.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /** Too lazy to include the right OS/2 header, duplicating the define we
|
---|
50 | * need here. */
|
---|
51 | #define MY_SEM_INDEFINITE_WAIT UINT32_MAX
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Converts the timeout to a millisecond value that can be fed to KernBlock.
|
---|
56 | *
|
---|
57 | * @returns Relative timeout in milliseconds.
|
---|
58 | * @param fFlags The semaphore wait flags.
|
---|
59 | * @param uTimeout The timeout value.
|
---|
60 | */
|
---|
61 | uint32_t rtR0SemWaitOs2ConvertTimeout(uint32_t fFlags, uint64_t uTimeout)
|
---|
62 | {
|
---|
63 | /*
|
---|
64 | * Simple & common cases.
|
---|
65 | */
|
---|
66 | if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
67 | return MY_SEM_INDEFINITE_WAIT;
|
---|
68 |
|
---|
69 | if ( (fFlags & (RTSEMWAIT_FLAGS_MILLISECS | RTSEMWAIT_FLAGS_ABSOLUTE))
|
---|
70 | == RTSEMWAIT_FLAGS_MILLISECS)
|
---|
71 | {
|
---|
72 | if (uTimeout < UINT32_MAX)
|
---|
73 | return (uint32_t)uTimeout;
|
---|
74 | return MY_SEM_INDEFINITE_WAIT;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (!uTimeout)
|
---|
78 | return 0;
|
---|
79 |
|
---|
80 | if (uTimeout == UINT64_MAX)
|
---|
81 | return MY_SEM_INDEFINITE_WAIT;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * For the more complicated cases (nano or/and abs), convert thru nano (lazy bird).
|
---|
85 | */
|
---|
86 | if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
87 | {
|
---|
88 | if (uTimeout >= UINT64_MAX / RT_NS_1MS * RT_NS_1MS)
|
---|
89 | return MY_SEM_INDEFINITE_WAIT;
|
---|
90 | uTimeout = uTimeout * RT_NS_1MS;
|
---|
91 | }
|
---|
92 | if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
|
---|
93 | {
|
---|
94 | uint64_t u64Now = RTTimeSystemNanoTS();
|
---|
95 | if (u64Now >= uTimeout)
|
---|
96 | return 0;
|
---|
97 | uTimeout = uTimeout - u64Now;
|
---|
98 | }
|
---|
99 | uTimeout /= RT_NS_1MS;
|
---|
100 | if (uTimeout >= UINT32_MAX)
|
---|
101 | return MY_SEM_INDEFINITE_WAIT;
|
---|
102 | return (uint32_t)uTimeout;
|
---|
103 | }
|
---|
104 |
|
---|