[26703] | 1 | /** @file
|
---|
| 2 | * IPRT - Polling I/O Handles.
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[98103] | 6 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[26703] | 7 | *
|
---|
[96407] | 8 | * This file is part of VirtualBox base platform packages, as
|
---|
| 9 | * available from https://www.virtualbox.org.
|
---|
[26703] | 10 | *
|
---|
[96407] | 11 | * This program is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU General Public License
|
---|
| 13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 14 | * License.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful, but
|
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 23 | *
|
---|
[26703] | 24 | * The contents of this file may alternatively be used under the terms
|
---|
| 25 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[26703] | 28 | * CDDL are applicable instead of those of the GPL.
|
---|
| 29 | *
|
---|
| 30 | * You may elect to license modified versions of this file under the
|
---|
| 31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 32 | *
|
---|
| 33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[26703] | 34 | */
|
---|
| 35 |
|
---|
[76557] | 36 | #ifndef IPRT_INCLUDED_poll_h
|
---|
| 37 | #define IPRT_INCLUDED_poll_h
|
---|
[76507] | 38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 39 | # pragma once
|
---|
| 40 | #endif
|
---|
[26703] | 41 |
|
---|
| 42 | #include <iprt/cdefs.h>
|
---|
| 43 | #include <iprt/types.h>
|
---|
| 44 |
|
---|
| 45 | RT_C_DECLS_BEGIN
|
---|
| 46 |
|
---|
| 47 | /** @defgroup grp_rt_poll RTPoll - Polling I/O Handles
|
---|
| 48 | * @ingroup grp_rt
|
---|
| 49 | * @{
|
---|
| 50 | */
|
---|
| 51 |
|
---|
[26721] | 52 | /** @name Poll events
|
---|
| 53 | * @{ */
|
---|
| 54 | /** Readable without blocking. */
|
---|
[26801] | 55 | #define RTPOLL_EVT_READ RT_BIT_32(0)
|
---|
[26721] | 56 | /** Writable without blocking. */
|
---|
[26801] | 57 | #define RTPOLL_EVT_WRITE RT_BIT_32(1)
|
---|
[26721] | 58 | /** Error condition, hangup, exception or similar. */
|
---|
[26801] | 59 | #define RTPOLL_EVT_ERROR RT_BIT_32(2)
|
---|
| 60 | /** Mask of the valid bits. */
|
---|
| 61 | #define RTPOLL_EVT_VALID_MASK UINT32_C(0x00000007)
|
---|
[26721] | 62 | /** @} */
|
---|
| 63 |
|
---|
[26703] | 64 | /**
|
---|
[33540] | 65 | * Polls on the specified poll set until an event occurs on one of the handles
|
---|
[26721] | 66 | * or the timeout expires.
|
---|
[26703] | 67 | *
|
---|
| 68 | * @returns IPRT status code.
|
---|
[33540] | 69 | * @retval VINF_SUCCESS if an event occurred on a handle. Note that these
|
---|
[26756] | 70 | * @retval VERR_INVALID_HANDLE if @a hPollSet is invalid.
|
---|
[31453] | 71 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
[26801] | 72 | * user is responsible for ensuring single threaded access.
|
---|
[26756] | 73 | * @retval VERR_TIMEOUT if @a cMillies ellapsed without any events.
|
---|
| 74 | * @retval VERR_DEADLOCK if @a cMillies is set to RT_INDEFINITE_WAIT and there
|
---|
| 75 | * are no valid handles in the set.
|
---|
| 76 | *
|
---|
| 77 | * @param hPollSet The set to poll on.
|
---|
| 78 | * @param cMillies Number of milliseconds to wait. Use
|
---|
| 79 | * RT_INDEFINITE_WAIT to wait for ever.
|
---|
| 80 | * @param pfEvents Where to return details about the events that
|
---|
[33540] | 81 | * occurred. Optional.
|
---|
[26756] | 82 | * @param pid Where to return the ID associated with the
|
---|
| 83 | * handle when calling RTPollSetAdd. Optional.
|
---|
| 84 | *
|
---|
| 85 | * @sa RTPollNoResume
|
---|
[26801] | 86 | *
|
---|
| 87 | * @remarks The caller is responsible for ensuring
|
---|
[26703] | 88 | */
|
---|
[26756] | 89 | RTDECL(int) RTPoll(RTPOLLSET hPollSet, RTMSINTERVAL cMillies, uint32_t *pfEvents, uint32_t *pid);
|
---|
[26703] | 90 |
|
---|
| 91 | /**
|
---|
[26756] | 92 | * Same as RTPoll except that it will return when interrupted.
|
---|
| 93 | *
|
---|
| 94 | * @returns IPRT status code.
|
---|
[33540] | 95 | * @retval VINF_SUCCESS if an event occurred on a handle. Note that these
|
---|
[26756] | 96 | * @retval VERR_INVALID_HANDLE if @a hPollSet is invalid.
|
---|
[31453] | 97 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
[26801] | 98 | * user is responsible for ensuring single threaded access.
|
---|
[26756] | 99 | * @retval VERR_TIMEOUT if @a cMillies ellapsed without any events.
|
---|
| 100 | * @retval VERR_DEADLOCK if @a cMillies is set to RT_INDEFINITE_WAIT and there
|
---|
| 101 | * are no valid handles in the set.
|
---|
| 102 | * @retval VERR_INTERRUPTED if a signal or other asynchronous event interrupted
|
---|
| 103 | * the polling.
|
---|
| 104 | *
|
---|
| 105 | * @param hPollSet The set to poll on.
|
---|
| 106 | * @param cMillies Number of milliseconds to wait. Use
|
---|
| 107 | * RT_INDEFINITE_WAIT to wait for ever.
|
---|
| 108 | * @param pfEvents Where to return details about the events that
|
---|
[33540] | 109 | * occurred. Optional.
|
---|
[26756] | 110 | * @param pid Where to return the ID associated with the
|
---|
| 111 | * handle when calling RTPollSetAdd. Optional.
|
---|
| 112 | */
|
---|
| 113 | RTDECL(int) RTPollNoResume(RTPOLLSET hPollSet, RTMSINTERVAL cMillies, uint32_t *pfEvents, uint32_t *pid);
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
[26731] | 116 | * Creates a poll set with no members.
|
---|
[26703] | 117 | *
|
---|
| 118 | * @returns IPRT status code.
|
---|
[26756] | 119 | * @param phPollSet Where to return the poll set handle.
|
---|
[26703] | 120 | */
|
---|
[26801] | 121 | RTDECL(int) RTPollSetCreate(PRTPOLLSET phPollSet);
|
---|
[26703] | 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Destroys a poll set.
|
---|
| 125 | *
|
---|
| 126 | * @returns IPRT status code.
|
---|
[26756] | 127 | * @param hPollSet The poll set to destroy. NIL_POLLSET is quietly
|
---|
| 128 | * ignored (VINF_SUCCESS).
|
---|
[26703] | 129 | */
|
---|
[26721] | 130 | RTDECL(int) RTPollSetDestroy(RTPOLLSET hPollSet);
|
---|
[26703] | 131 |
|
---|
| 132 | /**
|
---|
[26721] | 133 | * Adds a generic handle to the poll set.
|
---|
[26703] | 134 | *
|
---|
[70478] | 135 | * If a handle is entered more than once, it is recommended to add the one with
|
---|
| 136 | * RTPOLL_EVT_ERROR first to ensure that you get the right ID back when an error
|
---|
| 137 | * actually occurs. On some hosts it is possible that polling for
|
---|
| 138 | * RTPOLL_EVT_READ on a socket may cause it to return error conditions because
|
---|
| 139 | * the two cannot so easily be distinguished.
|
---|
| 140 | *
|
---|
| 141 | * Also note that RTPOLL_EVT_ERROR may be returned by RTPoll even if not asked
|
---|
| 142 | * for.
|
---|
| 143 | *
|
---|
[26703] | 144 | * @returns IPRT status code
|
---|
[31453] | 145 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
[26801] | 146 | * user is responsible for ensuring single threaded access.
|
---|
| 147 | * @retval VERR_POLL_HANDLE_NOT_POLLABLE if the specified handle is not
|
---|
| 148 | * pollable.
|
---|
| 149 | * @retval VERR_POLL_HANDLE_ID_EXISTS if the handle ID is already in use in the
|
---|
| 150 | * set.
|
---|
| 151 | *
|
---|
[26756] | 152 | * @param hPollSet The poll set to modify.
|
---|
[33540] | 153 | * @param pHandle The handle to add. NIL handles are quietly
|
---|
[26801] | 154 | * ignored.
|
---|
[26756] | 155 | * @param fEvents Which events to poll for.
|
---|
| 156 | * @param id The handle ID.
|
---|
[26703] | 157 | */
|
---|
[26731] | 158 | RTDECL(int) RTPollSetAdd(RTPOLLSET hPollSet, PCRTHANDLE pHandle, uint32_t fEvents, uint32_t id);
|
---|
[26703] | 159 |
|
---|
| 160 | /**
|
---|
[26721] | 161 | * Removes a generic handle from the poll set.
|
---|
[26703] | 162 | *
|
---|
| 163 | * @returns IPRT status code
|
---|
[26801] | 164 | * @retval VERR_INVALID_HANDLE if @a hPollSet not valid.
|
---|
[31453] | 165 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
[26801] | 166 | * user is responsible for ensuring single threaded access.
|
---|
| 167 | * @retval VERR_POLL_HANDLE_ID_NOT_FOUND if @a id doesn't resolve to a valid
|
---|
| 168 | * handle.
|
---|
| 169 | *
|
---|
[26756] | 170 | * @param hPollSet The poll set to modify.
|
---|
| 171 | * @param id The handle ID of the handle that should be
|
---|
| 172 | * removed.
|
---|
[26703] | 173 | */
|
---|
[26731] | 174 | RTDECL(int) RTPollSetRemove(RTPOLLSET hPollSet, uint32_t id);
|
---|
[26703] | 175 |
|
---|
[26756] | 176 |
|
---|
[26721] | 177 | /**
|
---|
[26756] | 178 | * Query a handle in the poll set by it's ID.
|
---|
| 179 | *
|
---|
| 180 | * @returns IPRT status code
|
---|
| 181 | * @retval VINF_SUCCESS if the handle was found. @a *pHandle is set.
|
---|
| 182 | * @retval VERR_INVALID_HANDLE if @a hPollSet is invalid.
|
---|
[31453] | 183 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
[26801] | 184 | * user is responsible for ensuring single threaded access.
|
---|
| 185 | * @retval VERR_POLL_HANDLE_ID_NOT_FOUND if there is no handle with that ID.
|
---|
[26756] | 186 | *
|
---|
| 187 | * @param hPollSet The poll set to query.
|
---|
| 188 | * @param id The ID of the handle.
|
---|
| 189 | * @param pHandle Where to return the handle details. Optional.
|
---|
| 190 | */
|
---|
| 191 | RTDECL(int) RTPollSetQueryHandle(RTPOLLSET hPollSet, uint32_t id, PRTHANDLE pHandle);
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * Gets the number of handles in the set.
|
---|
| 195 | *
|
---|
| 196 | * @retval The handle count.
|
---|
[26801] | 197 | * @retval UINT32_MAX if @a hPollSet is invalid or there is concurrent access.
|
---|
[26756] | 198 | *
|
---|
| 199 | * @param hPollSet The poll set.
|
---|
| 200 | */
|
---|
[27509] | 201 | RTDECL(uint32_t) RTPollSetGetCount(RTPOLLSET hPollSet);
|
---|
[26756] | 202 |
|
---|
| 203 | /**
|
---|
[31453] | 204 | * Modifies the events to poll for for the given id.
|
---|
| 205 | *
|
---|
| 206 | * @returns IPRT status code.
|
---|
| 207 | * @retval VERR_INVALID_HANDLE if @a hPollSet not valid.
|
---|
| 208 | * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
|
---|
| 209 | * user is responsible for ensuring single threaded access.
|
---|
| 210 | * @retval VERR_POLL_HANDLE_ID_NOT_FOUND if @a id doesn't resolve to a valid
|
---|
| 211 | * handle.
|
---|
| 212 | *
|
---|
| 213 | * @param hPollSet The poll set to modify.
|
---|
| 214 | * @param id The handle ID to change the events for.
|
---|
| 215 | * @param fEvents Which events to poll for.
|
---|
| 216 | */
|
---|
| 217 | RTDECL(int) RTPollSetEventsChange(RTPOLLSET hPollSet, uint32_t id, uint32_t fEvents);
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
[26721] | 220 | * Adds a pipe handle to the set.
|
---|
| 221 | *
|
---|
[26801] | 222 | * @returns See RTPollSetAdd.
|
---|
| 223 | *
|
---|
[26756] | 224 | * @param hPollSet The poll set.
|
---|
| 225 | * @param hPipe The pipe handle.
|
---|
| 226 | * @param fEvents Which events to poll for.
|
---|
| 227 | * @param id The handle ID.
|
---|
[26721] | 228 | *
|
---|
| 229 | * @todo Maybe we could figure out what to poll for depending on the kind of
|
---|
| 230 | * pipe we're dealing with.
|
---|
| 231 | */
|
---|
[26731] | 232 | DECLINLINE(int) RTPollSetAddPipe(RTPOLLSET hPollSet, RTPIPE hPipe, uint32_t fEvents, uint32_t id)
|
---|
[26721] | 233 | {
|
---|
| 234 | RTHANDLE Handle;
|
---|
| 235 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
[27509] | 236 | Handle.u.uInt = 0;
|
---|
[26721] | 237 | Handle.u.hPipe = hPipe;
|
---|
[26731] | 238 | return RTPollSetAdd(hPollSet, &Handle, fEvents, id);
|
---|
[26721] | 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
| 242 | * Adds a socket handle to the set.
|
---|
| 243 | *
|
---|
[26801] | 244 | * @returns See RTPollSetAdd.
|
---|
| 245 | *
|
---|
[26756] | 246 | * @param hPollSet The poll set.
|
---|
| 247 | * @param hSocket The socket handle.
|
---|
| 248 | * @param fEvents Which events to poll for.
|
---|
| 249 | * @param id The handle ID.
|
---|
[26721] | 250 | */
|
---|
[26731] | 251 | DECLINLINE(int) RTPollSetAddSocket(RTPOLLSET hPollSet, RTSOCKET hSocket, uint32_t fEvents, uint32_t id)
|
---|
[26721] | 252 | {
|
---|
| 253 | RTHANDLE Handle;
|
---|
| 254 | Handle.enmType = RTHANDLETYPE_SOCKET;
|
---|
[27509] | 255 | Handle.u.uInt = 0;
|
---|
[26721] | 256 | Handle.u.hSocket = hSocket;
|
---|
[26731] | 257 | return RTPollSetAdd(hPollSet, &Handle, fEvents, id);
|
---|
[26721] | 258 | }
|
---|
| 259 |
|
---|
[26703] | 260 | /** @} */
|
---|
| 261 |
|
---|
| 262 | RT_C_DECLS_END
|
---|
| 263 |
|
---|
[76585] | 264 | #endif /* !IPRT_INCLUDED_poll_h */
|
---|
[26703] | 265 |
|
---|