VirtualBox

source: vbox/trunk/include/iprt/poll.h@ 99802

Last change on this file since 99802 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/** @file
2 * IPRT - Polling I/O Handles.
3 */
4
5/*
6 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
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 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
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
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.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_poll_h
37#define IPRT_INCLUDED_poll_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43#include <iprt/types.h>
44
45RT_C_DECLS_BEGIN
46
47/** @defgroup grp_rt_poll RTPoll - Polling I/O Handles
48 * @ingroup grp_rt
49 * @{
50 */
51
52/** @name Poll events
53 * @{ */
54/** Readable without blocking. */
55#define RTPOLL_EVT_READ RT_BIT_32(0)
56/** Writable without blocking. */
57#define RTPOLL_EVT_WRITE RT_BIT_32(1)
58/** Error condition, hangup, exception or similar. */
59#define RTPOLL_EVT_ERROR RT_BIT_32(2)
60/** Mask of the valid bits. */
61#define RTPOLL_EVT_VALID_MASK UINT32_C(0x00000007)
62/** @} */
63
64/**
65 * Polls on the specified poll set until an event occurs on one of the handles
66 * or the timeout expires.
67 *
68 * @returns IPRT status code.
69 * @retval VINF_SUCCESS if an event occurred on a handle. Note that these
70 * @retval VERR_INVALID_HANDLE if @a hPollSet is invalid.
71 * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
72 * user is responsible for ensuring single threaded access.
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
81 * occurred. Optional.
82 * @param pid Where to return the ID associated with the
83 * handle when calling RTPollSetAdd. Optional.
84 *
85 * @sa RTPollNoResume
86 *
87 * @remarks The caller is responsible for ensuring
88 */
89RTDECL(int) RTPoll(RTPOLLSET hPollSet, RTMSINTERVAL cMillies, uint32_t *pfEvents, uint32_t *pid);
90
91/**
92 * Same as RTPoll except that it will return when interrupted.
93 *
94 * @returns IPRT status code.
95 * @retval VINF_SUCCESS if an event occurred on a handle. Note that these
96 * @retval VERR_INVALID_HANDLE if @a hPollSet is invalid.
97 * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
98 * user is responsible for ensuring single threaded access.
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
109 * occurred. Optional.
110 * @param pid Where to return the ID associated with the
111 * handle when calling RTPollSetAdd. Optional.
112 */
113RTDECL(int) RTPollNoResume(RTPOLLSET hPollSet, RTMSINTERVAL cMillies, uint32_t *pfEvents, uint32_t *pid);
114
115/**
116 * Creates a poll set with no members.
117 *
118 * @returns IPRT status code.
119 * @param phPollSet Where to return the poll set handle.
120 */
121RTDECL(int) RTPollSetCreate(PRTPOLLSET phPollSet);
122
123/**
124 * Destroys a poll set.
125 *
126 * @returns IPRT status code.
127 * @param hPollSet The poll set to destroy. NIL_POLLSET is quietly
128 * ignored (VINF_SUCCESS).
129 */
130RTDECL(int) RTPollSetDestroy(RTPOLLSET hPollSet);
131
132/**
133 * Adds a generic handle to the poll set.
134 *
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 *
144 * @returns IPRT status code
145 * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
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 *
152 * @param hPollSet The poll set to modify.
153 * @param pHandle The handle to add. NIL handles are quietly
154 * ignored.
155 * @param fEvents Which events to poll for.
156 * @param id The handle ID.
157 */
158RTDECL(int) RTPollSetAdd(RTPOLLSET hPollSet, PCRTHANDLE pHandle, uint32_t fEvents, uint32_t id);
159
160/**
161 * Removes a generic handle from the poll set.
162 *
163 * @returns IPRT status code
164 * @retval VERR_INVALID_HANDLE if @a hPollSet not valid.
165 * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
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 *
170 * @param hPollSet The poll set to modify.
171 * @param id The handle ID of the handle that should be
172 * removed.
173 */
174RTDECL(int) RTPollSetRemove(RTPOLLSET hPollSet, uint32_t id);
175
176
177/**
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.
183 * @retval VERR_CONCURRENT_ACCESS if another thread is already accessing the set. The
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.
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 */
191RTDECL(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.
197 * @retval UINT32_MAX if @a hPollSet is invalid or there is concurrent access.
198 *
199 * @param hPollSet The poll set.
200 */
201RTDECL(uint32_t) RTPollSetGetCount(RTPOLLSET hPollSet);
202
203/**
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 */
217RTDECL(int) RTPollSetEventsChange(RTPOLLSET hPollSet, uint32_t id, uint32_t fEvents);
218
219/**
220 * Adds a pipe handle to the set.
221 *
222 * @returns See RTPollSetAdd.
223 *
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.
228 *
229 * @todo Maybe we could figure out what to poll for depending on the kind of
230 * pipe we're dealing with.
231 */
232DECLINLINE(int) RTPollSetAddPipe(RTPOLLSET hPollSet, RTPIPE hPipe, uint32_t fEvents, uint32_t id)
233{
234 RTHANDLE Handle;
235 Handle.enmType = RTHANDLETYPE_PIPE;
236 Handle.u.uInt = 0;
237 Handle.u.hPipe = hPipe;
238 return RTPollSetAdd(hPollSet, &Handle, fEvents, id);
239}
240
241/**
242 * Adds a socket handle to the set.
243 *
244 * @returns See RTPollSetAdd.
245 *
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.
250 */
251DECLINLINE(int) RTPollSetAddSocket(RTPOLLSET hPollSet, RTSOCKET hSocket, uint32_t fEvents, uint32_t id)
252{
253 RTHANDLE Handle;
254 Handle.enmType = RTHANDLETYPE_SOCKET;
255 Handle.u.uInt = 0;
256 Handle.u.hSocket = hSocket;
257 return RTPollSetAdd(hPollSet, &Handle, fEvents, id);
258}
259
260/** @} */
261
262RT_C_DECLS_END
263
264#endif /* !IPRT_INCLUDED_poll_h */
265
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette