Apache Portable Runtime
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
apr_poll.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APR_POLL_H
18 #define APR_POLL_H
19 /**
20  * @file apr_poll.h
21  * @brief APR Poll interface
22  */
23 #include "apr.h"
24 #include "apr_pools.h"
25 #include "apr_errno.h"
26 #include "apr_inherit.h"
27 #include "apr_file_io.h"
28 #include "apr_network_io.h"
29 
30 #if APR_HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 /**
39  * @defgroup apr_poll Poll Routines
40  * @ingroup APR
41  * @{
42  */
43 
44 /**
45  * Poll options
46  */
47 #define APR_POLLIN 0x001 /**< Can read without blocking */
48 #define APR_POLLPRI 0x002 /**< Priority data available */
49 #define APR_POLLOUT 0x004 /**< Can write without blocking */
50 #define APR_POLLERR 0x010 /**< Pending error */
51 #define APR_POLLHUP 0x020 /**< Hangup occurred */
52 #define APR_POLLNVAL 0x040 /**< Descriptor invalid */
53 
54 /**
55  * Pollset Flags
56  */
57 #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or removing a descriptor is
58  * thread-safe
59  */
60 #define APR_POLLSET_NOCOPY 0x002 /**< Descriptors passed to apr_pollset_add()
61  * are not copied
62  */
63 #define APR_POLLSET_WAKEABLE 0x004 /**< Poll operations are interruptable by
64  * apr_pollset_wakeup()
65  */
66 #define APR_POLLSET_NODEFAULT 0x010 /**< Do not try to use the default method if
67  * the specified non-default method cannot be
68  * used
69  */
70 
71 /**
72  * Pollset Methods
73  */
74 typedef enum {
75  APR_POLLSET_DEFAULT, /**< Platform default poll method */
76  APR_POLLSET_SELECT, /**< Poll uses select method */
77  APR_POLLSET_KQUEUE, /**< Poll uses kqueue method */
78  APR_POLLSET_PORT, /**< Poll uses Solaris event port method */
79  APR_POLLSET_EPOLL, /**< Poll uses epoll method */
80  APR_POLLSET_POLL, /**< Poll uses poll method */
81  APR_POLLSET_AIO_MSGQ /**< Poll uses z/OS asio method */
83 
84 /** Used in apr_pollfd_t to determine what the apr_descriptor is */
85 typedef enum {
86  APR_NO_DESC, /**< nothing here */
87  APR_POLL_SOCKET, /**< descriptor refers to a socket */
88  APR_POLL_FILE, /**< descriptor refers to a file */
89  APR_POLL_LASTDESC /**< @deprecated descriptor is the last one in the list */
91 
92 /** Union of either an APR file or socket. */
93 typedef union {
94  apr_file_t *f; /**< file */
95  apr_socket_t *s; /**< socket */
97 
98 /** @see apr_pollfd_t */
99 typedef struct apr_pollfd_t apr_pollfd_t;
100 
101 /** Poll descriptor set. */
102 struct apr_pollfd_t {
103  apr_pool_t *p; /**< associated pool */
104  apr_datatype_e desc_type; /**< descriptor type */
105  apr_int16_t reqevents; /**< requested events */
106  apr_int16_t rtnevents; /**< returned events */
107  apr_descriptor desc; /**< @see apr_descriptor */
108  void *client_data; /**< allows app to associate context */
109 };
110 
112 /* General-purpose poll API for arbitrarily large numbers of
113  * file descriptors
114  */
116 /** Opaque structure used for pollset API */
118 
119 /**
120  * Set up a pollset object
121  * @param pollset The pointer in which to return the newly created object
122  * @param size The maximum number of descriptors that this pollset can hold
123  * @param p The pool from which to allocate the pollset
124  * @param flags Optional flags to modify the operation of the pollset.
125  *
126  * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
127  * created on which it is safe to make concurrent calls to
128  * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
129  * from separate threads. This feature is only supported on some
130  * platforms; the apr_pollset_create() call will fail with
131  * APR_ENOTIMPL on platforms where it is not supported.
132  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
133  * created with an additional internal pipe object used for the
134  * apr_pollset_wakeup() call. The actual size of pollset is
135  * in that case @a size + 1. This feature is only supported on some
136  * platforms; the apr_pollset_create() call will fail with
137  * APR_ENOTIMPL on platforms where it is not supported.
138  * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
139  * structures passed to apr_pollset_add() are not copied and
140  * must have a lifetime at least as long as the pollset.
141  * @remark Some poll methods (including APR_POLLSET_KQUEUE,
142  * APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
143  * fixed limit on the size of the pollset. For these methods,
144  * the size parameter controls the maximum number of
145  * descriptors that will be returned by a single call to
146  * apr_pollset_poll().
147  */
149  apr_uint32_t size,
150  apr_pool_t *p,
151  apr_uint32_t flags);
152 
153 /**
154  * Set up a pollset object
155  * @param pollset The pointer in which to return the newly created object
156  * @param size The maximum number of descriptors that this pollset can hold
157  * @param p The pool from which to allocate the pollset
158  * @param flags Optional flags to modify the operation of the pollset.
159  * @param method Poll method to use. See #apr_pollset_method_e. If this
160  * method cannot be used, the default method will be used unless the
161  * APR_POLLSET_NODEFAULT flag has been specified.
162  *
163  * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
164  * created on which it is safe to make concurrent calls to
165  * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
166  * from separate threads. This feature is only supported on some
167  * platforms; the apr_pollset_create_ex() call will fail with
168  * APR_ENOTIMPL on platforms where it is not supported.
169  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
170  * created with additional internal pipe object used for the
171  * apr_pollset_wakeup() call. The actual size of pollset is
172  * in that case size + 1. This feature is only supported on some
173  * platforms; the apr_pollset_create_ex() call will fail with
174  * APR_ENOTIMPL on platforms where it is not supported.
175  * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
176  * structures passed to apr_pollset_add() are not copied and
177  * must have a lifetime at least as long as the pollset.
178  * @remark Some poll methods (including APR_POLLSET_KQUEUE,
179  * APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
180  * fixed limit on the size of the pollset. For these methods,
181  * the size parameter controls the maximum number of
182  * descriptors that will be returned by a single call to
183  * apr_pollset_poll().
184  */
186  apr_uint32_t size,
187  apr_pool_t *p,
188  apr_uint32_t flags,
189  apr_pollset_method_e method);
190 
191 /**
192  * Destroy a pollset object
193  * @param pollset The pollset to destroy
194  */
196 
197 /**
198  * Add a socket or file descriptor to a pollset
199  * @param pollset The pollset to which to add the descriptor
200  * @param descriptor The descriptor to add
201  * @remark If you set client_data in the descriptor, that value
202  * will be returned in the client_data field whenever this
203  * descriptor is signalled in apr_pollset_poll().
204  * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
205  * and thread T1 is blocked in a call to apr_pollset_poll() for
206  * this same pollset that is being modified via apr_pollset_add()
207  * in thread T2, the currently executing apr_pollset_poll() call in
208  * T1 will either: (1) automatically include the newly added descriptor
209  * in the set of descriptors it is watching or (2) return immediately
210  * with APR_EINTR. Option (1) is recommended, but option (2) is
211  * allowed for implementations where option (1) is impossible
212  * or impractical.
213  * @remark If the pollset has been created with APR_POLLSET_NOCOPY, the
214  * apr_pollfd_t structure referenced by descriptor will not be copied
215  * and must have a lifetime at least as long as the pollset.
216  * @remark Do not add the same socket or file descriptor to the same pollset
217  * multiple times, even if the requested events differ for the
218  * different calls to apr_pollset_add(). If the events of interest
219  * for a descriptor change, you must first remove the descriptor
220  * from the pollset with apr_pollset_remove(), then add it again
221  * specifying all requested events.
222  */
224  const apr_pollfd_t *descriptor);
225 
226 /**
227  * Remove a descriptor from a pollset
228  * @param pollset The pollset from which to remove the descriptor
229  * @param descriptor The descriptor to remove
230  * @remark If the descriptor is not found, APR_NOTFOUND is returned.
231  * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
232  * and thread T1 is blocked in a call to apr_pollset_poll() for
233  * this same pollset that is being modified via apr_pollset_remove()
234  * in thread T2, the currently executing apr_pollset_poll() call in
235  * T1 will either: (1) automatically exclude the newly added descriptor
236  * in the set of descriptors it is watching or (2) return immediately
237  * with APR_EINTR. Option (1) is recommended, but option (2) is
238  * allowed for implementations where option (1) is impossible
239  * or impractical.
240  * @remark apr_pollset_remove() cannot be used to remove a subset of requested
241  * events for a descriptor. The reqevents field in the apr_pollfd_t
242  * parameter must contain the same value when removing as when adding.
243  */
245  const apr_pollfd_t *descriptor);
246 
247 /**
248  * Block for activity on the descriptor(s) in a pollset
249  * @param pollset The pollset to use
250  * @param timeout The amount of time in microseconds to wait. This is a
251  * maximum, not a minimum. If a descriptor is signalled, the
252  * function will return before this time. If timeout is
253  * negative, the function will block until a descriptor is
254  * signalled or until apr_pollset_wakeup() has been called.
255  * @param num Number of signalled descriptors (output parameter)
256  * @param descriptors Array of signalled descriptors (output parameter)
257  * @remark APR_EINTR will be returned if the pollset has been created with
258  * APR_POLLSET_WAKEABLE, apr_pollset_wakeup() has been called while
259  * waiting for activity, and there were no signalled descriptors at the
260  * time of the wakeup call.
261  * @remark Multiple signalled conditions for the same descriptor may be reported
262  * in one or more returned apr_pollfd_t structures, depending on the
263  * implementation.
264  */
266  apr_interval_time_t timeout,
267  apr_int32_t *num,
268  const apr_pollfd_t **descriptors);
269 
270 /**
271  * Interrupt the blocked apr_pollset_poll() call.
272  * @param pollset The pollset to use
273  * @remark If the pollset was not created with APR_POLLSET_WAKEABLE the
274  * return value is APR_EINIT.
275  */
277 
278 /**
279  * Poll the descriptors in the poll structure
280  * @param aprset The poll structure we will be using.
281  * @param numsock The number of descriptors we are polling
282  * @param nsds The number of descriptors signalled (output parameter)
283  * @param timeout The amount of time in microseconds to wait. This is a
284  * maximum, not a minimum. If a descriptor is signalled, the
285  * function will return before this time. If timeout is
286  * negative, the function will block until a descriptor is
287  * signalled or until apr_pollset_wakeup() has been called.
288  * @remark The number of descriptors signalled is returned in the third argument.
289  * This is a blocking call, and it will not return until either a
290  * descriptor has been signalled or the timeout has expired.
291  * @remark The rtnevents field in the apr_pollfd_t array will only be filled-
292  * in if the return value is APR_SUCCESS.
293  */
294 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock,
295  apr_int32_t *nsds,
296  apr_interval_time_t timeout);
297 
298 /**
299  * Return a printable representation of the pollset method.
300  * @param pollset The pollset to use
301  */
302 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset);
303 
304 /**
305  * Return a printable representation of the default pollset method
306  * (APR_POLLSET_DEFAULT).
307  */
308 APR_DECLARE(const char *) apr_poll_method_defname(void);
309 
310 /** Opaque structure used for pollcb API */
311 typedef struct apr_pollcb_t apr_pollcb_t;
312 
313 /**
314  * Set up a pollcb object
315  * @param pollcb The pointer in which to return the newly created object
316  * @param size The maximum number of descriptors that a single _poll can return.
317  * @param p The pool from which to allocate the pollcb
318  * @param flags Optional flags to modify the operation of the pollcb.
319  *
320  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create()
321  * call will fail with APR_ENOTIMPL on platforms where it is not supported.
322  */
323 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
324  apr_uint32_t size,
325  apr_pool_t *p,
326  apr_uint32_t flags);
327 
328 /**
329  * Set up a pollcb object
330  * @param pollcb The pointer in which to return the newly created object
331  * @param size The maximum number of descriptors that a single _poll can return.
332  * @param p The pool from which to allocate the pollcb
333  * @param flags Optional flags to modify the operation of the pollcb.
334  * @param method Poll method to use. See #apr_pollset_method_e. If this
335  * method cannot be used, the default method will be used unless the
336  * APR_POLLSET_NODEFAULT flag has been specified.
337  *
338  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex()
339  * call will fail with APR_ENOTIMPL on platforms where it is not supported.
340  */
341 APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb,
342  apr_uint32_t size,
343  apr_pool_t *p,
344  apr_uint32_t flags,
345  apr_pollset_method_e method);
346 
347 /**
348  * Add a socket or file descriptor to a pollcb
349  * @param pollcb The pollcb to which to add the descriptor
350  * @param descriptor The descriptor to add
351  * @remark If you set client_data in the descriptor, that value will be
352  * returned in the client_data field whenever this descriptor is
353  * signalled in apr_pollcb_poll().
354  * @remark Unlike the apr_pollset API, the descriptor is not copied, and users
355  * must retain the memory used by descriptor, as the same pointer will
356  * be returned to them from apr_pollcb_poll.
357  * @remark Do not add the same socket or file descriptor to the same pollcb
358  * multiple times, even if the requested events differ for the
359  * different calls to apr_pollcb_add(). If the events of interest
360  * for a descriptor change, you must first remove the descriptor
361  * from the pollcb with apr_pollcb_remove(), then add it again
362  * specifying all requested events.
363  */
364 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
365  apr_pollfd_t *descriptor);
366 /**
367  * Remove a descriptor from a pollcb
368  * @param pollcb The pollcb from which to remove the descriptor
369  * @param descriptor The descriptor to remove
370  * @remark apr_pollcb_remove() cannot be used to remove a subset of requested
371  * events for a descriptor. The reqevents field in the apr_pollfd_t
372  * parameter must contain the same value when removing as when adding.
373  */
374 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
375  apr_pollfd_t *descriptor);
376 
377 /** Function prototype for pollcb handlers
378  * @param baton Opaque baton passed into apr_pollcb_poll()
379  * @param descriptor Contains the notification for an active descriptor,
380  * the rtnevents member contains what events were triggered
381  * for this descriptor.
382  */
383 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
384 
385 /**
386  * Block for activity on the descriptor(s) in a pollcb
387  * @param pollcb The pollcb to use
388  * @param timeout The amount of time in microseconds to wait. This is a
389  * maximum, not a minimum. If a descriptor is signalled, the
390  * function will return before this time. If timeout is
391  * negative, the function will block until a descriptor is
392  * signalled.
393  * @param func Callback function to call for each active descriptor.
394  * @param baton Opaque baton passed to the callback function.
395  * @remark Multiple signalled conditions for the same descriptor may be reported
396  * in one or more calls to the callback function, depending on the
397  * implementation.
398  */
399 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
400  apr_interval_time_t timeout,
401  apr_pollcb_cb_t func,
402  void *baton);
403 
404 /** @} */
405 
406 #ifdef __cplusplus
407 }
408 #endif
409 
410 #endif /* ! APR_POLL_H */
411 
Definition: apr_poll.h:102
struct apr_socket_t apr_socket_t
Definition: apr_network_io.h:218
apr_status_t apr_pollcb_create_ex(apr_pollcb_t **pollcb, apr_uint32_t size, apr_pool_t *p, apr_uint32_t flags, apr_pollset_method_e method)
Definition: apr_poll.h:95
APR Network library.
Definition: apr_poll.h:86
apr_datatype_e desc_type
Definition: apr_poll.h:113
Definition: apr_poll.h:97
apr_status_t apr_pollset_remove(apr_pollset_t *pollset, const apr_pollfd_t *descriptor)
apr_pollset_method_e
Definition: apr_poll.h:83
Definition: apr_poll.h:85
APR File Handle Inheritance Helpers.
apr_status_t apr_pollset_create_ex(apr_pollset_t **pollset, apr_uint32_t size, apr_pool_t *p, apr_uint32_t flags, apr_pollset_method_e method)
apr_status_t apr_pollcb_create(apr_pollcb_t **pollcb, apr_uint32_t size, apr_pool_t *p, apr_uint32_t flags)
apr_datatype_e
Definition: apr_poll.h:94
APR File I/O Handling.
apr_int64_t apr_interval_time_t
Definition: apr_time.h:56
apr_status_t apr_pollcb_add(apr_pollcb_t *pollcb, apr_pollfd_t *descriptor)
apr_int16_t reqevents
Definition: apr_poll.h:114
#define APR_DECLARE(type)
Definition: apr.h:479
apr_status_t(* apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor)
Definition: apr_poll.h:392
Definition: apr_poll.h:89
struct apr_file_t apr_file_t
Definition: apr_file_io.h:209
APR memory allocation.
apr_pool_t * p
Definition: apr_poll.h:112
apr_status_t apr_pollset_wakeup(apr_pollset_t *pollset)
apr_descriptor desc
Definition: apr_poll.h:116
Definition: apr_poll.h:111
struct apr_pollset_t apr_pollset_t
Definition: apr_poll.h:126
APR Error Codes.
APR Platform Definitions.
const char * apr_poll_method_defname(void)
Definition: apr_poll.h:88
const char * apr_pollset_method_name(apr_pollset_t *pollset)
apr_int16_t rtnevents
Definition: apr_poll.h:115
apr_status_t apr_pollset_destroy(apr_pollset_t *pollset)
apr_status_t apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock, apr_int32_t *nsds, apr_interval_time_t timeout)
Definition: apr_poll.h:96
void * client_data
Definition: apr_poll.h:117
Definition: apr_poll.h:87
apr_status_t apr_pollset_create(apr_pollset_t **pollset, apr_uint32_t size, apr_pool_t *p, apr_uint32_t flags)
struct apr_pollcb_t apr_pollcb_t
Definition: apr_poll.h:320
Definition: apr_poll.h:84
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
apr_status_t apr_pollcb_remove(apr_pollcb_t *pollcb, apr_pollfd_t *descriptor)
int apr_status_t
Definition: apr_errno.h:44
apr_status_t apr_pollset_add(apr_pollset_t *pollset, const apr_pollfd_t *descriptor)
Definition: apr_poll.h:98
apr_status_t apr_pollset_poll(apr_pollset_t *pollset, apr_interval_time_t timeout, apr_int32_t *num, const apr_pollfd_t **descriptors)
apr_status_t apr_pollcb_poll(apr_pollcb_t *pollcb, apr_interval_time_t timeout, apr_pollcb_cb_t func, void *baton)
Definition: apr_poll.h:90