00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_POLL_H 00018 #define APR_POLL_H 00019 /** 00020 * @file apr_poll.h 00021 * @brief APR Poll interface 00022 */ 00023 #include "apr.h" 00024 #include "apr_pools.h" 00025 #include "apr_errno.h" 00026 #include "apr_inherit.h" 00027 #include "apr_file_io.h" 00028 #include "apr_network_io.h" 00029 00030 #if APR_HAVE_NETINET_IN_H 00031 #include <netinet/in.h> 00032 #endif 00033 00034 #ifdef __cplusplus 00035 extern "C" { 00036 #endif /* __cplusplus */ 00037 00038 /** 00039 * @defgroup apr_poll Poll Routines 00040 * @ingroup APR 00041 * @{ 00042 */ 00043 00044 /** 00045 * Poll options 00046 */ 00047 #define APR_POLLIN 0x001 /**< Can read without blocking */ 00048 #define APR_POLLPRI 0x002 /**< Priority data available */ 00049 #define APR_POLLOUT 0x004 /**< Can write without blocking */ 00050 #define APR_POLLERR 0x010 /**< Pending error */ 00051 #define APR_POLLHUP 0x020 /**< Hangup occurred */ 00052 #define APR_POLLNVAL 0x040 /**< Descriptor invalid */ 00053 00054 /** 00055 * Pollset Flags 00056 */ 00057 #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or removing a descriptor is 00058 * thread-safe 00059 */ 00060 #define APR_POLLSET_NOCOPY 0x002 /**< Descriptors passed to apr_pollset_add() 00061 * are not copied 00062 */ 00063 #define APR_POLLSET_WAKEABLE 0x004 /**< Poll operations are interruptable by 00064 * apr_pollset_wakeup() 00065 */ 00066 #define APR_POLLSET_NODEFAULT 0x010 /**< Do not try to use the default method if 00067 * the specified non-default method cannot be 00068 * used 00069 */ 00070 00071 /** 00072 * Pollset Methods 00073 */ 00074 typedef enum { 00075 APR_POLLSET_DEFAULT, /**< Platform default poll method */ 00076 APR_POLLSET_SELECT, /**< Poll uses select method */ 00077 APR_POLLSET_KQUEUE, 00078 APR_POLLSET_PORT, 00079 APR_POLLSET_EPOLL, 00080 APR_POLLSET_POLL 00081 } apr_pollset_method_e; 00082 00083 /** Used in apr_pollfd_t to determine what the apr_descriptor is */ 00084 typedef enum { 00085 APR_NO_DESC, /**< nothing here */ 00086 APR_POLL_SOCKET, /**< descriptor refers to a socket */ 00087 APR_POLL_FILE, /**< descriptor refers to a file */ 00088 APR_POLL_LASTDESC /**< @deprecated descriptor is the last one in the list */ 00089 } apr_datatype_e ; 00090 00091 /** Union of either an APR file or socket. */ 00092 typedef union { 00093 apr_file_t *f; /**< file */ 00094 apr_socket_t *s; /**< socket */ 00095 } apr_descriptor; 00096 00097 /** @see apr_pollfd_t */ 00098 typedef struct apr_pollfd_t apr_pollfd_t; 00099 00100 /** Poll descriptor set. */ 00101 struct apr_pollfd_t { 00102 apr_pool_t *p; /**< associated pool */ 00103 apr_datatype_e desc_type; /**< descriptor type */ 00104 apr_int16_t reqevents; /**< requested events */ 00105 apr_int16_t rtnevents; /**< returned events */ 00106 apr_descriptor desc; /**< @see apr_descriptor */ 00107 void *client_data; /**< allows app to associate context */ 00108 }; 00109 00110 00111 /* General-purpose poll API for arbitrarily large numbers of 00112 * file descriptors 00113 */ 00114 00115 /** Opaque structure used for pollset API */ 00116 typedef struct apr_pollset_t apr_pollset_t; 00117 00118 /** 00119 * Set up a pollset object 00120 * @param pollset The pointer in which to return the newly created object 00121 * @param size The maximum number of descriptors that this pollset can hold 00122 * @param p The pool from which to allocate the pollset 00123 * @param flags Optional flags to modify the operation of the pollset. 00124 * 00125 * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is 00126 * created on which it is safe to make concurrent calls to 00127 * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll() 00128 * from separate threads. This feature is only supported on some 00129 * platforms; the apr_pollset_create() call will fail with 00130 * APR_ENOTIMPL on platforms where it is not supported. 00131 * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is 00132 * created with an additional internal pipe object used for the 00133 * apr_pollset_wakeup() call. The actual size of pollset is 00134 * in that case size + 1. This feature is only supported on some 00135 * platforms; the apr_pollset_create() call will fail with 00136 * APR_ENOTIMPL on platforms where it is not supported. 00137 * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t 00138 * structures passed to apr_pollset_add() are not copied and 00139 * must have a lifetime at least as long as the pollset. 00140 */ 00141 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset, 00142 apr_uint32_t size, 00143 apr_pool_t *p, 00144 apr_uint32_t flags); 00145 00146 /** 00147 * Set up a pollset object 00148 * @param pollset The pointer in which to return the newly created object 00149 * @param size The maximum number of descriptors that this pollset can hold 00150 * @param p The pool from which to allocate the pollset 00151 * @param flags Optional flags to modify the operation of the pollset. 00152 * @param method Poll method to use. See @apr_pollset_method_e. If this 00153 * method cannot be used, the default method will be used unless the 00154 * APR_POLLSET_NODEFAULT flag has been specified. 00155 * 00156 * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is 00157 * created on which it is safe to make concurrent calls to 00158 * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll() 00159 * from separate threads. This feature is only supported on some 00160 * platforms; the apr_pollset_create_ex() call will fail with 00161 * APR_ENOTIMPL on platforms where it is not supported. 00162 * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is 00163 * created with additional internal pipe object used for the 00164 * apr_pollset_wakeup() call. The actual size of pollset is 00165 * in that case size + 1. This feature is only supported on some 00166 * platforms; the apr_pollset_create_ex() call will fail with 00167 * APR_ENOTIMPL on platforms where it is not supported. 00168 * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t 00169 * structures passed to apr_pollset_add() are not copied and 00170 * must have a lifetime at least as long as the pollset. 00171 */ 00172 APR_DECLARE(apr_status_t) apr_pollset_create_ex(apr_pollset_t **pollset, 00173 apr_uint32_t size, 00174 apr_pool_t *p, 00175 apr_uint32_t flags, 00176 apr_pollset_method_e method); 00177 00178 /** 00179 * Destroy a pollset object 00180 * @param pollset The pollset to destroy 00181 */ 00182 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset); 00183 00184 /** 00185 * Add a socket or file descriptor to a pollset 00186 * @param pollset The pollset to which to add the descriptor 00187 * @param descriptor The descriptor to add 00188 * @remark If you set client_data in the descriptor, that value 00189 * will be returned in the client_data field whenever this 00190 * descriptor is signalled in apr_pollset_poll(). 00191 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00192 * and thread T1 is blocked in a call to apr_pollset_poll() for 00193 * this same pollset that is being modified via apr_pollset_add() 00194 * in thread T2, the currently executing apr_pollset_poll() call in 00195 * T1 will either: (1) automatically include the newly added descriptor 00196 * in the set of descriptors it is watching or (2) return immediately 00197 * with APR_EINTR. Option (1) is recommended, but option (2) is 00198 * allowed for implementations where option (1) is impossible 00199 * or impractical. 00200 * @remark If the pollset has been created with APR_POLLSET_NOCOPY, the 00201 * apr_pollfd_t structure referenced by descriptor will not be copied 00202 * and must have a lifetime at least as long as the pollset. 00203 * @remark Do not add the same socket or file descriptor to the same pollset 00204 * multiple times, even if the requested events differ for the 00205 * different calls to apr_pollset_add(). If the events of interest 00206 * for a descriptor change, you must first remove the descriptor 00207 * from the pollset with apr_pollset_remove(), then add it again 00208 * specifying all requested events. 00209 */ 00210 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset, 00211 const apr_pollfd_t *descriptor); 00212 00213 /** 00214 * Remove a descriptor from a pollset 00215 * @param pollset The pollset from which to remove the descriptor 00216 * @param descriptor The descriptor to remove 00217 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00218 * and thread T1 is blocked in a call to apr_pollset_poll() for 00219 * this same pollset that is being modified via apr_pollset_remove() 00220 * in thread T2, the currently executing apr_pollset_poll() call in 00221 * T1 will either: (1) automatically exclude the newly added descriptor 00222 * in the set of descriptors it is watching or (2) return immediately 00223 * with APR_EINTR. Option (1) is recommended, but option (2) is 00224 * allowed for implementations where option (1) is impossible 00225 * or impractical. 00226 * @remark apr_pollset_remove() cannot be used to remove a subset of requested 00227 * events for a descriptor. The reqevents field in the apr_pollfd_t 00228 * parameter must contain the same value when removing as when adding. 00229 */ 00230 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset, 00231 const apr_pollfd_t *descriptor); 00232 00233 /** 00234 * Block for activity on the descriptor(s) in a pollset 00235 * @param pollset The pollset to use 00236 * @param timeout The amount of time in microseconds to wait. This is a 00237 * maximum, not a minimum. If a descriptor is signalled, the 00238 * function will return before this time. If timeout is 00239 * negative, the function will block until a descriptor is 00240 * signalled or until apr_pollset_wakeup() has been called. 00241 * @param num Number of signalled descriptors (output parameter) 00242 * @param descriptors Array of signalled descriptors (output parameter) 00243 * @remark APR_EINTR will be returned if the pollset has been created with 00244 * APR_POLLSET_WAKEABLE, apr_pollset_wakeup() has been called while 00245 * waiting for activity, and there were no signalled descriptors at the 00246 * time of the wakeup call. 00247 * @remark Multiple signalled conditions for the same descriptor may be reported 00248 * in one or more returned apr_pollfd_t structures, depending on the 00249 * implementation. 00250 */ 00251 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset, 00252 apr_interval_time_t timeout, 00253 apr_int32_t *num, 00254 const apr_pollfd_t **descriptors); 00255 00256 /** 00257 * Interrupt the blocked apr_pollset_poll() call. 00258 * @param pollset The pollset to use 00259 * @remark If the pollset was not created with APR_POLLSET_WAKEABLE the 00260 * return value is APR_EINIT. 00261 */ 00262 APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset); 00263 00264 /** 00265 * Poll the descriptors in the poll structure 00266 * @param aprset The poll structure we will be using. 00267 * @param numsock The number of descriptors we are polling 00268 * @param nsds The number of descriptors signalled (output parameter) 00269 * @param timeout The amount of time in microseconds to wait. This is a 00270 * maximum, not a minimum. If a descriptor is signalled, the 00271 * function will return before this time. If timeout is 00272 * negative, the function will block until a descriptor is 00273 * signalled or until apr_pollset_wakeup() has been called. 00274 * @remark The number of descriptors signalled is returned in the third argument. 00275 * This is a blocking call, and it will not return until either a 00276 * descriptor has been signalled or the timeout has expired. 00277 * @remark The rtnevents field in the apr_pollfd_t array will only be filled- 00278 * in if the return value is APR_SUCCESS. 00279 */ 00280 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock, 00281 apr_int32_t *nsds, 00282 apr_interval_time_t timeout); 00283 00284 /** 00285 * Return a printable representation of the pollset method. 00286 * @param pollset The pollset to use 00287 */ 00288 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset); 00289 00290 /** 00291 * Return a printable representation of the default pollset method 00292 * (APR_POLLSET_DEFAULT). 00293 */ 00294 APR_DECLARE(const char *) apr_poll_method_defname(void); 00295 00296 /** Opaque structure used for pollset API */ 00297 typedef struct apr_pollcb_t apr_pollcb_t; 00298 00299 /** 00300 * Set up a pollcb object 00301 * @param pollcb The pointer in which to return the newly created object 00302 * @param size The maximum number of descriptors that a single _poll can return. 00303 * @param p The pool from which to allocate the pollcb 00304 * @param flags Optional flags to modify the operation of the pollcb. 00305 * 00306 * @remark Pollcb is only supported on some platforms; the apr_pollcb_create() 00307 * call will fail with APR_ENOTIMPL on platforms where it is not supported. 00308 */ 00309 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb, 00310 apr_uint32_t size, 00311 apr_pool_t *pool, 00312 apr_uint32_t flags); 00313 00314 /** 00315 * Set up a pollcb object 00316 * @param pollcb The pointer in which to return the newly created object 00317 * @param size The maximum number of descriptors that a single _poll can return. 00318 * @param p The pool from which to allocate the pollcb 00319 * @param flags Optional flags to modify the operation of the pollcb. 00320 * @param method Poll method to use. See @apr_pollset_method_e. If this 00321 * method cannot be used, the default method will be used unless the 00322 * APR_POLLSET_NODEFAULT flag has been specified. 00323 * 00324 * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex() 00325 * call will fail with APR_ENOTIMPL on platforms where it is not supported. 00326 */ 00327 APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb, 00328 apr_uint32_t size, 00329 apr_pool_t *pool, 00330 apr_uint32_t flags, 00331 apr_pollset_method_e method); 00332 00333 /** 00334 * Add a socket or file descriptor to a pollcb 00335 * @param pollcb The pollcb to which to add the descriptor 00336 * @param descriptor The descriptor to add 00337 * @remark If you set client_data in the descriptor, that value will be 00338 * returned in the client_data field whenever this descriptor is 00339 * signalled in apr_pollcb_poll(). 00340 * @remark Unlike the apr_pollset API, the descriptor is not copied, and users 00341 * must retain the memory used by descriptor, as the same pointer will 00342 * be returned to them from apr_pollcb_poll. 00343 * @remark Do not add the same socket or file descriptor to the same pollcb 00344 * multiple times, even if the requested events differ for the 00345 * different calls to apr_pollcb_add(). If the events of interest 00346 * for a descriptor change, you must first remove the descriptor 00347 * from the pollcb with apr_pollcb_remove(), then add it again 00348 * specifying all requested events. 00349 */ 00350 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb, 00351 apr_pollfd_t *descriptor); 00352 /** 00353 * Remove a descriptor from a pollcb 00354 * @param pollcb The pollcb from which to remove the descriptor 00355 * @param descriptor The descriptor to remove 00356 * @remark apr_pollcb_remove() cannot be used to remove a subset of requested 00357 * events for a descriptor. The reqevents field in the apr_pollfd_t 00358 * parameter must contain the same value when removing as when adding. 00359 */ 00360 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb, 00361 apr_pollfd_t *descriptor); 00362 00363 /** Function prototype for pollcb handlers 00364 * @param baton Opaque baton passed into apr_pollcb_poll 00365 * @param descriptor Contains the notification for an active descriptor, 00366 * the rtnevents member contains what events were triggered 00367 * for this descriptor. 00368 */ 00369 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor); 00370 00371 /** 00372 * Block for activity on the descriptor(s) in a pollcb 00373 * @param pollcb The pollcb to use 00374 * @param timeout The amount of time in microseconds to wait. This is a 00375 * maximum, not a minimum. If a descriptor is signalled, the 00376 * function will return before this time. If timeout is 00377 * negative, the function will block until a descriptor is 00378 * signalled. 00379 * @param func Callback function to call for each active descriptor. 00380 * @param baton Opaque baton passed to the callback function. 00381 * @remark Multiple signalled conditions for the same descriptor may be reported 00382 * in one or more calls to the callback function, depending on the 00383 * implementation. 00384 */ 00385 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb, 00386 apr_interval_time_t timeout, 00387 apr_pollcb_cb_t func, 00388 void *baton); 00389 00390 /** @} */ 00391 00392 #ifdef __cplusplus 00393 } 00394 #endif 00395 00396 #endif /* ! APR_POLL_H */ 00397
1.5.8