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